filters private groups fix in feed and add in profiles

This commit is contained in:
Adolfo Reyna
2021-10-10 10:13:52 -07:00
parent 74f9108fa7
commit d495cfa057

View File

@@ -100,7 +100,8 @@ postDB = (DB)=>{
}); });
} }
DB.getPosts = (profileId) => { DB.getPosts = async (profileId) => {
const profile = await DB.getProfile(profileId);
let query = {}; let query = {};
if(profileId) { if(profileId) {
const id = DB.ObjectID(profileId); const id = DB.ObjectID(profileId);
@@ -111,12 +112,35 @@ postDB = (DB)=>{
] ]
}; };
} }
return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().catch((err)=>{ return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().then(async (posts)=>{
return await filterPrivateGroups(posts, profile);
}).catch((err)=>{
console.log(err); console.log(err);
return false; return false;
}); });
} }
filterPrivateGroups = async (posts, profile) =>{
let filteredPosts = [];
for(i in posts){
let p = posts[i];
let isPostingAPrivateGroup = await DB.isGroupPrivate(p.profileid);
let isPostingToAPrivateGroup = p.toProfile ? await DB.isGroupPrivate(p.toProfile) : false;
if(!isPostingAPrivateGroup && !isPostingToAPrivateGroup){
filteredPosts.push(p);
continue;
}
if(isPostingAPrivateGroup && !profile[p.profileid]){
continue;
}
if(isPostingToAPrivateGroup && !profile[p.toProfile]){
continue;
}
filteredPosts.push(p);
}
return filteredPosts;
}
DB.getFeed = async (profileId) => { DB.getFeed = async (profileId) => {
const profile = await DB.getProfile(profileId); const profile = await DB.getProfile(profileId);
let ids = profile.following.map((id)=>DB.ObjectID(id)); let ids = profile.following.map((id)=>DB.ObjectID(id));
@@ -133,21 +157,7 @@ postDB = (DB)=>{
] ]
}; };
return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().then(async (posts)=>{ 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 await filterPrivateGroups(posts, profile);
let filteredPosts = [];
for(p in posts){
if(!DB.isGroupPrivate(p.profileid) && !DB.isGroupPrivate(p.toProfile)){
filteredPosts.push(p);
}
if(DB.isGroupPrivate(p.profileid) && !profile[p.profileid]){
continue;
}
if(DB.isGroupPrivate(p.toProfile) && !profile[p.toProfile]){
continue;
}
filteredPosts.push(p);
}
return posts;
}).catch((err)=>{ }).catch((err)=>{
console.log(err); console.log(err);
return false; return false;
@@ -156,12 +166,27 @@ postDB = (DB)=>{
DB.getPostsOfUser = (userId) => { DB.getPostsOfUser = (userId) => {
let userid = DB.ObjectID(userId); let userid = DB.ObjectID(userId);
console.log("getPostsOfUser")
return DB.postCols.find({userid}).sort({_id: -1}).limit(20).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;
}); });
} }
DB.getImagesOfUser = (userId) => {
let userid = DB.ObjectID(userId);
let query = {
userid,
content: {
$regex: "@image"
}
}
return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().catch((err)=>{
console.log(err);
return false;
});
}
DB.getPost = (postId) => { DB.getPost = (postId) => {
let _id = DB.ObjectID(postId); let _id = DB.ObjectID(postId);
return DB.postCols.findOne({_id}).catch((err)=>{ return DB.postCols.findOne({_id}).catch((err)=>{