mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
refactor {group,inbox}-chatReporter variables
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
import {
|
import { getGroupUrl, getUserInfo } from '../email';
|
||||||
} from '../errors';
|
|
||||||
import { getUserInfo } from '../email';
|
|
||||||
import { getAuthorEmailFromMessage } from '../chat';
|
import { getAuthorEmailFromMessage } from '../chat';
|
||||||
|
|
||||||
export default class ChatReporter {
|
export default class ChatReporter {
|
||||||
@@ -11,27 +9,51 @@ export default class ChatReporter {
|
|||||||
|
|
||||||
async validate () {}
|
async validate () {}
|
||||||
|
|
||||||
async notify (group, message) {
|
async getMessageVariables (group, message) {
|
||||||
const reporterEmailContent = getUserInfo(this.user, ['email']).email;
|
const reporterEmail = getUserInfo(this.user, ['email']).email;
|
||||||
this.authorEmail = await getAuthorEmailFromMessage(message);
|
|
||||||
this.emailVariables = [
|
const authorVariables = await this.getAuthorVariables(message);
|
||||||
|
const groupUrl = getGroupUrl(group);
|
||||||
|
|
||||||
|
return [
|
||||||
{name: 'MESSAGE_TIME', content: (new Date(message.timestamp)).toString()},
|
{name: 'MESSAGE_TIME', content: (new Date(message.timestamp)).toString()},
|
||||||
{name: 'MESSAGE_TEXT', content: message.text},
|
{name: 'MESSAGE_TEXT', content: message.text},
|
||||||
|
|
||||||
{name: 'REPORTER_DISPLAYNAME', content: this.user.profile.name},
|
{name: 'REPORTER_DISPLAYNAME', content: this.user.profile.name},
|
||||||
{name: 'REPORTER_USERNAME', content: this.user.auth.local.username},
|
{name: 'REPORTER_USERNAME', content: this.user.auth.local.username},
|
||||||
{name: 'REPORTER_UUID', content: this.user._id},
|
{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: 'REPORTER_MODAL_URL', content: `/static/front/#?memberId=${this.user._id}`},
|
||||||
|
|
||||||
{name: 'AUTHOR_DISPLAYNAME', content: message.user},
|
...authorVariables,
|
||||||
{name: 'AUTHOR_USERNAME', content: message.username},
|
|
||||||
{name: 'AUTHOR_UUID', content: message.uuid},
|
{name: 'GROUP_NAME', content: group.name},
|
||||||
{name: 'AUTHOR_EMAIL', content: this.authorEmail},
|
{name: 'GROUP_TYPE', content: group.type},
|
||||||
{name: 'AUTHOR_MODAL_URL', content: `/static/front/#?memberId=${message.uuid}`},
|
{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 () {
|
async flag () {
|
||||||
throw new Error('Flag must be implemented');
|
throw new Error('Flag must be implemented');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
BadRequest,
|
BadRequest,
|
||||||
NotFound,
|
NotFound,
|
||||||
} from '../errors';
|
} from '../errors';
|
||||||
import { getGroupUrl, sendTxn } from '../email';
|
import { sendTxn } from '../email';
|
||||||
import slack from '../slack';
|
import slack from '../slack';
|
||||||
import { model as Group } from '../../models/group';
|
import { model as Group } from '../../models/group';
|
||||||
import { chatModel as Chat } from '../../models/message';
|
import { chatModel as Chat } from '../../models/message';
|
||||||
@@ -50,16 +50,12 @@ export default class GroupChatReporter extends ChatReporter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async notify (group, message, userComment, automatedComment = '') {
|
async notify (group, message, userComment, automatedComment = '') {
|
||||||
await super.notify(group, message);
|
let emailVariables = await this.getMessageVariables(group, message);
|
||||||
|
emailVariables = emailVariables.concat([
|
||||||
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 || ''},
|
{name: 'REPORTER_COMMENT', content: userComment || ''},
|
||||||
]));
|
]);
|
||||||
|
|
||||||
|
sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods-with-comments', emailVariables);
|
||||||
|
|
||||||
slack.sendFlagNotification({
|
slack.sendFlagNotification({
|
||||||
authorEmail: this.authorEmail,
|
authorEmail: this.authorEmail,
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import ChatReporter from './chatReporter';
|
|||||||
import {
|
import {
|
||||||
BadRequest,
|
BadRequest,
|
||||||
} from '../errors';
|
} from '../errors';
|
||||||
import { getGroupUrl, sendTxn } from '../email';
|
import { getUserInfo, sendTxn} from '../email';
|
||||||
import slack from '../slack';
|
import slack from '../slack';
|
||||||
import apiError from '../apiError';
|
import apiError from '../apiError';
|
||||||
|
|
||||||
import * as inboxLib from '../inbox';
|
import * as inboxLib from '../inbox';
|
||||||
|
import {getAuthorEmailFromMessage} from '../chat';
|
||||||
|
|
||||||
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 };
|
||||||
@@ -48,16 +49,12 @@ export default class InboxChatReporter extends ChatReporter {
|
|||||||
_id: 'N/A',
|
_id: 'N/A',
|
||||||
};
|
};
|
||||||
|
|
||||||
await super.notify(group, message);
|
let emailVariables = await this.getMessageVariables(group, message);
|
||||||
|
emailVariables = emailVariables.concat([
|
||||||
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'},
|
|
||||||
{name: 'REPORTER_COMMENT', content: userComment || ''},
|
{name: 'REPORTER_COMMENT', content: userComment || ''},
|
||||||
]));
|
]);
|
||||||
|
|
||||||
|
sendTxn(FLAG_REPORT_EMAILS, 'flag-report-to-mods-with-comments', emailVariables);
|
||||||
|
|
||||||
slack.sendInboxFlagNotification({
|
slack.sendInboxFlagNotification({
|
||||||
authorEmail: this.authorEmail,
|
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) {
|
updateMessageAndSave (message, ...changedFields) {
|
||||||
for (const changedField of changedFields) {
|
for (const changedField of changedFields) {
|
||||||
message.markModified(changedField);
|
message.markModified(changedField);
|
||||||
|
|||||||
Reference in New Issue
Block a user