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 extractedUsernames = comment.extractMentions()
152 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
153 { usernames: extractedUsernames, text: comment.text }
156 let users = await UserModel.listByUsernames(extractedUsernames)
158 if (comment.Video.isOwned()) {
159 const userException = await UserModel.loadByVideoId(comment.videoId)
160 users = users.filter(u => u.id !== userException.id)
163 // Don't notify if I mentioned myself
164 users = users.filter(u => u.Account.id !== comment.accountId)
166 if (users.length === 0) return
168 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
170 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
172 function settingGetter (user: UserModel) {
173 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
175 return user.NotificationSetting.commentMention
178 async function notificationCreator (user: UserModel) {
179 const notification = await UserNotificationModel.create({
180 type: UserNotificationType.COMMENT_MENTION,
182 commentId: comment.id
184 notification.Comment = comment
189 function emailSender (emails: string[]) {
190 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
193 return this.notify({ users, settingGetter, notificationCreator, emailSender })
196 private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) {
197 if (actorFollow.ActorFollowing.isOwned() === false) return
199 // Account follows one of our account?
200 let followType: 'account' | 'channel' = 'channel'
201 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
203 // Account follows one of our channel?
205 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
206 followType = 'account'
211 if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) {
212 actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel
214 const followerAccount = actorFollow.ActorFollower.Account
216 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
217 if (accountMuted) return
219 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
221 function settingGetter (user: UserModel) {
222 return user.NotificationSetting.newFollow
225 async function notificationCreator (user: UserModel) {
226 const notification = await UserNotificationModel.create({
227 type: UserNotificationType.NEW_FOLLOW,
229 actorFollowId: actorFollow.id
231 notification.ActorFollow = actorFollow
236 function emailSender (emails: string[]) {
237 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
240 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
243 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
244 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
245 if (moderators.length === 0) return
247 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
249 function settingGetter (user: UserModel) {
250 return user.NotificationSetting.videoAbuseAsModerator
253 async function notificationCreator (user: UserModel) {
254 const notification = await UserNotificationModel.create({
255 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
257 videoAbuseId: videoAbuse.id
259 notification.VideoAbuse = videoAbuse
264 function emailSender (emails: string[]) {
265 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
268 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
271 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
272 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
275 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
277 function settingGetter (user: UserModel) {
278 return user.NotificationSetting.blacklistOnMyVideo
281 async function notificationCreator (user: UserModel) {
282 const notification = await UserNotificationModel.create({
283 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
285 videoBlacklistId: videoBlacklist.id
287 notification.VideoBlacklist = videoBlacklist
292 function emailSender (emails: string[]) {
293 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
296 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
299 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) {
300 const user = await UserModel.loadByVideoId(video.id)
303 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
305 function settingGetter (user: UserModel) {
306 return user.NotificationSetting.blacklistOnMyVideo
309 async function notificationCreator (user: UserModel) {
310 const notification = await UserNotificationModel.create({
311 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
315 notification.Video = video
320 function emailSender (emails: string[]) {
321 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
324 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
327 private async notifyOwnedVideoHasBeenPublished (video: VideoModel) {
328 const user = await UserModel.loadByVideoId(video.id)
331 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
333 function settingGetter (user: UserModel) {
334 return user.NotificationSetting.myVideoPublished
337 async function notificationCreator (user: UserModel) {
338 const notification = await UserNotificationModel.create({
339 type: UserNotificationType.MY_VIDEO_PUBLISHED,
343 notification.Video = video
348 function emailSender (emails: string[]) {
349 return Emailer.Instance.myVideoPublishedNotification(emails, video)
352 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
355 private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) {
356 const user = await UserModel.loadByVideoImportId(videoImport.id)
359 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
361 function settingGetter (user: UserModel) {
362 return user.NotificationSetting.myVideoImportFinished
365 async function notificationCreator (user: UserModel) {
366 const notification = await UserNotificationModel.create({
367 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
369 videoImportId: videoImport.id
371 notification.VideoImport = videoImport
376 function emailSender (emails: string[]) {
378 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
379 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
382 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
385 private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) {
386 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
387 if (moderators.length === 0) return
390 'Notifying %s moderators of new user registration of %s.',
391 moderators.length, registeredUser.Account.Actor.preferredUsername
394 function settingGetter (user: UserModel) {
395 return user.NotificationSetting.newUserRegistration
398 async function notificationCreator (user: UserModel) {
399 const notification = await UserNotificationModel.create({
400 type: UserNotificationType.NEW_USER_REGISTRATION,
402 accountId: registeredUser.Account.id
404 notification.Account = registeredUser.Account
409 function emailSender (emails: string[]) {
410 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
413 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
416 private async notify (options: {
418 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
419 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
420 settingGetter: (user: UserModel) => UserNotificationSettingValue
422 const emails: string[] = []
424 for (const user of options.users) {
425 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
426 const notification = await options.notificationCreator(user)
428 PeerTubeSocket.Instance.sendNotification(user.id, notification)
431 if (this.isEmailEnabled(user, options.settingGetter(user))) {
432 emails.push(user.email)
436 if (emails.length !== 0) {
437 await options.emailSender(emails)
441 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
442 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified !== true) return false
444 return value & UserNotificationSettingValue.EMAIL
447 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
448 return value & UserNotificationSettingValue.WEB
451 static get Instance () {
452 return this.instance || (this.instance = new this())
456 // ---------------------------------------------------------------------------