Adding media and recent watch media
This commit is contained in:
@@ -12,6 +12,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -20,6 +21,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
DB.updatePost = (postid, newContent, oldContent) => {
|
||||
if(!DB.ObjectID.isValid(postid)) return false;
|
||||
const id = DB.ObjectID(postid);
|
||||
let update = {
|
||||
$set:{
|
||||
@@ -37,6 +39,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
DB.newReaction = (postid, profileid, reaction) => {
|
||||
if(!DB.ObjectID.isValid(postid)) return false;
|
||||
const id = DB.ObjectID(postid);
|
||||
let update = {
|
||||
$set:{
|
||||
@@ -51,6 +54,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
DB.removeReaction = (postid, profileid) => {
|
||||
if(!DB.ObjectID.isValid(postid)) return false;
|
||||
const id = DB.ObjectID(postid);
|
||||
let update = {
|
||||
$unset:{
|
||||
@@ -67,6 +71,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
DB.bookmarkPost = async (postid, profileId)=>{
|
||||
if(!DB.ObjectID.isValid(postid)) return false;
|
||||
const _id = DB.ObjectID(postid);
|
||||
let update = {
|
||||
$addToSet:{
|
||||
@@ -80,6 +85,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
DB.unbookmarkPost = async (postid, profileId)=>{
|
||||
if(!DB.ObjectID.isValid(postid)) return false;
|
||||
const _id = DB.ObjectID(postid);
|
||||
let update = {
|
||||
$pull:{
|
||||
@@ -93,6 +99,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
DB.newComment = (postid, comment) => {
|
||||
if(!DB.ObjectID.isValid(postid)) return false;
|
||||
const id = DB.ObjectID(postid);
|
||||
return DB.postCols.updateOne({_id: id}, {
|
||||
$push: {
|
||||
@@ -108,6 +115,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
DB.newCommentReaction = (postid, commentDate, profileid, reaction) => {
|
||||
if(!DB.ObjectID.isValid(postid)) return false;
|
||||
const id = DB.ObjectID(postid);
|
||||
let update = {
|
||||
$set:{
|
||||
@@ -126,6 +134,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
DB.removeCommentReaction = (postid, commentDate, profileid) => {
|
||||
if(!DB.ObjectID.isValid(postid)) return false;
|
||||
const id = DB.ObjectID(postid);
|
||||
let update = {
|
||||
$unset:{
|
||||
@@ -193,6 +202,7 @@ postDB = (DB)=>{
|
||||
}
|
||||
|
||||
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));
|
||||
@@ -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);
|
||||
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);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
DB.getImagesOfUser = (userId) => {
|
||||
let userid = DB.ObjectID(userId);
|
||||
DB.getMediaTagPostOfUser = (profileId, mediaTag = "@image:", limit = 20) => {
|
||||
if(!DB.ObjectID.isValid(profileId)) return [];
|
||||
let profileid = DB.ObjectID(profileId);
|
||||
let query = {
|
||||
userid,
|
||||
profileid,
|
||||
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);
|
||||
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);
|
||||
@@ -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;
|
||||
@@ -101,6 +101,15 @@ DB.getDB.then((DB) => {
|
||||
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) => {
|
||||
videoId = req.params.id;
|
||||
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) => {
|
||||
const postId = req.params.id;
|
||||
const post = await DB.getPost(postId);
|
||||
|
||||
Reference in New Issue
Block a user