Adding media and recent watch media

This commit is contained in:
aeroreyna
2022-12-30 07:54:49 -05:00
parent b616a6f6f9
commit 2e339f6b36
2 changed files with 112 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ postDB = (DB)=>{
} }
DB.removePost = (postid) => { DB.removePost = (postid) => {
if(!DB.ObjectID.isValid(postid)) return false;
const _id = DB.ObjectID(postid); const _id = DB.ObjectID(postid);
return DB.postCols.deleteOne({_id}).catch((err)=>{ return DB.postCols.deleteOne({_id}).catch((err)=>{
console.log(err); console.log(err);
@@ -20,6 +21,7 @@ postDB = (DB)=>{
} }
DB.updatePost = (postid, newContent, oldContent) => { DB.updatePost = (postid, newContent, oldContent) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid); const id = DB.ObjectID(postid);
let update = { let update = {
$set:{ $set:{
@@ -37,6 +39,7 @@ postDB = (DB)=>{
} }
DB.newReaction = (postid, profileid, reaction) => { DB.newReaction = (postid, profileid, reaction) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid); const id = DB.ObjectID(postid);
let update = { let update = {
$set:{ $set:{
@@ -51,6 +54,7 @@ postDB = (DB)=>{
} }
DB.removeReaction = (postid, profileid) => { DB.removeReaction = (postid, profileid) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid); const id = DB.ObjectID(postid);
let update = { let update = {
$unset:{ $unset:{
@@ -67,6 +71,7 @@ postDB = (DB)=>{
} }
DB.bookmarkPost = async (postid, profileId)=>{ DB.bookmarkPost = async (postid, profileId)=>{
if(!DB.ObjectID.isValid(postid)) return false;
const _id = DB.ObjectID(postid); const _id = DB.ObjectID(postid);
let update = { let update = {
$addToSet:{ $addToSet:{
@@ -80,6 +85,7 @@ postDB = (DB)=>{
} }
DB.unbookmarkPost = async (postid, profileId)=>{ DB.unbookmarkPost = async (postid, profileId)=>{
if(!DB.ObjectID.isValid(postid)) return false;
const _id = DB.ObjectID(postid); const _id = DB.ObjectID(postid);
let update = { let update = {
$pull:{ $pull:{
@@ -93,6 +99,7 @@ postDB = (DB)=>{
} }
DB.newComment = (postid, comment) => { DB.newComment = (postid, comment) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid); const id = DB.ObjectID(postid);
return DB.postCols.updateOne({_id: id}, { return DB.postCols.updateOne({_id: id}, {
$push: { $push: {
@@ -108,6 +115,7 @@ postDB = (DB)=>{
} }
DB.newCommentReaction = (postid, commentDate, profileid, reaction) => { DB.newCommentReaction = (postid, commentDate, profileid, reaction) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid); const id = DB.ObjectID(postid);
let update = { let update = {
$set:{ $set:{
@@ -126,6 +134,7 @@ postDB = (DB)=>{
} }
DB.removeCommentReaction = (postid, commentDate, profileid) => { DB.removeCommentReaction = (postid, commentDate, profileid) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid); const id = DB.ObjectID(postid);
let update = { let update = {
$unset:{ $unset:{
@@ -193,6 +202,7 @@ postDB = (DB)=>{
} }
DB.getFeed = async (profileId) => { DB.getFeed = async (profileId) => {
if(!DB.ObjectID.isValid(profileId)) return [];
const profile = await DB.getProfile(profileId); const profile = await DB.getProfile(profileId);
if(!profile) return []; if(!profile) return [];
let ids = profile.following.map((id)=>DB.ObjectID(id)); let ids = profile.following.map((id)=>DB.ObjectID(id));
@@ -226,30 +236,33 @@ postDB = (DB)=>{
}); });
} }
DB.getPostsOfUser = (userId) => { DB.getPostsOfUser = (userId, limit = 20) => {
if(!DB.ObjectID.isValid(userId)) return [];
let userid = DB.ObjectID(userId); let userid = DB.ObjectID(userId);
console.log("getPostsOfUser") 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(limit).toArray().catch((err)=>{
console.log(err); console.log(err);
return false; return false;
}); });
} }
DB.getImagesOfUser = (userId) => { DB.getMediaTagPostOfUser = (profileId, mediaTag = "@image:", limit = 20) => {
let userid = DB.ObjectID(userId); if(!DB.ObjectID.isValid(profileId)) return [];
let profileid = DB.ObjectID(profileId);
let query = { let query = {
userid, profileid,
content: { content: {
$regex: "@image" "$regex": mediaTag
} }
} }
return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().catch((err)=>{ return DB.postCols.find(query).sort({_id: -1}).limit(limit).toArray().catch((err)=>{
console.log(err); console.log(err);
return false; return false;
}); });
} }
DB.getPost = (postId) => { DB.getPost = (postId) => {
if(!DB.ObjectID.isValid(postId)) return [];
let _id = DB.ObjectID(postId); let _id = DB.ObjectID(postId);
return DB.postCols.findOne({_id}).catch((err)=>{ return DB.postCols.findOne({_id}).catch((err)=>{
console.log(err); console.log(err);
@@ -257,6 +270,15 @@ postDB = (DB)=>{
}); });
} }
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; module.exports = postDB;

View File

@@ -101,6 +101,15 @@ DB.getDB.then((DB) => {
return res.json(posts); return res.json(posts);
}); });
router.get("/usr/:id/images", async (req, res) => {
const profileid = req.params.id;
const posts = await DB.getMediaTagPostOfUser(profileid);
return res.json({
status: "ok",
posts
});
});
router.get("/video/:id", async (req, res) => { router.get("/video/:id", async (req, res) => {
videoId = req.params.id; videoId = req.params.id;
return res.json([]); return res.json([]);
@@ -218,6 +227,80 @@ DB.getDB.then((DB) => {
}) })
}); });
router.get("/images", async (req, res) => {
const profileid = getProfileId(req);
const posts = await DB.getMediaTagPostOfUser(profileid);
return res.json({
status: "ok",
posts
});
});
router.get("/embedded", async (req, res) => {
const profileid = getProfileId(req);
const posts = await DB.getMediaTagPostOfUser(profileid, "@iframe:");
return res.json({
status: "ok",
posts
});
});
router.get("/media", async (req, res) => {
const profileid = getProfileId(req);
const posts = await DB.getMediaTagPostOfUser(profileid, "@youtube:|@vimeo:|@hls:");
return res.json({
status: "ok",
posts
});
});
router.get("/course/recent", async (req, res) => {
const profileid = getProfileId(req);
const profile = await DB.getProfileCache(profileid);
const mediaPostsId = []
const recentMedia = {};
const mediaProfileP = []
let postPromises = [];
// Get watch information from profile data
Object.keys(profile.data).forEach(key => {
// So far we only use watch info using postid keys
if(DB.ObjectID.isValid(key)){
mediaPostsId.push(profile.data[key]);
postPromises.push(DB.getPostCached(profile.data[key].postId));
if(!recentMedia[profile.data[key].profileId]){
recentMedia[profile.data[key].profileId] = [];
mediaProfileP.push(DB.getProfileCache(profile.data[key].profileId));
}
}
});
recentMediaArray = mediaPostsId.sort((a,b)=>{
return b.ts - a.ts;
});
const postObjs = await Promise.all(postPromises);
const postObjsMap = postObjs.reduce((map, item)=>{
map[item._id] = item;
return map
}, {});
recentMediaArray.forEach(record => {
const postAndRecord = {
watchRecord: record,
... postObjsMap[record.postId]
}
recentMedia[record.profileId].push(postAndRecord);
});
// Transform profiles array to dict
let mediaProfile = await Promise.all(mediaProfileP);
let mediaProfileMap = mediaProfile.reduce((dict, item) => {
dict[item._id] = item;
return dict;
}, {});
return res.json({
status: "ok",
recentMedia,
mediaProfileMap,
});
});
router.get("/:id", async (req, res) => { router.get("/:id", async (req, res) => {
const postId = req.params.id; const postId = req.params.id;
const post = await DB.getPost(postId); const post = await DB.getPost(postId);