aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/shared/abuse/abstract-new-abuse-message.ts
blob: 1dc1ccfc2359b12d487f6e661a03afe71fd14cbe (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
import { WEBSERVER } from '@server/initializers/constants'
import { AccountModel } from '@server/models/account/account'
import { UserNotificationModel } from '@server/models/user/user-notification'
import { MAbuseFull, MAbuseMessage, MAccountDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models'
import { UserNotificationType } from '@shared/models'
import { AbstractNotification } from '../common/abstract-notification'

type NewAbuseMessagePayload = {
  abuse: MAbuseFull
  message: MAbuseMessage
}

export abstract class AbstractNewAbuseMessage extends AbstractNotification <NewAbuseMessagePayload> {
  protected messageAccount: MAccountDefault

  async loadMessageAccount () {
    this.messageAccount = await AccountModel.load(this.message.accountId)
  }

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

  createNotification (user: MUserWithNotificationSetting) {
    const notification = UserNotificationModel.build<UserNotificationModelForApi>({
      type: UserNotificationType.ABUSE_NEW_MESSAGE,
      userId: user.id,
      abuseId: this.abuse.id
    })
    notification.Abuse = this.abuse

    return notification
  }

  protected createEmailFor (to: string, target: 'moderator' | 'reporter') {
    const text = 'New message on report #' + this.abuse.id
    const abuseUrl = target === 'moderator'
      ? WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + this.abuse.id
      : WEBSERVER.URL + '/my-account/abuses?search=%23' + this.abuse.id

    const action = {
      text: 'View report #' + this.abuse.id,
      url: abuseUrl
    }

    return {
      template: 'abuse-new-message',
      to,
      subject: text,
      locals: {
        abuseId: this.abuse.id,
        abuseUrl: action.url,
        messageAccountName: this.messageAccount.getDisplayName(),
        messageText: this.message.message,
        action
      }
    }
  }

  protected get abuse () {
    return this.payload.abuse
  }

  protected get message () {
    return this.payload.message
  }
}