Files
EMI-Backend/routes/post.js
2022-02-21 22:37:38 -08:00

222 lines
7.2 KiB
JavaScript

var express = require('express')
var router = express.Router()
const DB = require("./../mongoDB.js");
const Post = require("./../def/post.js");
const Notifications = require("./../notifications.js");
DB.getDB.then((DB) => {
const getProfileId = (req) => {
return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id);
};
const postBelongToProfile = (post, profileid) => {
if(!post) return false;
return post.profileid == profileid;
};
const generateNonOrganicPosts = async (req, profileid) => {
let posts = [];
// Popular Profiles
const popularProfiles = await DB.getPopularProfiles(5);
let content = "Active users to follow:\n";
popularProfiles.forEach((p)=>{
content += "@p:" + p._id + "\n";
});
let popularProfilesPost = new Post({profileid: 1, content, nonOrganicType: "PopularUsers"});
posts.push(popularProfilesPost);
// Popular Groups
const popularGroups = await DB.getPopularGroups(5);
content = "Popular groups to follow:\n";
popularGroups.forEach((p)=>{
content += "@p:" + p._id + "\n";
});
let popularGroupsPost = new Post({profileid: 1, content, nonOrganicType: "PopularGroups"});
posts.push(popularGroupsPost);
const yourFollowsInterests = await DB.getFriendsFriends(profileid);
content = "People your follow has these interests:\n";
yourFollowsInterests.forEach((p)=>{
content += "@p:" + p + "\n";
});
let yourFollowsInterestsPost = new Post({profileid: 1, content, nonOrganicType: "PopularUsers"});
posts.push(yourFollowsInterestsPost);
return posts;
};
const mergePosts = (organic, nonOrganic) => {
if(!organic || organic.length < 5) return nonOrganic;
//organic.push(...nonOrganic);
let mergedPosts = [];
while(organic.length + nonOrganic.length){
if(organic.length == 0){
mergedPosts.push(nonOrganic.shift());
continue;
}
if(nonOrganic.length == 0){
mergedPosts.push(organic.shift());
continue;
}
if(Math.random() < 0.2){
mergedPosts.push(nonOrganic.shift());
} else {
mergedPosts.push(organic.shift());
}
}
return mergedPosts;
};
router.get("/", async (req, res) => {
const profileid = getProfileId(req);
let organicPosts = await DB.getFeed(profileid);
//Add non-organic posts
const nonOrganicPosts = await generateNonOrganicPosts(req, profileid);
const posts = mergePosts(organicPosts, nonOrganicPosts);
return res.json(posts);
});
router.get("/usr/:id", async (req, res) => {
const profileId = req.params.id;
const viewerProdileId = getProfileId(req);
const profile = await DB.getProfileCache(profileId);
const posts = await DB.getPosts(profileId, viewerProdileId);
if (profile.isCourse) {
//check for subscription
//const viewerProdile = await DB.getProfileCache(viewerProdileId);
//if (!viewerProdile.subscription || viewerProdile.subscription < (new Date()-1)) {
// return res.json([posts[posts.length - 1]]);
//}
}
return res.json(posts);
});
router.post("/", async (req, res) => {
let post = {
profileid: getProfileId(req),
...req.body
}
if (post.toProfile && await DB.isGroupPrivate(post.toProfile)) {
let requestProfile = getProfileId(req) + "";
let group = await DB.getProfileCache(post.toProfile);
if (!group.subscribed[requestProfile] && group._id != requestProfile) {
return res.json({
status: "You are not part of this private group",
});
}
}
post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined;
let postObj = new Post(post);
let dbr = await DB.newPost(postObj);
post = postObj.toObj();
post._id = dbr.insertedId;
if (post.toProfile && post.toProfile != post.profileid) {
Notifications.youGotANewPost(post)
}
return res.json({
status: "ok",
...post
})
});
router.post("/react", async (req, res) => {
let profileid = getProfileId(req);
let postid = req.body.postid;
let reaction = {
type: "like",
createdAt: new Date()
};
r = await DB.newReaction(postid, profileid, reaction);
return res.json({
status: "ok"
});
})
router.post("/unreact", async (req, res) => {
let profileid = getProfileId(req);
let postid = req.body.postid;
r = await DB.removeReaction(postid, profileid);
return res.json({
status: "ok"
})
});
router.post("/bookmark", async (req, res) => {
let profileid = getProfileId(req);
let postid = req.body.postid;
r = await DB.bookmarkPost(postid, profileid);
return res.json({
status: "ok"
});
})
router.post("/unbookmark", async (req, res) => {
let profileid = getProfileId(req);
let postid = req.body.postid;
r = await DB.unbookmarkPost(postid, profileid);
return res.json({
status: "ok"
})
});
router.post("/comment/", async (req, res) => {
let profileid = getProfileId(req);
let postid = req.body.postid;
let content = req.body.content;
let comment = {
profileid: profileid,
content: content,
createdAt: new Date(),
lastUpdated: new Date(),
reactions: {}
}
Notifications.youGotANewPostComment(postid, profileid, content)
r = await DB.newComment(postid, comment);
return res.json({
status: "ok",
...comment
})
});
router.post("/comment/react", async (req, res) => {
let userid = getProfileId(req);
let postid = req.body.postid;
let commentDate = new Date(req.body.commentDate);
let reaction = {
type: "like",
createdAt: new Date()
};
r = await DB.newCommentReaction(postid, commentDate, userid, reaction);
return res.json({
status: "ok"
})
});
router.get("/:id", async (req, res) => {
const postId = req.params.id;
const post = await DB.getPost(postId);
return res.json({
status: "ok",
...post
});
});
router.delete("/:id", async (req, res) => {
const profileid = getProfileId(req) + '';
const postId = req.params.id;
const post = await DB.getPost(postId);
if(!postBelongToProfile(post, profileid))
return res.json({
status: "This post is not yours."
});
await DB.removePost(postId);
return res.json({
status: "ok"
});
});
});
module.exports = router