aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/notifier.ts
blob: 66cfc31c4b61da61a1430a4bc84f5a2e717fa203 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { MUser, MUserDefault } from '@server/types/models/user'
import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist'
import { UserNotificationSettingValue } from '../../../shared/models/users'
import { logger } from '../../helpers/logger'
import { CONFIG } from '../../initializers/config'
import { MAbuseFull, MAbuseMessage, MActorFollowFull, MApplication, MPlugin } from '../../types/models'
import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../../types/models/video'
import { JobQueue } from '../job-queue'
import { PeerTubeSocket } from '../peertube-socket'
import { Hooks } from '../plugins/hooks'
import {
  AbstractNotification,
  AbuseStateChangeForReporter,
  AutoFollowForInstance,
  CommentMention,
  FollowForInstance,
  FollowForUser,
  ImportFinishedForOwner,
  ImportFinishedForOwnerPayload,
  NewAbuseForModerators,
  NewAbuseMessageForModerators,
  NewAbuseMessageForReporter,
  NewAbusePayload,
  NewAutoBlacklistForModerators,
  NewBlacklistForOwner,
  NewCommentForVideoOwner,
  NewPeerTubeVersionForAdmins,
  NewPluginVersionForAdmins,
  NewVideoForSubscribers,
  OwnedPublicationAfterAutoUnblacklist,
  OwnedPublicationAfterScheduleUpdate,
  OwnedPublicationAfterTranscoding,
  RegistrationForModerators,
  StudioEditionFinishedForOwner,
  UnblacklistForOwner
} from './shared'

class Notifier {

  private readonly notificationModels = {
    newVideo: [ NewVideoForSubscribers ],
    publicationAfterTranscoding: [ OwnedPublicationAfterTranscoding ],
    publicationAfterScheduleUpdate: [ OwnedPublicationAfterScheduleUpdate ],
    publicationAfterAutoUnblacklist: [ OwnedPublicationAfterAutoUnblacklist ],
    newComment: [ CommentMention, NewCommentForVideoOwner ],
    newAbuse: [ NewAbuseForModerators ],
    newBlacklist: [ NewBlacklistForOwner ],
    unblacklist: [ UnblacklistForOwner ],
    importFinished: [ ImportFinishedForOwner ],
    userRegistration: [ RegistrationForModerators ],
    userFollow: [ FollowForUser ],
    instanceFollow: [ FollowForInstance ],
    autoInstanceFollow: [ AutoFollowForInstance ],
    newAutoBlacklist: [ NewAutoBlacklistForModerators ],
    abuseStateChange: [ AbuseStateChangeForReporter ],
    newAbuseMessage: [ NewAbuseMessageForReporter, NewAbuseMessageForModerators ],
    newPeertubeVersion: [ NewPeerTubeVersionForAdmins ],
    newPluginVersion: [ NewPluginVersionForAdmins ],
    videoStudioEditionFinished: [ StudioEditionFinishedForOwner ]
  }

  private static instance: Notifier

  private constructor () {
  }

  notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void {
    const models = this.notificationModels.newVideo

    this.sendNotifications(models, video)
      .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
  }

  notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
    const models = this.notificationModels.publicationAfterTranscoding

