aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/shared/abuse/new-abuse-for-moderators.ts
blob: c3c7c5515691ff203ca5744aa1da309ffd55b053 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { logger } from '@server/helpers/logger'
import { WEBSERVER } from '@server/initializers/constants'
import { getAbuseTargetUrl } from '@server/lib/activitypub/url'
import { UserModel } from '@server/models/user/user'
import { UserNotificationModel } from '@server/models/user/user-notification'
import { MAbuseFull, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models'
import { UserAbuse, UserNotificationType, UserRight } from '@shared/models'
import { AbstractNotification } from '../common/abstract-notification'

export type NewAbusePayload = { abuse: UserAbuse, abuseInstance: MAbuseFull, reporter: string }

export class NewAbuseForModerators extends AbstractNotification <NewAbusePayload> {
  private moderators: MUserDefault[]

  async prepare () {
    this.moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
  }

  log () {
    logger.info('Notifying %s user/moderators of new abuse %s.', this.moderators.length, getAbuseTargetUrl(this.payload.abuseInstance))
  }

  getSetting (user: MUserWithNotificationSetting) {
    return user.NotificationSetting.abuseAsModerator
  }

  getTargetUsers () {
    return this.moderators
  }

  async createNotification (user: MUserWithNotificationSetting) {
    const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
      type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS,
      userId: user.id,
      abuseId: this.payload.abuseInstance.id
    })
    notification.Abuse = this.payload.abuseInstance

    return notification
  }

  createEmail (to: string) {
    const abuseInstance = this.payload.abuseInstance

    if (abuseInstance.VideoAbuse) return this.createVideoAbuseEmail(to)
    if (abuseInstance.VideoCommentAbuse) return this.createCommentAbuseEmail(to)

    return this.createAccountAbuseEmail(to)
  }

  private createVideoAbuseEmail (to: string) {
    const video = this.payload.abuseInstance.VideoAbuse.Video
    const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()

    return {
      template: 'video-abuse-new',
      to,
      subject: `New video abuse report from ${this.payload.reporter}`,
      locals: {
        videoUrl,
        isLocal: video.remote === false,
        videoCreatedAt: new Date(video.createdAt).toLocaleString(),
        videoPublishedAt: new Date(video.publishedAt).toLocaleString(),
        videoName: video.name,
        reason: this.payload.abuse.reason,
        videoChannel: this.payload.abuse.video.channel,
        reporter: this.payload.reporter,
        action: this.buildEmailAction()
      }
    }
  }

  private createCommentAbuseEmail (to: string) {
    const comment = this.payload.abuseInstance.VideoCommentAbuse.VideoComment
    const commentUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() + ';threadId=' + comment.getThreadId()

    return {
      template: 'video-comment-abuse-new',
      to,
      subject: `New comment abuse report from ${this.payload.reporter}`,
      locals: {
        commentUrl,
        videoName: comment.Video.name,
        isLocal: comment.isOwned(),
        commentCreatedAt: new Date(comment.createdAt).toLocaleString(),
        reason: this.payload.abuse.reason,
        flaggedAccount: this.payload.abuseInstance.FlaggedAccount.getDisplayName(),
        reporter: this.payload.reporter,
        action: this.buildEmailAction()
      }
    }
  }

  private createAccountAbuseEmail (to: string) {
    const account = this.payload.abuseInstance.FlaggedAccount
    const accountUrl = account.getClientUrl()

    return {
      template: 'account-abuse-new',
      to,
      subject: `New account abuse report from ${this.payload.reporter}`,
      locals: {
        accountUrl,
        accountDisplayName: account.getDisplayName(),
        isLocal: account.isOwned(),
        reason: this.payload.abuse.reason,
        reporter: this.payload.reporter,
        action: this.buildEmailAction()
      }
    }
  }

  private buildEmailAction () {
    return {
      text: 'View report #' + this.payload.abuseInstance.id,
      url: WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + this.payload.abuseInstance.id
    }
  }
}