diff --git a/server/controllers/comment.controller.js b/server/controllers/comment.controller.js index a6445d3f..6f146323 100644 --- a/server/controllers/comment.controller.js +++ b/server/controllers/comment.controller.js @@ -89,7 +89,7 @@ async function idsToUsers(ids) { const user = await User.get(id); users.push(user); } catch (e) { - console.log('e.idsToUsers', e); + console.log('e.idsToUsers', e); // eslint-disable-line } } /* eslint-disable no-await-in-loop */ @@ -135,7 +135,7 @@ async function update(req, res, next) { usersMentioned: usersWeShouldEmail }); } catch (e) { - console.log('e', e); + console.log('e', e); // eslint-disable-line } } else { comment.mentions = []; @@ -249,6 +249,7 @@ async function create(req, res, next) { const comment = new Comment(); comment.content = content; + let usersMentioned = []; if (mentions) { usersMentioned = await idsToUsers(mentions); @@ -303,6 +304,7 @@ async function create(req, res, next) { content, userWhoReplied: user }); + return ForumThread.increaseCommentCount(entityId).then(() => res.status(201).json({ result: commentSaved })); } diff --git a/server/helpers/forumNotifications.helper.js b/server/helpers/forumNotifications.helper.js index 653b7314..4a77d0b2 100644 --- a/server/helpers/forumNotifications.helper.js +++ b/server/helpers/forumNotifications.helper.js @@ -6,12 +6,15 @@ import ForumThread from '../models/forumThread.model'; function getUserDescription(userWhoReplied) { let userDesc = 'Someone'; + if (userWhoReplied.username) { userDesc = userWhoReplied.username; } + if (userWhoReplied.name) { userDesc = userWhoReplied.name; } + return userDesc; } @@ -23,27 +26,34 @@ async function sendForumNotificationEmail({ threadId, content, userWhoReplied }) const thread = await ForumThread.get(threadId); const userToEmail = thread.author; const { email, _id } = thread.author; - if (userToEmail.emailNotiicationSettings && - userToEmail.emailNotiicationSettings.unsubscribedFromThreads) return; + const { emailNotificationSettings } = userToEmail; + const userIsAuthor = (userIdWhoReplied.toString() === _id.toString()); + const isUnsubscribed = ( + emailNotificationSettings && + emailNotificationSettings.unsubscribedFromThreads + ); + + if (isUnsubscribed || userIsAuthor) { + return; + } - // Don't email if you are the author and replying to own stuff: - if (userIdWhoReplied.toString() === _id.toString()) return; const contentSummary = content.substr(0, 50); const msg = { to: email, from: config.email.fromAddress, - subject: 'Someone commented on your thread', + subject: `New comment on ${thread.title || contentSummary}`, text: `${userDesc} commented in your thread: ${config.baseUrl}/forum/${threadId}`, html: `${userDesc} commented on your post.

- "${contentSummary}..." + "${content}" [ click to read more]

Unsubscribe ` }; + sgMail.send(msg); } catch (e) { - console.log('Error emailing notification', e); + console.log('Error emailing notification', e); // eslint-disable-line } } @@ -57,21 +67,31 @@ async function sendReplyNotificationEmail({ const parentComment = await Comment.get(parentCommentId); const userToEmail = parentComment.author; const { email, _id } = parentComment.author; - if (userToEmail.emailNotiicationSettings && - userToEmail.emailNotiicationSettings.unsubscribedFromCommentReplies) return; - // Don't send if parentComment is owned by thread creator. To prevent + if ( + userToEmail.emailNotificationSettings && + userToEmail.emailNotificationSettings.unsubscribedFromCommentReplies + ) { + return; + } + + // Don't send if parentComment is owned by thread creator. To prevent // double emailing. Make sure thread notifications are turned on: - if (userToEmail.emailNotiicationSettings && - !userToEmail.emailNotiicationSettings.unsubscribedFromThreads) { + if ( + userToEmail.emailNotificationSettings && + !userToEmail.emailNotificationSettings.unsubscribedFromThreads + ) { const thread = await ForumThread.get(threadId); - if (thread.author._id.toString() === _id.toString()) return; + if (thread.author._id.toString() === _id.toString()) { + return; + } } // Don't email if you are the author and replying to own stuff: - if (userIdWhoReplied.toString() === _id.toString()) return; + if (userIdWhoReplied.toString() === _id.toString()) { + return; + } - const contentSummary = content.substr(0, 50); const msg = { to: email, from: config.email.fromAddress, @@ -80,47 +100,57 @@ async function sendReplyNotificationEmail({ html: `${userDesc} replied to your comment.

- "${contentSummary}..." + "${content}..." [ click to read more]

Unsubscribe ` }; sgMail.send(msg); } catch (e) { - console.log('Error emailing notification:', e); + console.log('Error emailing notification:', e); // eslint-disable-line } } +async function sendMentionsNotificationEmail(props) { + const { threadId, userWhoReplied, usersMentioned = [] } = props; + const userDesc = getUserDescription(userWhoReplied); + const { title } = await ForumThread.findById(threadId); + let { content } = props; + + // Fix `@mentions` labels + usersMentioned.forEach((user) => { + content = content.replace(user._id, user.name); // eslint-disable-line + }); -async function sendMentionsNotificationEmail({ - content, threadId, userWhoReplied, usersMentioned -}) { try { - const userDesc = getUserDescription(userWhoReplied); + each(usersMentioned, (userToEmail) => { // eslint-disable-line + const { email, emailNotificationSettings } = userToEmail; + const validEmail = !!(email); + const isUnsubscribed = ( + emailNotificationSettings && + emailNotificationSettings.unsubscribedFromMentions + ); - const contentSummary = content.substr(0, 50); - each(usersMentioned, (userToEmail) => { - if (userToEmail.emailNotiicationSettings && - userToEmail.emailNotiicationSettings.unsubscribedFromMentions) { - console.log('Unsubscribed from mentions', userToEmail); - } else { - const { email } = userToEmail; - const msg = { - to: email, - from: config.email.fromAddress, - subject: 'Someone mentioned you in a thread', - text: `${userDesc} mentioned you: ${config.baseUrl}/forum/${threadId}`, - html: `${userDesc} mentioned you. -
-
- "${contentSummary}..." - [ click to read more] -

Unsubscribe ` - }; - sgMail.send(msg); + if (isUnsubscribed || !validEmail) { + return console.log('Unsubscribed from mentions', userToEmail); // eslint-disable-line } + + const msg = { + to: email, + from: config.email.fromAddress, + subject: `New comment on ${title}`, + text: `${userDesc} mentioned you: ${config.baseUrl}/forum/${threadId}`, + html: `${userDesc} mentioned you. +
+
+ "${content}" + [ click to read more] +

Unsubscribe` + }; + + sgMail.send(msg); }); } catch (e) { - console.log('Error emailing notification', e); + console.log('Error emailing notification', e); // eslint-disable-line } }