aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
blob: 968b5bca901f2c179c93dead6d34afd48e56abca (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
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 { AbuseState, UserNotificationType } from '@shared/models'
import { AbstractNotification } from '../common/abstract-notification'

export class AbuseStateChangeForReporter extends AbstractNotification <MAbuseFull> {

  private user: MUserDefault

  async prepare () {
    const reporter = this.abuse.ReporterAccount
    if (reporter.isOwned() !== true) return

    this.user = await UserModel.loadByAccountActorId(this.abuse.ReporterAccount.actorId)
  }

  log () {
    logger.info('Notifying reporter of abuse % of state change.', getAbuseTargetUrl(this.abuse))
  }

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

  getTargetUsers () {
    if (!this.user) return []

    return [ this.user ]
  }

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

    return notification
  }

  createEmail (to: string) {
    const text = this.abuse.state === AbuseState.ACCEPTED
      ? 'Report #' + this.abuse.id + ' has been accepted'
      : 'Report #' + this.abuse.id + ' has been rejected'

    const abuseUrl = WEBSERVER.URL + '/my-account/abuses?search=%23' + this.abuse.id

    const action = {
      text,
      url: abuseUrl
    }

    return {
      template: 'abuse-state-change',
      to,
      subject: text,
      locals: {
        action,
        abuseId: this.abuse.id,
        abuseUrl,
        isAccepted: this.abuse.state === AbuseState.ACCEPTED
      }
    }
  }

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