add private groups

This commit is contained in:
Adolfo Reyna
2021-09-25 21:20:41 -07:00
parent 83941c59d5
commit 070912f2d3
7 changed files with 77 additions and 30 deletions

View File

@@ -127,12 +127,15 @@ postDB = (DB)=>{
{profileid: { {profileid: {
$in: ids $in: ids
}}, }},
{toProfile: { //{toProfile: {
$in: ids // $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); console.log(err);
return false; return false;
}); });
@@ -140,7 +143,7 @@ postDB = (DB)=>{
DB.getPostsOfUser = (userId) => { DB.getPostsOfUser = (userId) => {
let userid = DB.ObjectID(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); console.log(err);
return false; return false;
}); });

View File

@@ -36,13 +36,7 @@ userDB = (DB) => {
DB.getProfileCache = async (profileId) => { DB.getProfileCache = async (profileId) => {
if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId]; if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId];
const _id = DB.ObjectID(profileId); return DB.getProfile(profileId);
let r = await DB.profileCols.findOne({ _id }).catch((err) => {
console.log(err);
return false;
});
if (r) userProfileCache[profileId] = r;
return r;
} }
DB.getProfiles = async (query) => { DB.getProfiles = async (query) => {
@@ -148,11 +142,17 @@ userDB = (DB) => {
return r; 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) => { DB.getGroup = async (groupid) => {
const _id = DB.ObjectID(groupid); const _id = DB.ObjectID(groupid);
//if(userProfileCache[groupid]) return userProfileCache[groupid]; //if(userProfileCache[groupid]) return userProfileCache[groupid];
let r = await DB.profileCols.findOne({_id, isGroup: true}) let r = await DB.profileCols.findOne({_id, isGroup: true}).catch((err) => {
.toArray().catch((err) => {
console.log(err); console.log(err);
return false; return false;
}); });
@@ -160,15 +160,35 @@ userDB = (DB) => {
return r; 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); const _id = DB.ObjectID(groupid);
let update = { let update = {
$set:{ $set:{
["subscribed." + profileid]: new Date() ["subscribed." + profileid]: new Date()
} },
$unset:{
["pending." + profileid]: ""
}
} }
DB.followProfile(profileid, groupid) 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); console.log(err);
return false; return false;
}); });

View File

View File

@@ -1,8 +0,0 @@
User = require("./User.js")
class Gropu extends User {
constructor(){
super()
this.isGroup = true;
}
}

View File

@@ -16,10 +16,15 @@ class User {
this.following = info.following || []; this.following = info.following || [];
this.lastUpdate = info.lastUpdate || new Date(); this.lastUpdate = info.lastUpdate || new Date();
this.newsFeedCache = info.newsFeedCache || []; this.newsFeedCache = info.newsFeedCache || [];
this.notifications = info.notifications || [];
//groupRelated
this.isGroup = info.isGroup || false; this.isGroup = info.isGroup || false;
this.isCourse = info.isCourse || false; this.isCourse = info.isCourse || false;
this.subscribed = info.subscribed || {}; this.isPrivate = info.isPrivate || false;
this.notifications = info.notifications || []; this.subscribed = info.subscribed || {}; //Subscribed user to groups
this.pending = info.subscribed || {}; //Private groups require authorization
} }
toObj(){ toObj(){
@@ -32,10 +37,13 @@ class User {
r.following = this.following; r.following = this.following;
r.lastUpdate = this.lastUpdate; r.lastUpdate = this.lastUpdate;
r.newsFeedCache = this.newsFeedCache; r.newsFeedCache = this.newsFeedCache;
r.notifications = this.notifications;
r.isGroup = this.isGroup; r.isGroup = this.isGroup;
r.isCourse = this.isCourse; r.isCourse = this.isCourse;
r.isPrivate = this.isPrivate;
r.subscribed = this.subscribed; r.subscribed = this.subscribed;
r.notifications = this.notifications; r.pending = this.pending;
return r; return r;
} }

View File

@@ -19,8 +19,15 @@ DB.getDB.then((DB)=>{
router.get("/usr/:id", async (req, res) => { router.get("/usr/:id", async (req, res) => {
const profileId = req.params.id; 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); const posts = await DB.getPosts(profileId);
return res.json(posts) return res.json(posts);
}); });
router.post("/", async (req, res) => { router.post("/", async (req, res) => {

View File

@@ -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) => { router.get("/groups/:id", async (req, res) => {
const groupid = req.params.id; const groupid = req.params.id;
let groups = await DB.getGroup(groupid); let groups = await DB.getGroup(groupid);
@@ -80,7 +92,9 @@ DB.getDB.then((DB)=>{
router.get("/groups/:id/subscribe", async (req, res) => { router.get("/groups/:id/subscribe", async (req, res) => {
const groupid = req.params.id; const groupid = req.params.id;
const profileid = getProfileId(req); 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({ return res.json({
status: "ok" status: "ok"
}); });
@@ -90,6 +104,7 @@ DB.getDB.then((DB)=>{
const groupid = req.params.id; const groupid = req.params.id;
const profileid = getProfileId(req); const profileid = getProfileId(req);
DB.unsubscribeToGroup(profileid, groupid); DB.unsubscribeToGroup(profileid, groupid);
//Add notification to group owner
return res.json({ return res.json({
status: "ok" status: "ok"
}); });
@@ -139,6 +154,7 @@ DB.getDB.then((DB)=>{
let followProfileId = req.params.id; let followProfileId = req.params.id;
const profileid = getProfileId(req); const profileid = getProfileId(req);
DB.followProfile(profileid, followProfileId); DB.followProfile(profileid, followProfileId);
//Add notification to user
return res.json({ return res.json({
status: "ok" status: "ok"
}); });
@@ -148,6 +164,7 @@ DB.getDB.then((DB)=>{
let followProfileId = req.params.id; let followProfileId = req.params.id;
const profileid = getProfileId(req); const profileid = getProfileId(req);
DB.unfollowProfile(profileid, followProfileId); DB.unfollowProfile(profileid, followProfileId);
//Add notification to user
return res.json({ return res.json({
status: "ok" status: "ok"
}); });