aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-07-28 09:57:16 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-07-31 11:35:19 +0200
commitd573926e9b94fb19c8f51c53f71fc853182e1761 (patch)
tree907cc81c7275efe30aa90047c0763a7254bd1063 /server
parent594d3e48d8a887bbf48ce4cc594c1c36c9640fb1 (diff)
downloadPeerTube-d573926e9b94fb19c8f51c53f71fc853182e1761.tar.gz
PeerTube-d573926e9b94fb19c8f51c53f71fc853182e1761.tar.zst
PeerTube-d573926e9b94fb19c8f51c53f71fc853182e1761.zip
Add migrations for abuse messages
Diffstat (limited to 'server')
-rw-r--r--server/initializers/constants.ts2
-rw-r--r--server/initializers/migrations/0525-abuse-messages.ts54
-rw-r--r--server/lib/emailer.ts30
-rw-r--r--server/lib/emails/abuse-new-message/html.pug4
-rw-r--r--server/lib/emails/abuse-state-change/html.pug4
-rw-r--r--server/lib/notifier.ts9
6 files changed, 87 insertions, 16 deletions
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'
23 23
24// --------------------------------------------------------------------------- 24// ---------------------------------------------------------------------------
25 25
26const LAST_MIGRATION_VERSION = 520 26const LAST_MIGRATION_VERSION = 525
27 27
28// --------------------------------------------------------------------------- 28// ---------------------------------------------------------------------------
29 29
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 @@
1import * as Sequelize from 'sequelize'
2
3async function up (utils: {
4 transaction: Sequelize.Transaction
5 queryInterface: Sequelize.QueryInterface
6 sequelize: Sequelize.Sequelize
7}): Promise<void> {
8 await utils.sequelize.query(`
9 CREATE TABLE IF NOT EXISTS "abuseMessage" (
10 "id" serial,
11 "message" text NOT NULL,
12 "byModerator" boolean NOT NULL,
13 "accountId" integer REFERENCES "account" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
14 "abuseId" integer NOT NULL REFERENCES "abuse" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
15 "createdAt" timestamp WITH time zone NOT NULL,
16 "updatedAt" timestamp WITH time zone NOT NULL,
17 PRIMARY KEY ("id")
18 );
19 `)
20
21 const notificationSettingColumns = [ 'abuseStateChange', 'abuseNewMessage' ]
22
23 for (const column of notificationSettingColumns) {
24 const data = {
25 type: Sequelize.INTEGER,
26 defaultValue: null,
27 allowNull: true
28 }
29 await utils.queryInterface.addColumn('userNotificationSetting', column, data)
30 }
31
32 {
33 const query = 'UPDATE "userNotificationSetting" SET "abuseStateChange" = 3, "abuseNewMessage" = 3'
34 await utils.sequelize.query(query)
35 }
36
37 for (const column of notificationSettingColumns) {
38 const data = {
39 type: Sequelize.INTEGER,
40 defaultValue: null,
41 allowNull: false
42 }
43 await utils.queryInterface.changeColumn('userNotificationSetting', column, data)
44 }
45}
46
47function down (options) {
48 throw new Error('Not implemented.')
49}
50
51export {
52 up,
53 down
54}
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'
11import { bunyanLogger, logger } from '../helpers/logger' 11import { bunyanLogger, logger } from '../helpers/logger'
12import { CONFIG, isEmailEnabled } from '../initializers/config' 12import { CONFIG, isEmailEnabled } from '../initializers/config'
13import { WEBSERVER } from '../initializers/constants' 13import { WEBSERVER } from '../initializers/constants'
14import { MAbuseFull, MAbuseMessage, MActorFollowActors, MActorFollowFull, MUser } from '../types/models' 14import { MAbuseFull, MAbuseMessage, MAccountDefault, MActorFollowActors, MActorFollowFull, MUser } from '../types/models'
15import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video' 15import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video'
16import { JobQueue } from './job-queue' 16import { JobQueue } from './job-queue'
17 17
@@ -362,9 +362,11 @@ class Emailer {
362 ? 'Report #' + abuse.id + ' has been accepted' 362 ? 'Report #' + abuse.id + ' has been accepted'
363 : 'Report #' + abuse.id + ' has been rejected' 363 : 'Report #' + abuse.id + ' has been rejected'
364 364
365 const abuseUrl = WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id
366
365 const action = { 367 const action = {
366 text, 368 text,
367 url: WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id 369 url: abuseUrl
368 } 370 }
369 371
370 const emailPayload: EmailPayload = { 372 const emailPayload: EmailPayload = {
@@ -374,6 +376,7 @@ class Emailer {
374 locals: { 376 locals: {
375 action, 377 action,
376 abuseId: abuse.id, 378 abuseId: abuse.id,
379 abuseUrl,
377 isAccepted: abuse.state === AbuseState.ACCEPTED 380 isAccepted: abuse.state === AbuseState.ACCEPTED
378 } 381 }
379 } 382 }
@@ -381,15 +384,24 @@ class Emailer {
381 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) 384 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
382 } 385 }
383 386
384 addAbuseNewMessageNotification (to: string[], options: { target: 'moderator' | 'reporter', abuse: MAbuseFull, message: MAbuseMessage }) { 387 addAbuseNewMessageNotification (
385 const { abuse, target, message } = options 388 to: string[],
389 options: {
390 target: 'moderator' | 'reporter'
391 abuse: MAbuseFull
392 message: MAbuseMessage
393 accountMessage: MAccountDefault
394 }) {
395 const { abuse, target, message, accountMessage } = options
396
397 const text = 'New message on report #' + abuse.id
398 const abuseUrl = target === 'moderator'
399 ? WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + abuse.id
400 : WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id
386 401
387 const text = 'New message on abuse #' + abuse.id
388 const action = { 402 const action = {
389 text, 403 text,
390 url: target === 'moderator' 404 url: abuseUrl
391 ? WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + abuse.id
392 : WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id
393 } 405 }
394 406
395 const emailPayload: EmailPayload = { 407 const emailPayload: EmailPayload = {
@@ -397,7 +409,9 @@ class Emailer {
397 to, 409 to,
398 subject: text, 410 subject: text,
399 locals: { 411 locals: {
412 abuseId: abuse.id,
400 abuseUrl: action.url, 413 abuseUrl: action.url,
414 messageAccountName: accountMessage.getDisplayName(),
401 messageText: message.message, 415 messageText: message.message,
402 action 416 action
403 } 417 }
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
2include ../common/mixins.pug 2include ../common/mixins.pug
3 3
4block title 4block title
5 | New abuse message 5 | New message on abuse report
6 6
7block content 7block content
8 p 8 p
9 | A new message was created on #[a(href=WEBSERVER.URL) abuse ##{abuseId} on #{WEBSERVER.HOST}] 9 | A new message by #{messageAccountName} was posted on #[a(href=abuseUrl) abuse report ##{abuseId}] on #{WEBSERVER.HOST}
10 blockquote #{messageText} 10 blockquote #{messageText}
11 br(style="display: none;") 11 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
2include ../common/mixins.pug 2include ../common/mixins.pug
3 3
4block title 4block title
5 | Abuse state changed 5 | Abuse report state changed
6 6
7block content 7block content
8 p 8 p
9 | #[a(href=abuseUrl) Your abuse ##{abuseId} on #{WEBSERVER.HOST}] has been #{isAccepted ? 'accepted' : 'rejected'} 9 | #[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
24import { isBlockedByServerOrAccount } from './blocklist' 24import { isBlockedByServerOrAccount } from './blocklist'
25import { Emailer } from './emailer' 25import { Emailer } from './emailer'
26import { PeerTubeSocket } from './peertube-socket' 26import { PeerTubeSocket } from './peertube-socket'
27import { AccountModel } from '@server/models/account/account'
27 28
28class Notifier { 29class Notifier {
29 30
@@ -137,7 +138,7 @@ class Notifier {
137 }) 138 })
138 } 139 }
139 140
140 notifyOnAbuseMessage (abuse: MAbuseFull, message: AbuseMessageModel): void { 141 notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void {
141 this.notifyOfNewAbuseMessage(abuse, message) 142 this.notifyOfNewAbuseMessage(abuse, message)
142 .catch(err => { 143 .catch(err => {
143 logger.error('Cannot notify on new abuse %d message.', abuse.id, { err }) 144 logger.error('Cannot notify on new abuse %d message.', abuse.id, { err })
@@ -436,6 +437,8 @@ class Notifier {
436 const url = this.getAbuseUrl(abuse) 437 const url = this.getAbuseUrl(abuse)
437 logger.info('Notifying reporter and moderators of new abuse message on %s.', url) 438 logger.info('Notifying reporter and moderators of new abuse message on %s.', url)
438 439
440 const accountMessage = await AccountModel.load(message.accountId)
441
439 function settingGetter (user: MUserWithNotificationSetting) { 442 function settingGetter (user: MUserWithNotificationSetting) {
440 return user.NotificationSetting.abuseNewMessage 443 return user.NotificationSetting.abuseNewMessage
441 } 444 }
@@ -452,11 +455,11 @@ class Notifier {
452 } 455 }
453 456
454 function emailSenderReporter (emails: string[]) { 457 function emailSenderReporter (emails: string[]) {
455 return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message }) 458 return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message, accountMessage })
456 } 459 }
457 460
458 function emailSenderModerators (emails: string[]) { 461 function emailSenderModerators (emails: string[]) {
459 return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message }) 462 return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message, accountMessage })
460 } 463 }
461 464
462 async function buildReporterOptions () { 465 async function buildReporterOptions () {