    this.sendNotifications(models, video)
      .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
  }

  notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void {
    const models = this.notificationModels.publicationAfterScheduleUpdate

    this.sendNotifications(models, video)
      .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err }))
  }

  notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void {
    const models = this.notificationModels.publicationAfterAutoUnblacklist

    this.sendNotifications(models, video)
      .catch(err => {
        logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })
      })
  }

  notifyOnNewComment (comment: MCommentOwnerVideo): void {
    const models = this.notificationModels.newComment

    this.sendNotifications(models, comment)
      .catch(err => logger.error('Cannot notify of new comment.', comment.url, { err }))
  }

  notifyOnNewAbuse (payload: NewAbusePayload): void {
    const models = this.notificationModels.newAbuse

    this.sendNotifications(models, payload)
      .catch(err => logger.error('Cannot notify of new abuse %d.', payload.abuseInstance.id, { err }))
  }

  notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
    const models = this.notificationModels.newAutoBlacklist

    this.sendNotifications(models, videoBlacklist)
      .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
  }

  notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
    const models = this.notificationModels.newBlacklist

    this.sendNotifications(models, videoBlacklist)
      .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
  }

  notifyOnVideoUnblacklist (video: MVideoFullLight): void {
    const models = this.notificationModels.unblacklist

    this.sendNotifications(models, video)
        .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
  }

  notifyOnFinishedVideoImport (payload: ImportFinishedForOwnerPayload): void {
    const models = this.notificationModels.importFinished

    this.sendNotifications(models, payload)
        .catch(err => {
          logger.error('Cannot notify owner that its video import %s is finished.', payload.videoImport.getTargetIdentifier(), { err })
        })
  }

  notifyOnNewUserRegistration (user: MUserDefault): void {
    const models = this.notificationModels.userRegistration

    this.sendNotifications(models, user)
      .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
  }

  notifyOfNewUserFollow (actorFollow: MActorFollowFull): void {
    const models = this.notificationModels.userFollow

    this.sendNotifications(models, actorFollow)
      .catch(err => {
        logger.error(
          'Cannot notify owner of channel %s of a new follow by %s.',
          actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
          actorFollow.ActorFollower.Account.getDisplayName(),
          { err }
        )
      })
  }

  notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
    const models = this.notificationModels.instanceFollow

    this.sendNotifications(models, actorFollow)
      .catch(err => logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }))
  }

  notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void {
    const models = this.notificationModels.autoInstanceFollow

    this.sendNotifications(models, actorFollow)
      .catch(err => logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err }))
  }

  notifyOnAbuseStateChange (abuse: MAbuseFull): void {
    const models = this.notificationModels.abuseStateChange

    this.sendNotifications(models, abuse)
      .catch(err => logger.error('Cannot notify of abuse %d state change.', abuse.id, { err }))
  }

  notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void {
    const models = this.notificationModels.newAbuseMessage

    this.sendNotifications(models, { abuse, message })
      .catch(err => logger.error('Cannot notify on new abuse %d message.', abuse.id, { err }))
  }

  notifyOfNewPeerTubeVersion (application: MApplication, latestVersion: string) {
    const models = this.notificationModels.newPeertubeVersion

    this.sendNotifications(models, { application, latestVersion })
      .catch(err => logger.error('Cannot notify on new PeerTubeb version %s.', latestVersion, { err }))
  }

  notifyOfNewPluginVersion (plugin: MPlugin) {
    const models = this.notificationModels.newPluginVersion

    this.sendNotifications(models, plugin)
      .catch(err => logger.error('Cannot notify on new plugin version %s.', plugin.name, { err }))
  }

  notifyOfFinishedVideoStudioEdition (video: MVideoFullLight) {
    const models = this.notificationModels.videoStudioEditionFinished

    this.sendNotifications(models, video)
      .catch(err => logger.error('Cannot notify on finished studio edition %s.', video.url, { err }))
  }

  private async notify <T> (object: AbstractNotification<T>) {
    await object.prepare()

    const users = object.getTargetUsers()

    if (users.length === 0) return
    if (await object.isDisabled()) return

    object.log()

    const toEmails: string[] = []

    for (const user of users) {
      const setting = object.getSetting(user)

      const webNotificationEnabled = this.isWebNotificationEnabled(setting)
      const emailNotificationEnabled = this.isEmailEnabled(user, setting)
      const notification = object.createNotification(user)

      if (webNotificationEnabled) {
        await notification.save()

        PeerTubeSocket.Instance.sendNotification(user.id, notification)
      }

      if (emailNotificationEnabled) {
        toEmails.push(user.email)
      }

      Hooks.runAction('action:notifier.notification.created', { webNotificationEnabled, emailNotificationEnabled, user, notification })
    }

    for (const to of toEmails) {
      const payload = await object.createEmail(to)
      JobQueue.Instance.createJobAsync({ type: 'email', payload })
    }
  }

  private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) {
    if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false

    return value & UserNotificationSettingValue.EMAIL
  }

  private isWebNotificationEnabled (value: UserNotificationSettingValue) {
    return value & UserNotificationSettingValue.WEB
  }

  private async sendNotifications <T> (models: (new (payload: T) => AbstractNotification<T>)[], payload: T) {
    for (const model of models) {
      // eslint-disable-next-line new-cap
      await this.notify(new model(payload))
    }
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}

// ---------------------------------------------------------------------------

export {
  Notifier
}