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/constants'
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 notifyOnNewVideo (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.VideoBlacklist) 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 notifyOfNewFollow (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 private async notifySubscribersOfNewVideo (video: VideoModel) {
108 // List all followers that are users
109 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
111 logger.info('Notifying %d users of new video %s.', users.length, video.url)
113 function settingGetter (user: UserModel) {
114 return user.NotificationSetting.newVideoFromSubscription
117 async function notificationCreator (user: UserModel) {
118 const notification = await UserNotificationModel.create({
119 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
123 notification.Video = video
128 function emailSender (emails: string[]) {
129 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
132 return this.notify({ users, settingGetter, notificationCreator, emailSender })
135 private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) {
136 if (comment.Video.isOwned() === false) return
138 const user = await UserModel.loadByVideoId(comment.videoId)
140 // Not our user or user comments its own video
141 if (!user || comment.Account.userId === user.id) return
143 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
144 if (accountMuted) return
146 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
148 function settingGetter (user: UserModel) {
149 return user.NotificationSetting.newCommentOnMyVideo
152 async function notificationCreator (user: UserModel) {
153 const notification = await UserNotificationModel.create({
154 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
156 commentId: comment.id
158 notification.Comment = comment
163 function emailSender (emails: string[]) {
164 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
167 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
170 private async notifyOfCommentMention (comment: VideoCommentModel) {
171 const extractedUsernames = comment.extractMentions()
173 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
174 { usernames: extractedUsernames, text: comment.text }
177 let users = await UserModel.listByUsernames(extractedUsernames)
179 if (comment.Video.isOwned()) {
180 const userException = await UserModel.loadByVideoId(comment.videoId)
181 users = users.filter(u => u.id !== userException.id)
184 // Don't notify if I mentioned myself
185 users = users.filter(u => u.Account.id !== comment.accountId)
187 if (users.length === 0) return
189 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
191 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
193 function settingGetter (user: UserModel) {
194 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
196 return user.NotificationSetting.commentMention
199 async function notificationCreator (user: UserModel) {
200 const notification = await UserNotificationModel.create({
201 type: UserNotificationType.COMMENT_MENTION,
203 commentId: comment.id
205 notification.Comment = comment
210 function emailSender (emails: string[]) {
211 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
214 return this.notify({ users, settingGetter, notificationCreator, emailSender })
217 private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) {
218 if (actorFollow.ActorFollowing.isOwned() === false) return
220 // Account follows one of our account?
221 let followType: 'account' | 'channel' = 'channel'
222 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
224 // Account follows one of our channel?
226 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
227 followType = 'account'
232 if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) {
233 actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel
235 const followerAccount = actorFollow.ActorFollower.Account
237 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
238 if (accountMuted) return
240 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
242 function settingGetter (user: UserModel) {
243 return user.NotificationSetting.newFollow
246 async function notificationCreator (user: UserModel) {
247 const notification = await UserNotificationModel.create({
248 type: UserNotificationType.NEW_FOLLOW,
250 actorFollowId: actorFollow.id
252 notification.ActorFollow = actorFollow
257 function emailSender (emails: string[]) {
258 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
261 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
264 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
265 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
266 if (moderators.length === 0) return
268 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
270 function settingGetter (user: UserModel) {
271 return user.NotificationSetting.videoAbuseAsModerator
274 async function notificationCreator (user: UserModel) {
275 const notification = await UserNotificationModel.create({
276 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
278 videoAbuseId: videoAbuse.id
280 notification.VideoAbuse = videoAbuse
285 function emailSender (emails: string[]) {
286 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
289 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
292 private async notifyModeratorsOfVideoAutoBlacklist (video: VideoModel) {
293 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
294 if (moderators.length === 0) return
296 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, video.url)
298 function settingGetter (user: UserModel) {
299 return user.NotificationSetting.videoAutoBlacklistAsModerator
301 async function notificationCreator (user: UserModel) {
303 const notification = await UserNotificationModel.create({
304 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
308 notification.Video = video
313 function emailSender (emails: string[]) {
314 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video)
317 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
320 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
321 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
324 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
326 function settingGetter (user: UserModel) {
327 return user.NotificationSetting.blacklistOnMyVideo
330 async function notificationCreator (user: UserModel) {
331 const notification = await UserNotificationModel.create({
332 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
334 videoBlacklistId: videoBlacklist.id
336 notification.VideoBlacklist = videoBlacklist
341 function emailSender (emails: string[]) {
342 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
345 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
348 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) {
349 const user = await UserModel.loadByVideoId(video.id)
352 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
354 function settingGetter (user: UserModel) {
355 return user.NotificationSetting.blacklistOnMyVideo
358 async function notificationCreator (user: UserModel) {
359 const notification = await UserNotificationModel.create({
360 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
364 notification.Video = video
369 function emailSender (emails: string[]) {
370 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
373 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
376 private async notifyOwnedVideoHasBeenPublished (video: VideoModel) {
377 const user = await UserModel.loadByVideoId(video.id)
380 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
382 function settingGetter (user: UserModel) {
383 return user.NotificationSetting.myVideoPublished
386 async function notificationCreator (user: UserModel) {
387 const notification = await UserNotificationModel.create({
388 type: UserNotificationType.MY_VIDEO_PUBLISHED,
392 notification.Video = video
397 function emailSender (emails: string[]) {
398 return Emailer.Instance.myVideoPublishedNotification(emails, video)
401 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
404 private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) {
405 const user = await UserModel.loadByVideoImportId(videoImport.id)
408 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
410 function settingGetter (user: UserModel) {
411 return user.NotificationSetting.myVideoImportFinished
414 async function notificationCreator (user: UserModel) {
415 const notification = await UserNotificationModel.create({
416 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
418 videoImportId: videoImport.id
420 notification.VideoImport = videoImport
425 function emailSender (emails: string[]) {
427 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
428 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
431 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
434 private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) {
435 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
436 if (moderators.length === 0) return
439 'Notifying %s moderators of new user registration of %s.',
440 moderators.length, registeredUser.Account.Actor.preferredUsername
443 function settingGetter (user: UserModel) {
444 return user.NotificationSetting.newUserRegistration
447 async function notificationCreator (user: UserModel) {
448 const notification = await UserNotificationModel.create({
449 type: UserNotificationType.NEW_USER_REGISTRATION,
451 accountId: registeredUser.Account.id
453 notification.Account = registeredUser.Account
458 function emailSender (emails: string[]) {
459 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
462 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
465 private async notify (options: {
467 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
468 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
469 settingGetter: (user: UserModel) => UserNotificationSettingValue
471 const emails: string[] = []
473 for (const user of options.users) {
474 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
475 const notification = await options.notificationCreator(user)
477 PeerTubeSocket.Instance.sendNotification(user.id, notification)
480 if (this.isEmailEnabled(user, options.settingGetter(user))) {
481 emails.push(user.email)
485 if (emails.length !== 0) {
486 await options.emailSender(emails)
490 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
491 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
493 return value & UserNotificationSettingValue.EMAIL
496 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
497 return value & UserNotificationSettingValue.WEB
500 static get Instance () {
501 return this.instance || (this.instance = new this())
505 // ---------------------------------------------------------------------------