From 4069054117ab7653ab4641a55716101332027b6f Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 29 Jul 2021 10:25:05 -0700 Subject: [PATCH] posting and commenting --- dbTools/post.js | 56 ++++++++++++++++++++++++++++++++++++++++++++- def/user.js | 2 +- index.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ mongoDB.js | 2 ++ 4 files changed, 119 insertions(+), 2 deletions(-) diff --git a/dbTools/post.js b/dbTools/post.js index ff16c9f..f829991 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -11,8 +11,43 @@ postDB = (DB)=>{ }); } + DB.newReaction = (postid, userid, reaction) => { + const id = DB.ObjectID(postid); + let update = { + $set:{ + reactions:{ + [userid]: reaction + }, + lastUpdated: new Date() + } + } + return DB.postCols.updateOne({_id: id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.removeReaction = (postid, userid) => { + const id = DB.ObjectID(postid); + let update = { + $unset:{ + reactions:{ + [userid]: {} + } + }, + $set: { + lastUpdated: new Date() + } + } + return DB.postCols.updateOne({_id: id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } + DB.newComment = (postid, comment) => { - return DB.postCols.updateOne(postid, { + const id = DB.ObjectID(postid); + return DB.postCols.updateOne({_id: id}, { $push: { comments: comment }, @@ -25,6 +60,25 @@ postDB = (DB)=>{ }); } + DB.newCommentReaction = (postid, commentDate, userid, reaction) => { + const id = DB.ObjectID(postid); + let update = { + $set:{ + ["comments.$.reactions." + userid]: reaction, + "comments.$.lastUpdated": new Date(), + lastUpdated: new Date() + } + } + console.log(JSON.stringify(update)); + return DB.postCols.updateOne({ + _id: id, + "comments.createdAt": commentDate + }, update).catch((err)=>{ + console.log(err); + return false; + }); + } + DB.getPosts = (userObj) => { return DB.postCols.find().toArray().catch((err)=>{ console.log(err); diff --git a/def/user.js b/def/user.js index 9b42117..14537dd 100644 --- a/def/user.js +++ b/def/user.js @@ -2,7 +2,7 @@ class User { constructor(json){ this.username = 'aeroreyna'; this.following = []; - this.lastUpdate = new Date();ß + this.lastUpdate = new Date(); this.newsFeedCache = []; this.isGroup = false; } diff --git a/index.js b/index.js index 01de9fc..2e4b9e7 100644 --- a/index.js +++ b/index.js @@ -147,8 +147,69 @@ DB.getDB.then((DB)=>{ return res.json({ status: "ok" }) + }); + + app.get("/post/newComment", sessionChecker, async (req, res) => { + let userid = getUserId(req); + let postid = req.query.postid; + let content = req.query.content; + let comment = { + userid: userid, + content: content, + createdAt: new Date(), + lastUpdated: new Date(), + reactions: {} + } + console.log("comment", postid, comment); + r = await DB.newComment(postid, comment); + console.log(r) + return res.json({ + status: "ok" + }) + }); + + app.get("/post/react", sessionChecker, async (req, res) => { + let userid = getUserId(req); + let postid = req.query.postid; + let reaction = { + type: "like", + createdAt: new Date() + }; + console.log("reaction". postid, reaction); + r = await DB.newReaction(postid, userid, reaction); + console.log(r); + return res.json({ + status: "ok" + }); }) + app.get("/post/unreact", sessionChecker, async (req, res) => { + let userid = getUserId(req); + let postid = req.query.postid; + r = await DB.removeReaction(postid, userid); + console.log(r) + return res.json({ + status: "ok" + }) + }); + + app.get("/post/comment/react", sessionChecker, async (req, res) => { + let userid = getUserId(req); + let postid = req.query.postid; + let commentDate = new Date(req.query.commentDate); + let reaction = { + type: "like", + createdAt: new Date() + }; + console.log(req.query) + console.log("comment reaction", postid, commentDate, reaction); + r = await DB.newCommentReaction(postid, commentDate, userid, reaction); + console.log(r) + 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 93b5677..2a599d4 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -1,5 +1,6 @@ const mongo = require('mongodb'); const MongoClient = mongo.MongoClient; +const ObjectID = mongo.ObjectID; const DBName = "EMI_SOCIAL"; const mongoUrl = process.env.MONGO_URL; const postDB = require("./dbTools/post.js"); @@ -11,6 +12,7 @@ const getDB = new Promise((resolve, reject) => { if (err) return reject(err); DB.db = db; + DB.ObjectID = ObjectID; console.log("Connected to DB!"); DB.usersCol = db.db(DBName).collection("users"); DB.tokensCol = db.db(DBName).collection("tokens");