diff --git a/dbTools/post.js b/dbTools/post.js index e142e25..1ad64ce 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -77,8 +77,13 @@ postDB = (DB)=>{ }); } - DB.getPosts = (userid) => { - let query = userid ? {userid} : {}; + DB.getPosts = (profileId) => { + let query = profileId ? { + $or: [ + {_id: profileId}, + {toUser: profileId} + ] + } : {}; 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 new file mode 100644 index 0000000..b5caade --- /dev/null +++ b/dbTools/profile.js @@ -0,0 +1,49 @@ +const DBName = "EMI_SOCIAL"; + +let userProfileCache = {}; + +userDB = (DB) => { + DB.profileCols = DB.db.db(DBName).collection("profiles"); + + DB.newProfile = (profileObj) => { + return DB.profileCols.insertOne(profileObj.toObj()).catch((err) => { + console.log(err); + return false; + }); + } + + DB.getProfile = async (profileId) => { + if (userProfileCache[profileId]) return userProfileCache[profileId]; + const _id = DB.ObjectID(profileId); + let r = await DB.profileCols.findOne({ _id }).catch((err) => { + console.log(err); + return false; + }); + if (r) userProfileCache[profileId] = r; + return r; + } + + DB.getProfiles = async (userId) => { + const userid = DB.ObjectID(userId); + return await DB.profileCols.find({ userid }).toArray().catch((err) => { + console.log(err); + return false; + }); + } + + DB.latestProfile = async (userId) => { + const userid = DB.ObjectID(userId); + let r = await DB.profileCols.find({ userid }) + .sort({ lastUpdate: -1 }).limit(1) + .toArray().catch((err) => { + console.log(err); + return false; + }); + if (r[0]) userProfileCache[r[0].id] = r[0]; + return r[0]; + } + + +} + +module.exports = userDB; \ No newline at end of file diff --git a/dbTools/user.js b/dbTools/user.js deleted file mode 100644 index d715c49..0000000 --- a/dbTools/user.js +++ /dev/null @@ -1,31 +0,0 @@ -const DBName = "EMI_SOCIAL"; -const User = require("./../def/user.js"); - -let userProfileCache = {}; - -userDB = (DB)=>{ - DB.profileCols = DB.db.db(DBName).collection("profiles"); - - DB.newProfile = (userObj) => { - console.log(userObj.toObj()) - return DB.profileCols.insertOne(userObj.toObj()).catch((err)=>{ - console.log(err); - return false; - }); - } - - DB.getProfile = async (userid)=>{ - if(userProfileCache[userid]) return userProfileCache[userid]; - const id = DB.ObjectID(userid) - let r = await DB.profileCols.findOne({userid: id}).catch((err)=>{ - console.log(err); - return false; - }); - if(r) userProfileCache[userid] = r; - return r; - } - - -} - -module.exports = userDB; \ No newline at end of file diff --git a/def/post.js b/def/post.js index 20ddfaa..8856ad3 100644 --- a/def/post.js +++ b/def/post.js @@ -7,7 +7,8 @@ class Post { this.reactions = info.reactions || {}; this.comments = info.comments || []; //This should record edits - this.contentHistory = info.contentHistory || []; + this.contentHistory = info.contentHistory || []; + this.toUser = info.toUser || ''; // Any reaction or comment updates this ts, // this will be used as index to query new posts this.lastUpdated = info.lastUpdated || this.createdAt; @@ -38,6 +39,7 @@ class Post { r.comments = this.comments; r.contentHistory = this.contentHistory; r.lastUpdated = this.lastUpdated; + r.toUser = this.toUser; return r; } } diff --git a/def/user.js b/def/profile.js similarity index 100% rename from def/user.js rename to def/profile.js diff --git a/index.js b/index.js index 6d3fa15..2e48a0a 100644 --- a/index.js +++ b/index.js @@ -22,17 +22,21 @@ const DB = require("./mongoDB.js"); // Utilities const getSessionId = function(req){ const session_id = req.cookies.session_id || req.query.session_id || req.body.session_id; - return session_id + return session_id; } const getUserId = function(req){ const user_sid = req.cookies.user_sid || req.query.user_sid || req.body.user_sid; - return user_sid + return user_sid; +} +const getProfileId = function(req){ + const profile_id = req.cookies.profile_id || req.query.profile_id || req.body.profile_id; + return profile_id; } // Definitions const Post = require("./def/post.js") -const User = require("./def/user.js"); -var userRoute = require('./routes/user.js'); +const Profile = require("./def/profile.js"); +var profileRoute = require('./routes/profile.js'); var postRoute = require('./routes/post.js'); DB.getDB.then((DB)=>{ @@ -41,9 +45,11 @@ DB.getDB.then((DB)=>{ const sessionChecker = async (req, res, next) => { const session_id = getSessionId(req); const user_sid = getUserId(req); + const profile_id = getProfileId(req); if (session_id && user_sid) { const userInfo = await DB.checkSessionOnDB(session_id, user_sid); req.userInfo = userInfo; + req.profileInfo = {_id: profile_id} if(!userInfo) return res.redirect('/login'); next(); } else { @@ -76,8 +82,8 @@ DB.getDB.then((DB)=>{ userid: success.insertedId, profile: profile, } - const userObj = new User(user); - DB.newProfile(userObj) + const userObj = new Profile(user); + await DB.newProfile(userObj); return await login(req, res); } res.redirect('/signup'); @@ -113,10 +119,15 @@ DB.getDB.then((DB)=>{ const doc = await DB.newSession(user._id); res.cookie('user_sid', user._id, cookiesOptions); res.cookie('session_id', doc.insertedId, cookiesOptions); + //Chooses the most recent update profile + const latestProfile = await DB.latestProfile(user._id); + console.log("latestProfile", latestProfile) + res.cookie('profile_id', latestProfile._id, cookiesOptions); return res.json({ status: "ok", user_sid: user._id, - session_id: doc.insertedId + session_id: doc.insertedId, + profile_id: latestProfile._id }); } app.route('/login').get(async (req, res) => { @@ -143,7 +154,7 @@ DB.getDB.then((DB)=>{ return logout(req, res); }); - app.use('/user', sessionChecker, userRoute); + app.use('/user', sessionChecker, profileRoute); app.use('/post', sessionChecker, postRoute); // route for handling 404 requests(unavailable routes) diff --git a/mongoDB.js b/mongoDB.js index 91c820d..b97b739 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -4,7 +4,7 @@ const ObjectID = mongo.ObjectID; const DBName = "EMI_SOCIAL"; const mongoUrl = process.env.MONGO_URL; const postDB = require("./dbTools/post.js"); -const userDB = require("./dbTools/user.js"); +const profileDB = require("./dbTools/profile.js"); const getDB = new Promise((resolve, reject) => { @@ -51,7 +51,7 @@ const getDB = new Promise((resolve, reject) => { } postDB(DB); - userDB(DB); + profileDB(DB); resolve(DB); }); diff --git a/routes/post.js b/routes/post.js index ed5e13e..f1db037 100644 --- a/routes/post.js +++ b/routes/post.js @@ -25,7 +25,7 @@ DB.getDB.then((DB)=>{ router.get("/new", async (req, res) => { let post = { userid: getUserId(req), - content: req.query.content + ...req.query } let postObj = new Post(post); let dbr = await DB.newPost(postObj) diff --git a/routes/user.js b/routes/profile.js similarity index 82% rename from routes/user.js rename to routes/profile.js index c604be4..a21e528 100644 --- a/routes/user.js +++ b/routes/profile.js @@ -1,8 +1,8 @@ var express = require('express') var router = express.Router() -const DB = require("./../mongoDB.js"); -const User = require("./../def/user.js"); +const DB = require("../mongoDB.js"); +const Profile = require("../def/profile.js"); DB.getDB.then((DB)=>{ @@ -11,7 +11,7 @@ DB.getDB.then((DB)=>{ userid: getUserId(req), ... req.query.content }; - let userObj = new User(user); + let userObj = new Profile(user); DB.newProfile(userObj) return res.json({ status: "ok"