rename to profile, users are people

This commit is contained in:
Adolfo Reyna
2021-08-18 07:57:19 -07:00
parent 416c14d03b
commit 95a7bcb3ff
6 changed files with 46 additions and 33 deletions

View File

@@ -3,19 +3,19 @@ const Post = require("./../def/post.js")
postDB = (DB)=>{ postDB = (DB)=>{
DB.postCols = DB.db.db(DBName).collection("posts"); DB.postCols = DB.db.db(DBName).collection("posts");
DB.newPost = (postObj) => { DB.newPost = (postObj) => {
console.log(postObj)
return DB.postCols.insertOne(postObj.toObj()).catch((err)=>{ return DB.postCols.insertOne(postObj.toObj()).catch((err)=>{
console.log(err); console.log(err);
return false; return false;
}); });
} }
DB.newReaction = (postid, userid, reaction) => { DB.newReaction = (postid, profileid, reaction) => {
const id = DB.ObjectID(postid); const id = DB.ObjectID(postid);
let update = { let update = {
$set:{ $set:{
["reactions." + userid]: reaction, ["reactions." + profileid]: reaction,
lastUpdated: new Date() lastUpdated: new Date()
} }
} }
@@ -78,12 +78,16 @@ postDB = (DB)=>{
} }
DB.getPosts = (profileId) => { DB.getPosts = (profileId) => {
let query = profileId ? { let query = {};
$or: [ if(profileId) {
{_id: profileId}, const id = DB.ObjectID(profileId);
{toUser: profileId} query = {
] $or: [
} : {}; {profileid: id},
{toProfile: id}
]
};
}
return DB.postCols.find(query).sort({_id: -1}).toArray().catch((err)=>{ return DB.postCols.find(query).sort({_id: -1}).toArray().catch((err)=>{
console.log(err); console.log(err);
return false; return false;

View File

@@ -23,9 +23,9 @@ userDB = (DB) => {
return r; return r;
} }
DB.getProfiles = async (userId) => { DB.getProfiles = async (profileId) => {
const userid = DB.ObjectID(userId); const _id = DB.ObjectID(profileId);
return await DB.profileCols.find({ userid }).toArray().catch((err) => { return await DB.profileCols.find({ _id }).toArray().catch((err) => {
console.log(err); console.log(err);
return false; return false;
}); });

View File

@@ -1,14 +1,14 @@
class Post { class Post {
constructor(info){ constructor(info){
if(!info || !info.userid) throw "Can not construct empty posts"; if(!info || !info.profileid) throw "Can not construct empty posts";
this.userid = info.userid; this.profileid = info.profileid;
this.content = info.content; this.content = info.content;
this.createdAt = info.createdAt || new Date(); this.createdAt = info.createdAt || new Date();
this.reactions = info.reactions || {}; this.reactions = info.reactions || {};
this.comments = info.comments || []; this.comments = info.comments || [];
//This should record edits //This should record edits
this.contentHistory = info.contentHistory || []; this.contentHistory = info.contentHistory || [];
this.toUser = info.toUser || ''; this.toProfile = info.toProfile || '';
// Any reaction or comment updates this ts, // Any reaction or comment updates this ts,
// this will be used as index to query new posts // this will be used as index to query new posts
this.lastUpdated = info.lastUpdated || this.createdAt; this.lastUpdated = info.lastUpdated || this.createdAt;
@@ -20,26 +20,26 @@ class Post {
} }
addReaction(reaction){ addReaction(reaction){
if(this.reactions[reaction.userid] && if(this.reactions[reaction.profileid] &&
this.reactions[reaction.userid].type === reaction.type){ this.reactions[reaction.profileid].type === reaction.type){
delete this.reactions[reaction.userid] delete this.reactions[reaction.profileid]
} }
else{ else{
this.reactions[reaction.userid] = reaction; this.reactions[reaction.profileid] = reaction;
} }
this.lastUpdated = new Date(); this.lastUpdated = new Date();
} }
toObj=function(){ toObj=function(){
let r = {} let r = {}
r.userid = this.userid; r.profileid = this.profileid;
r.content = this.content; r.content = this.content;
r.createdAt = this.createdAt; r.createdAt = this.createdAt;
r.reactions = this.reactions; r.reactions = this.reactions;
r.comments = this.comments; r.comments = this.comments;
r.contentHistory = this.contentHistory; r.contentHistory = this.contentHistory;
r.lastUpdated = this.lastUpdated; r.lastUpdated = this.lastUpdated;
r.toUser = this.toUser; r.toProfile = this.toProfile;
return r; return r;
} }
} }

View File

@@ -59,7 +59,11 @@ DB.getDB.then((DB)=>{
// route for Home-Page // route for Home-Page
app.get('/', sessionChecker, async (req, res) => { 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"}); res.json({status: "ok"});
}); });

View File

@@ -11,22 +11,27 @@ const getUserId = function(req){
DB.getDB.then((DB)=>{ DB.getDB.then((DB)=>{
const getProfileId = (req)=>{
return DB.ObjectID(req.cookies.profile_id);
}
router.get("/", async (req, res) => { router.get("/", async (req, res) => {
let posts = await DB.getPosts(); let posts = await DB.getPosts();
return res.json(posts) return res.json(posts)
}); });
router.get("/usr/:id", async (req, res) => { router.get("/usr/:id", async (req, res) => {
const userid = req.params.id; const profileId = req.params.id;
const posts = await DB.getPosts(userid); const posts = await DB.getPosts(profileId);
return res.json(posts) return res.json(posts)
}); });
router.get("/new", async (req, res) => { router.get("/new", async (req, res) => {
let post = { let post = {
userid: getUserId(req), profileid: getProfileId(req),
...req.query ...req.query
} }
post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined;
let postObj = new Post(post); let postObj = new Post(post);
let dbr = await DB.newPost(postObj) let dbr = await DB.newPost(postObj)
post = postObj.toObj(); post = postObj.toObj();
@@ -38,11 +43,11 @@ DB.getDB.then((DB)=>{
}); });
router.get("/newComment", async (req, res) => { router.get("/newComment", async (req, res) => {
let userid = getUserId(req); let profileid = getProfileId(req);
let postid = req.query.postid; let postid = req.query.postid;
let content = req.query.content; let content = req.query.content;
let comment = { let comment = {
userid: userid, profileid: profileid,
content: content, content: content,
createdAt: new Date(), createdAt: new Date(),
lastUpdated: new Date(), lastUpdated: new Date(),
@@ -58,7 +63,7 @@ DB.getDB.then((DB)=>{
}); });
router.get("/react", async (req, res) => { router.get("/react", async (req, res) => {
let userid = getUserId(req); let userid = getProfileId(req);
let postid = req.query.postid; let postid = req.query.postid;
let reaction = { let reaction = {
type: "like", type: "like",
@@ -73,7 +78,7 @@ DB.getDB.then((DB)=>{
}) })
router.get("/unreact", async (req, res) => { router.get("/unreact", async (req, res) => {
let userid = getUserId(req); let userid = getProfileId(req);
let postid = req.query.postid; let postid = req.query.postid;
r = await DB.removeReaction(postid, userid); r = await DB.removeReaction(postid, userid);
console.log(r) console.log(r)
@@ -83,7 +88,7 @@ DB.getDB.then((DB)=>{
}); });
router.get("/comment/react", async (req, res) => { router.get("/comment/react", async (req, res) => {
let userid = getUserId(req); let userid = getProfileId(req);
let postid = req.query.postid; let postid = req.query.postid;
let commentDate = new Date(req.query.commentDate); let commentDate = new Date(req.query.commentDate);
let reaction = { let reaction = {

View File

@@ -19,11 +19,11 @@ DB.getDB.then((DB)=>{
}); });
router.get("/:id", async (req, res) => { router.get("/:id", async (req, res) => {
let userid = req.params.id; let profileId = req.params.id;
let user = await DB.getProfile(userid); let profile = await DB.getProfile(profileId);
return res.json({ return res.json({
status: "ok", status: "ok",
user ... profile
}); });
}); });