notification to subscribers on groups

This commit is contained in:
Adolfo Reyna
2021-09-14 22:06:37 -07:00
parent 52617bbcec
commit 0907c1c1b5
2 changed files with 59 additions and 18 deletions

View File

@@ -39,6 +39,15 @@ const getDB = new Promise((resolve, reject) => {
return DB.usersCol.findOne({ _id }); return DB.usersCol.findOne({ _id });
} }
let usernamesCache = {}
DB.getUsernameByIdCache = async (userid)=>{
if(usernamesCache[userid]) return usernamesCache[userid];
const _id = new mongo.ObjectID(userid);
let user = await DB.usersCol.findOne({ _id });
usernamesCache[userid] = user.username;
return usernamesCache[userid];
}
DB.newUser = (userInformation)=>{ DB.newUser = (userInformation)=>{
return DB.usersCol.insertOne(userInformation).catch((err)=>{ return DB.usersCol.insertOne(userInformation).catch((err)=>{
console.log(err); console.log(err);

View File

@@ -3,36 +3,33 @@ const DBGetter = require("./mongoDB.js");
const Notifications = { const Notifications = {
async sendEmail(to, subject, html) { async sendEmail(to, subject, html) {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({ let transporter = nodemailer.createTransport({
host: "mail.emmint.com", host: "mail.emmint.com",
port: 465, port: 465,
secure: true, // true for 465, false for other ports secure: true,
auth: { auth: {
user: "noreply@emmint.com", // generated ethereal user user: "noreply@emmint.com",
pass: process.env.EMAILPASS, // generated ethereal password pass: process.env.EMAILPASS,
}, },
}); });
// send mail with defined transport object
let info = await transporter.sendMail({ let info = await transporter.sendMail({
from: '"EMI Social" <noreply@emmint.com', // sender address from: '"EMI Social" <noreply@emmint.com',
to, // list of receivers to,
subject, // Subject line subject,
//text: "Hello world?", // plain text body html,
html, // html body
}); });
console.log("Message sent: %s", info.messageId); console.log("Message sent: %s", info.messageId);
}, },
async youGotANewPostComment(postId, whoPostedId, Message){ async youGotANewPostComment(postId, whoPostedId, message){
const DB = await DBGetter.getDB; const DB = await DBGetter.getDB;
const post = await DB.getPost(postId) const post = await DB.getPost(postId)
const profile = await DB.getProfileCache(post.profileid); const profile = await DB.getProfileCache(post.profileid);
const user = await DB.getUserById(profile.userid); const user = await DB.getUserById(profile.userid);
const senderProfile = await DB.getProfileCache(whoPostedId); 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 message = ` let html = `
<p>Hello ${profile.profile.firstName},</p> <p>Hello ${profile.profile.firstName},</p>
<p>You got a comment on your post:</p> <p>You got a comment on your post:</p>
@@ -45,7 +42,7 @@ const Notifications = {
<p>Comment:</p> <p>Comment:</p>
<blockquote cite="https://social.emmint.com/"> <blockquote cite="https://social.emmint.com/">
<p>${Message}</p> <p>${message}</p>
</blockquote> </blockquote>
<figcaption>— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}</figcaption> <figcaption>— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}</figcaption>
@@ -54,21 +51,56 @@ const Notifications = {
<p>Blessings</p> <p>Blessings</p>
`; `;
this.sendEmail(user.username, subject, message) this.sendEmail(user.username, subject, html)
}, },
async youGotANewPost(toProfileId, whoPostedId, Message){ async yourGroupGotANewPost(groupProfile, whoPostedId, 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);
const senderProfile = await DB.getProfileCache(whoPostedId);
users.forEach((user, index)=>{
let profile = subscribed[index];
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>
`;
this.sendEmail(user, subject, html)
})
},
async youGotANewPost(toProfileId, whoPostedId, message){
const DB = await DBGetter.getDB; const DB = await DBGetter.getDB;
const profile = await DB.getProfileCache(toProfileId); const profile = await DB.getProfileCache(toProfileId);
if(profile.isGroup){
return this.yourGroupGotANewPost(profile, whoPostedId, message);
}
const user = await DB.getUserById(profile.userid); const user = await DB.getUserById(profile.userid);
const senderProfile = await DB.getProfileCache(whoPostedId); 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 message = ` let html = `
<p>Hello ${profile.profile.firstName},</p> <p>Hello ${profile.profile.firstName},</p>
<p>You got a new post:</p> <p>You got a new post:</p>
<blockquote cite="https://social.emmint.com/"> <blockquote cite="https://social.emmint.com/">
<p>${Message}</p> <p>${message}</p>
</blockquote> </blockquote>
<figcaption>— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}</figcaption> <figcaption>— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}</figcaption>
@@ -77,7 +109,7 @@ const Notifications = {
<p>Blessings</p> <p>Blessings</p>
`; `;
this.sendEmail(user.username, subject, message) this.sendEmail(user.username, subject, html)
} }
} }