From 95a7bcb3ff08cec2aadc813db52e0040f5647479 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Wed, 18 Aug 2021 07:57:19 -0700 Subject: [PATCH] rename to profile, users are people --- dbTools/post.js | 22 +++++++++++++--------- dbTools/profile.js | 6 +++--- def/post.js | 18 +++++++++--------- index.js | 6 +++++- routes/post.js | 21 +++++++++++++-------- routes/profile.js | 6 +++--- 6 files changed, 46 insertions(+), 33 deletions(-) diff --git a/dbTools/post.js b/dbTools/post.js index 1ad64ce..f734b09 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -3,19 +3,19 @@ const Post = require("./../def/post.js") postDB = (DB)=>{ DB.postCols = DB.db.db(DBName).collection("posts"); + DB.newPost = (postObj) => { - console.log(postObj) return DB.postCols.insertOne(postObj.toObj()).catch((err)=>{ console.log(err); return false; }); } - DB.newReaction = (postid, userid, reaction) => { + DB.newReaction = (postid, profileid, reaction) => { const id = DB.ObjectID(postid); let update = { $set:{ - ["reactions." + userid]: reaction, + ["reactions." + profileid]: reaction, lastUpdated: new Date() } } @@ -78,12 +78,16 @@ postDB = (DB)=>{ } DB.getPosts = (profileId) => { - let query = profileId ? { - $or: [ - {_id: profileId}, - {toUser: profileId} - ] - } : {}; + let query = {}; + if(profileId) { + const id = DB.ObjectID(profileId); + query = { + $or: [ + {profileid: id}, + {toProfile: id} + ] + }; + } return DB.postCols.find(query).sort({_id: -1}).toArray().catch((err)=>{ console.log(err); return false; diff --git a/dbTools/profile.js b/dbTools/profile.js index b5caade..97daadd 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -23,9 +23,9 @@ userDB = (DB) => { return r; } - DB.getProfiles = async (userId) => { - const userid = DB.ObjectID(userId); - return await DB.profileCols.find({ userid }).toArray().catch((err) => { + DB.getProfiles = async (profileId) => { + const _id = DB.ObjectID(profileId); + return await DB.profileCols.find({ _id }).toArray().catch((err) => { console.log(err); return false; }); diff --git a/def/post.js b/def/post.js index 8856ad3..e900285 100644 --- a/def/post.js +++ b/def/post.js @@ -1,14 +1,14 @@ class Post { constructor(info){ - if(!info || !info.userid) throw "Can not construct empty posts"; - this.userid = info.userid; + if(!info || !info.profileid) throw "Can not construct empty posts"; + this.profileid = info.profileid; this.content = info.content; this.createdAt = info.createdAt || new Date(); this.reactions = info.reactions || {}; this.comments = info.comments || []; //This should record edits this.contentHistory = info.contentHistory || []; - this.toUser = info.toUser || ''; + this.toProfile = info.toProfile || ''; // Any reaction or comment updates this ts, // this will be used as index to query new posts this.lastUpdated = info.lastUpdated || this.createdAt; @@ -20,26 +20,26 @@ class Post { } addReaction(reaction){ - if(this.reactions[reaction.userid] && - this.reactions[reaction.userid].type === reaction.type){ - delete this.reactions[reaction.userid] + if(this.reactions[reaction.profileid] && + this.reactions[reaction.profileid].type === reaction.type){ + delete this.reactions[reaction.profileid] } else{ - this.reactions[reaction.userid] = reaction; + this.reactions[reaction.profileid] = reaction; } this.lastUpdated = new Date(); } toObj=function(){ let r = {} - r.userid = this.userid; + r.profileid = this.profileid; r.content = this.content; r.createdAt = this.createdAt; r.reactions = this.reactions; r.comments = this.comments; r.contentHistory = this.contentHistory; r.lastUpdated = this.lastUpdated; - r.toUser = this.toUser; + r.toProfile = this.toProfile; return r; } } diff --git a/index.js b/index.js index 2e48a0a..5ed3a53 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,11 @@ DB.getDB.then((DB)=>{ // route for Home-Page app.get('/', sessionChecker, async (req, res) => { - if(req.userInfo) return res.json({status: "ok", userInfo: req.userInfo}); + if(req.userInfo) return res.json({ + status: "ok", + userInfo: req.userInfo, + profileInfo:req.profileInfo + }); res.json({status: "ok"}); }); diff --git a/routes/post.js b/routes/post.js index f1db037..18791d2 100644 --- a/routes/post.js +++ b/routes/post.js @@ -11,22 +11,27 @@ const getUserId = function(req){ DB.getDB.then((DB)=>{ + const getProfileId = (req)=>{ + return DB.ObjectID(req.cookies.profile_id); + } + router.get("/", async (req, res) => { let posts = await DB.getPosts(); return res.json(posts) }); router.get("/usr/:id", async (req, res) => { - const userid = req.params.id; - const posts = await DB.getPosts(userid); + const profileId = req.params.id; + const posts = await DB.getPosts(profileId); return res.json(posts) }); router.get("/new", async (req, res) => { let post = { - userid: getUserId(req), + profileid: getProfileId(req), ...req.query } + post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined; let postObj = new Post(post); let dbr = await DB.newPost(postObj) post = postObj.toObj(); @@ -38,11 +43,11 @@ DB.getDB.then((DB)=>{ }); router.get("/newComment", async (req, res) => { - let userid = getUserId(req); + let profileid = getProfileId(req); let postid = req.query.postid; let content = req.query.content; let comment = { - userid: userid, + profileid: profileid, content: content, createdAt: new Date(), lastUpdated: new Date(), @@ -58,7 +63,7 @@ DB.getDB.then((DB)=>{ }); router.get("/react", async (req, res) => { - let userid = getUserId(req); + let userid = getProfileId(req); let postid = req.query.postid; let reaction = { type: "like", @@ -73,7 +78,7 @@ DB.getDB.then((DB)=>{ }) router.get("/unreact", async (req, res) => { - let userid = getUserId(req); + let userid = getProfileId(req); let postid = req.query.postid; r = await DB.removeReaction(postid, userid); console.log(r) @@ -83,7 +88,7 @@ DB.getDB.then((DB)=>{ }); router.get("/comment/react", async (req, res) => { - let userid = getUserId(req); + let userid = getProfileId(req); let postid = req.query.postid; let commentDate = new Date(req.query.commentDate); let reaction = { diff --git a/routes/profile.js b/routes/profile.js index a21e528..2801e62 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -19,11 +19,11 @@ DB.getDB.then((DB)=>{ }); router.get("/:id", async (req, res) => { - let userid = req.params.id; - let user = await DB.getProfile(userid); + let profileId = req.params.id; + let profile = await DB.getProfile(profileId); return res.json({ status: "ok", - user + ... profile }); });