Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions server/controllers/comment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -303,6 +304,7 @@ async function create(req, res, next) {
content,
userWhoReplied: user
});

return ForumThread.increaseCommentCount(entityId).then(() =>
res.status(201).json({ result: commentSaved }));
}
Expand Down
114 changes: 72 additions & 42 deletions server/helpers/forumNotifications.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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.
<br />
<br />
"${contentSummary}..."
"${content}"
[<strong> <a href="${config.baseUrl}/forum/${threadId}/"> click to read more</a>]
<br /><br /> <a href="${config.baseUrl}/notification-settings/"> Unsubscribe </a>`
};

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

Expand All @@ -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,
Expand All @@ -80,47 +100,57 @@ async function sendReplyNotificationEmail({
html: `${userDesc} replied to your comment.
<br />
<br />
"${contentSummary}..."
"${content}..."
[<strong> <a href="${config.baseUrl}/forum/${threadId}/"> click to read more</a>]
<br /><br /> <a href="${config.baseUrl}/notification-settings/"> Unsubscribe </a>`
};
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.
<br />
<br />
"${contentSummary}..."
[<strong> <a href="${config.baseUrl}/forum/${threadId}/"> click to read more</a>]
<br /><br /> <a href="${config.baseUrl}/notification-settings/"> Unsubscribe </a>`
};
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.
<br />
<br />
"${content}"
<strong>[<a href="${config.baseUrl}/post/${threadId}/"> click to read more</a>]
<br /><br /><a href="${config.baseUrl}/notification-settings/">Unsubscribe</a>`
};

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

Expand Down