1 import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
2 import { logger } from '../helpers/logger'
3 import { VideoModel } from '../models/video/video'
4 import { Emailer } from './emailer'
5 import { UserNotificationModel } from '../models/account/user-notification'
6 import { VideoCommentModel } from '../models/video/video-comment'
7 import { UserModel } from '../models/account/user'
8 import { PeerTubeSocket } from './peertube-socket'
9 import { CONFIG } from '../initializers/config'
10 import { VideoPrivacy, VideoState } from '../../shared/models/videos'
11 import { VideoAbuseModel } from '../models/video/video-abuse'
12 import { VideoBlacklistModel } from '../models/video/video-blacklist'
13 import * as Bluebird from 'bluebird'
14 import { VideoImportModel } from '../models/video/video-import'
15 import { AccountBlocklistModel } from '../models/account/account-blocklist'
16 import { ActorFollowModel } from '../models/activitypub/actor-follow'
17 import { AccountModel } from '../models/account/account'
21 private static instance: Notifier
23 private constructor () {}
25 notifyOnNewVideoIfNeeded (video: VideoModel): void {
26 // Only notify on public and published videos which are not blacklisted
27 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return
29 this.notifySubscribersOfNewVideo(video)
30 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
33 notifyOnVideoPublishedAfterTranscoding (video: VideoModel): void {
34 // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update
35 if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return
37 this.notifyOwnedVideoHasBeenPublished(video)
38 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
41 notifyOnVideoPublishedAfterScheduledUpdate (video: VideoModel): void {
42 // don't notify if video is still blacklisted or waiting for transcoding
43 if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
45 this.notifyOwnedVideoHasBeenPublished(video)
46 .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err }))
49 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: VideoModel): void {
50 // don't notify if video is still waiting for transcoding or scheduled update
51 if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
53 this.notifyOwnedVideoHasBeenPublished(video)
54 .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
57 notifyOnNewComment (comment: VideoCommentModel): void {
58 this.notifyVideoOwnerOfNewComment(comment)
59 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
61 this.notifyOfCommentMention(comment)
62 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
65 notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void {
66 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
67 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
70 notifyOnVideoAutoBlacklist (video: VideoModel): void {
71 this.notifyModeratorsOfVideoAutoBlacklist(video)
72 .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', video.url, { err }))
75 notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void {
76 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
77 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
80 notifyOnVideoUnblacklist (video: VideoModel): void {
81 this.notifyVideoOwnerOfUnblacklist(video)
82 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
85 notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void {
86 this.notifyOwnerVideoImportIsFinished(videoImport, success)
87 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
90 notifyOnNewUserRegistration (user: UserModel): void {
91 this.notifyModeratorsOfNewUserRegistration(user)
92 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
95 notifyOfNewUserFollow (actorFollow: ActorFollowModel): void {
96 this.notifyUserOfNewActorFollow(actorFollow)
99 'Cannot notify owner of channel %s of a new follow by %s.',
100 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
101 actorFollow.ActorFollower.Account.getDisplayName(),
107 notifyOfNewInstanceFollow (actorFollow: ActorFollowModel): void {
108 this.notifyAdminsOfNewInstanceFollow(actorFollow)
110 logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err })
114 private async notifySubscribersOfNewVideo (video: VideoModel) {
115 // List all followers that are users
116 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
118 logger.info('Notifying %d users of new video %s.', users.length, video.url)
120 function settingGetter (user: UserModel) {
121 return user.NotificationSetting.newVideoFromSubscription
124 async function notificationCreator (user: UserModel) {
125 const notification = await UserNotificationModel.create({
126 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
130 notification.Video = video
135 function emailSender (emails: string[]) {
136 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
139 return this.notify({ users, settingGetter, notificationCreator, emailSender })
142 private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) {
143 if (comment.Video.isOwned() === false) return
145 const user = await UserModel.loadByVideoId(comment.videoId)
147 // Not our user or user comments its own video
148 if (!user || comment.Account.userId === user.id) return
150 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
151 if (accountMuted) return
153 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
155 function settingGetter (user: UserModel) {
156 return user.NotificationSetting.newCommentOnMyVideo
159 async function notificationCreator (user: UserModel) {
160 const notification = await UserNotificationModel.create({
161 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
163 commentId: comment.id
165 notification.Comment = comment
170 function emailSender (emails: string[]) {
171 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
174 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
177 private async notifyOfCommentMention (comment: VideoCommentModel) {
178 const extractedUsernames = comment.extractMentions()
180 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
181 { usernames: extractedUsernames, text: comment.text }
184 let users = await UserModel.listByUsernames(extractedUsernames)
186 if (comment.Video.isOwned()) {
187 const userException = await UserModel.loadByVideoId(comment.videoId)
188 users = users.filter(u => u.id !== userException.id)
191 // Don't notify if I mentioned myself
192 users = users.filter(u => u.Account.id !== comment.accountId)
194 if (users.length === 0) return
196 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
198 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
200 function settingGetter (user: UserModel) {
201 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
203 return user.NotificationSetting.commentMention
206 async function notificationCreator (user: UserModel) {
207 const notification = await UserNotificationModel.create({
208 type: UserNotificationType.COMMENT_MENTION,
210 commentId: comment.id
212 notification.Comment = comment
217 function emailSender (emails: string[]) {
218 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
221 return this.notify({ users, settingGetter, notificationCreator, emailSender })
224 private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) {
225 if (actorFollow.ActorFollowing.isOwned() === false) return
227 // Account follows one of our account?
228 let followType: 'account' | 'channel' = 'channel'
229 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
231 // Account follows one of our channel?
233 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
234 followType = 'account'
239 if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) {
240 actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel
242 const followerAccount = actorFollow.ActorFollower.Account
244 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
245 if (accountMuted) return
247 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
249 function settingGetter (user: UserModel) {
250 return user.NotificationSetting.newFollow
253 async function notificationCreator (user: UserModel) {
254 const notification = await UserNotificationModel.create({
255 type: UserNotificationType.NEW_FOLLOW,
257 actorFollowId: actorFollow.id
259 notification.ActorFollow = actorFollow
264 function emailSender (emails: string[]) {
265 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
268 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
271 private async notifyAdminsOfNewInstanceFollow (actorFollow: ActorFollowModel) {
272 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
274 logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url)
276 function settingGetter (user: UserModel) {
277 return user.NotificationSetting.newInstanceFollower
280 async function notificationCreator (user: UserModel) {
281 const notification = await UserNotificationModel.create({
282 type: UserNotificationType.NEW_INSTANCE_FOLLOWER,
284 actorFollowId: actorFollow.id
286 notification.ActorFollow = actorFollow
291 function emailSender (emails: string[]) {
292 return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow)
295 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
298 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
299 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
300 if (moderators.length === 0) return
302 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
304 function settingGetter (user: UserModel) {
305 return user.NotificationSetting.videoAbuseAsModerator
308 async function notificationCreator (user: UserModel) {
309 const notification = await UserNotificationModel.create({
310 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
312 videoAbuseId: videoAbuse.id
314 notification.VideoAbuse = videoAbuse
319 function emailSender (emails: string[]) {
320 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
323 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
326 private async notifyModeratorsOfVideoAutoBlacklist (video: VideoModel) {
327 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
328 if (moderators.length === 0) return
330 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, video.url)
332 function settingGetter (user: UserModel) {
333 return user.NotificationSetting.videoAutoBlacklistAsModerator
335 async function notificationCreator (user: UserModel) {
337 const notification = await UserNotificationModel.create({
338 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
342 notification.Video = video
347 function emailSender (emails: string[]) {
348 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video)
351 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
354 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
355 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
358 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
360 function settingGetter (user: UserModel) {
361 return user.NotificationSetting.blacklistOnMyVideo
364 async function notificationCreator (user: UserModel) {
365 const notification = await UserNotificationModel.create({
366 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
368 videoBlacklistId: videoBlacklist.id
370 notification.VideoBlacklist = videoBlacklist
375 function emailSender (emails: string[]) {
376 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
379 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
382 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) {
383 const user = await UserModel.loadByVideoId(video.id)
386 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
388 function settingGetter (user: UserModel) {
389 return user.NotificationSetting.blacklistOnMyVideo
392 async function notificationCreator (user: UserModel) {
393 const notification = await UserNotificationModel.create({
394 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
398 notification.Video = video
403 function emailSender (emails: string[]) {
404 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
407 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
410 private async notifyOwnedVideoHasBeenPublished (video: VideoModel) {
411 const user = await UserModel.loadByVideoId(video.id)
414 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
416 function settingGetter (user: UserModel) {
417 return user.NotificationSetting.myVideoPublished
420 async function notificationCreator (user: UserModel) {
421 const notification = await UserNotificationModel.create({
422 type: UserNotificationType.MY_VIDEO_PUBLISHED,
426 notification.Video = video
431 function emailSender (emails: string[]) {
432 return Emailer.Instance.myVideoPublishedNotification(emails, video)
435 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
438 private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) {
439 const user = await UserModel.loadByVideoImportId(videoImport.id)
442 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
444 function settingGetter (user: UserModel) {
445 return user.NotificationSetting.myVideoImportFinished
448 async function notificationCreator (user: UserModel) {
449 const notification = await UserNotificationModel.create({
450 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
452 videoImportId: videoImport.id
454 notification.VideoImport = videoImport
459 function emailSender (emails: string[]) {
461 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
462 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
465 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
468 private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) {
469 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
470 if (moderators.length === 0) return
473 'Notifying %s moderators of new user registration of %s.',
474 moderators.length, registeredUser.Account.Actor.preferredUsername
477 function settingGetter (user: UserModel) {
478 return user.NotificationSetting.newUserRegistration
481 async function notificationCreator (user: UserModel) {
482 const notification = await UserNotificationModel.create({
483 type: UserNotificationType.NEW_USER_REGISTRATION,
485 accountId: registeredUser.Account.id
487 notification.Account = registeredUser.Account
492 function emailSender (emails: string[]) {
493 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
496 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
499 private async notify (options: {
501 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
502 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
503 settingGetter: (user: UserModel) => UserNotificationSettingValue
505 const emails: string[] = []
507 for (const user of options.users) {
508 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
509 const notification = await options.notificationCreator(user)
511 PeerTubeSocket.Instance.sendNotification(user.id, notification)
514 if (this.isEmailEnabled(user, options.settingGetter(user))) {
515 emails.push(user.email)
519 if (emails.length !== 0) {
520 await options.emailSender(emails)
524 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
525 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
527 return value & UserNotificationSettingValue.EMAIL
530 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
531 return value & UserNotificationSettingValue.WEB
534 static get Instance () {
535 return this.instance || (this.instance = new this())
539 // ---------------------------------------------------------------------------