News post emails

This commit is contained in:
aeroreyna
2022-02-27 11:04:10 -08:00
parent 3d9393c0b2
commit 57a91c0470
5 changed files with 86 additions and 14 deletions

View File

@@ -167,7 +167,7 @@ postDB = (DB)=>{
$in: ids $in: ids
}} }}
], ],
nonOrganicType: null nonOrganicType: null // Exlcude news
}; };
return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().then(async (posts)=>{ return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().then(async (posts)=>{
return await filterPrivateGroups(posts, profile); return await filterPrivateGroups(posts, profile);

View File

@@ -40,6 +40,7 @@ userDB = (DB) => {
DB.getProfile = async (profileId) => { DB.getProfile = async (profileId) => {
//if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId]; //if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId];
if(!profileId) return false; if(!profileId) return false;
try{
const _id = DB.ObjectID(profileId); const _id = DB.ObjectID(profileId);
let r = await DB.profileCols.findOne({ _id }).catch((err) => { let r = await DB.profileCols.findOne({ _id }).catch((err) => {
console.log(err); console.log(err);
@@ -47,6 +48,9 @@ userDB = (DB) => {
}); });
if (r) userProfileCache[profileId] = r; if (r) userProfileCache[profileId] = r;
return r; return r;
}catch(_){
return {};
}
} }
DB.getPopularProfiles = async (limit = 10) => { DB.getPopularProfiles = async (limit = 10) => {

View File

@@ -36,6 +36,11 @@ const getDB = new Promise((resolve, reject) => {
return DB.usersCol.findOne({ username: username }); 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)=>{ DB.resetUserPassword = (username, password)=>{
return DB.usersCol.updateOne({username}, {$set:{password}}) return DB.usersCol.updateOne({username}, {$set:{password}})
.catch(console.error); .catch(console.error);

View File

@@ -17,9 +17,8 @@ const sendEmail = async (to, subject, html) => {
to, to,
subject, subject,
html, html,
}); }).catch(console.error);
if(info && info.messageId) console.log("Email sent: %s", info.messageId);
console.log("Email sent: %s", info.messageId);
}; };
const yourBookmarkedPostGotACommentTemplate = (post, userEmail, postProfile, senderProfile, bookedProfile, message) => { const yourBookmarkedPostGotACommentTemplate = (post, userEmail, postProfile, senderProfile, bookedProfile, message) => {
@@ -152,6 +151,65 @@ const yourGroupHasARequestTemplate = (groupProfile, ownerEmail, senderProfile) =
sendEmail(ownerEmail, subject, html) 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<linksFound.length; i++ ) {
let replace = linksFound[i];
if ( !( linksFound[i].match( /(http(s?)):\/\// ) ) ) { replace = 'http://' + linksFound[i] }
let linkText = replace.split( '/' )[2];
if ( linkText.substring( 0, 3 ) == 'www' ) { linkText = linkText.replace( 'www.', '' ) }
if ( linkText.match( /youtu/ ) ) {
let youtubeID = replace.split( '/' ).slice(-1)[0];
aLink.push( '<div class="video-wrapper"><iframe src="https://www.youtube.com/embed/' + youtubeID + '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>' )
}
else if ( linkText.match( /vimeo/ ) ) {
let vimeoID = replace.split( '/' ).slice(-1)[0];
aLink.push( '<div class="video-wrapper"><iframe src="https://player.vimeo.com/video/' + vimeoID + '" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>' )
}
else {
aLink.push( '<a href="' + replace + '" target="_blank">' + linkText + '</a>' );
}
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 = `
<p>Hello beloved,</p>
<p>We have news for you:</p>
<blockquote cite="https://social.emmint.com/">
<p>${filteredContent}</p>
</blockquote>
<figcaption>— Emmanuel International Ministries</figcaption>
<p>Check more updates <a href="https://social.emmint.com/">on the website</a></p>
<p>Blessings</p>
`;
for (const user of users) {
console.log(user, 'Sending news')
await sendEmail(user.username, subject, html);
}
};
const Notifications = { const Notifications = {
sendEmail, sendEmail,
async yourBookmarkedPostGotAComment(post, postProfile, senderProfile, message) { async yourBookmarkedPostGotAComment(post, postProfile, senderProfile, message) {
@@ -215,11 +273,16 @@ const Notifications = {
if(profile.isGroup) { if(profile.isGroup) {
return this.yourGroupGotANewPost(profile, senderProfile, message, post); 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`; const notifBody = `${senderProfile.profile.firstName} post in your profile`;
DB.addNotification(toProfileId, notifBody, post._id); DB.addNotification(toProfileId, notifBody, post._id);
return youGotANewPostTemplate(profile, user.username, senderProfile, message); return youGotANewPostTemplate(profile, user.username, senderProfile, message);
}, },
youHaveAnInvitation, youHaveAnInvitation,
broadcastNews,
async yourGroupHasARequest(requesterProfileId, groupId){ async yourGroupHasARequest(requesterProfileId, groupId){
const DB = await DBGetter.getDB; const DB = await DBGetter.getDB;
const requesterProfile = await DB.getProfileCache(requesterProfileId); const requesterProfile = await DB.getProfileCache(requesterProfileId);

View File

@@ -120,8 +120,8 @@ DB.getDB.then((DB) => {
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) { if ((post.toProfile && post.toProfile != post.profileid) || post.nonOrganicType == 'News') {
Notifications.youGotANewPost(post) Notifications.youGotANewPost(post);
} }
return res.json({ return res.json({
status: "ok", status: "ok",