From e7c9d564042ea8ea6dff03491040bfbbcaa40df3 Mon Sep 17 00:00:00 2001 From: aeroreyna Date: Mon, 4 Apr 2022 21:14:12 -0700 Subject: [PATCH] Unreact to comments --- dbTools/post.js | 24 ++++++++++++++++++++++-- routes/post.js | 10 ++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/dbTools/post.js b/dbTools/post.js index 18f60be..a55dcb9 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -90,11 +90,11 @@ postDB = (DB)=>{ }); } - DB.newCommentReaction = (postid, commentDate, userid, reaction) => { + DB.newCommentReaction = (postid, commentDate, profileid, reaction) => { const id = DB.ObjectID(postid); let update = { $set:{ - ["comments.$.reactions." + userid]: reaction, + ["comments.$.reactions." + profileid]: reaction, "comments.$.lastUpdated": new Date(), lastUpdated: new Date() } @@ -108,6 +108,26 @@ postDB = (DB)=>{ }); } + DB.removeCommentReaction = (postid, commentDate, profileid) => { + const id = DB.ObjectID(postid); + let update = { + $unset:{ + ["comments.$.reactions." + profileid]: '', + "comments.$.lastUpdated": new Date(), + }, + $set: { + lastUpdated: new Date() + } + } + return DB.postCols.updateOne({ + _id: id, + "comments.createdAt": commentDate + }, update).catch((err)=>{ + console.log(err); + return false; + }); + } + DB.getPosts = async (profileId, viewerProfileId) => { const profile = await DB.getProfile(viewerProfileId); let query = {}; diff --git a/routes/post.js b/routes/post.js index 5d5ae7b..6d37d05 100644 --- a/routes/post.js +++ b/routes/post.js @@ -218,6 +218,16 @@ DB.getDB.then((DB) => { }) }); + router.post("/comment/unreact", async (req, res) => { + let profileid = getProfileId(req); + let postid = req.body.postid; + let commentDate = new Date(req.body.commentDate); + r = await DB.removeCommentReaction(postid, commentDate, profileid); + return res.json({ + status: "ok" + }) + }); + router.get("/:id", async (req, res) => { const postId = req.params.id; const post = await DB.getPost(postId);