sendInboxFlagNotification (for private message flag content)

This commit is contained in:
negue
2018-06-02 19:27:28 +02:00
parent 5a0eed7eae
commit ca5927fe73
2 changed files with 81 additions and 23 deletions

View File

@@ -2,16 +2,14 @@ import nconf from 'nconf';
import ChatReporter from './chatReporter'; import ChatReporter from './chatReporter';
import { import {
BadRequest,
NotFound, NotFound,
} from '../errors'; } from '../errors';
import { getGroupUrl, sendTxn } from '../email'; import { getGroupUrl, sendTxn } from '../email';
import slack from '../slack'; import slack from '../slack';
import { model as Group } from '../../models/group';
import { model as Chat } from '../../models/chat';
import apiError from '../apiError'; 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) => { const FLAG_REPORT_EMAILS = nconf.get('FLAG_REPORT_EMAIL').split(',').map((email) => {
return { email, canSend: true }; return { email, canSend: true };
}); });
@@ -29,23 +27,21 @@ export default class InboxChatReporter extends ChatReporter {
let validationErrors = this.req.validationErrors(); let validationErrors = this.req.validationErrors();
if (validationErrors) throw validationErrors; if (validationErrors) throw validationErrors;
let group = await Group.getGroup({ let messages = this.user.inbox.messages;
user: this.user,
groupId: this.groupId,
optionalMembership: this.user.contributor.admin,
});
if (!group) throw new NotFound(this.res.t('groupNotFound'));
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) 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; 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); await super.notify(group, message);
const groupUrl = getGroupUrl(group); const groupUrl = getGroupUrl(group);
@@ -57,22 +53,20 @@ export default class InboxChatReporter extends ChatReporter {
{name: 'REPORTER_COMMENT', content: userComment || ''}, {name: 'REPORTER_COMMENT', content: userComment || ''},
])); ]));
slack.sendFlagNotification({ slack.sendInboxFlagNotification({
authorEmail: this.authorEmail, authorEmail: this.authorEmail,
flagger: this.user, flagger: this.user,
group,
message, message,
userComment, userComment,
}); });
} }
async flagGroupMessage (group, message) { async flagInboxMessage (message) {
// Log user ids that have flagged the message // Log user ids that have flagged the message
if (!message.flags) message.flags = {}; if (!message.flags) message.flags = {};
// TODO fix error type // TODO fix error type
if (message.flags[this.user._id] && !this.user.contributor.admin) throw new NotFound(this.res.t('messageGroupChatFlagAlreadyReported')); if (message.flags[this.user._id] && !this.user.contributor.admin) throw new NotFound(this.res.t('messageGroupChatFlagAlreadyReported'));
message.flags[this.user._id] = true; message.flags[this.user._id] = true;
message.markModified('flags');
// Log total number of flags (publicly viewable) // Log total number of flags (publicly viewable)
if (!message.flagCount) message.flagCount = 0; if (!message.flagCount) message.flagCount = 0;
@@ -83,13 +77,16 @@ export default class InboxChatReporter extends ChatReporter {
message.flagCount++; message.flagCount++;
} }
await message.save(); this.user.inbox.messages[message.id] = message;
this.user.markModified('inbox.messages');
await this.user.save();
} }
async flag () { async flag () {
let {message, group, userComment} = await this.validate(); let {message, userComment} = await this.validate();
await this.flagGroupMessage(group, message); await this.flagInboxMessage(message);
await this.notify(group, message, userComment); await this.notify(message, userComment);
return message; return message;
} }
} }

View File

@@ -44,7 +44,7 @@ function sendFlagNotification ({
let titleLink; let titleLink;
let authorName; let authorName;
let title = `Flag in ${group.name}`; 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) { if (userComment) {
text += ` and commented: ${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 ({ function sendSubscriptionNotification ({
buyer, buyer,
recipient, recipient,
@@ -150,6 +210,7 @@ function sendSlurNotification ({
module.exports = { module.exports = {
sendFlagNotification, sendFlagNotification,
sendInboxFlagNotification,
sendSubscriptionNotification, sendSubscriptionNotification,
sendSlurNotification, sendSlurNotification,
}; };