From 6904ad913ef2f839e7a8ee458341d6675c652421 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Fri, 23 Jul 2021 22:04:33 -0700 Subject: [PATCH] new posts --- dbTools/post.js | 36 ++++++++++++++++++++++++++++++++++++ def/post.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ def/user.js | 12 +++++++++--- index.js | 24 ++++++++++++++++++++++-- mongoDB.js | 7 +++++-- 5 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 dbTools/post.js create mode 100644 def/post.js diff --git a/dbTools/post.js b/dbTools/post.js new file mode 100644 index 0000000..ff16c9f --- /dev/null +++ b/dbTools/post.js @@ -0,0 +1,36 @@ +const DBName = "EMI_SOCIAL"; +const Post = require("./../def/post.js") + +postDB = (DB)=>{ + DB.postCols = DB.db.db(DBName).collection("posts"); + DB.newPost = (postObj) => { + console.log(postObj) + return DB.postCols.insertOne(postObj.toObj()).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.newComment = (postid, comment) => { + return DB.postCols.updateOne(postid, { + $push: { + comments: comment + }, + $set: { + lastUpdated: new Date() + } + }).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.getPosts = (userObj) => { + return DB.postCols.find().toArray().catch((err)=>{ + console.log(err); + return false; + }); + } +} + +module.exports = postDB; \ No newline at end of file diff --git a/def/post.js b/def/post.js new file mode 100644 index 0000000..a69842f --- /dev/null +++ b/def/post.js @@ -0,0 +1,45 @@ +class Post { + constructor(info){ + if(!info || !info.userid) throw "Can not construct empty posts" + this.userid = info.userid; + this.content = info.content; + this.createdAt = info.createdAt || new Date(); + this.reactions = info.reactions || {}; + this.comments = info.comments || []; + //This should record edits + this.contentHistory = info.contentHistory || []; + // Any reaction or comment updates this ts, + // this will be used as index to query new posts + this.lastUpdated = info.lastUpdated || this.createdAt; + } + + addComment(comment){ + this.comments.push(comment); + this.lastUpdated = new Date(); + } + + addReaction(reaction){ + if(this.reactions[reaction.userid] && + this.reactions[reaction.userid].type === reaction.type){ + delete this.reactions[reaction.userid] + } + else{ + this.reactions[reaction.userid] = reaction; + } + this.lastUpdated = new Date(); + } + + toObj=function(){ + let r = {} + r.userid = this.userid; + r.content = this.content; + r.createdAt = this.createdAt; + r.reactions = this.reactions; + r.comments = this.comments; + r.contentHistory = this.contentHistory; + r.lastUpdated = this.lastUpdated; + return r; + } +} + +module.exports = Post; \ No newline at end of file diff --git a/def/user.js b/def/user.js index 589958f..9b42117 100644 --- a/def/user.js +++ b/def/user.js @@ -7,8 +7,14 @@ class User { this.isGroup = false; } - newPost(){} - newComment(){} - newReaction(){} + newPost(content){ + + } + newComment(post){ + + } + newReaction(){ + + } } \ No newline at end of file diff --git a/index.js b/index.js index 1fafd54..01de9fc 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,9 @@ const getUserId = function(req){ return user_sid } +// Definitions +const Post = require("./def/post.js") + DB.getDB.then((DB)=>{ // middleware function to check for logged-in users @@ -78,8 +81,8 @@ DB.getDB.then((DB)=>{ maxAge: 1000 * 60 * 60 * 24 * 30, // would expire after 30 days httpOnly: true, // The cookie only accessible by the web server //signed: true // Indicates if the cookie should be signed - sameSite: 'none', - secure: true, + sameSite: 'none', // This and secure are required for properly + secure: true, // manage cockies in cros-domain }; // route for user Login @@ -129,6 +132,23 @@ DB.getDB.then((DB)=>{ return logout(req, res); }); + app.get("/post/", sessionChecker, async (req, res) => { + let posts = await DB.getPosts(); + return res.json(posts) + }); + + app.get("/post/new", sessionChecker, async (req, res) => { + let post = { + userid: getUserId(req), + content: req.query.content + } + let postObj = new Post(post); + DB.newPost(postObj) + return res.json({ + status: "ok" + }) + }) + // route for handling 404 requests(unavailable routes) app.use(function (req, res, next) { res.status(404).send("Sorry can't find that!") diff --git a/mongoDB.js b/mongoDB.js index efa8c86..93b5677 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -1,7 +1,8 @@ const mongo = require('mongodb'); const MongoClient = mongo.MongoClient; const DBName = "EMI_SOCIAL"; -const mongoUrl = process.env.MONGO_URL +const mongoUrl = process.env.MONGO_URL; +const postDB = require("./dbTools/post.js"); const getDB = new Promise((resolve, reject) => { @@ -12,7 +13,7 @@ const getDB = new Promise((resolve, reject) => { DB.db = db; console.log("Connected to DB!"); DB.usersCol = db.db(DBName).collection("users"); - DB.tokensCol = db.db(DBName).collection("AmArr5EHx7sn3Gr"); + DB.tokensCol = db.db(DBName).collection("tokens"); DB.checkSessionOnDB = async (session_id, user_sid)=>{ const temp_id = new mongo.ObjectID(session_id); @@ -46,6 +47,8 @@ const getDB = new Promise((resolve, reject) => { const temp_id = new mongo.ObjectID(session_id); return DB.tokensCol.deleteOne({"_id":temp_id}); } + + postDB(DB); resolve(DB); });