sending emails when new post to profiles
This commit is contained in:
@@ -23,6 +23,17 @@ userDB = (DB) => {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DB.getProfileCache = async (profileId) => {
|
||||||
|
if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId];
|
||||||
|
const _id = DB.ObjectID(profileId);
|
||||||
|
let r = await DB.profileCols.findOne({ _id }).catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
if (r) userProfileCache[profileId] = r;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
DB.getProfiles = async (query) => {
|
DB.getProfiles = async (query) => {
|
||||||
let r = await DB.profileCols.find({isGroup: false})
|
let r = await DB.profileCols.find({isGroup: false})
|
||||||
.sort({ lastUpdate: -1 }).limit(20)
|
.sort({ lastUpdate: -1 }).limit(20)
|
||||||
|
|||||||
@@ -34,6 +34,11 @@ const getDB = new Promise((resolve, reject) => {
|
|||||||
return DB.usersCol.findOne({ username: username });
|
return DB.usersCol.findOne({ username: username });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DB.getUserById = (userid)=>{
|
||||||
|
const _id = new mongo.ObjectID(userid);
|
||||||
|
return DB.usersCol.findOne({ _id });
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
53
notifications.js
Normal file
53
notifications.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
const nodemailer = require("nodemailer");
|
||||||
|
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
|
||||||
|
auth: {
|
||||||
|
user: "noreply@emmint.com", // generated ethereal user
|
||||||
|
pass: process.env.EMAILPASS, // generated ethereal password
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// send mail with defined transport object
|
||||||
|
let info = await transporter.sendMail({
|
||||||
|
from: '"EMI Social" <noreply@emmint.com', // sender address
|
||||||
|
to, // list of receivers
|
||||||
|
subject, // Subject line
|
||||||
|
//text: "Hello world?", // plain text body
|
||||||
|
html, // html body
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("Message sent: %s", info.messageId);
|
||||||
|
},
|
||||||
|
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);
|
||||||
|
let subject = senderProfile.profile.firstName + " post on your profile";
|
||||||
|
let message = `
|
||||||
|
<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>
|
||||||
|
|
||||||
|
`;
|
||||||
|
this.sendEmail(user.username, subject, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Notifications
|
||||||
16
package-lock.json
generated
16
package-lock.json
generated
@@ -15,7 +15,8 @@
|
|||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"mongodb": "^3.6.3"
|
"mongodb": "^3.6.3",
|
||||||
|
"nodemailer": "^6.6.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@mapbox/node-pre-gyp": {
|
"node_modules/@mapbox/node-pre-gyp": {
|
||||||
@@ -775,6 +776,14 @@
|
|||||||
"node": "4.x || >=6.0.0"
|
"node": "4.x || >=6.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/nodemailer": {
|
||||||
|
"version": "6.6.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.3.tgz",
|
||||||
|
"integrity": "sha512-faZFufgTMrphYoDjvyVpbpJcYzwyFnbAMmQtj1lVBYAUSm3SOy2fIdd9+Mr4UxPosBa0JRw9bJoIwQn+nswiew==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/nopt": {
|
"node_modules/nopt": {
|
||||||
"version": "5.0.0",
|
"version": "5.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz",
|
||||||
@@ -1734,6 +1743,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
|
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
|
||||||
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
|
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
|
||||||
},
|
},
|
||||||
|
"nodemailer": {
|
||||||
|
"version": "6.6.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.3.tgz",
|
||||||
|
"integrity": "sha512-faZFufgTMrphYoDjvyVpbpJcYzwyFnbAMmQtj1lVBYAUSm3SOy2fIdd9+Mr4UxPosBa0JRw9bJoIwQn+nswiew=="
|
||||||
|
},
|
||||||
"nopt": {
|
"nopt": {
|
||||||
"version": "5.0.0",
|
"version": "5.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz",
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"mongodb": "^3.6.3"
|
"mongodb": "^3.6.3",
|
||||||
|
"nodemailer": "^6.6.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ var router = express.Router()
|
|||||||
|
|
||||||
const DB = require("./../mongoDB.js");
|
const DB = require("./../mongoDB.js");
|
||||||
const Post = require("./../def/post.js");
|
const Post = require("./../def/post.js");
|
||||||
|
const Notifications = require("./../notifications.js")
|
||||||
|
|
||||||
DB.getDB.then((DB)=>{
|
DB.getDB.then((DB)=>{
|
||||||
|
|
||||||
@@ -29,9 +30,13 @@ DB.getDB.then((DB)=>{
|
|||||||
}
|
}
|
||||||
post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined;
|
post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined;
|
||||||
let postObj = new Post(post);
|
let postObj = new Post(post);
|
||||||
let dbr = await DB.newPost(postObj)
|
let dbr = await DB.newPost(postObj);
|
||||||
post = postObj.toObj();
|
post = postObj.toObj();
|
||||||
post._id = dbr.insertedId;
|
post._id = dbr.insertedId;
|
||||||
|
if(post.toProfile && post.toProfile != post.profileid){
|
||||||
|
//send email notification
|
||||||
|
Notifications.youGotANewPost(post.toProfile, post.profileid, post.content)
|
||||||
|
}
|
||||||
return res.json({
|
return res.json({
|
||||||
status: "ok",
|
status: "ok",
|
||||||
...post
|
...post
|
||||||
|
|||||||
Reference in New Issue
Block a user