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
27 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED) return
29 this.notifySubscribersOfNewVideo(video)
30 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
33 notifyOnPendingVideoPublished (video: VideoModel): void {
34 // Only notify on public videos that has been published while the user waited transcoding/scheduled update
35 if (video.waitTranscoding === false && !video.ScheduleVideoUpdate) return
37 this.notifyOwnedVideoHasBeenPublished(video)
38 .catch(err => logger.error('Cannot notify owner that its video %s has been published.', video.url, { err }))
41 notifyOnNewComment (comment: VideoCommentModel): void {
42 this.notifyVideoOwnerOfNewComment(comment)
43 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
45 this.notifyOfCommentMention(comment)
46 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
49 notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void {
50 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
51 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
54 notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void {
55 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
56 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
59 notifyOnVideoUnblacklist (video: VideoModel): void {
60 this.notifyVideoOwnerOfUnblacklist(video)
61 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', video.url, { err }))
64 notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void {
65 this.notifyOwnerVideoImportIsFinished(videoImport, success)
66 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
69 notifyOnNewUserRegistration (user: UserModel): void {
70 this.notifyModeratorsOfNewUserRegistration(user)
71 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
74 notifyOfNewFollow (actorFollow: ActorFollowModel): void {
75 this.notifyUserOfNewActorFollow(actorFollow)
78 'Cannot notify owner of channel %s of a new follow by %s.',
79 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
80 actorFollow.ActorFollower.Account.getDisplayName(),
86 private async notifySubscribersOfNewVideo (video: VideoModel) {
87 // List all followers that are users
88 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
90 logger.info('Notifying %d users of new video %s.', users.length, video.url)
92 function settingGetter (user: UserModel) {
93 return user.NotificationSetting.newVideoFromSubscription
96 async function notificationCreator (user: UserModel) {
97 const notification = await UserNotificationModel.create({
98 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
102 notification.Video = video
107 function emailSender (emails: string[]) {
108 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
111 return this.notify({ users, settingGetter, notificationCreator, emailSender })
114 private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) {
115 if (comment.Video.isOwned() === false) return
117 const user = await UserModel.loadByVideoId(comment.videoId)
119 // Not our user or user comments its own video
120 if (!user || comment.Account.userId === user.id) return
122 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
123 if (accountMuted) return
125 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
127 function settingGetter (user: UserModel) {
128 return user.NotificationSetting.newCommentOnMyVideo
131 async function notificationCreator (user: UserModel) {
132 const notification = await UserNotificationModel.create({
133 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
135 commentId: comment.id
137 notification.Comment = comment
142 function emailSender (emails: string[]) {
143 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
146 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
149 private async notifyOfCommentMention (comment: VideoCommentModel) {
150 const usernames = comment.extractMentions()
151 logger.debug('Extracted %d username from comment %s.', usernames.length, comment.url, { usernames, text: comment.text })
153 let users = await UserModel.listByUsernames(usernames)
155 if (comment.Video.isOwned()) {
156 const userException = await UserModel.loadByVideoId(comment.videoId)
157 users = users.filter(u => u.id !== userException.id)
160 // Don't notify if I mentioned myself
161 users = users.filter(u => u.Account.id !== comment.accountId)
163 if (users.length === 0) return
165 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
167 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
169 function settingGetter (user: UserModel) {
170 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
172 return user.NotificationSetting.commentMention
175 async function notificationCreator (user: UserModel) {
176 const notification = await UserNotificationModel.create({
177 type: UserNotificationType.COMMENT_MENTION,
179 commentId: comment.id
181 notification.Comment = comment
186 function emailSender (emails: string[]) {
187 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
190 return this.notify({ users, settingGetter, notificationCreator, emailSender })
193 private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) {
194 if (actorFollow.ActorFollowing.isOwned() === false) return
196 // Account follows one of our account?
197 let followType: 'account' | 'channel' = 'channel'
198 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
200 // Account follows one of our channel?
202 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
203 followType = 'account'
208 if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) {
209 actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel
211 const followerAccount = actorFollow.ActorFollower.Account
213 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
214 if (accountMuted) return
216 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
218 function settingGetter (user: UserModel) {
219 return user.NotificationSetting.newFollow
222 async function notificationCreator (user: UserModel) {
223 const notification = await UserNotificationModel.create({
224 type: UserNotificationType.NEW_FOLLOW,
226 actorFollowId: actorFollow.id
228 notification.ActorFollow = actorFollow
233 function emailSender (emails: string[]) {
234 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
237 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
240 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
241 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
242 if (moderators.length === 0) return
244 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
246 function settingGetter (user: UserModel) {
247 return user.NotificationSetting.videoAbuseAsModerator
250 async function notificationCreator (user: UserModel) {
251 const notification = await UserNotificationModel.create({
252 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
254 videoAbuseId: videoAbuse.id
256 notification.VideoAbuse = videoAbuse
261 function emailSender (emails: string[]) {
262 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
265 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
268 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
269 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
272 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
274 function settingGetter (user: UserModel) {
275 return user.NotificationSetting.blacklistOnMyVideo
278 async function notificationCreator (user: UserModel) {
279 const notification = await UserNotificationModel.create({
280 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
282 videoBlacklistId: videoBlacklist.id
284 notification.VideoBlacklist = videoBlacklist
289 function emailSender (emails: string[]) {
290 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
293 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
296 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) {
297 const user = await UserModel.loadByVideoId(video.id)
300 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
302 function settingGetter (user: UserModel) {
303 return user.NotificationSetting.blacklistOnMyVideo
306 async function notificationCreator (user: UserModel) {
307 const notification = await UserNotificationModel.create({
308 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
312 notification.Video = video
317 function emailSender (emails: string[]) {
318 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
321 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
324 private async notifyOwnedVideoHasBeenPublished (video: VideoModel) {
325 const user = await UserModel.loadByVideoId(video.id)
328 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
330 function settingGetter (user: UserModel) {
331 return user.NotificationSetting.myVideoPublished
334 async function notificationCreator (user: UserModel) {
335 const notification = await UserNotificationModel.create({
336 type: UserNotificationType.MY_VIDEO_PUBLISHED,
340 notification.Video = video
345 function emailSender (emails: string[]) {
346 return Emailer.Instance.myVideoPublishedNotification(emails, video)
349 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
352 private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) {
353 const user = await UserModel.loadByVideoImportId(videoImport.id)
356 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
358 function settingGetter (user: UserModel) {
359 return user.NotificationSetting.myVideoImportFinished
362 async function notificationCreator (user: UserModel) {
363 const notification = await UserNotificationModel.create({
364 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
366 videoImportId: videoImport.id
368 notification.VideoImport = videoImport
373 function emailSender (emails: string[]) {
375 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
376 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
379 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
382 private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) {
383 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
384 if (moderators.length === 0) return
387 'Notifying %s moderators of new user registration of %s.',
388 moderators.length, registeredUser.Account.Actor.preferredUsername
391 function settingGetter (user: UserModel) {
392 return user.NotificationSetting.newUserRegistration
395 async function notificationCreator (user: UserModel) {
396 const notification = await UserNotificationModel.create({
397 type: UserNotificationType.NEW_USER_REGISTRATION,
399 accountId: registeredUser.Account.id
401 notification.Account = registeredUser.Account
406 function emailSender (emails: string[]) {
407 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
410 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
413 private async notify (options: {
415 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
416 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
417 settingGetter: (user: UserModel) => UserNotificationSettingValue
419 const emails: string[] = []
421 for (const user of options.users) {
422 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
423 const notification = await options.notificationCreator(user)
425 PeerTubeSocket.Instance.sendNotification(user.id, notification)
428 if (this.isEmailEnabled(user, options.settingGetter(user))) {
429 emails.push(user.email)
433 if (emails.length !== 0) {
434 await options.emailSender(emails)
438 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
439 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified !== true) return false
441 return value & UserNotificationSettingValue.EMAIL
444 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
445 return value & UserNotificationSettingValue.WEB
448 static get Instance () {
449 return this.instance || (this.instance = new this())
453 // ---------------------------------------------------------------------------