new posts
This commit is contained in:
36
dbTools/post.js
Normal file
36
dbTools/post.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const DBName = "EMI_SOCIAL";
|
||||
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.newComment = (postid, comment) => {
|
||||
return DB.postCols.updateOne(postid, {
|
||||
$push: {
|
||||
comments: comment
|
||||
},
|
||||
$set: {
|
||||
lastUpdated: new Date()
|
||||
}
|
||||
}).catch((err)=>{
|
||||
console.log(err);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
DB.getPosts = (userObj) => {
|
||||
return DB.postCols.find().toArray().catch((err)=>{
|
||||
console.log(err);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = postDB;
|
||||
45
def/post.js
Normal file
45
def/post.js
Normal file
@@ -0,0 +1,45 @@
|
||||
class Post {
|
||||
constructor(info){
|
||||
if(!info || !info.userid) throw "Can not construct empty posts"
|
||||
this.userid = info.userid;
|
||||
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 || [];
|
||||
// Any reaction or comment updates this ts,
|
||||
// this will be used as index to query new posts
|
||||
this.lastUpdated = info.lastUpdated || this.createdAt;
|
||||
}
|
||||
|
||||
addComment(comment){
|
||||
this.comments.push(comment);
|
||||
this.lastUpdated = new Date();
|
||||
}
|
||||
|
||||
addReaction(reaction){
|
||||
if(this.reactions[reaction.userid] &&
|
||||
this.reactions[reaction.userid].type === reaction.type){
|
||||
delete this.reactions[reaction.userid]
|
||||
}
|
||||
else{
|
||||
this.reactions[reaction.userid] = reaction;
|
||||
}
|
||||
this.lastUpdated = new Date();
|
||||
}
|
||||
|
||||
toObj=function(){
|
||||
let r = {}
|
||||
r.userid = this.userid;
|
||||
r.content = this.content;
|
||||
r.createdAt = this.createdAt;
|
||||
r.reactions = this.reactions;
|
||||
r.comments = this.comments;
|
||||
r.contentHistory = this.contentHistory;
|
||||
r.lastUpdated = this.lastUpdated;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Post;
|
||||
12
def/user.js
12
def/user.js
@@ -7,8 +7,14 @@ class User {
|
||||
this.isGroup = false;
|
||||
}
|
||||
|
||||
newPost(){}
|
||||
newComment(){}
|
||||
newReaction(){}
|
||||
newPost(content){
|
||||
|
||||
}
|
||||
newComment(post){
|
||||
|
||||
}
|
||||
newReaction(){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
24
index.js
24
index.js
@@ -29,6 +29,9 @@ const getUserId = function(req){
|
||||
return user_sid
|
||||
}
|
||||
|
||||
// Definitions
|
||||
const Post = require("./def/post.js")
|
||||
|
||||
DB.getDB.then((DB)=>{
|
||||
|
||||
// middleware function to check for logged-in users
|
||||
@@ -78,8 +81,8 @@ DB.getDB.then((DB)=>{
|
||||
maxAge: 1000 * 60 * 60 * 24 * 30, // would expire after 30 days
|
||||
httpOnly: true, // The cookie only accessible by the web server
|
||||
//signed: true // Indicates if the cookie should be signed
|
||||
sameSite: 'none',
|
||||
secure: true,
|
||||
sameSite: 'none', // This and secure are required for properly
|
||||
secure: true, // manage cockies in cros-domain
|
||||
};
|
||||
|
||||
// route for user Login
|
||||
@@ -129,6 +132,23 @@ DB.getDB.then((DB)=>{
|
||||
return logout(req, res);
|
||||
});
|
||||
|
||||
app.get("/post/", sessionChecker, async (req, res) => {
|
||||
let posts = await DB.getPosts();
|
||||
return res.json(posts)
|
||||
});
|
||||
|
||||
app.get("/post/new", sessionChecker, async (req, res) => {
|
||||
let post = {
|
||||
userid: getUserId(req),
|
||||
content: req.query.content
|
||||
}
|
||||
let postObj = new Post(post);
|
||||
DB.newPost(postObj)
|
||||
return res.json({
|
||||
status: "ok"
|
||||
})
|
||||
})
|
||||
|
||||
// route for handling 404 requests(unavailable routes)
|
||||
app.use(function (req, res, next) {
|
||||
res.status(404).send("Sorry can't find that!")
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
const mongo = require('mongodb');
|
||||
const MongoClient = mongo.MongoClient;
|
||||
const DBName = "EMI_SOCIAL";
|
||||
const mongoUrl = process.env.MONGO_URL
|
||||
const mongoUrl = process.env.MONGO_URL;
|
||||
const postDB = require("./dbTools/post.js");
|
||||
|
||||
|
||||
const getDB = new Promise((resolve, reject) => {
|
||||
@@ -12,7 +13,7 @@ const getDB = new Promise((resolve, reject) => {
|
||||
DB.db = db;
|
||||
console.log("Connected to DB!");
|
||||
DB.usersCol = db.db(DBName).collection("users");
|
||||
DB.tokensCol = db.db(DBName).collection("AmArr5EHx7sn3Gr");
|
||||
DB.tokensCol = db.db(DBName).collection("tokens");
|
||||
|
||||
DB.checkSessionOnDB = async (session_id, user_sid)=>{
|
||||
const temp_id = new mongo.ObjectID(session_id);
|
||||
@@ -47,6 +48,8 @@ const getDB = new Promise((resolve, reject) => {
|
||||
return DB.tokensCol.deleteOne({"_id":temp_id});
|
||||
}
|
||||
|
||||
postDB(DB);
|
||||
|
||||
resolve(DB);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user