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

@@ -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<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 = {
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);