diff --git a/dbTools/post.js b/dbTools/post.js index dac82d8..55f2c64 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -29,9 +29,7 @@ postDB = (DB)=>{ const id = DB.ObjectID(postid); let update = { $unset:{ - reactions:{ - [userid]: {} - } + ["reactions." + profileid]: "" }, $set: { lastUpdated: new Date() diff --git a/dbTools/profile.js b/dbTools/profile.js index 2960d39..0cbb12b 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -13,7 +13,7 @@ userDB = (DB) => { } DB.getProfile = async (profileId) => { - if (userProfileCache[profileId]) return userProfileCache[profileId]; + //if (userProfileCache[profileId]) return userProfileCache[profileId]; const _id = DB.ObjectID(profileId); let r = await DB.profileCols.findOne({ _id }).catch((err) => { console.log(err); @@ -68,6 +68,31 @@ userDB = (DB) => { return r; } + DB.subscribeToGroup = async (profileid, groupid) => { + const _id = DB.ObjectID(groupid); + let update = { + $set:{ + ["subscribed." + profileid]: new Date() + } + } + return DB.profileCols.updateOne({_id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.unsubscribeToGroup = async (profileid, groupid) => { + const _id = DB.ObjectID(groupid); + let update = { + $unset:{ + ["subscribed." + profileid]: "", + } + } + return DB.profileCols.updateOne({_id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } } diff --git a/def/profile.js b/def/profile.js index e9950d3..09a7335 100644 --- a/def/profile.js +++ b/def/profile.js @@ -18,6 +18,7 @@ class User { this.newsFeedCache = info.newsFeedCache || []; this.isGroup = info.isGroup || false; this.isCourse = info.isCourse || false; + this.subscribed = info.subscribed || {}; } toObj(){ @@ -32,6 +33,7 @@ class User { r.newsFeedCache = this.newsFeedCache; r.isGroup = this.isGroup; r.isCourse = this.isCourse; + r.subscribed = this.subscribed; return r; } diff --git a/routes/post.js b/routes/post.js index 0b3133b..afa383a 100644 --- a/routes/post.js +++ b/routes/post.js @@ -7,7 +7,7 @@ const Post = require("./../def/post.js"); DB.getDB.then((DB)=>{ const getProfileId = (req)=>{ - return DB.ObjectID(req.cookies.profile_id); + return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id); } router.get("/", async (req, res) => { diff --git a/routes/profile.js b/routes/profile.js index e38d6c3..74c9a99 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -11,6 +11,10 @@ DB.getDB.then((DB)=>{ return DB.ObjectID(user_sid); } + const getProfileId = (req)=>{ + return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id); + } + router.get("/mine", async (req, res) => { let userid = req.cookies.user_sid; let profiles = await DB.getUserProfiles(userid); @@ -65,6 +69,24 @@ DB.getDB.then((DB)=>{ }); }); + router.get("/groups/:id/subscribe", async (req, res) => { + const groupid = req.params.id; + const profileid = getProfileId(req); + DB.subscribeToGroup(profileid, groupid); + return res.json({ + status: "ok" + }); + }); + + router.get("/groups/:id/unsubscribe", async (req, res) => { + const groupid = req.params.id; + const profileid = getProfileId(req); + DB.unsubscribeToGroup(profileid, groupid).then(console.log); + return res.json({ + status: "ok" + }); + }); + router.get("/:id", async (req, res) => { let profileId = req.params.id; let profile = await DB.getProfile(profileId);