From d573926e9b94fb19c8f51c53f71fc853182e1761 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 28 Jul 2020 09:57:16 +0200 Subject: Add migrations for abuse messages --- server/initializers/constants.ts | 2 +- .../initializers/migrations/0525-abuse-messages.ts | 54 ++++++++++++++++++++++ server/lib/emailer.ts | 30 ++++++++---- server/lib/emails/abuse-new-message/html.pug | 4 +- server/lib/emails/abuse-state-change/html.pug | 4 +- server/lib/notifier.ts | 9 ++-- 6 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 server/initializers/migrations/0525-abuse-messages.ts (limited to 'server') diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index a40a22395..ca6c2a7ff 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -23,7 +23,7 @@ import { CONFIG, registerConfigChangedHandler } from './config' // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 520 +const LAST_MIGRATION_VERSION = 525 // --------------------------------------------------------------------------- diff --git a/server/initializers/migrations/0525-abuse-messages.ts b/server/initializers/migrations/0525-abuse-messages.ts new file mode 100644 index 000000000..c8fd7cbcf --- /dev/null +++ b/server/initializers/migrations/0525-abuse-messages.ts @@ -0,0 +1,54 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction + queryInterface: Sequelize.QueryInterface + sequelize: Sequelize.Sequelize +}): Promise { + await utils.sequelize.query(` + CREATE TABLE IF NOT EXISTS "abuseMessage" ( + "id" serial, + "message" text NOT NULL, + "byModerator" boolean NOT NULL, + "accountId" integer REFERENCES "account" ("id") ON DELETE SET NULL ON UPDATE CASCADE, + "abuseId" integer NOT NULL REFERENCES "abuse" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + "createdAt" timestamp WITH time zone NOT NULL, + "updatedAt" timestamp WITH time zone NOT NULL, + PRIMARY KEY ("id") + ); + `) + + const notificationSettingColumns = [ 'abuseStateChange', 'abuseNewMessage' ] + + for (const column of notificationSettingColumns) { + const data = { + type: Sequelize.INTEGER, + defaultValue: null, + allowNull: true + } + await utils.queryInterface.addColumn('userNotificationSetting', column, data) + } + + { + const query = 'UPDATE "userNotificationSetting" SET "abuseStateChange" = 3, "abuseNewMessage" = 3' + await utils.sequelize.query(query) + } + + for (const column of notificationSettingColumns) { + const data = { + type: Sequelize.INTEGER, + defaultValue: null, + allowNull: false + } + await utils.queryInterface.changeColumn('userNotificationSetting', column, data) + } +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index 9c49aa2f6..25b0aaedd 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -11,7 +11,7 @@ import { isTestInstance, root } from '../helpers/core-utils' import { bunyanLogger, logger } from '../helpers/logger' import { CONFIG, isEmailEnabled } from '../initializers/config' import { WEBSERVER } from '../initializers/constants' -import { MAbuseFull, MAbuseMessage, MActorFollowActors, MActorFollowFull, MUser } from '../types/models' +import { MAbuseFull, MAbuseMessage, MAccountDefault, MActorFollowActors, MActorFollowFull, MUser } from '../types/models' import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video' import { JobQueue } from './job-queue' @@ -362,9 +362,11 @@ class Emailer { ? 'Report #' + abuse.id + ' has been accepted' : 'Report #' + abuse.id + ' has been rejected' + const abuseUrl = WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id + const action = { text, - url: WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id + url: abuseUrl } const emailPayload: EmailPayload = { @@ -374,6 +376,7 @@ class Emailer { locals: { action, abuseId: abuse.id, + abuseUrl, isAccepted: abuse.state === AbuseState.ACCEPTED } } @@ -381,15 +384,24 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } - addAbuseNewMessageNotification (to: string[], options: { target: 'moderator' | 'reporter', abuse: MAbuseFull, message: MAbuseMessage }) { - const { abuse, target, message } = options + addAbuseNewMessageNotification ( + to: string[], + options: { + target: 'moderator' | 'reporter' + abuse: MAbuseFull + message: MAbuseMessage + accountMessage: MAccountDefault + }) { + const { abuse, target, message, accountMessage } = options + + const text = 'New message on report #' + abuse.id + const abuseUrl = target === 'moderator' + ? WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + abuse.id + : WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id - const text = 'New message on abuse #' + abuse.id const action = { text, - url: target === 'moderator' - ? WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + abuse.id - : WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id + url: abuseUrl } const emailPayload: EmailPayload = { @@ -397,7 +409,9 @@ class Emailer { to, subject: text, locals: { + abuseId: abuse.id, abuseUrl: action.url, + messageAccountName: accountMessage.getDisplayName(), messageText: message.message, action } diff --git a/server/lib/emails/abuse-new-message/html.pug b/server/lib/emails/abuse-new-message/html.pug index a4180aba1..0841775d2 100644 --- a/server/lib/emails/abuse-new-message/html.pug +++ b/server/lib/emails/abuse-new-message/html.pug @@ -2,10 +2,10 @@ extends ../common/greetings include ../common/mixins.pug block title - | New abuse message + | New message on abuse report block content p - | A new message was created on #[a(href=WEBSERVER.URL) abuse ##{abuseId} on #{WEBSERVER.HOST}] + | A new message by #{messageAccountName} was posted on #[a(href=abuseUrl) abuse report ##{abuseId}] on #{WEBSERVER.HOST} blockquote #{messageText} br(style="display: none;") diff --git a/server/lib/emails/abuse-state-change/html.pug b/server/lib/emails/abuse-state-change/html.pug index a94c8521d..ca89a2f05 100644 --- a/server/lib/emails/abuse-state-change/html.pug +++ b/server/lib/emails/abuse-state-change/html.pug @@ -2,8 +2,8 @@ extends ../common/greetings include ../common/mixins.pug block title - | Abuse state changed + | Abuse report state changed block content p - | #[a(href=abuseUrl) Your abuse ##{abuseId} on #{WEBSERVER.HOST}] has been #{isAccepted ? 'accepted' : 'rejected'} + | #[a(href=abuseUrl) Your abuse report ##{abuseId}] on #{WEBSERVER.HOST} has been #{isAccepted ? 'accepted' : 'rejected'} diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index 5c50fcf01..9c2f16c27 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts @@ -24,6 +24,7 @@ import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../type import { isBlockedByServerOrAccount } from './blocklist' import { Emailer } from './emailer' import { PeerTubeSocket } from './peertube-socket' +import { AccountModel } from '@server/models/account/account' class Notifier { @@ -137,7 +138,7 @@ class Notifier { }) } - notifyOnAbuseMessage (abuse: MAbuseFull, message: AbuseMessageModel): void { + notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void { this.notifyOfNewAbuseMessage(abuse, message) .catch(err => { logger.error('Cannot notify on new abuse %d message.', abuse.id, { err }) @@ -436,6 +437,8 @@ class Notifier { const url = this.getAbuseUrl(abuse) logger.info('Notifying reporter and moderators of new abuse message on %s.', url) + const accountMessage = await AccountModel.load(message.accountId) + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.abuseNewMessage } @@ -452,11 +455,11 @@ class Notifier { } function emailSenderReporter (emails: string[]) { - return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message }) + return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message, accountMessage }) } function emailSenderModerators (emails: string[]) { - return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message }) + return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message, accountMessage }) } async function buildReporterOptions () { -- cgit v1.2.3