Files
EMI-Backend/dbTools/post.js
T

341 lines
11 KiB
JavaScript

const DBName = "EMI_SOCIAL";
const Post = require("./../def/post.js")
postDB = (DB)=>{
DB.postCols = DB.db.db(DBName).collection("posts");
DB.newPost = (postObj) => {
return DB.postCols.insertOne(postObj.toObj()).catch((err)=>{
console.log(err);
return false;
});
}
DB.removePost = (postid) => {
if(!DB.ObjectID.isValid(postid)) return false;
const _id = DB.ObjectID(postid);
return DB.postCols.deleteOne({_id}).catch((err)=>{
console.log(err);
return false;
});
}
DB.updatePost = (postid, newContent, oldContent) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);
let update = {
$set:{
content: newContent,
// lastUpdated: new Date() // add back when finish updating videos.
},
$push: {
contentHistory: oldContent
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.addTranslation = (postid, lang, translatedText) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);
let update = {
$set:{
["translations." + lang]: translatedText
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.newReaction = (postid, profileid, reaction) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);
let update = {
$set:{
["reactions." + profileid]: reaction,
lastUpdated: new Date()
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.removeReaction = (postid, profileid) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);
let update = {
$unset:{
["reactions." + profileid]: ""
},
$set: {
lastUpdated: new Date()
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.bookmarkPost = async (postid, profileId)=>{
if(!DB.ObjectID.isValid(postid)) return false;
const _id = DB.ObjectID(postid);
let update = {
$addToSet:{
bookmarks: DB.ObjectID(profileId)
}
}
return DB.postCols.updateOne({_id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.unbookmarkPost = async (postid, profileId)=>{
if(!DB.ObjectID.isValid(postid)) return false;
const _id = DB.ObjectID(postid);
let update = {
$pull:{
bookmarks: DB.ObjectID(profileId)
}
}
return DB.postCols.updateOne({_id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.newComment = (postid, comment) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);
return DB.postCols.updateOne({_id: id}, {
$push: {
comments: comment
},
$set: {
lastUpdated: new Date()
}
}).catch((err)=>{
console.log(err);
return false;
});
}
DB.newCommentReaction = (postid, commentDate, profileid, reaction) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);
let update = {
$set:{
["comments.$.reactions." + profileid]: reaction,
"comments.$.lastUpdated": new Date(),
lastUpdated: new Date()
}
}
return DB.postCols.updateOne({
_id: id,
"comments.createdAt": commentDate
}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.removeCommentReaction = (postid, commentDate, profileid) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);
let update = {
$unset:{
["comments.$.reactions." + profileid]: '',
"comments.$.lastUpdated": new Date(),
},
$set: {
lastUpdated: new Date()
}
}
return DB.postCols.updateOne({
_id: id,
"comments.createdAt": commentDate
}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.getPosts = async (profileId, viewerProfileId) => {
const profile = await DB.getProfile(viewerProfileId);
let query = {};
if(profileId && DB.ObjectID.isValid(profileId)) {
const id = DB.ObjectID(profileId);
query = {
$or: [
{profileid: id},
{toProfile: id}
]
};
} else {
if(profileId) return [];
}
return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().then(async (posts)=>{
return await filterPrivateGroups(posts, profile);
}).catch((err)=>{
console.log(err);
return false;
});
}
// Filter out posts that are posted by private groups that the viewer is not part of.
filterPrivateGroups = async (posts, profile) =>{
let validatedPosts = [];
let following = {};
profile.following.forEach(element => {
following[element] = 1;
});
for(i in posts){
let p = posts[i];
let isPostedByAPrivateGroup = await DB.isGroupPrivate(p.profileid);
let isPostedToAPrivateGroup = p.toProfile ? await DB.isGroupPrivate(p.toProfile) : false;
// Not private posts
if(!isPostedByAPrivateGroup && !isPostedToAPrivateGroup){
validatedPosts.push(p);
continue;
}
// skipps private post if viewer is not part of the group
if(isPostedByAPrivateGroup && !following[p.profileid]){
continue;
}
if(isPostedToAPrivateGroup && !following[p.toProfile]){
continue;
}
// Private but the user is part of the group
validatedPosts.push(p);
}
return validatedPosts;
}
DB.getFeed = async (profileId) => {
if(!DB.ObjectID.isValid(profileId)) return [];
const profile = await DB.getProfile(profileId);
if(!profile) return [];
let ids = profile.following.map((id)=>DB.ObjectID(id));
query = {
$or: [
{profileid: {
$in: ids
}},
{toProfile: {
$in: [...ids, DB.ObjectID(profileId)]
}}
],
nonOrganicType: null // Exlcude news
};
return DB.postCols.find(query).sort({lastUpdated: -1}).limit(30).toArray().then(async (posts)=>{
return await filterPrivateGroups(posts, profile);
}).catch((err)=>{
console.log(err);
return false;
});
}
DB.getPromotionalPosts = async (profileId) => {
if(!DB.ObjectID.isValid(profileId)) return [];
const profile = await DB.getProfile(profileId);
if(!profile) return [];
const query = {
nonOrganicType: null // Exlcude news
};
return DB.postCols.find(query).sort({lastUpdated: -1}).limit(50).toArray().then(async (posts)=>{
return await filterPrivateGroups(posts, profile);
}).catch((err)=>{
console.log(err);
return false;
});
}
// For all post with tags const query = { content: { $regex: '#\\w+', $options: 'i' } };
DB.getPostsByTag = async (tag, profileId, limit = 50) => {
if(!DB.ObjectID.isValid(profileId)) return [];
const profile = await DB.getProfile(profileId);
if(!profile) return [];
let query = {
content: {
"$regex": tag
},
nonOrganicType: null // Exlcude news
};
return DB.postCols.find(query).sort({lastUpdated: -1}).limit(limit).toArray().then(async (posts)=>{
return await filterPrivateGroups(posts, profile);
}).catch((err)=>{
console.log(err);
return false;
});
}
DB.getNews = async () => {
let query = {
nonOrganicType: 'News'
};
return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().catch((err)=>{
console.log(err);
return [];
});
}
DB.getPostsOfUser = (userId, limit = 20) => {
if(!DB.ObjectID.isValid(userId)) return [];
let userid = DB.ObjectID(userId);
console.log("getPostsOfUser")
return DB.postCols.find({userid}).sort({_id: -1}).limit(limit).toArray().catch((err)=>{
console.log(err);
return false;
});
}
DB.getMediaTagPostOfUser = async (profileId, viewerProfileId, mediaTag = "@image:", limit = 30) => {
if(!DB.ObjectID.isValid(profileId) || !DB.ObjectID.isValid(profileId)) return [];
const profile = await DB.getProfile(viewerProfileId);
let profileid = DB.ObjectID(profileId);
let query = {
$or: [
{profileid: profileid},
{toProfile: profileid}
],
content: {
"$regex": mediaTag
}
}
return DB.postCols.find(query).sort({_id: -1}).limit(limit).toArray().then(async (posts)=>{
return await filterPrivateGroups(posts, profile);
}).catch((err)=>{
console.log(err);
return false;
});
}
DB.getPost = (postId) => {
if(!DB.ObjectID.isValid(postId)) return [];
let _id = DB.ObjectID(postId);
return DB.postCols.findOne({_id}).catch((err)=>{
console.log(err);
return false;
});
}
const postsCache = {};
DB.getPostCached = async (postId, refresh = false) => {
if(!DB.ObjectID.isValid(postId)) return [];
if(!postsCache[postId] || refresh){
postsCache[postId] = await DB.getPost(postId);
}
return postsCache[postId];
}
}
module.exports = postDB;