add private groups
This commit is contained in:
@@ -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;
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
User = require("./User.js")
|
||||
|
||||
class Gropu extends User {
|
||||
constructor(){
|
||||
super()
|
||||
this.isGroup = true;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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"
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user