working on users having multiple profiles
This commit is contained in:
@@ -77,8 +77,13 @@ postDB = (DB)=>{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DB.getPosts = (userid) => {
|
DB.getPosts = (profileId) => {
|
||||||
let query = userid ? {userid} : {};
|
let query = profileId ? {
|
||||||
|
$or: [
|
||||||
|
{_id: profileId},
|
||||||
|
{toUser: profileId}
|
||||||
|
]
|
||||||
|
} : {};
|
||||||
return DB.postCols.find(query).sort({_id: -1}).toArray().catch((err)=>{
|
return DB.postCols.find(query).sort({_id: -1}).toArray().catch((err)=>{
|
||||||
console.log(err);
|
console.log(err);
|
||||||
return false;
|
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.comments = info.comments || [];
|
||||||
//This should record edits
|
//This should record edits
|
||||||
this.contentHistory = info.contentHistory || [];
|
this.contentHistory = info.contentHistory || [];
|
||||||
|
this.toUser = info.toUser || '';
|
||||||
// Any reaction or comment updates this ts,
|
// Any reaction or comment updates this ts,
|
||||||
// this will be used as index to query new posts
|
// this will be used as index to query new posts
|
||||||
this.lastUpdated = info.lastUpdated || this.createdAt;
|
this.lastUpdated = info.lastUpdated || this.createdAt;
|
||||||
@@ -38,6 +39,7 @@ class Post {
|
|||||||
r.comments = this.comments;
|
r.comments = this.comments;
|
||||||
r.contentHistory = this.contentHistory;
|
r.contentHistory = this.contentHistory;
|
||||||
r.lastUpdated = this.lastUpdated;
|
r.lastUpdated = this.lastUpdated;
|
||||||
|
r.toUser = this.toUser;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
index.js
27
index.js
@@ -22,17 +22,21 @@ const DB = require("./mongoDB.js");
|
|||||||
// Utilities
|
// Utilities
|
||||||
const getSessionId = function(req){
|
const getSessionId = function(req){
|
||||||
const session_id = req.cookies.session_id || req.query.session_id || req.body.session_id;
|
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 getUserId = function(req){
|
||||||
const user_sid = req.cookies.user_sid || req.query.user_sid || req.body.user_sid;
|
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
|
// Definitions
|
||||||
const Post = require("./def/post.js")
|
const Post = require("./def/post.js")
|
||||||
const User = require("./def/user.js");
|
const Profile = require("./def/profile.js");
|
||||||
var userRoute = require('./routes/user.js');
|
var profileRoute = require('./routes/profile.js');
|
||||||
var postRoute = require('./routes/post.js');
|
var postRoute = require('./routes/post.js');
|
||||||
|
|
||||||
DB.getDB.then((DB)=>{
|
DB.getDB.then((DB)=>{
|
||||||
@@ -41,9 +45,11 @@ DB.getDB.then((DB)=>{
|
|||||||
const sessionChecker = async (req, res, next) => {
|
const sessionChecker = async (req, res, next) => {
|
||||||
const session_id = getSessionId(req);
|
const session_id = getSessionId(req);
|
||||||
const user_sid = getUserId(req);
|
const user_sid = getUserId(req);
|
||||||
|
const profile_id = getProfileId(req);
|
||||||
if (session_id && user_sid) {
|
if (session_id && user_sid) {
|
||||||
const userInfo = await DB.checkSessionOnDB(session_id, user_sid);
|
const userInfo = await DB.checkSessionOnDB(session_id, user_sid);
|
||||||
req.userInfo = userInfo;
|
req.userInfo = userInfo;
|
||||||
|
req.profileInfo = {_id: profile_id}
|
||||||
if(!userInfo) return res.redirect('/login');
|
if(!userInfo) return res.redirect('/login');
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
@@ -76,8 +82,8 @@ DB.getDB.then((DB)=>{
|
|||||||
userid: success.insertedId,
|
userid: success.insertedId,
|
||||||
profile: profile,
|
profile: profile,
|
||||||
}
|
}
|
||||||
const userObj = new User(user);
|
const userObj = new Profile(user);
|
||||||
DB.newProfile(userObj)
|
await DB.newProfile(userObj);
|
||||||
return await login(req, res);
|
return await login(req, res);
|
||||||
}
|
}
|
||||||
res.redirect('/signup');
|
res.redirect('/signup');
|
||||||
@@ -113,10 +119,15 @@ DB.getDB.then((DB)=>{
|
|||||||
const doc = await DB.newSession(user._id);
|
const doc = await DB.newSession(user._id);
|
||||||
res.cookie('user_sid', user._id, cookiesOptions);
|
res.cookie('user_sid', user._id, cookiesOptions);
|
||||||
res.cookie('session_id', doc.insertedId, 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({
|
return res.json({
|
||||||
status: "ok",
|
status: "ok",
|
||||||
user_sid: user._id,
|
user_sid: user._id,
|
||||||
session_id: doc.insertedId
|
session_id: doc.insertedId,
|
||||||
|
profile_id: latestProfile._id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
app.route('/login').get(async (req, res) => {
|
app.route('/login').get(async (req, res) => {
|
||||||
@@ -143,7 +154,7 @@ DB.getDB.then((DB)=>{
|
|||||||
return logout(req, res);
|
return logout(req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use('/user', sessionChecker, userRoute);
|
app.use('/user', sessionChecker, profileRoute);
|
||||||
app.use('/post', sessionChecker, postRoute);
|
app.use('/post', sessionChecker, postRoute);
|
||||||
|
|
||||||
// route for handling 404 requests(unavailable routes)
|
// route for handling 404 requests(unavailable routes)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ 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");
|
||||||
const userDB = require("./dbTools/user.js");
|
const profileDB = require("./dbTools/profile.js");
|
||||||
|
|
||||||
|
|
||||||
const getDB = new Promise((resolve, reject) => {
|
const getDB = new Promise((resolve, reject) => {
|
||||||
@@ -51,7 +51,7 @@ const getDB = new Promise((resolve, reject) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
postDB(DB);
|
postDB(DB);
|
||||||
userDB(DB);
|
profileDB(DB);
|
||||||
|
|
||||||
resolve(DB);
|
resolve(DB);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ DB.getDB.then((DB)=>{
|
|||||||
router.get("/new", async (req, res) => {
|
router.get("/new", async (req, res) => {
|
||||||
let post = {
|
let post = {
|
||||||
userid: getUserId(req),
|
userid: getUserId(req),
|
||||||
content: req.query.content
|
...req.query
|
||||||
}
|
}
|
||||||
let postObj = new Post(post);
|
let postObj = new Post(post);
|
||||||
let dbr = await DB.newPost(postObj)
|
let dbr = await DB.newPost(postObj)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
var express = require('express')
|
var express = require('express')
|
||||||
var router = express.Router()
|
var router = express.Router()
|
||||||
|
|
||||||
const DB = require("./../mongoDB.js");
|
const DB = require("../mongoDB.js");
|
||||||
const User = require("./../def/user.js");
|
const Profile = require("../def/profile.js");
|
||||||
|
|
||||||
DB.getDB.then((DB)=>{
|
DB.getDB.then((DB)=>{
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ DB.getDB.then((DB)=>{
|
|||||||
userid: getUserId(req),
|
userid: getUserId(req),
|
||||||
... req.query.content
|
... req.query.content
|
||||||
};
|
};
|
||||||
let userObj = new User(user);
|
let userObj = new Profile(user);
|
||||||
DB.newProfile(userObj)
|
DB.newProfile(userObj)
|
||||||
return res.json({
|
return res.json({
|
||||||
status: "ok"
|
status: "ok"
|
||||||
Reference in New Issue
Block a user