internal notifications

This commit is contained in:
Adolfo Reyna
2021-09-26 21:32:25 -07:00
parent 997d28d702
commit 600ec549b4
3 changed files with 17 additions and 4 deletions
+4 -2
View File
@@ -115,13 +115,15 @@ userDB = (DB) => {
}); });
} }
DB.addNotification = async (profileid, message) => { DB.addNotification = async (profileid, message, postid, commentIndx) => {
const _id = DB.ObjectID(profileid); const _id = DB.ObjectID(profileid);
let update = { let update = {
$push:{ $push:{
notifications: { notifications: {
ts: new Date(), ts: new Date(),
body: message body: message,
postid,
commentIndx
} }
} }
} }
+12 -1
View File
@@ -131,6 +131,8 @@ const Notifications = {
usersEmails.forEach((userEmail, index) => { usersEmails.forEach((userEmail, index) => {
const bookedProfile = subscribed[index]; const bookedProfile = subscribed[index];
if (bookedProfile._id == senderProfile._id) return 0; if (bookedProfile._id == senderProfile._id) return 0;
const notifBody = `${senderProfile.profile.firstName} commented in a post you follow`;
DB.addNotification(bookedProfile._id, notifBody, post._id, post.comments.length - 1);
yourBookmarkedPostGotACommentTemplate(post, userEmail, postProfile, senderProfile, bookedProfile, message); yourBookmarkedPostGotACommentTemplate(post, userEmail, postProfile, senderProfile, bookedProfile, message);
}); });
}, },
@@ -144,6 +146,8 @@ const Notifications = {
this.yourBookmarkedPostGotAComment(post, postProfile, senderProfile, message) this.yourBookmarkedPostGotAComment(post, postProfile, senderProfile, message)
} }
if (postProfile.isCourse || senderProfile._id == postProfile._id) return 0; //Course owners do not need to receive notifs if (postProfile.isCourse || senderProfile._id == postProfile._id) return 0; //Course owners do not need to receive notifs
const notifBody = `${senderProfile.profile.firstName} commented in your post`;
DB.addNotification(post.profileid, notifBody, postId, post.comments.length - 1);
return youGotANewPostCommentTemplate(post, userEmail, postProfile, senderProfile, message); return youGotANewPostCommentTemplate(post, userEmail, postProfile, senderProfile, message);
}, },
async yourGroupGotANewPost(groupProfile, senderProfile, message) { async yourGroupGotANewPost(groupProfile, senderProfile, message) {
@@ -159,10 +163,15 @@ const Notifications = {
users.forEach((userEmail, index) => { users.forEach((userEmail, index) => {
let userProfile = subscribed[index]; //who is this email sending to let userProfile = subscribed[index]; //who is this email sending to
if (userProfile._id == senderProfile._id) return 0; //avoid sending self notifications if (userProfile._id == senderProfile._id) return 0; //avoid sending self notifications
const notifBody = `${senderProfile.profile.firstName} post in the group ${groupProfile.profile.firstName} ${groupProfile.profile.lastName}`;
DB.addNotification(userProfile._id, notifBody, post._id);
yourGroupGotANewPostTemplate(groupProfile, userEmail, userProfile, senderProfile, message); yourGroupGotANewPostTemplate(groupProfile, userEmail, userProfile, senderProfile, message);
}); });
}, },
async youGotANewPost(toProfileId, whoPostedId, message) { async youGotANewPost(post) {
const toProfileId = post.toProfile;
const whoPostedId = post.profileid;
const message = post.content;
const DB = await DBGetter.getDB; const DB = await DBGetter.getDB;
const profile = await DB.getProfileCache(toProfileId); const profile = await DB.getProfileCache(toProfileId);
const user = await DB.getUserById(profile.userid); const user = await DB.getUserById(profile.userid);
@@ -170,6 +179,8 @@ const Notifications = {
if (profile.isGroup) { if (profile.isGroup) {
return this.yourGroupGotANewPost(profile, senderProfile, message); return this.yourGroupGotANewPost(profile, senderProfile, message);
} }
const notifBody = `${senderProfile.profile.firstName} post in your profile`;
DB.addNotification(toProfileId, notifBody, post._id);
return youGotANewPostTemplate(profile, user.username, senderProfile, message); return youGotANewPostTemplate(profile, user.username, senderProfile, message);
} }
} }
+1 -1
View File
@@ -50,7 +50,7 @@ DB.getDB.then((DB)=>{
post = postObj.toObj(); post = postObj.toObj();
post._id = dbr.insertedId; post._id = dbr.insertedId;
if(post.toProfile && post.toProfile != post.profileid){ if(post.toProfile && post.toProfile != post.profileid){
Notifications.youGotANewPost(post.toProfile, post.profileid, post.content) Notifications.youGotANewPost(post)
} }
return res.json({ return res.json({
status: "ok", status: "ok",