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)=>{
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 ? {
let query = {};
if(profileId) {
const id = DB.ObjectID(profileId);
query = {
$or: [
{_id: profileId},
{toUser: profileId}
{profileid: id},
{toProfile: id}
]
} : {};
};
}
return DB.postCols.find(query).sort({_id: -1}).toArray().catch((err)=>{
console.log(err);
return false;

View File

@@ -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;
});

View File

@@ -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;
}
}

View File

@@ -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"});
});

View File

@@ -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 = {

View File

@@ -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
});
});