posting and commenting

This commit is contained in:
Adolfo Reyna
2021-07-29 10:25:05 -07:00
parent 6904ad913e
commit 4069054117
4 changed files with 119 additions and 2 deletions

View File

@@ -11,8 +11,43 @@ postDB = (DB)=>{
}); });
} }
DB.newReaction = (postid, userid, reaction) => {
const id = DB.ObjectID(postid);
let update = {
$set:{
reactions:{
[userid]: reaction
},
lastUpdated: new Date()
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.removeReaction = (postid, userid) => {
const id = DB.ObjectID(postid);
let update = {
$unset:{
reactions:{
[userid]: {}
}
},
$set: {
lastUpdated: new Date()
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.newComment = (postid, comment) => { DB.newComment = (postid, comment) => {
return DB.postCols.updateOne(postid, { const id = DB.ObjectID(postid);
return DB.postCols.updateOne({_id: id}, {
$push: { $push: {
comments: comment comments: comment
}, },
@@ -25,6 +60,25 @@ postDB = (DB)=>{
}); });
} }
DB.newCommentReaction = (postid, commentDate, userid, reaction) => {
const id = DB.ObjectID(postid);
let update = {
$set:{
["comments.$.reactions." + userid]: reaction,
"comments.$.lastUpdated": new Date(),
lastUpdated: new Date()
}
}
console.log(JSON.stringify(update));
return DB.postCols.updateOne({
_id: id,
"comments.createdAt": commentDate
}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.getPosts = (userObj) => { DB.getPosts = (userObj) => {
return DB.postCols.find().toArray().catch((err)=>{ return DB.postCols.find().toArray().catch((err)=>{
console.log(err); console.log(err);

View File

@@ -2,7 +2,7 @@ class User {
constructor(json){ constructor(json){
this.username = 'aeroreyna'; this.username = 'aeroreyna';
this.following = []; this.following = [];
this.lastUpdate = new Date();ß this.lastUpdate = new Date();
this.newsFeedCache = []; this.newsFeedCache = [];
this.isGroup = false; this.isGroup = false;
} }

View File

@@ -147,8 +147,69 @@ DB.getDB.then((DB)=>{
return res.json({ return res.json({
status: "ok" status: "ok"
}) })
});
app.get("/post/newComment", sessionChecker, async (req, res) => {
let userid = getUserId(req);
let postid = req.query.postid;
let content = req.query.content;
let comment = {
userid: userid,
content: content,
createdAt: new Date(),
lastUpdated: new Date(),
reactions: {}
}
console.log("comment", postid, comment);
r = await DB.newComment(postid, comment);
console.log(r)
return res.json({
status: "ok"
})
});
app.get("/post/react", sessionChecker, async (req, res) => {
let userid = getUserId(req);
let postid = req.query.postid;
let reaction = {
type: "like",
createdAt: new Date()
};
console.log("reaction". postid, reaction);
r = await DB.newReaction(postid, userid, reaction);
console.log(r);
return res.json({
status: "ok"
});
}) })
app.get("/post/unreact", sessionChecker, async (req, res) => {
let userid = getUserId(req);
let postid = req.query.postid;
r = await DB.removeReaction(postid, userid);
console.log(r)
return res.json({
status: "ok"
})
});
app.get("/post/comment/react", sessionChecker, async (req, res) => {
let userid = getUserId(req);
let postid = req.query.postid;
let commentDate = new Date(req.query.commentDate);
let reaction = {
type: "like",
createdAt: new Date()
};
console.log(req.query)
console.log("comment reaction", postid, commentDate, reaction);
r = await DB.newCommentReaction(postid, commentDate, userid, reaction);
console.log(r)
return res.json({
status: "ok"
})
});
// route for handling 404 requests(unavailable routes) // route for handling 404 requests(unavailable routes)
app.use(function (req, res, next) { app.use(function (req, res, next) {
res.status(404).send("Sorry can't find that!") res.status(404).send("Sorry can't find that!")

View File

@@ -1,5 +1,6 @@
const mongo = require('mongodb'); const mongo = require('mongodb');
const MongoClient = mongo.MongoClient; const MongoClient = mongo.MongoClient;
const ObjectID = mongo.ObjectID;
const DBName = "EMI_SOCIAL"; const DBName = "EMI_SOCIAL";
const mongoUrl = process.env.MONGO_URL; const mongoUrl = process.env.MONGO_URL;
const postDB = require("./dbTools/post.js"); const postDB = require("./dbTools/post.js");
@@ -11,6 +12,7 @@ const getDB = new Promise((resolve, reject) => {
if (err) return reject(err); if (err) return reject(err);
DB.db = db; DB.db = db;
DB.ObjectID = ObjectID;
console.log("Connected to DB!"); console.log("Connected to DB!");
DB.usersCol = db.db(DBName).collection("users"); DB.usersCol = db.db(DBName).collection("users");
DB.tokensCol = db.db(DBName).collection("tokens"); DB.tokensCol = db.db(DBName).collection("tokens");