1 import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
2 import { logger } from '../helpers/logger'
3 import { Emailer } from './emailer'
4 import { UserNotificationModel } from '../models/account/user-notification'
5 import { UserModel } from '../models/account/user'
6 import { PeerTubeSocket } from './peertube-socket'
7 import { CONFIG } from '../initializers/config'
8 import { VideoPrivacy, VideoState } from '../../shared/models/videos'
9 import * as Bluebird from 'bluebird'
10 import { AccountBlocklistModel } from '../models/account/account-blocklist'
15 MVideoBlacklistLightVideo,
18 } from '../typings/models/video'
22 MUserNotifSettingAccount,
23 MUserWithNotificationSetting,
24 UserNotificationModelForApi
25 } from '@server/typings/models/user'
26 import { MActorFollowFull } from '../typings/models'
27 import { MVideoImportVideo } from '@server/typings/models/video/video-import'
31 private static instance: Notifier
33 private constructor () {}
35 notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void {
36 // Only notify on public and published videos which are not blacklisted
37 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return
39 this.notifySubscribersOfNewVideo(video)
40 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
43 notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
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
47 this.notifyOwnedVideoHasBeenPublished(video)
48 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
51 notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void {
52 // don't notify if video is still blacklisted or waiting for transcoding
53 if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
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 }))
59 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void {
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
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
67 notifyOnNewComment (comment: MCommentOwnerVideo): void {
68 this.notifyVideoOwnerOfNewComment(comment)
69 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
71 this.notifyOfCommentMention(comment)
72 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
75 notifyOnNewVideoAbuse (videoAbuse: MVideoAbuseVideo): void {
76 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
77 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
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 }))
85 notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
86 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
87 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
90 notifyOnVideoUnblacklist (video: MVideoFullLight): void {
91 this.notifyVideoOwnerOfUnblacklist(video)
92 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
95 notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void {
96 this.notifyOwnerVideoImportIsFinished(videoImport, success)
97 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
100 notifyOnNewUserRegistration (user: MUserDefault): void {
101 this.notifyModeratorsOfNewUserRegistration(user)
102 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
105 notifyOfNewUserFollow (actorFollow: MActorFollowFull): void {
106 this.notifyUserOfNewActorFollow(actorFollow)
109 'Cannot notify owner of channel %s of a new follow by %s.',
110 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
111 actorFollow.ActorFollower.Account.getDisplayName(),
117 notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
118 this.notifyAdminsOfNewInstanceFollow(actorFollow)
120 logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err })
124 notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void {
125 this.notifyAdminsOfAutoInstanceFollowing(actorFollow)
127 logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err })
131 private async notifySubscribersOfNewVideo (video: MVideoAccountLight) {
132 // List all followers that are users
133 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
135 logger.info('Notifying %d users of new video %s.', users.length, video.url)
137 function settingGetter (user: MUserWithNotificationSetting) {
138 return user.NotificationSetting.newVideoFromSubscription
141 async function notificationCreator (user: MUserWithNotificationSetting) {
142 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
143 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
147 notification.Video = video
152 function emailSender (emails: string[]) {
153 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
156 return this.notify({ users, settingGetter, notificationCreator, emailSender })
159 private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) {
160 if (comment.Video.isOwned() === false) return
162 const user = await UserModel.loadByVideoId(comment.videoId)
164 // Not our user or user comments its own video
165 if (!user || comment.Account.userId === user.id) return
167 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
168 if (accountMuted) return
170 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
172 function settingGetter (user: MUserWithNotificationSetting) {
173 return user.NotificationSetting.newCommentOnMyVideo
176 async function notificationCreator (user: MUserWithNotificationSetting) {
177 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
178 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
180 commentId: comment.id
182 notification.Comment = comment
187 function emailSender (emails: string[]) {
188 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
191 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
194 private async notifyOfCommentMention (comment: MCommentOwnerVideo) {
195 const extractedUsernames = comment.extractMentions()
197 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
198 { usernames: extractedUsernames, text: comment.text }
201 let users = await UserModel.listByUsernames(extractedUsernames)
203 if (comment.Video.isOwned()) {
204 const userException = await UserModel.loadByVideoId(comment.videoId)
205 users = users.filter(u => u.id !== userException.id)
208 // Don't notify if I mentioned myself
209 users = users.filter(u => u.Account.id !== comment.accountId)
211 if (users.length === 0) return
213 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
215 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
217 function settingGetter (user: MUserNotifSettingAccount) {
218 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
220 return user.NotificationSetting.commentMention
223 async function notificationCreator (user: MUserNotifSettingAccount) {
224 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
225 type: UserNotificationType.COMMENT_MENTION,
227 commentId: comment.id
229 notification.Comment = comment
234 function emailSender (emails: string[]) {
235 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
238 return this.notify({ users, settingGetter, notificationCreator, emailSender })
241 private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) {
242 if (actorFollow.ActorFollowing.isOwned() === false) return
244 // Account follows one of our account?
245 let followType: 'account' | 'channel' = 'channel'
246 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
248 // Account follows one of our channel?
250 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
251 followType = 'account'
256 const followerAccount = actorFollow.ActorFollower.Account
258 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
259 if (accountMuted) return
261 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
263 function settingGetter (user: MUserWithNotificationSetting) {
264 return user.NotificationSetting.newFollow
267 async function notificationCreator (user: MUserWithNotificationSetting) {
268 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
269 type: UserNotificationType.NEW_FOLLOW,
271 actorFollowId: actorFollow.id
273 notification.ActorFollow = actorFollow
278 function emailSender (emails: string[]) {
279 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
282 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
285 private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) {
286 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
288 logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url)
290 function settingGetter (user: MUserWithNotificationSetting) {
291 return user.NotificationSetting.newInstanceFollower
294 async function notificationCreator (user: MUserWithNotificationSetting) {
295 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
296 type: UserNotificationType.NEW_INSTANCE_FOLLOWER,
298 actorFollowId: actorFollow.id
300 notification.ActorFollow = actorFollow
305 function emailSender (emails: string[]) {
306 return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow)
309 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
312 private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) {
313 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
315 logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url)
317 function settingGetter (user: MUserWithNotificationSetting) {
318 return user.NotificationSetting.autoInstanceFollowing
321 async function notificationCreator (user: MUserWithNotificationSetting) {
322 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
323 type: UserNotificationType.AUTO_INSTANCE_FOLLOWING,
325 actorFollowId: actorFollow.id
327 notification.ActorFollow = actorFollow
332 function emailSender (emails: string[]) {
333 return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow)
336 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
339 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) {
340 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
341 if (moderators.length === 0) return
343 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
345 function settingGetter (user: MUserWithNotificationSetting) {
346 return user.NotificationSetting.videoAbuseAsModerator
349 async function notificationCreator (user: MUserWithNotificationSetting) {
350 const notification: UserNotificationModelForApi = await UserNotificationModel.create<UserNotificationModelForApi>({
351 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
353 videoAbuseId: videoAbuse.id
355 notification.VideoAbuse = videoAbuse
360 function emailSender (emails: string[]) {
361 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
364 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
367 private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) {
368 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
369 if (moderators.length === 0) return
371 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url)
373 function settingGetter (user: MUserWithNotificationSetting) {
374 return user.NotificationSetting.videoAutoBlacklistAsModerator
377 async function notificationCreator (user: MUserWithNotificationSetting) {
378 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
379 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
381 videoBlacklistId: videoBlacklist.id
383 notification.VideoBlacklist = videoBlacklist
388 function emailSender (emails: string[]) {
389 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist)
392 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
395 private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) {
396 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
399 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
401 function settingGetter (user: MUserWithNotificationSetting) {
402 return user.NotificationSetting.blacklistOnMyVideo
405 async function notificationCreator (user: MUserWithNotificationSetting) {
406 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
407 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
409 videoBlacklistId: videoBlacklist.id
411 notification.VideoBlacklist = videoBlacklist
416 function emailSender (emails: string[]) {
417 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
420 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
423 private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) {
424 const user = await UserModel.loadByVideoId(video.id)
427 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
429 function settingGetter (user: MUserWithNotificationSetting) {
430 return user.NotificationSetting.blacklistOnMyVideo
433 async function notificationCreator (user: MUserWithNotificationSetting) {
434 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
435 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
439 notification.Video = video
444 function emailSender (emails: string[]) {
445 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
448 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
451 private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) {
452 const user = await UserModel.loadByVideoId(video.id)
455 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
457 function settingGetter (user: MUserWithNotificationSetting) {
458 return user.NotificationSetting.myVideoPublished
461 async function notificationCreator (user: MUserWithNotificationSetting) {
462 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
463 type: UserNotificationType.MY_VIDEO_PUBLISHED,
467 notification.Video = video
472 function emailSender (emails: string[]) {
473 return Emailer.Instance.myVideoPublishedNotification(emails, video)
476 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
479 private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) {
480 const user = await UserModel.loadByVideoImportId(videoImport.id)
483 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
485 function settingGetter (user: MUserWithNotificationSetting) {
486 return user.NotificationSetting.myVideoImportFinished
489 async function notificationCreator (user: MUserWithNotificationSetting) {
490 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
491 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
493 videoImportId: videoImport.id
495 notification.VideoImport = videoImport
500 function emailSender (emails: string[]) {
502 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
503 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
506 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
509 private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) {
510 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
511 if (moderators.length === 0) return
514 'Notifying %s moderators of new user registration of %s.',
515 moderators.length, registeredUser.username
518 function settingGetter (user: MUserWithNotificationSetting) {
519 return user.NotificationSetting.newUserRegistration
522 async function notificationCreator (user: MUserWithNotificationSetting) {
523 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
524 type: UserNotificationType.NEW_USER_REGISTRATION,
526 accountId: registeredUser.Account.id
528 notification.Account = registeredUser.Account
533 function emailSender (emails: string[]) {
534 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
537 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
540 private async notify <T extends MUserWithNotificationSetting> (options: {
542 notificationCreator: (user: T) => Promise<UserNotificationModelForApi>,
543 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
544 settingGetter: (user: T) => UserNotificationSettingValue
546 const emails: string[] = []
548 for (const user of options.users) {
549 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
550 const notification = await options.notificationCreator(user)
552 PeerTubeSocket.Instance.sendNotification(user.id, notification)
555 if (this.isEmailEnabled(user, options.settingGetter(user))) {
556 emails.push(user.email)
560 if (emails.length !== 0) {
561 await options.emailSender(emails)
565 private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) {
566 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
568 return value & UserNotificationSettingValue.EMAIL
571 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
572 return value & UserNotificationSettingValue.WEB
575 static get Instance () {
576 return this.instance || (this.instance = new this())
580 // ---------------------------------------------------------------------------