Files
EMI-Backend/notifications.js
T
2021-09-26 21:32:25 -07:00

188 lines
6.7 KiB
JavaScript

const nodemailer = require("nodemailer");
const DBGetter = require("./mongoDB.js");
const sendEmail = async (to, subject, html) => {
let transporter = nodemailer.createTransport({
host: "mail.emmint.com",
port: 465,
secure: true,
auth: {
user: "noreply@emmint.com",
pass: process.env.EMAILPASS,
},
});
let info = await transporter.sendMail({
from: '"EMI Social" <noreply@emmint.com',
to,
subject,
html,
});
console.log("Email sent: %s", info.messageId);
};
const yourBookmarkedPostGotACommentTemplate = (post, userEmail, postProfile, senderProfile, bookedProfile, message) => {
let subject = senderProfile.profile.firstName + " commented on the post you follow";
let html = `
<p>Hello ${bookedProfile.profile.firstName},</p>
<p>One of the post you bookmarked has a new comment:</p>
<blockquote cite="https://social.emmint.com/">
<p>${post.content}</p>
</blockquote>
<figcaption>— ${postProfile.profile.firstName} ${postProfile.profile.lastName}</figcaption>
<p>Comment:</p>
<blockquote cite="https://social.emmint.com/">
<p>${message}</p>
</blockquote>
<figcaption>— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}</figcaption>
<p><a href="https://social.emmint.com/">Check it on the site</a></p>
<p>Blessings</p>
`;
sendEmail(userEmail, subject, html);
};
const youGotANewPostCommentTemplate = (post, userEmail, profile, senderProfile, message) => {
let subject = senderProfile.profile.firstName + " comment on your post";
let html = `
<p>Hello ${profile.profile.firstName},</p>
<p>You got a comment on your post:</p>
<blockquote cite="https://social.emmint.com/">
<p>${post.content}</p>
</blockquote>
<figcaption>— You</figcaption>
<p>Comment:</p>
<blockquote cite="https://social.emmint.com/">
<p>${message}</p>
</blockquote>
<figcaption>— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}</figcaption>
<p><a href="https://social.emmint.com/">Check it on the site</a></p>
<p>Blessings</p>
`;
return sendEmail(userEmail, subject, html);
};
const yourGroupGotANewPostTemplate = (groupProfile, userEmail, profile, senderProfile, message) => {
let subject = senderProfile.profile.firstName + " posted on one of the groups you follow";
let html = `
<p>Hello ${profile.profile.firstName},</p>
<p>${groupProfile.profile.firstName} ${groupProfile.profile.lastName} have new post:</p>
<blockquote cite="https://social.emmint.com/">
<p>${message}</p>
</blockquote>
<figcaption>— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}</figcaption>
<p><a href="https://social.emmint.com/">Check it on the site</a></p>
<p>Blessings</p>
`;
return sendEmail(userEmail, subject, html);
};
const youGotANewPostTemplate = (profile, userEmail, senderProfile, message) => {
let subject = senderProfile.profile.firstName + " post on your profile";
let html = `
<p>Hello ${profile.profile.firstName},</p>
<p>You got a new post:</p>
<blockquote cite="https://social.emmint.com/">
<p>${message}</p>
</blockquote>
<figcaption>— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}</figcaption>
<p><a href="https://social.emmint.com/">Check it on the site</a></p>
<p>Blessings</p>
`;
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;
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);
});
},
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
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);
},
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
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);
});
},
async youGotANewPost(post) {
const toProfileId = post.toProfile;
const whoPostedId = post.profileid;
const message = post.content;
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);
}
const notifBody = `${senderProfile.profile.firstName} post in your profile`;
DB.addNotification(toProfileId, notifBody, post._id);
return youGotANewPostTemplate(profile, user.username, senderProfile, message);
}
}
module.exports = Notifications