diff --git a/dbTools/profile.js b/dbTools/profile.js index f2b2ac6..be7f84e 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -284,13 +284,36 @@ userDB = (DB) => { postid, commentIndx, actorid, + viewed: false, } } } - return DB.profileCols.updateOne({ _id }, update).catch((err) => { + const r = await DB.profileCols.updateOne({ _id }, update).catch((err) => { console.log(err); return false; }); + if (userProfileCache[profileid]) delete userProfileCache[profileid]; + return r; + } + + DB.markNotificationsViewed = async (profileid) => { + const _id = DB.ObjectID(profileid); + const update = { + $set: { + "notifications.$[n].viewed": true + } + }; + const options = { + arrayFilters: [ + { "n.viewed": { $ne: true } } + ] + }; + const r = await DB.profileCols.updateOne({ _id }, update, options).catch((err) => { + console.log(err); + return false; + }); + if (userProfileCache[profileid]) delete userProfileCache[profileid]; + return r; } DB.isSubscriptor = async (profileid) => { diff --git a/routes/profile.js b/routes/profile.js index b9f7de4..91ac64b 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -784,6 +784,26 @@ DB.getDB.then((DB) => { } }); + router.post("/notifications/viewed", async (req, res) => { + try { + const profileid = getProfileId(req); + const result = await DB.markNotificationsViewed(profileid); + if (!result) { + return res.status(400).json({ + status: "Could not update notifications" + }); + } + return res.json({ + status: "ok" + }); + } catch (error) { + console.error("Error marking notifications as viewed", error); + return res.status(500).json({ + status: "Internal server error" + }); + } + }); + /** * @swagger * /user/{id}: