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: {
$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;
});

View File

@@ -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;
});