refactor notifications to add internal notifications
This commit is contained in:
140
notifications.js
140
notifications.js
@@ -1,8 +1,7 @@
|
|||||||
const nodemailer = require("nodemailer");
|
const nodemailer = require("nodemailer");
|
||||||
const DBGetter = require("./mongoDB.js");
|
const DBGetter = require("./mongoDB.js");
|
||||||
|
|
||||||
const Notifications = {
|
const sendEmail = async (to, subject, html) => {
|
||||||
async sendEmail(to, subject, html) {
|
|
||||||
let transporter = nodemailer.createTransport({
|
let transporter = nodemailer.createTransport({
|
||||||
host: "mail.emmint.com",
|
host: "mail.emmint.com",
|
||||||
port: 465,
|
port: 465,
|
||||||
@@ -20,33 +19,20 @@ const Notifications = {
|
|||||||
html,
|
html,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Message sent: %s", info.messageId);
|
console.log("Email sent: %s", info.messageId);
|
||||||
},
|
};
|
||||||
async yourBookmarkedPostGotAComment(post, whoPostedId, message){
|
|
||||||
const DB = await DBGetter.getDB;
|
const yourBookmarkedPostGotACommentTemplate = (post, userEmail, postProfile, senderProfile, bookedProfile, message) => {
|
||||||
let subscribedPromise = post.bookmarks.map((profileid)=>{
|
|
||||||
return DB.getProfileCache(profileid);
|
|
||||||
});
|
|
||||||
let subscribed = await Promise.all(subscribedPromise);
|
|
||||||
let usersPromise = subscribed.map((profile)=>{
|
|
||||||
return DB.getUsernameByIdCache(profile.userid);
|
|
||||||
});
|
|
||||||
let users = await Promise.all(usersPromise);
|
|
||||||
const senderProfile = await DB.getProfileCache(whoPostedId);
|
|
||||||
const postPprofile = await DB.getProfileCache(post.profileid);
|
|
||||||
users.forEach((user, index)=>{
|
|
||||||
let profile = subscribed[index];
|
|
||||||
if(profile._id == senderProfile._id) return 0;
|
|
||||||
let subject = senderProfile.profile.firstName + " commented on the post you follow";
|
let subject = senderProfile.profile.firstName + " commented on the post you follow";
|
||||||
let html = `
|
let html = `
|
||||||
<p>Hello ${profile.profile.firstName},</p>
|
<p>Hello ${bookedProfile.profile.firstName},</p>
|
||||||
|
|
||||||
<p>One of the post you bookmarked has a new comment:</p>
|
<p>One of the post you bookmarked has a new comment:</p>
|
||||||
|
|
||||||
<blockquote cite="https://social.emmint.com/">
|
<blockquote cite="https://social.emmint.com/">
|
||||||
<p>${post.content}</p>
|
<p>${post.content}</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<figcaption>— ${postPprofile.profile.firstName} ${postPprofile.profile.lastName}</figcaption>
|
<figcaption>— ${postProfile.profile.firstName} ${postProfile.profile.lastName}</figcaption>
|
||||||
|
|
||||||
<p>Comment:</p>
|
<p>Comment:</p>
|
||||||
|
|
||||||
@@ -60,20 +46,10 @@ const Notifications = {
|
|||||||
<p>Blessings</p>
|
<p>Blessings</p>
|
||||||
|
|
||||||
`;
|
`;
|
||||||
this.sendEmail(user, subject, html)
|
sendEmail(userEmail, subject, html);
|
||||||
})
|
};
|
||||||
},
|
|
||||||
async youGotANewPostComment(postId, whoPostedId, message){
|
const youGotANewPostCommentTemplate = (post, userEmail, profile, senderProfile, message) => {
|
||||||
const DB = await DBGetter.getDB;
|
|
||||||
const post = await DB.getPost(postId)
|
|
||||||
if(post.bookmarks){
|
|
||||||
//send notification to boorkmaked profiles
|
|
||||||
this.yourBookmarkedPostGotAComment(post, whoPostedId, message)
|
|
||||||
}
|
|
||||||
const profile = await DB.getProfileCache(post.profileid);
|
|
||||||
if(profile.isCourse) return 0; //Course owners do not need to receive notifs
|
|
||||||
const user = await DB.getUserById(profile.userid);
|
|
||||||
const senderProfile = await DB.getProfileCache(whoPostedId);
|
|
||||||
let subject = senderProfile.profile.firstName + " comment on your post";
|
let subject = senderProfile.profile.firstName + " comment on your post";
|
||||||
let html = `
|
let html = `
|
||||||
<p>Hello ${profile.profile.firstName},</p>
|
<p>Hello ${profile.profile.firstName},</p>
|
||||||
@@ -97,22 +73,10 @@ const Notifications = {
|
|||||||
<p>Blessings</p>
|
<p>Blessings</p>
|
||||||
|
|
||||||
`;
|
`;
|
||||||
this.sendEmail(user.username, subject, html)
|
return sendEmail(userEmail, subject, html);
|
||||||
},
|
};
|
||||||
async yourGroupGotANewPost(groupProfile, whoPostedId, message){
|
|
||||||
const DB = await DBGetter.getDB;
|
const yourGroupGotANewPostTemplate = (groupProfile, userEmail, profile, senderProfile, message) => {
|
||||||
let subscribedPromise = Object.keys(groupProfile.subscribed).map((profileid)=>{
|
|
||||||
return DB.getProfileCache(profileid);
|
|
||||||
});
|
|
||||||
let subscribed = await Promise.all(subscribedPromise);
|
|
||||||
let usersPromise = subscribed.map((profile)=>{
|
|
||||||
return DB.getUsernameByIdCache(profile.userid);
|
|
||||||
});
|
|
||||||
let users = await Promise.all(usersPromise);
|
|
||||||
const senderProfile = await DB.getProfileCache(whoPostedId);
|
|
||||||
users.forEach((user, index)=>{
|
|
||||||
let profile = subscribed[index];
|
|
||||||
if(profile._id == senderProfile._id) return 0;
|
|
||||||
let subject = senderProfile.profile.firstName + " posted on one of the groups you follow";
|
let subject = senderProfile.profile.firstName + " posted on one of the groups you follow";
|
||||||
let html = `
|
let html = `
|
||||||
<p>Hello ${profile.profile.firstName},</p>
|
<p>Hello ${profile.profile.firstName},</p>
|
||||||
@@ -129,17 +93,10 @@ const Notifications = {
|
|||||||
<p>Blessings</p>
|
<p>Blessings</p>
|
||||||
|
|
||||||
`;
|
`;
|
||||||
this.sendEmail(user, subject, html)
|
return sendEmail(userEmail, subject, html);
|
||||||
})
|
};
|
||||||
},
|
|
||||||
async youGotANewPost(toProfileId, whoPostedId, message){
|
const youGotANewPostTemplate = (profile, userEmail, senderProfile, message) => {
|
||||||
const DB = await DBGetter.getDB;
|
|
||||||
const profile = await DB.getProfileCache(toProfileId);
|
|
||||||
if(profile.isGroup){
|
|
||||||
return this.yourGroupGotANewPost(profile, whoPostedId, message);
|
|
||||||
}
|
|
||||||
const user = await DB.getUserById(profile.userid);
|
|
||||||
const senderProfile = await DB.getProfileCache(whoPostedId);
|
|
||||||
let subject = senderProfile.profile.firstName + " post on your profile";
|
let subject = senderProfile.profile.firstName + " post on your profile";
|
||||||
let html = `
|
let html = `
|
||||||
<p>Hello ${profile.profile.firstName},</p>
|
<p>Hello ${profile.profile.firstName},</p>
|
||||||
@@ -156,7 +113,64 @@ const Notifications = {
|
|||||||
<p>Blessings</p>
|
<p>Blessings</p>
|
||||||
|
|
||||||
`;
|
`;
|
||||||
this.sendEmail(user.username, subject, html)
|
sendEmail(userEmail, subject, html)
|
||||||
|
}
|
||||||
|
|
||||||
|
const Notifications = {
|
||||||
|
sendEmail,
|
||||||
|
async yourBookmarkedPostGotAComment(post, postProfile, senderProfile, message) {
|
||||||
|
const DB = await DBGetter.getDB;
|
||||||
|
const subscribedPromise = post.bookmarks.map((profileid) => {
|
||||||
|
return DB.getProfileCache(profileid);
|
||||||
|
});
|
||||||
|
const subscribed = await Promise.all(subscribedPromise);
|
||||||
|
const usersPromise = subscribed.map((profile) => {
|
||||||
|
return DB.getUsernameByIdCache(profile.userid);
|
||||||
|
});
|
||||||
|
const usersEmails = await Promise.all(usersPromise);
|
||||||
|
usersEmails.forEach((userEmail, index) => {
|
||||||
|
const bookedProfile = subscribed[index];
|
||||||
|
if (bookedProfile._id == senderProfile._id) return 0;
|
||||||
|
yourBookmarkedPostGotACommentTemplate(post, userEmail, postProfile, senderProfile, bookedProfile, message);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async youGotANewPostComment(postId, whoPostedId, message) {
|
||||||
|
const DB = await DBGetter.getDB;
|
||||||
|
const post = await DB.getPost(postId);
|
||||||
|
const postProfile = await DB.getProfileCache(post.profileid);
|
||||||
|
const senderProfile = await DB.getProfileCache(whoPostedId);
|
||||||
|
const userEmail = await DB.getUsernameByIdCache(postProfile.userid);
|
||||||
|
if (post.bookmarks) {
|
||||||
|
this.yourBookmarkedPostGotAComment(post, postProfile, senderProfile, message)
|
||||||
|
}
|
||||||
|
if (postProfile.isCourse || senderProfile._id == postProfile._id) return 0; //Course owners do not need to receive notifs
|
||||||
|
return youGotANewPostCommentTemplate(post, userEmail, postProfile, senderProfile, message);
|
||||||
|
},
|
||||||
|
async yourGroupGotANewPost(groupProfile, senderProfile, message) {
|
||||||
|
const DB = await DBGetter.getDB;
|
||||||
|
let subscribedPromise = Object.keys(groupProfile.subscribed).map((profileid) => {
|
||||||
|
return DB.getProfileCache(profileid);
|
||||||
|
});
|
||||||
|
let subscribed = await Promise.all(subscribedPromise);
|
||||||
|
let usersPromise = subscribed.map((profile) => {
|
||||||
|
return DB.getUsernameByIdCache(profile.userid);
|
||||||
|
});
|
||||||
|
let users = await Promise.all(usersPromise);
|
||||||
|
users.forEach((userEmail, index) => {
|
||||||
|
let userProfile = subscribed[index]; //who is this email sending to
|
||||||
|
if (userProfile._id == senderProfile._id) return 0; //avoid sending self notifications
|
||||||
|
yourGroupGotANewPostTemplate(groupProfile, userEmail, userProfile, senderProfile, message);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async youGotANewPost(toProfileId, whoPostedId, message) {
|
||||||
|
const DB = await DBGetter.getDB;
|
||||||
|
const profile = await DB.getProfileCache(toProfileId);
|
||||||
|
const user = await DB.getUserById(profile.userid);
|
||||||
|
const senderProfile = await DB.getProfileCache(whoPostedId);
|
||||||
|
if (profile.isGroup) {
|
||||||
|
return this.yourGroupGotANewPost(profile, senderProfile, message);
|
||||||
|
}
|
||||||
|
return youGotANewPostTemplate(profile, user.username, senderProfile, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user