groups working
This commit is contained in:
@@ -67,7 +67,6 @@ postDB = (DB)=>{
|
|||||||
lastUpdated: new Date()
|
lastUpdated: new Date()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(JSON.stringify(update));
|
|
||||||
return DB.postCols.updateOne({
|
return DB.postCols.updateOne({
|
||||||
_id: id,
|
_id: id,
|
||||||
"comments.createdAt": commentDate
|
"comments.createdAt": commentDate
|
||||||
@@ -101,6 +100,15 @@ postDB = (DB)=>{
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DB.getPost = (postId) => {
|
||||||
|
let _id = DB.ObjectID(postId);
|
||||||
|
return DB.postCols.findOne({_id}).catch((err)=>{
|
||||||
|
console.log(err);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = postDB;
|
module.exports = postDB;
|
||||||
@@ -23,9 +23,9 @@ userDB = (DB) => {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
DB.getProfiles = async (profileId) => {
|
DB.getUserProfiles = async (userId) => {
|
||||||
const _id = DB.ObjectID(profileId);
|
const userid = DB.ObjectID(userId);
|
||||||
return await DB.profileCols.find({ _id }).toArray().catch((err) => {
|
return await DB.profileCols.find({ userid }).toArray().catch((err) => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
@@ -43,6 +43,29 @@ userDB = (DB) => {
|
|||||||
return r[0];
|
return r[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Groups
|
||||||
|
DB.getGroups = async () => {
|
||||||
|
let r = await DB.profileCols.find({isGroup: true})
|
||||||
|
.sort({ lastUpdate: -1 }).limit(10)
|
||||||
|
.toArray().catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.getGroup = async (groupid) => {
|
||||||
|
const _id = DB.ObjectID(groupid);
|
||||||
|
if(userProfileCache[groupid]) return userProfileCache[groupid];
|
||||||
|
let r = await DB.profileCols.findOne({_id, isGroup: true})
|
||||||
|
.toArray().catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
if (r) userProfileCache[r.id] = r;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ class User {
|
|||||||
firstName: info.profile && info.profile.firstName || '',
|
firstName: info.profile && info.profile.firstName || '',
|
||||||
lastName: info.profile && info.profile.lastName || '',
|
lastName: info.profile && info.profile.lastName || '',
|
||||||
photo: info.profile && info.profile.photo || '',
|
photo: info.profile && info.profile.photo || '',
|
||||||
location: info.profile && info.profile.location || '',
|
location: info.profile && info.profile.location || 'USA',
|
||||||
language: info.profile && info.profile.language || '',
|
language: info.profile && info.profile.language || 'en',
|
||||||
status: info.profile && info.profile.status || '',
|
status: info.profile && info.profile.status || '',
|
||||||
description: info.profile && info.profile.description || '',
|
description: info.profile && info.profile.description || '',
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,11 +4,6 @@ var router = express.Router()
|
|||||||
const DB = require("./../mongoDB.js");
|
const DB = require("./../mongoDB.js");
|
||||||
const Post = require("./../def/post.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)=>{
|
DB.getDB.then((DB)=>{
|
||||||
|
|
||||||
const getProfileId = (req)=>{
|
const getProfileId = (req)=>{
|
||||||
@@ -26,10 +21,10 @@ DB.getDB.then((DB)=>{
|
|||||||
return res.json(posts)
|
return res.json(posts)
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/new", async (req, res) => {
|
router.post("/", async (req, res) => {
|
||||||
let post = {
|
let post = {
|
||||||
profileid: getProfileId(req),
|
profileid: getProfileId(req),
|
||||||
...req.query
|
...req.body
|
||||||
}
|
}
|
||||||
post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined;
|
post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined;
|
||||||
let postObj = new Post(post);
|
let postObj = new Post(post);
|
||||||
@@ -42,10 +37,33 @@ DB.getDB.then((DB)=>{
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/newComment", async (req, res) => {
|
router.post("/react", async (req, res) => {
|
||||||
|
let userid = getProfileId(req);
|
||||||
|
let postid = req.body.postid;
|
||||||
|
let reaction = {
|
||||||
|
type: "like",
|
||||||
|
createdAt: new Date()
|
||||||
|
};
|
||||||
|
r = await DB.newReaction(postid, userid, reaction);
|
||||||
|
return res.json({
|
||||||
|
status: "ok"
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
router.post("/unreact", async (req, res) => {
|
||||||
|
let userid = getProfileId(req);
|
||||||
|
let postid = req.body.postid;
|
||||||
|
r = await DB.removeReaction(postid, userid);
|
||||||
|
console.log(r)
|
||||||
|
return res.json({
|
||||||
|
status: "ok"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/comment/", async (req, res) => {
|
||||||
let profileid = getProfileId(req);
|
let profileid = getProfileId(req);
|
||||||
let postid = req.query.postid;
|
let postid = req.body.postid;
|
||||||
let content = req.query.content;
|
let content = req.body.content;
|
||||||
let comment = {
|
let comment = {
|
||||||
profileid: profileid,
|
profileid: profileid,
|
||||||
content: content,
|
content: content,
|
||||||
@@ -55,55 +73,35 @@ DB.getDB.then((DB)=>{
|
|||||||
}
|
}
|
||||||
console.log("comment", postid, comment);
|
console.log("comment", postid, comment);
|
||||||
r = await DB.newComment(postid, comment);
|
r = await DB.newComment(postid, comment);
|
||||||
console.log(r)
|
|
||||||
return res.json({
|
return res.json({
|
||||||
status: "ok",
|
status: "ok",
|
||||||
...comment
|
...comment
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/react", async (req, res) => {
|
router.post("/comment/react", async (req, res) => {
|
||||||
let userid = getProfileId(req);
|
let userid = getProfileId(req);
|
||||||
let postid = req.query.postid;
|
let postid = req.body.postid;
|
||||||
|
let commentDate = new Date(req.body.commentDate);
|
||||||
let reaction = {
|
let reaction = {
|
||||||
type: "like",
|
type: "like",
|
||||||
createdAt: new Date()
|
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 = getProfileId(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 = getProfileId(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);
|
r = await DB.newCommentReaction(postid, commentDate, userid, reaction);
|
||||||
console.log(r)
|
|
||||||
return res.json({
|
return res.json({
|
||||||
status: "ok"
|
status: "ok"
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get("/:id", async (req, res) => {
|
||||||
|
const postId = req.params.id;
|
||||||
|
const post = await DB.getPost(postId);
|
||||||
|
return res.json({
|
||||||
|
status: "ok",
|
||||||
|
... post
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
@@ -6,15 +6,53 @@ const Profile = require("../def/profile.js");
|
|||||||
|
|
||||||
DB.getDB.then((DB)=>{
|
DB.getDB.then((DB)=>{
|
||||||
|
|
||||||
|
const getUserId = function(req){
|
||||||
|
const user_sid = req.cookies.user_sid || req.query.user_sid || req.body.user_sid;
|
||||||
|
return DB.ObjectID(user_sid);
|
||||||
|
}
|
||||||
|
|
||||||
router.get("/new", async (req, res) => {
|
router.get("/new", async (req, res) => {
|
||||||
let user = {
|
let profile = {
|
||||||
userid: getUserId(req),
|
userid: getUserId(req),
|
||||||
... req.query.content
|
... req.query.content
|
||||||
};
|
};
|
||||||
let userObj = new Profile(user);
|
let profileObj = new Profile(profile);
|
||||||
DB.newProfile(userObj)
|
let r = await DB.newProfile(profileObj);
|
||||||
return res.json({
|
return res.json({
|
||||||
status: "ok"
|
status: "ok",
|
||||||
|
... profileObj.toObj()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/groups", async (req, res) => {
|
||||||
|
let groups = await DB.getGroups();
|
||||||
|
return res.json({
|
||||||
|
status: "ok",
|
||||||
|
groups
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/groups", async (req, res) => {
|
||||||
|
let profile = {
|
||||||
|
userid: getUserId(req),
|
||||||
|
isGroup: true,
|
||||||
|
... req.body
|
||||||
|
};
|
||||||
|
console.log("newGroup", profile)
|
||||||
|
let profileObj = new Profile(profile);
|
||||||
|
DB.newProfile(profileObj)
|
||||||
|
return res.json({
|
||||||
|
status: "ok",
|
||||||
|
... profileObj.toObj()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/groups/:id", async (req, res) => {
|
||||||
|
const groupid = req.params.id;
|
||||||
|
let groups = await DB.getGroup(groupid);
|
||||||
|
return res.json({
|
||||||
|
status: "ok",
|
||||||
|
groups
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user