diff --git a/website/server/libs/chatReporting/chatReporter.js b/website/server/libs/chatReporting/chatReporter.js index 5ed7971316..af44748d0b 100644 --- a/website/server/libs/chatReporting/chatReporter.js +++ b/website/server/libs/chatReporting/chatReporter.js @@ -1,6 +1,4 @@ -import { -} from '../errors'; -import { getUserInfo } from '../email'; +import { getGroupUrl, getUserInfo } from '../email'; import { getAuthorEmailFromMessage } from '../chat'; export default class ChatReporter { @@ -11,27 +9,51 @@ export default class ChatReporter { async validate () {} - async notify (group, message) { - const reporterEmailContent = getUserInfo(this.user, ['email']).email; - this.authorEmail = await getAuthorEmailFromMessage(message); - this.emailVariables = [ + async getMessageVariables (group, message) { + const reporterEmail = getUserInfo(this.user, ['email']).email; + + const authorVariables = await this.getAuthorVariables(message); + const groupUrl = getGroupUrl(group); + + return [ {name: 'MESSAGE_TIME', content: (new Date(message.timestamp)).toString()}, {name: 'MESSAGE_TEXT', content: message.text}, {name: 'REPORTER_DISPLAYNAME', content: this.user.profile.name}, {name: 'REPORTER_USERNAME', content: this.user.auth.local.username}, {name: 'REPORTER_UUID', content: this.user._id}, - {name: 'REPORTER_EMAIL', content: reporterEmailContent}, + {name: 'REPORTER_EMAIL', content: reporterEmail}, {name: 'REPORTER_MODAL_URL', content: `/static/front/#?memberId=${this.user._id}`}, - {name: 'AUTHOR_DISPLAYNAME', content: message.user}, - {name: 'AUTHOR_USERNAME', content: message.username}, - {name: 'AUTHOR_UUID', content: message.uuid}, - {name: 'AUTHOR_EMAIL', content: this.authorEmail}, - {name: 'AUTHOR_MODAL_URL', content: `/static/front/#?memberId=${message.uuid}`}, + ...authorVariables, + + {name: 'GROUP_NAME', content: group.name}, + {name: 'GROUP_TYPE', content: group.type}, + {name: 'GROUP_ID', content: group._id}, + {name: 'GROUP_URL', content: groupUrl || 'N/A'}, ]; } + createGenericAuthorVariables (prefix, {user, username, uuid, email}) { + return [ + {name: `${prefix}_DISPLAYNAME`, content: user}, + {name: `${prefix}_USERNAME`, content: username}, + {name: `${prefix}_UUID`, content: uuid}, + {name: `${prefix}_EMAIL`, content: email}, + {name: `${prefix}_MODAL_URL`, content: `/static/front/#?memberId=${uuid}`}, + ]; + } + + async getAuthorVariables (message) { + this.authorEmail = await getAuthorEmailFromMessage(message); + return this.createGenericAuthorVariables('AUTHOR', { + user: message.user, + username: message.username, + uuid: message.uuid, + email: this.authorEmail, + }); + } + async flag () { throw new Error('Flag must be implemented'); } diff --git a/website/server/libs/chatReporting/groupChatReporter.js b/website/server/libs/chatReporting/groupChatReporter.js index 6b2e8600d8..3f0478e02c 100644 --- a/website/server/libs/chatReporting/groupChatReporter.js +++ b/website/server/libs/chatReporting/groupChatReporter.js @@ -6,7 +6,7 @@ import { BadRequest, NotFound, } from '../errors'; -import { getGroupUrl, sendTxn } from '../email'; +import { sendTxn } from '../email'; import slack from '../slack'; import { model as Group } from '../../models/group'; import { chatModel as Chat } from '../../models/message'; @@ -50,16 +50,12 @@ export default class GroupChatReporter extends ChatReporter { } async notify (group, message, userComment, automatedComment = '') { - await super.notify(group, message); - - const groupUrl = getGroupUrl(group); - sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods-with-comments', this.emailVariables.concat([ - {name: 'GROUP_NAME', content: group.name}, - {name: 'GROUP_TYPE', content: group.type}, - {name: 'GROUP_ID', content: group._id}, - {name: 'GROUP_URL', content: groupUrl}, + let emailVariables = await this.getMessageVariables(group, message); + emailVariables = emailVariables.concat([ {name: 'REPORTER_COMMENT', content: userComment || ''}, - ])); + ]); + + sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods-with-comments', emailVariables); slack.sendFlagNotification({ authorEmail: this.authorEmail, diff --git a/website/server/libs/chatReporting/inboxChatReporter.js b/website/server/libs/chatReporting/inboxChatReporter.js index 4c2823ecca..207b570fd3 100644 --- a/website/server/libs/chatReporting/inboxChatReporter.js +++ b/website/server/libs/chatReporting/inboxChatReporter.js @@ -5,11 +5,12 @@ import ChatReporter from './chatReporter'; import { BadRequest, } from '../errors'; -import { getGroupUrl, sendTxn } from '../email'; +import { getUserInfo, sendTxn} from '../email'; import slack from '../slack'; import apiError from '../apiError'; import * as inboxLib from '../inbox'; +import {getAuthorEmailFromMessage} from '../chat'; const FLAG_REPORT_EMAILS = nconf.get('FLAG_REPORT_EMAIL').split(',').map((email) => { return { email, canSend: true }; @@ -48,16 +49,12 @@ export default class InboxChatReporter extends ChatReporter { _id: 'N/A', }; - await super.notify(group, message); - - const groupUrl = getGroupUrl(group); - sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods-with-comments', this.emailVariables.concat([ - {name: 'GROUP_NAME', content: group.name}, - {name: 'GROUP_TYPE', content: group.type}, - {name: 'GROUP_ID', content: group._id}, - {name: 'GROUP_URL', content: groupUrl || 'N/A'}, + let emailVariables = await this.getMessageVariables(group, message); + emailVariables = emailVariables.concat([ {name: 'REPORTER_COMMENT', content: userComment || ''}, - ])); + ]); + + sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods-with-comments', emailVariables); slack.sendInboxFlagNotification({ authorEmail: this.authorEmail, @@ -67,6 +64,33 @@ export default class InboxChatReporter extends ChatReporter { }); } + async getAuthorVariables (message) { + const messageUser = { + user: message.user, + username: message.username, + uuid: message.uuid, + email: await getAuthorEmailFromMessage(message), + }; + + const reporter = { + user: this.user.profile.name, + username: this.user.auth.local.username, + uuid: this.user._id, + email: getUserInfo(this.user, ['email']).email, + }; + + // if message.sent, the reporter is the author of this message + const sendingUser = message.sent ? reporter : messageUser; + const recipient = message.sent ? messageUser : reporter; + + this.authorEmail = sendingUser.email; + + return [ + ...this.createGenericAuthorVariables('AUTHOR', sendingUser), + ...this.createGenericAuthorVariables('RECIPIENT', recipient), + ]; + } + updateMessageAndSave (message, ...changedFields) { for (const changedField of changedFields) { message.markModified(changedField);