diff --git a/mongoDB.js b/mongoDB.js
index 4c1ccd3..3f0969d 100644
--- a/mongoDB.js
+++ b/mongoDB.js
@@ -39,6 +39,15 @@ const getDB = new Promise((resolve, reject) => {
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)=>{
return DB.usersCol.insertOne(userInformation).catch((err)=>{
console.log(err);
diff --git a/notifications.js b/notifications.js
index 43c75cf..5c99a72 100644
--- a/notifications.js
+++ b/notifications.js
@@ -3,36 +3,33 @@ const DBGetter = require("./mongoDB.js");
const Notifications = {
async sendEmail(to, subject, html) {
- // create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "mail.emmint.com",
port: 465,
- secure: true, // true for 465, false for other ports
+ secure: true,
auth: {
- user: "noreply@emmint.com", // generated ethereal user
- pass: process.env.EMAILPASS, // generated ethereal password
+ user: "noreply@emmint.com",
+ pass: process.env.EMAILPASS,
},
});
- // send mail with defined transport object
let info = await transporter.sendMail({
- from: '"EMI Social"
You got a comment on your post:
@@ -45,7 +42,7 @@ const Notifications = {Comment:
-${Message}
+${message}
Blessings
`; - 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 = ` +Hello ${profile.profile.firstName},
+ +${groupProfile.profile.firstName} ${groupProfile.profile.lastName} have new post:
+ +++${message}
+
Blessings
+ +`; + this.sendEmail(user, subject, html) + }) + }, + async youGotANewPost(toProfileId, whoPostedId, 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 message = ` + let html = `Hello ${profile.profile.firstName},
You got a new post:
-${Message}
+${message}
Blessings
`; - this.sendEmail(user.username, subject, message) + this.sendEmail(user.username, subject, html) } }