Files
habitica/website/server/libs/chatReporting/inboxChatReporter.js

93 lines
2.6 KiB
JavaScript

import nconf from 'nconf';
import ChatReporter from './chatReporter';
import {
NotFound,
} from '../errors';
import { getGroupUrl, sendTxn } from '../email';
import slack from '../slack';
import apiError from '../apiError';
import _find from 'lodash/find';
const FLAG_REPORT_EMAILS = nconf.get('FLAG_REPORT_EMAIL').split(',').map((email) => {
return { email, canSend: true };
});
export default class InboxChatReporter extends ChatReporter {
constructor (req, res) {
super(req, res);
this.user = res.locals.user;
}
async validate () {
this.req.checkParams('messageId', apiError('messageIdRequired')).notEmpty();
let validationErrors = this.req.validationErrors();
if (validationErrors) throw validationErrors;
let messages = this.user.inbox.messages;
const message = _find(messages, (m) => m.id === this.req.params.messageId);
if (!message) throw new NotFound(this.res.t('messageGroupChatNotFound'));
const userComment = this.req.body.comment;
return {message, userComment};
}
async notify (message, userComment) {
const group = {
type: 'private messages',
};
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},
{name: 'REPORTER_COMMENT', content: userComment || ''},
]));
slack.sendInboxFlagNotification({
authorEmail: this.authorEmail,
flagger: this.user,
message,
userComment,
});
}
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;
// Log total number of flags (publicly viewable)
if (!message.flagCount) message.flagCount = 0;
if (this.user.contributor.admin) {
// Arbitrary amount, higher than 2
message.flagCount = 5;
} else {
message.flagCount++;
}
this.user.inbox.messages[message.id] = message;
this.user.markModified('inbox.messages');
await this.user.save();
}
async flag () {
let {message, userComment} = await this.validate();
await this.flagInboxMessage(message);
await this.notify(message, userComment);
return message;
}
}