aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/shared/instance/registration-for-moderators.ts
blob: e9246742421555d08c3f50beff52fd715f5e535f (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
import { logger } from '@server/helpers/logger'
import { CONFIG } from '@server/initializers/config'
import { UserModel } from '@server/models/user/user'
import { UserNotificationModel } from '@server/models/user/user-notification'
import { MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models'
import { UserNotificationType, UserRight } from '@shared/models'
import { AbstractNotification } from '../common/abstract-notification'

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

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

  log () {
    logger.info('Notifying %s moderators of new user registration of %s.', this.moderators.length, this.payload.username)
  }

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

  getTargetUsers () {
    return this.moderators
  }

  createNotification (user: MUserWithNotificationSetting) {
    const notification = UserNotificationModel.build<UserNotificationModelForApi>({
      type: UserNotificationType.NEW_USER_REGISTRATION,
      userId: user.id,
      accountId: this.payload.Account.id
    })
    notification.Account = this.payload.Account

    return notification
  }

  createEmail (to: string) {
    return {
      template: 'user-registered',
      to,
      subject: `a new user registered on ${CONFIG.INSTANCE.NAME}: ${this.payload.username}`,
      locals: {
        user: this.payload
      }
    }
  }
}