routes on files
This commit is contained in:
@@ -15,9 +15,7 @@ postDB = (DB)=>{
|
|||||||
const id = DB.ObjectID(postid);
|
const id = DB.ObjectID(postid);
|
||||||
let update = {
|
let update = {
|
||||||
$set:{
|
$set:{
|
||||||
reactions:{
|
["reactions." + userid]: reaction,
|
||||||
[userid]: reaction
|
|
||||||
},
|
|
||||||
lastUpdated: new Date()
|
lastUpdated: new Date()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -80,7 +78,15 @@ postDB = (DB)=>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
DB.getPosts = (userObj) => {
|
DB.getPosts = (userObj) => {
|
||||||
return DB.postCols.find().toArray().catch((err)=>{
|
return DB.postCols.find().sort({_id: -1}).toArray().catch((err)=>{
|
||||||
|
console.log(err);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.getPostsOfUser = (userId) => {
|
||||||
|
let userid = DB.ObjectID(userId);
|
||||||
|
return DB.postCols.find({userid}).sort({_id: -1}).toArray().catch((err)=>{
|
||||||
console.log(err);
|
console.log(err);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|||||||
31
dbTools/user.js
Normal file
31
dbTools/user.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
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;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
class Post {
|
class Post {
|
||||||
constructor(info){
|
constructor(info){
|
||||||
if(!info || !info.userid) throw "Can not construct empty posts"
|
if(!info || !info.userid) throw "Can not construct empty posts";
|
||||||
this.userid = info.userid;
|
this.userid = info.userid;
|
||||||
this.content = info.content;
|
this.content = info.content;
|
||||||
this.createdAt = info.createdAt || new Date();
|
this.createdAt = info.createdAt || new Date();
|
||||||
|
|||||||
46
def/user.js
46
def/user.js
@@ -1,20 +1,38 @@
|
|||||||
class User {
|
class User {
|
||||||
constructor(json){
|
constructor(info){
|
||||||
this.username = 'aeroreyna';
|
if(!info || !info.userid) throw "Can not construct empty profile";
|
||||||
this.following = [];
|
this.userid = info.userid;
|
||||||
this.lastUpdate = new Date();
|
this.profile = {
|
||||||
this.newsFeedCache = [];
|
firstName: info.profile && info.profile.firstName || '',
|
||||||
this.isGroup = false;
|
lastName: info.profile && info.profile.lastName || '',
|
||||||
|
photo: info.profile && info.profile.photo || '',
|
||||||
|
location: info.profile && info.profile.location || '',
|
||||||
|
language: info.profile && info.profile.language || '',
|
||||||
|
status: info.profile && info.profile.status || '',
|
||||||
|
description: info.profile && info.profile.description || '',
|
||||||
|
};
|
||||||
|
this.data = info.data || {};
|
||||||
|
this.username = info.username || '';
|
||||||
|
this.following = info.following || [];
|
||||||
|
this.lastUpdate = info.lastUpdate || new Date();
|
||||||
|
this.newsFeedCache = info.newsFeedCache || [];
|
||||||
|
this.isGroup = info.isGroup || false;
|
||||||
}
|
}
|
||||||
|
|
||||||
newPost(content){
|
toObj(){
|
||||||
|
let r = {};
|
||||||
}
|
r.userid = this.userid
|
||||||
newComment(post){
|
r.username = this.username;
|
||||||
|
r.profile = this.profile;
|
||||||
}
|
r.data = this.data;
|
||||||
newReaction(){
|
r.username = this.username;
|
||||||
|
r.following = this.following;
|
||||||
|
r.lastUpdate = this.lastUpdate;
|
||||||
|
r.newsFeedCache = this.newsFeedCache;
|
||||||
|
r.isGroup = this.isGroup;
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = User;
|
||||||
94
index.js
94
index.js
@@ -31,6 +31,9 @@ const getUserId = function(req){
|
|||||||
|
|
||||||
// Definitions
|
// Definitions
|
||||||
const Post = require("./def/post.js")
|
const Post = require("./def/post.js")
|
||||||
|
const User = require("./def/user.js");
|
||||||
|
var userRoute = require('./routes/user.js');
|
||||||
|
var postRoute = require('./routes/post.js');
|
||||||
|
|
||||||
DB.getDB.then((DB)=>{
|
DB.getDB.then((DB)=>{
|
||||||
|
|
||||||
@@ -59,15 +62,23 @@ DB.getDB.then((DB)=>{
|
|||||||
const username = req.query.username || req.body.username;
|
const username = req.query.username || req.body.username;
|
||||||
const password = req.query.password || req.body.password;
|
const password = req.query.password || req.body.password;
|
||||||
const email = req.query.email || req.body.email;
|
const email = req.query.email || req.body.email;
|
||||||
|
const profile = req.query.profile || req.body.profile;
|
||||||
if(!username || !password || !email) return res.json({status: "fail"})
|
if(!username || !password || !email) return res.json({status: "fail"})
|
||||||
const hashedPassword = await bcrypt.hash(password, 10);
|
//const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
const hashedPassword = await bcrypt.hash('12345', 10);
|
||||||
const success = await DB.newUser({
|
const success = await DB.newUser({
|
||||||
username: username,
|
username: username,
|
||||||
email: email,
|
email: email,
|
||||||
password: hashedPassword
|
password: hashedPassword
|
||||||
});
|
});
|
||||||
if(success){
|
if(success){
|
||||||
return login(req, res);
|
let user = {
|
||||||
|
userid: success.insertedId,
|
||||||
|
profile: profile,
|
||||||
|
}
|
||||||
|
const userObj = new User(user);
|
||||||
|
DB.newProfile(userObj)
|
||||||
|
return await login(req, res);
|
||||||
}
|
}
|
||||||
res.redirect('/signup');
|
res.redirect('/signup');
|
||||||
}
|
}
|
||||||
@@ -132,83 +143,8 @@ DB.getDB.then((DB)=>{
|
|||||||
return logout(req, res);
|
return logout(req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/post/", sessionChecker, async (req, res) => {
|
app.use('/user', sessionChecker, userRoute);
|
||||||
let posts = await DB.getPosts();
|
app.use('/post', sessionChecker, postRoute);
|
||||||
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"
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
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) {
|
||||||
|
|||||||
@@ -4,6 +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 getDB = new Promise((resolve, reject) => {
|
const getDB = new Promise((resolve, reject) => {
|
||||||
@@ -34,7 +35,6 @@ const getDB = new Promise((resolve, reject) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DB.newUser = (userInformation)=>{
|
DB.newUser = (userInformation)=>{
|
||||||
console.log("NewUser", userInformation);
|
|
||||||
return DB.usersCol.insertOne(userInformation).catch((err)=>{
|
return DB.usersCol.insertOne(userInformation).catch((err)=>{
|
||||||
console.log(err);
|
console.log(err);
|
||||||
return false;
|
return false;
|
||||||
@@ -51,6 +51,7 @@ const getDB = new Promise((resolve, reject) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
postDB(DB);
|
postDB(DB);
|
||||||
|
userDB(DB);
|
||||||
|
|
||||||
resolve(DB);
|
resolve(DB);
|
||||||
});
|
});
|
||||||
|
|||||||
97
routes/post.js
Normal file
97
routes/post.js
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
var express = require('express')
|
||||||
|
var router = express.Router()
|
||||||
|
|
||||||
|
const DB = require("./../mongoDB.js");
|
||||||
|
const Post = require("./../def/post.js");
|
||||||
|
|
||||||
|
const getUserId = function(req){
|
||||||
|
const user_sid = req.cookies.user_sid || req.query.user_sid || req.body.user_sid;
|
||||||
|
return user_sid
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.getDB.then((DB)=>{
|
||||||
|
|
||||||
|
router.get("/", async (req, res) => {
|
||||||
|
let posts = await DB.getPosts();
|
||||||
|
return res.json(posts)
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/new", async (req, res) => {
|
||||||
|
let post = {
|
||||||
|
userid: getUserId(req),
|
||||||
|
content: req.query.content
|
||||||
|
}
|
||||||
|
let postObj = new Post(post);
|
||||||
|
let dbr = await DB.newPost(postObj)
|
||||||
|
post = postObj.toObj();
|
||||||
|
post._id = dbr.insertedId;
|
||||||
|
return res.json({
|
||||||
|
status: "ok",
|
||||||
|
...post
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/newComment", 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"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/react", 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"
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
router.get("/unreact", 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"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/comment/react", 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"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router
|
||||||
32
routes/user.js
Normal file
32
routes/user.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
var express = require('express')
|
||||||
|
var router = express.Router()
|
||||||
|
|
||||||
|
const DB = require("./../mongoDB.js");
|
||||||
|
const User = require("./../def/user.js");
|
||||||
|
|
||||||
|
DB.getDB.then((DB)=>{
|
||||||
|
|
||||||
|
router.get("/new", async (req, res) => {
|
||||||
|
let user = {
|
||||||
|
userid: getUserId(req),
|
||||||
|
... req.query.content
|
||||||
|
};
|
||||||
|
let userObj = new User(user);
|
||||||
|
DB.newProfile(userObj)
|
||||||
|
return res.json({
|
||||||
|
status: "ok"
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/:id", async (req, res) => {
|
||||||
|
let userid = req.params.id;
|
||||||
|
let user = await DB.getProfile(userid);
|
||||||
|
return res.json({
|
||||||
|
status: "ok",
|
||||||
|
user
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router
|
||||||
Reference in New Issue
Block a user