From ca5927fe733481e5518707a513445d4f2929846c Mon Sep 17 00:00:00 2001 From: negue Date: Sat, 2 Jun 2018 19:27:28 +0200 Subject: [PATCH] sendInboxFlagNotification (for private message flag content) --- .../libs/chatReporting/inboxChatReporter.js | 41 ++++++------ website/server/libs/slack.js | 63 ++++++++++++++++++- 2 files changed, 81 insertions(+), 23 deletions(-) diff --git a/website/server/libs/chatReporting/inboxChatReporter.js b/website/server/libs/chatReporting/inboxChatReporter.js index 841daa25d7..e78ba8c0ad 100644 --- a/website/server/libs/chatReporting/inboxChatReporter.js +++ b/website/server/libs/chatReporting/inboxChatReporter.js @@ -2,16 +2,14 @@ import nconf from 'nconf'; import ChatReporter from './chatReporter'; import { - BadRequest, NotFound, } from '../errors'; import { getGroupUrl, sendTxn } from '../email'; import slack from '../slack'; -import { model as Group } from '../../models/group'; -import { model as Chat } from '../../models/chat'; import apiError from '../apiError'; -const COMMUNITY_MANAGER_EMAIL = nconf.get('EMAILS:COMMUNITY_MANAGER_EMAIL'); +import _find from 'lodash/find'; + const FLAG_REPORT_EMAILS = nconf.get('FLAG_REPORT_EMAIL').split(',').map((email) => { return { email, canSend: true }; }); @@ -29,23 +27,21 @@ export default class InboxChatReporter extends ChatReporter { let validationErrors = this.req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup({ - user: this.user, - groupId: this.groupId, - optionalMembership: this.user.contributor.admin, - }); - if (!group) throw new NotFound(this.res.t('groupNotFound')); + let messages = this.user.inbox.messages; - const message = await Chat.findOne({_id: this.req.params.chatId}).exec(); + const message = _find(messages, (m) => m.id === this.req.params.messageId); if (!message) throw new NotFound(this.res.t('messageGroupChatNotFound')); - if (message.uuid === 'system') throw new BadRequest(this.res.t('messageCannotFlagSystemMessages', {communityManagerEmail: COMMUNITY_MANAGER_EMAIL})); const userComment = this.req.body.comment; - return {message, group, userComment}; + return {message, userComment}; } - async notify (group, message, userComment) { + async notify (message, userComment) { + const group = { + type: 'private messages', + }; + await super.notify(group, message); const groupUrl = getGroupUrl(group); @@ -57,22 +53,20 @@ export default class InboxChatReporter extends ChatReporter { {name: 'REPORTER_COMMENT', content: userComment || ''}, ])); - slack.sendFlagNotification({ + slack.sendInboxFlagNotification({ authorEmail: this.authorEmail, flagger: this.user, - group, message, userComment, }); } - async flagGroupMessage (group, message) { + async flagInboxMessage (message) { // Log user ids that have flagged the message if (!message.flags) message.flags = {}; // TODO fix error type if (message.flags[this.user._id] && !this.user.contributor.admin) throw new NotFound(this.res.t('messageGroupChatFlagAlreadyReported')); message.flags[this.user._id] = true; - message.markModified('flags'); // Log total number of flags (publicly viewable) if (!message.flagCount) message.flagCount = 0; @@ -83,13 +77,16 @@ export default class InboxChatReporter extends ChatReporter { message.flagCount++; } - await message.save(); + this.user.inbox.messages[message.id] = message; + this.user.markModified('inbox.messages'); + + await this.user.save(); } async flag () { - let {message, group, userComment} = await this.validate(); - await this.flagGroupMessage(group, message); - await this.notify(group, message, userComment); + let {message, userComment} = await this.validate(); + await this.flagInboxMessage(message); + await this.notify(message, userComment); return message; } } diff --git a/website/server/libs/slack.js b/website/server/libs/slack.js index fe3b9696e5..0d4060ce38 100644 --- a/website/server/libs/slack.js +++ b/website/server/libs/slack.js @@ -44,7 +44,7 @@ function sendFlagNotification ({ let titleLink; let authorName; let title = `Flag in ${group.name}`; - let text = `${flagger.profile.name} (${flagger.id}; language: ${flagger.preferences.language}) flagged a message`; + let text = `${flagger.profile.name} (${flagger.id}; language: ${flagger.preferences.language}) flagged a group message`; if (userComment) { text += ` and commented: ${userComment}`; @@ -81,6 +81,66 @@ function sendFlagNotification ({ }); } +function sendInboxFlagNotification ({ + authorEmail, + flagger, + message, + userComment, +}) { + if (SKIP_FLAG_METHODS) { + return; + } + let titleLink; + let authorName; + let title = `Flag in ${flagger.profile.name}'s Inbox`; + let text = `${flagger.profile.name} (${flagger.id}; language: ${flagger.preferences.language}) flagged a PM`; + + if (userComment) { + text += ` and commented: ${userComment}`; + } + + if (!message.user && message.uuid === 'system') { + authorName = 'System Message'; + } else { + authorName = `${message.user} - ${authorEmail} - ${message.uuid}`; + } + + let messageText = message.text; + + if (flagger.id === message.uuid) { + messageText += `: ${flagger.profile.name} is writing to itself.`; + } else { + let sender = ''; + let recipient = ''; + + if (message.sent) { + sender = flagger.profile.name; + recipient = message.user; + } else { + sender = message.user; + recipient = flagger.profile.name; + } + + messageText += `: ${sender} is writing this message to ${recipient}.`; + } + + flagSlack.send({ + text, + attachments: [{ + fallback: 'Flag Message', + color: 'danger', + author_name: authorName, + title, + title_link: titleLink, + text: messageText, + footer: `<${SLACK_FLAGGING_FOOTER_LINK}?groupId=privateMessages&chatId=${message.id}|Flag this message>`, + mrkdwn_in: [ + 'text', + ], + }], + }); +} + function sendSubscriptionNotification ({ buyer, recipient, @@ -150,6 +210,7 @@ function sendSlurNotification ({ module.exports = { sendFlagNotification, + sendInboxFlagNotification, sendSubscriptionNotification, sendSlurNotification, };