]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/notifier.ts
Add auto follow back support for instances
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
CommitLineData
cef534ed
C
1import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
2import { logger } from '../helpers/logger'
cef534ed
C
3import { Emailer } from './emailer'
4import { UserNotificationModel } from '../models/account/user-notification'
cef534ed
C
5import { UserModel } from '../models/account/user'
6import { PeerTubeSocket } from './peertube-socket'
6dd9de95 7import { CONFIG } from '../initializers/config'
cef534ed 8import { VideoPrivacy, VideoState } from '../../shared/models/videos'
cef534ed 9import * as Bluebird from 'bluebird'
dc133480 10import { AccountBlocklistModel } from '../models/account/account-blocklist'
453e83ea
C
11import {
12 MCommentOwnerVideo,
453e83ea
C
13 MVideoAbuseVideo,
14 MVideoAccountLight,
8424c402 15 MVideoBlacklistLightVideo,
453e83ea
C
16 MVideoBlacklistVideo,
17 MVideoFullLight
18} from '../typings/models/video'
8424c402
C
19import {
20 MUser,
21 MUserDefault,
22 MUserNotifSettingAccount,
23 MUserWithNotificationSetting,
24 UserNotificationModelForApi
25} from '@server/typings/models/user'
26import { MActorFollowFull } from '../typings/models'
453e83ea 27import { MVideoImportVideo } from '@server/typings/models/video/video-import'
cef534ed
C
28
29class Notifier {
30
31 private static instance: Notifier
32
33 private constructor () {}
34
453e83ea 35 notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void {
7ccddd7b 36 // Only notify on public and published videos which are not blacklisted
5b77537c 37 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return
cef534ed
C
38
39 this.notifySubscribersOfNewVideo(video)
40 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
41 }
42
453e83ea 43 notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
7ccddd7b
JM
44 // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update
45 if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return
dc133480
C
46
47 this.notifyOwnedVideoHasBeenPublished(video)
7ccddd7b
JM
48 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
49 }
50
453e83ea 51 notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void {
7ccddd7b
JM
52 // don't notify if video is still blacklisted or waiting for transcoding
53 if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
54
55 this.notifyOwnedVideoHasBeenPublished(video)
56 .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err }))
57 }
58
453e83ea 59 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void {
7ccddd7b
JM
60 // don't notify if video is still waiting for transcoding or scheduled update
61 if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
62
63 this.notifyOwnedVideoHasBeenPublished(video)
64 .catch(err => logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })) // tslint:disable-line:max-line-length
dc133480
C
65 }
66
453e83ea 67 notifyOnNewComment (comment: MCommentOwnerVideo): void {
cef534ed 68 this.notifyVideoOwnerOfNewComment(comment)
f7cc67b4
C
69 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
70
71 this.notifyOfCommentMention(comment)
72 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
cef534ed
C
73 }
74
453e83ea 75 notifyOnNewVideoAbuse (videoAbuse: MVideoAbuseVideo): void {
cef534ed
C
76 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
77 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
78 }
79
8424c402
C
80 notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
81 this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist)
82 .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
7ccddd7b
JM
83 }
84
453e83ea 85 notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
cef534ed
C
86 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
87 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
88 }
89
8424c402 90 notifyOnVideoUnblacklist (video: MVideoFullLight): void {
cef534ed 91 this.notifyVideoOwnerOfUnblacklist(video)
7ccddd7b 92 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
cef534ed
C
93 }
94
453e83ea 95 notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void {
dc133480
C
96 this.notifyOwnerVideoImportIsFinished(videoImport, success)
97 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
98 }
99
8424c402 100 notifyOnNewUserRegistration (user: MUserDefault): void {
f7cc67b4
C
101 this.notifyModeratorsOfNewUserRegistration(user)
102 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
103 }
104
8424c402 105 notifyOfNewUserFollow (actorFollow: MActorFollowFull): void {
f7cc67b4
C
106 this.notifyUserOfNewActorFollow(actorFollow)
107 .catch(err => {
108 logger.error(
109 'Cannot notify owner of channel %s of a new follow by %s.',
110 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
111 actorFollow.ActorFollower.Account.getDisplayName(),
883993c8 112 { err }
f7cc67b4
C
113 )
114 })
115 }
116
8424c402 117 notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
883993c8
C
118 this.notifyAdminsOfNewInstanceFollow(actorFollow)
119 .catch(err => {
120 logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err })
121 })
122 }
123
8424c402
C
124 notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void {
125 this.notifyAdminsOfAutoInstanceFollowing(actorFollow)
126 .catch(err => {
127 logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err })
128 })
129 }
130
453e83ea 131 private async notifySubscribersOfNewVideo (video: MVideoAccountLight) {
cef534ed
C
132 // List all followers that are users
133 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
134
135 logger.info('Notifying %d users of new video %s.', users.length, video.url)
136
8424c402 137 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
138 return user.NotificationSetting.newVideoFromSubscription
139 }
140
8424c402
C
141 async function notificationCreator (user: MUserWithNotificationSetting) {
142 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
143 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
144 userId: user.id,
145 videoId: video.id
146 })
8424c402 147 notification.Video = video
cef534ed
C
148
149 return notification
150 }
151
152 function emailSender (emails: string[]) {
153 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
154 }
155
156 return this.notify({ users, settingGetter, notificationCreator, emailSender })
157 }
158
453e83ea 159 private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) {
f7cc67b4
C
160 if (comment.Video.isOwned() === false) return
161
cef534ed
C
162 const user = await UserModel.loadByVideoId(comment.videoId)
163
164 // Not our user or user comments its own video
165 if (!user || comment.Account.userId === user.id) return
166
dc133480
C
167 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
168 if (accountMuted) return
169
cef534ed
C
170 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
171
8424c402 172 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
173 return user.NotificationSetting.newCommentOnMyVideo
174 }
175
8424c402
C
176 async function notificationCreator (user: MUserWithNotificationSetting) {
177 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
178 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
179 userId: user.id,
180 commentId: comment.id
181 })
8424c402 182 notification.Comment = comment
cef534ed
C
183
184 return notification
185 }
186
187 function emailSender (emails: string[]) {
188 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
189 }
190
191 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
192 }
193
453e83ea 194 private async notifyOfCommentMention (comment: MCommentOwnerVideo) {
41d71344
C
195 const extractedUsernames = comment.extractMentions()
196 logger.debug(
197 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
198 { usernames: extractedUsernames, text: comment.text }
199 )
1f6d57e3 200
41d71344 201 let users = await UserModel.listByUsernames(extractedUsernames)
f7cc67b4
C
202
203 if (comment.Video.isOwned()) {
204 const userException = await UserModel.loadByVideoId(comment.videoId)
205 users = users.filter(u => u.id !== userException.id)
206 }
207
208 // Don't notify if I mentioned myself
209 users = users.filter(u => u.Account.id !== comment.accountId)
210
cef534ed
C
211 if (users.length === 0) return
212
f7cc67b4
C
213 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
214
215 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
216
8424c402 217 function settingGetter (user: MUserNotifSettingAccount) {
f7cc67b4
C
218 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
219
220 return user.NotificationSetting.commentMention
221 }
222
8424c402
C
223 async function notificationCreator (user: MUserNotifSettingAccount) {
224 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
f7cc67b4
C
225 type: UserNotificationType.COMMENT_MENTION,
226 userId: user.id,
227 commentId: comment.id
228 })
8424c402 229 notification.Comment = comment
f7cc67b4
C
230
231 return notification
232 }
233
234 function emailSender (emails: string[]) {
235 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
236 }
237
238 return this.notify({ users, settingGetter, notificationCreator, emailSender })
239 }
240
8424c402 241 private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) {
f7cc67b4
C
242 if (actorFollow.ActorFollowing.isOwned() === false) return
243
244 // Account follows one of our account?
245 let followType: 'account' | 'channel' = 'channel'
246 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
247
248 // Account follows one of our channel?
249 if (!user) {
250 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
251 followType = 'account'
252 }
253
254 if (!user) return
255
f7cc67b4
C
256 const followerAccount = actorFollow.ActorFollower.Account
257
258 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
259 if (accountMuted) return
260
261 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
262
8424c402 263 function settingGetter (user: MUserWithNotificationSetting) {
f7cc67b4
C
264 return user.NotificationSetting.newFollow
265 }
266
8424c402
C
267 async function notificationCreator (user: MUserWithNotificationSetting) {
268 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
f7cc67b4
C
269 type: UserNotificationType.NEW_FOLLOW,
270 userId: user.id,
271 actorFollowId: actorFollow.id
272 })
8424c402 273 notification.ActorFollow = actorFollow
f7cc67b4
C
274
275 return notification
276 }
277
278 function emailSender (emails: string[]) {
279 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
280 }
281
282 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
283 }
284
8424c402 285 private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) {
883993c8
C
286 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
287
288 logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url)
289
8424c402 290 function settingGetter (user: MUserWithNotificationSetting) {
883993c8
C
291 return user.NotificationSetting.newInstanceFollower
292 }
293
8424c402
C
294 async function notificationCreator (user: MUserWithNotificationSetting) {
295 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
883993c8
C
296 type: UserNotificationType.NEW_INSTANCE_FOLLOWER,
297 userId: user.id,
298 actorFollowId: actorFollow.id
299 })
8424c402 300 notification.ActorFollow = actorFollow
883993c8
C
301
302 return notification
303 }
304
305 function emailSender (emails: string[]) {
306 return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow)
307 }
308
309 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
310 }
311
8424c402
C
312 private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) {
313 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
314
315 logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url)
316
317 function settingGetter (user: MUserWithNotificationSetting) {
318 return user.NotificationSetting.autoInstanceFollowing
319 }
320
321 async function notificationCreator (user: MUserWithNotificationSetting) {
322 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
323 type: UserNotificationType.AUTO_INSTANCE_FOLLOWING,
324 userId: user.id,
325 actorFollowId: actorFollow.id
326 })
327 notification.ActorFollow = actorFollow
328
329 return notification
330 }
331
332 function emailSender (emails: string[]) {
333 return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow)
334 }
335
336 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
337 }
338
453e83ea 339 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) {
f7cc67b4
C
340 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
341 if (moderators.length === 0) return
342
343 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
cef534ed 344
8424c402 345 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
346 return user.NotificationSetting.videoAbuseAsModerator
347 }
348
8424c402
C
349 async function notificationCreator (user: MUserWithNotificationSetting) {
350 const notification: UserNotificationModelForApi = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
351 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
352 userId: user.id,
353 videoAbuseId: videoAbuse.id
354 })
355 notification.VideoAbuse = videoAbuse
356
357 return notification
358 }
359
360 function emailSender (emails: string[]) {
361 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
362 }
363
f7cc67b4 364 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
cef534ed
C
365 }
366
8424c402 367 private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) {
7ccddd7b
JM
368 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
369 if (moderators.length === 0) return
370
8424c402 371 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url)
7ccddd7b 372
8424c402 373 function settingGetter (user: MUserWithNotificationSetting) {
7ccddd7b
JM
374 return user.NotificationSetting.videoAutoBlacklistAsModerator
375 }
7ccddd7b 376
8424c402
C
377 async function notificationCreator (user: MUserWithNotificationSetting) {
378 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
7ccddd7b
JM
379 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
380 userId: user.id,
8424c402 381 videoBlacklistId: videoBlacklist.id
7ccddd7b 382 })
8424c402 383 notification.VideoBlacklist = videoBlacklist
7ccddd7b
JM
384
385 return notification
386 }
387
388 function emailSender (emails: string[]) {
8424c402 389 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist)
7ccddd7b
JM
390 }
391
392 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
393 }
394
453e83ea 395 private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) {
cef534ed
C
396 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
397 if (!user) return
398
399 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
400
8424c402 401 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
402 return user.NotificationSetting.blacklistOnMyVideo
403 }
404
8424c402
C
405 async function notificationCreator (user: MUserWithNotificationSetting) {
406 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
407 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
408 userId: user.id,
409 videoBlacklistId: videoBlacklist.id
410 })
8424c402 411 notification.VideoBlacklist = videoBlacklist
cef534ed
C
412
413 return notification
414 }
415
416 function emailSender (emails: string[]) {
417 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
418 }
419
420 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
421 }
422
8424c402 423 private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) {
cef534ed
C
424 const user = await UserModel.loadByVideoId(video.id)
425 if (!user) return
426
427 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
428
8424c402 429 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
430 return user.NotificationSetting.blacklistOnMyVideo
431 }
432
8424c402
C
433 async function notificationCreator (user: MUserWithNotificationSetting) {
434 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
435 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
436 userId: user.id,
437 videoId: video.id
438 })
8424c402 439 notification.Video = video
cef534ed
C
440
441 return notification
442 }
443
444 function emailSender (emails: string[]) {
445 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
446 }
447
448 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
449 }
450
453e83ea 451 private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) {
dc133480
C
452 const user = await UserModel.loadByVideoId(video.id)
453 if (!user) return
454
455 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
456
8424c402 457 function settingGetter (user: MUserWithNotificationSetting) {
dc133480
C
458 return user.NotificationSetting.myVideoPublished
459 }
460
8424c402
C
461 async function notificationCreator (user: MUserWithNotificationSetting) {
462 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
dc133480
C
463 type: UserNotificationType.MY_VIDEO_PUBLISHED,
464 userId: user.id,
465 videoId: video.id
466 })
8424c402 467 notification.Video = video
dc133480
C
468
469 return notification
470 }
471
472 function emailSender (emails: string[]) {
473 return Emailer.Instance.myVideoPublishedNotification(emails, video)
474 }
475
476 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
477 }
478
453e83ea 479 private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) {
dc133480
C
480 const user = await UserModel.loadByVideoImportId(videoImport.id)
481 if (!user) return
482
483 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
484
8424c402 485 function settingGetter (user: MUserWithNotificationSetting) {
dc133480
C
486 return user.NotificationSetting.myVideoImportFinished
487 }
488
8424c402
C
489 async function notificationCreator (user: MUserWithNotificationSetting) {
490 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
dc133480
C
491 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
492 userId: user.id,
493 videoImportId: videoImport.id
494 })
8424c402 495 notification.VideoImport = videoImport
dc133480
C
496
497 return notification
498 }
499
500 function emailSender (emails: string[]) {
501 return success
502 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
503 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
504 }
505
506 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
507 }
508
8424c402 509 private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) {
f7cc67b4
C
510 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
511 if (moderators.length === 0) return
512
513 logger.info(
514 'Notifying %s moderators of new user registration of %s.',
453e83ea 515 moderators.length, registeredUser.username
f7cc67b4
C
516 )
517
8424c402 518 function settingGetter (user: MUserWithNotificationSetting) {
f7cc67b4
C
519 return user.NotificationSetting.newUserRegistration
520 }
521
8424c402
C
522 async function notificationCreator (user: MUserWithNotificationSetting) {
523 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
f7cc67b4
C
524 type: UserNotificationType.NEW_USER_REGISTRATION,
525 userId: user.id,
526 accountId: registeredUser.Account.id
527 })
8424c402 528 notification.Account = registeredUser.Account
f7cc67b4
C
529
530 return notification
531 }
532
533 function emailSender (emails: string[]) {
534 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
535 }
536
537 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
538 }
539
8424c402
C
540 private async notify <T extends MUserWithNotificationSetting> (options: {
541 users: T[],
542 notificationCreator: (user: T) => Promise<UserNotificationModelForApi>,
cef534ed 543 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
8424c402 544 settingGetter: (user: T) => UserNotificationSettingValue
cef534ed
C
545 }) {
546 const emails: string[] = []
547
548 for (const user of options.users) {
549 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
550 const notification = await options.notificationCreator(user)
551
552 PeerTubeSocket.Instance.sendNotification(user.id, notification)
553 }
554
555 if (this.isEmailEnabled(user, options.settingGetter(user))) {
556 emails.push(user.email)
557 }
558 }
559
560 if (emails.length !== 0) {
561 await options.emailSender(emails)
562 }
563 }
564
453e83ea 565 private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) {
1ed9b8ee 566 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
cef534ed 567
2f1548fd 568 return value & UserNotificationSettingValue.EMAIL
cef534ed
C
569 }
570
571 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
2f1548fd 572 return value & UserNotificationSettingValue.WEB
cef534ed
C
573 }
574
575 static get Instance () {
576 return this.instance || (this.instance = new this())
577 }
578}
579
580// ---------------------------------------------------------------------------
581
582export {
583 Notifier
584}