working on users having multiple profiles
This commit is contained in:
@@ -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;
|
||||
|
||||
49
dbTools/profile.js
Normal file
49
dbTools/profile.js
Normal file
@@ -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;
|
||||
@@ -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;
|
||||
@@ -8,6 +8,7 @@ class Post {
|
||||
this.comments = info.comments || [];
|
||||
//This should record edits
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
27
index.js
27
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)
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"
|
||||
Reference in New Issue
Block a user