From 070912f2d3baac2884a55013af530ca687497972 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Sat, 25 Sep 2021 21:20:41 -0700 Subject: [PATCH] add private groups --- dbTools/post.js | 13 ++++++++----- dbTools/profile.js | 44 ++++++++++++++++++++++++++++++++------------ def/content.js | 0 def/group.js | 8 -------- def/profile.js | 14 +++++++++++--- routes/post.js | 9 ++++++++- routes/profile.js | 19 ++++++++++++++++++- 7 files changed, 77 insertions(+), 30 deletions(-) delete mode 100644 def/content.js delete mode 100644 def/group.js diff --git a/dbTools/post.js b/dbTools/post.js index f3a6e03..d2ecfd0 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -127,12 +127,15 @@ postDB = (DB)=>{ {profileid: { $in: ids }}, - {toProfile: { - $in: ids - }} + //{toProfile: { + // $in: ids + //}} ] }; - return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().catch((err)=>{ + return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().then(async (posts)=>{ + //we need to filter when toProfile is private and not part of the following array + return posts; + }).catch((err)=>{ console.log(err); return false; }); @@ -140,7 +143,7 @@ postDB = (DB)=>{ DB.getPostsOfUser = (userId) => { let userid = DB.ObjectID(userId); - return DB.postCols.find({userid}).sort({_id: -1}).toArray().catch((err)=>{ + return DB.postCols.find({userid}).sort({_id: -1}).limit(20).toArray().catch((err)=>{ console.log(err); return false; }); diff --git a/dbTools/profile.js b/dbTools/profile.js index c90a39d..88f1cab 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -36,13 +36,7 @@ userDB = (DB) => { DB.getProfileCache = async (profileId) => { if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId]; - const _id = DB.ObjectID(profileId); - let r = await DB.profileCols.findOne({ _id }).catch((err) => { - console.log(err); - return false; - }); - if (r) userProfileCache[profileId] = r; - return r; + return DB.getProfile(profileId); } DB.getProfiles = async (query) => { @@ -148,11 +142,17 @@ userDB = (DB) => { return r; } + let privateGroupsCache = {}; + DB.isGroupPrivate = async (groupid) => { + if(userProfileCache[groupid]) return userProfileCache[groupid].isPrivate; + let g = await DB.getGroup(groupid); + return g.isPrivate; + } + DB.getGroup = async (groupid) => { const _id = DB.ObjectID(groupid); //if(userProfileCache[groupid]) return userProfileCache[groupid]; - let r = await DB.profileCols.findOne({_id, isGroup: true}) - .toArray().catch((err) => { + let r = await DB.profileCols.findOne({_id, isGroup: true}).catch((err) => { console.log(err); return false; }); @@ -160,15 +160,35 @@ userDB = (DB) => { return r; } - DB.subscribeToGroup = async (profileid, groupid) => { + DB.subscribeToGroup = async (profileid, groupid, reqSubscription = false) => { + const _id = DB.ObjectID(groupid); + const subOrRequest = reqSubscription ? "pending." : "subscribed."; + let update = { + $set:{ + [subOrRequest + profileid]: new Date() + } + } + if(!reqSubscription) DB.followProfile(profileid, groupid); + delete userProfileCache[groupid]; + return DB.profileCols.updateOne({_id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.acceptGroupJoinReq = async (profileid, groupid) => { const _id = DB.ObjectID(groupid); let update = { $set:{ ["subscribed." + profileid]: new Date() - } + }, + $unset:{ + ["pending." + profileid]: "" + } } DB.followProfile(profileid, groupid) - return DB.profileCols.updateOne({_id}, update).catch((err)=>{ + delete userProfileCache[groupid]; + return DB.profileCols.updateOne({_id}, update).then(console.log).catch((err)=>{ console.log(err); return false; }); diff --git a/def/content.js b/def/content.js deleted file mode 100644 index e69de29..0000000 diff --git a/def/group.js b/def/group.js deleted file mode 100644 index d298462..0000000 --- a/def/group.js +++ /dev/null @@ -1,8 +0,0 @@ -User = require("./User.js") - -class Gropu extends User { - constructor(){ - super() - this.isGroup = true; - } -} \ No newline at end of file diff --git a/def/profile.js b/def/profile.js index 1193d03..d3d45f8 100644 --- a/def/profile.js +++ b/def/profile.js @@ -16,10 +16,15 @@ class User { this.following = info.following || []; this.lastUpdate = info.lastUpdate || new Date(); this.newsFeedCache = info.newsFeedCache || []; + this.notifications = info.notifications || []; + + //groupRelated this.isGroup = info.isGroup || false; this.isCourse = info.isCourse || false; - this.subscribed = info.subscribed || {}; - this.notifications = info.notifications || []; + this.isPrivate = info.isPrivate || false; + this.subscribed = info.subscribed || {}; //Subscribed user to groups + this.pending = info.subscribed || {}; //Private groups require authorization + } toObj(){ @@ -32,10 +37,13 @@ class User { r.following = this.following; r.lastUpdate = this.lastUpdate; r.newsFeedCache = this.newsFeedCache; + r.notifications = this.notifications; + r.isGroup = this.isGroup; r.isCourse = this.isCourse; + r.isPrivate = this.isPrivate; r.subscribed = this.subscribed; - r.notifications = this.notifications; + r.pending = this.pending; return r; } diff --git a/routes/post.js b/routes/post.js index d3fda5f..4a43ef7 100644 --- a/routes/post.js +++ b/routes/post.js @@ -19,8 +19,15 @@ DB.getDB.then((DB)=>{ router.get("/usr/:id", async (req, res) => { const profileId = req.params.id; + if(await DB.isGroupPrivate(profileId)){ + let requestProfile = getProfileId(req) + ""; + let group = await DB.getProfileCache(profileId); + if(!group.subscribed[requestProfile] && profileId != requestProfile){ + return res.json([]); + } + } const posts = await DB.getPosts(profileId); - return res.json(posts) + return res.json(posts); }); router.post("/", async (req, res) => { diff --git a/routes/profile.js b/routes/profile.js index 5176a97..683a4c8 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -68,6 +68,18 @@ DB.getDB.then((DB)=>{ }); }); + router.post("/groups/accept", async (req, res) => { + //This function should be called to accept the join request + //of an user that attempt to join a private group. + const groupid = getProfileId(req); //It needs to have this profile context + const profileAcepted = DB.ObjectID(req.body.profileid); + DB.acceptGroupJoinReq(profileAcepted, groupid); + //Add Notification to accepted user + return res.json({ + status: "ok" + }); + }); + router.get("/groups/:id", async (req, res) => { const groupid = req.params.id; let groups = await DB.getGroup(groupid); @@ -80,7 +92,9 @@ 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); + const isPrivate = await DB.isGroupPrivate(groupid); + DB.subscribeToGroup(profileid, groupid, isPrivate); + //Add notification to group owner return res.json({ status: "ok" }); @@ -90,6 +104,7 @@ DB.getDB.then((DB)=>{ const groupid = req.params.id; const profileid = getProfileId(req); DB.unsubscribeToGroup(profileid, groupid); + //Add notification to group owner return res.json({ status: "ok" }); @@ -139,6 +154,7 @@ DB.getDB.then((DB)=>{ let followProfileId = req.params.id; const profileid = getProfileId(req); DB.followProfile(profileid, followProfileId); + //Add notification to user return res.json({ status: "ok" }); @@ -148,6 +164,7 @@ DB.getDB.then((DB)=>{ let followProfileId = req.params.id; const profileid = getProfileId(req); DB.unfollowProfile(profileid, followProfileId); + //Add notification to user return res.json({ status: "ok" });