From 57a91c04703b7c651313aa46ff04e584bfb2cef5 Mon Sep 17 00:00:00 2001 From: aeroreyna Date: Sun, 27 Feb 2022 11:04:10 -0800 Subject: [PATCH] News post emails --- dbTools/post.js | 2 +- dbTools/profile.js | 18 +++++++----- mongoDB.js | 5 ++++ notifications.js | 71 +++++++++++++++++++++++++++++++++++++++++++--- routes/post.js | 4 +-- 5 files changed, 86 insertions(+), 14 deletions(-) diff --git a/dbTools/post.js b/dbTools/post.js index cb7849e..ca43a4e 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -167,7 +167,7 @@ postDB = (DB)=>{ $in: ids }} ], - nonOrganicType: null + nonOrganicType: null // Exlcude news }; return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().then(async (posts)=>{ return await filterPrivateGroups(posts, profile); diff --git a/dbTools/profile.js b/dbTools/profile.js index c543810..34f5233 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -40,13 +40,17 @@ userDB = (DB) => { DB.getProfile = async (profileId) => { //if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId]; if(!profileId) return false; - 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; + try{ + 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; + }catch(_){ + return {}; + } } DB.getPopularProfiles = async (limit = 10) => { diff --git a/mongoDB.js b/mongoDB.js index 9993a54..cc956d0 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -36,6 +36,11 @@ const getDB = new Promise((resolve, reject) => { return DB.usersCol.findOne({ username: username }); } + DB.getAllEmails = () => { + // Change this to paginations chunks + return DB.usersCol.find().project({username: 1}).toArray(); + }; + DB.resetUserPassword = (username, password)=>{ return DB.usersCol.updateOne({username}, {$set:{password}}) .catch(console.error); diff --git a/notifications.js b/notifications.js index f35a16a..02b9c65 100644 --- a/notifications.js +++ b/notifications.js @@ -17,9 +17,8 @@ const sendEmail = async (to, subject, html) => { to, subject, html, - }); - - console.log("Email sent: %s", info.messageId); + }).catch(console.error); + if(info && info.messageId) console.log("Email sent: %s", info.messageId); }; const yourBookmarkedPostGotACommentTemplate = (post, userEmail, postProfile, senderProfile, bookedProfile, message) => { @@ -152,6 +151,65 @@ const yourGroupHasARequestTemplate = (groupProfile, ownerEmail, senderProfile) = sendEmail(ownerEmail, subject, html) } +const convertLinks = ( input ) => { + + let text = input; + const linksFound = text.match( /(?:www|https?)[^\s]+/g ); + const aLink = []; + + if ( linksFound != null ) { + + for ( let i=0; i' ) + } + else if ( linkText.match( /vimeo/ ) ) { + let vimeoID = replace.split( '/' ).slice(-1)[0]; + aLink.push( '
' ) + } + else { + aLink.push( '' + linkText + '' ); + } + text = text.split( linksFound[i] ).map(item => { return aLink[i].includes('iframe') ? item.trim() : item } ).join( aLink[i] ); + } + return text; + + } + else { + return input; + } +} + +const broadcastNews = async (post, users) => { + let subject = "We have news for you!"; + let filteredContent = convertLinks(post.content); + let html = ` +

Hello beloved,

+ +

We have news for you:

+ +
+

${filteredContent}

+
+
— Emmanuel International Ministries
+ +

Check more updates on the website

+ +

Blessings

+`; + + for (const user of users) { + console.log(user, 'Sending news') + await sendEmail(user.username, subject, html); + } +}; + const Notifications = { sendEmail, async yourBookmarkedPostGotAComment(post, postProfile, senderProfile, message) { @@ -212,14 +270,19 @@ const Notifications = { const profile = await DB.getProfileCache(toProfileId); const user = await DB.getUserById(profile.userid); const senderProfile = await DB.getProfileCache(whoPostedId); - if (profile.isGroup) { + if(profile.isGroup) { return this.yourGroupGotANewPost(profile, senderProfile, message, post); } + if(post.nonOrganicType == 'News'){ + const emails = await DB.getAllEmails(); + return this.broadcastNews(post, emails); + } const notifBody = `${senderProfile.profile.firstName} post in your profile`; DB.addNotification(toProfileId, notifBody, post._id); return youGotANewPostTemplate(profile, user.username, senderProfile, message); }, youHaveAnInvitation, + broadcastNews, async yourGroupHasARequest(requesterProfileId, groupId){ const DB = await DBGetter.getDB; const requesterProfile = await DB.getProfileCache(requesterProfileId); diff --git a/routes/post.js b/routes/post.js index 2484ba6..da32dc9 100644 --- a/routes/post.js +++ b/routes/post.js @@ -120,8 +120,8 @@ DB.getDB.then((DB) => { let dbr = await DB.newPost(postObj); post = postObj.toObj(); post._id = dbr.insertedId; - if (post.toProfile && post.toProfile != post.profileid) { - Notifications.youGotANewPost(post) + if ((post.toProfile && post.toProfile != post.profileid) || post.nonOrganicType == 'News') { + Notifications.youGotANewPost(post); } return res.json({ status: "ok",