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'
19 private static instance: Notifier
21 private constructor () {}
23 notifyOnNewVideo (video: VideoModel): void {
24 // Only notify on public and published videos
25 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED) return
27 this.notifySubscribersOfNewVideo(video)
28 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
31 notifyOnPendingVideoPublished (video: VideoModel): void {
32 // Only notify on public videos that has been published while the user waited transcoding/scheduled update
33 if (video.waitTranscoding === false && !video.ScheduleVideoUpdate) return
35 this.notifyOwnedVideoHasBeenPublished(video)
36 .catch(err => logger.error('Cannot notify owner that its video %s has been published.', video.url, { err }))
39 notifyOnNewComment (comment: VideoCommentModel): void {
40 this.notifyVideoOwnerOfNewComment(comment)
41 .catch(err => logger.error('Cannot notify of new comment %s.', comment.url, { err }))
44 notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void {
45 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
46 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
49 notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void {
50 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
51 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
54 notifyOnVideoUnblacklist (video: VideoModel): void {
55 this.notifyVideoOwnerOfUnblacklist(video)
56 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', video.url, { err }))
59 notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void {
60 this.notifyOwnerVideoImportIsFinished(videoImport, success)
61 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
64 private async notifySubscribersOfNewVideo (video: VideoModel) {
65 // List all followers that are users
66 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
68 logger.info('Notifying %d users of new video %s.', users.length, video.url)
70 function settingGetter (user: UserModel) {
71 return user.NotificationSetting.newVideoFromSubscription
74 async function notificationCreator (user: UserModel) {
75 const notification = await UserNotificationModel.create({
76 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
80 notification.Video = video
85 function emailSender (emails: string[]) {
86 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
89 return this.notify({ users, settingGetter, notificationCreator, emailSender })
92 private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) {
93 const user = await UserModel.loadByVideoId(comment.videoId)
95 // Not our user or user comments its own video
96 if (!user || comment.Account.userId === user.id) return
98 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
99 if (accountMuted) return
101 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
103 function settingGetter (user: UserModel) {
104 return user.NotificationSetting.newCommentOnMyVideo
107 async function notificationCreator (user: UserModel) {
108 const notification = await UserNotificationModel.create({
109 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
111 commentId: comment.id
113 notification.Comment = comment
118 function emailSender (emails: string[]) {
119 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
122 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
125 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
126 const users = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
127 if (users.length === 0) return
129 logger.info('Notifying %s user/moderators of new video abuse %s.', users.length, videoAbuse.Video.url)
131 function settingGetter (user: UserModel) {
132 return user.NotificationSetting.videoAbuseAsModerator
135 async function notificationCreator (user: UserModel) {
136 const notification = await UserNotificationModel.create({
137 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
139 videoAbuseId: videoAbuse.id
141 notification.VideoAbuse = videoAbuse
146 function emailSender (emails: string[]) {
147 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
150 return this.notify({ users, settingGetter, notificationCreator, emailSender })
153 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
154 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
157 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
159 function settingGetter (user: UserModel) {
160 return user.NotificationSetting.blacklistOnMyVideo
163 async function notificationCreator (user: UserModel) {
164 const notification = await UserNotificationModel.create({
165 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
167 videoBlacklistId: videoBlacklist.id
169 notification.VideoBlacklist = videoBlacklist
174 function emailSender (emails: string[]) {
175 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
178 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
181 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) {
182 const user = await UserModel.loadByVideoId(video.id)
185 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
187 function settingGetter (user: UserModel) {
188 return user.NotificationSetting.blacklistOnMyVideo
191 async function notificationCreator (user: UserModel) {
192 const notification = await UserNotificationModel.create({
193 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
197 notification.Video = video
202 function emailSender (emails: string[]) {
203 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
206 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
209 private async notifyOwnedVideoHasBeenPublished (video: VideoModel) {
210 const user = await UserModel.loadByVideoId(video.id)
213 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
215 function settingGetter (user: UserModel) {
216 return user.NotificationSetting.myVideoPublished
219 async function notificationCreator (user: UserModel) {
220 const notification = await UserNotificationModel.create({
221 type: UserNotificationType.MY_VIDEO_PUBLISHED,
225 notification.Video = video
230 function emailSender (emails: string[]) {
231 return Emailer.Instance.myVideoPublishedNotification(emails, video)
234 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
237 private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) {
238 const user = await UserModel.loadByVideoImportId(videoImport.id)
241 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
243 function settingGetter (user: UserModel) {
244 return user.NotificationSetting.myVideoImportFinished
247 async function notificationCreator (user: UserModel) {
248 const notification = await UserNotificationModel.create({
249 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
251 videoImportId: videoImport.id
253 notification.VideoImport = videoImport
258 function emailSender (emails: string[]) {
260 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
261 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
264 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
267 private async notify (options: {
269 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
270 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
271 settingGetter: (user: UserModel) => UserNotificationSettingValue
273 const emails: string[] = []
275 for (const user of options.users) {
276 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
277 const notification = await options.notificationCreator(user)
279 PeerTubeSocket.Instance.sendNotification(user.id, notification)
282 if (this.isEmailEnabled(user, options.settingGetter(user))) {
283 emails.push(user.email)
287 if (emails.length !== 0) {
288 await options.emailSender(emails)
292 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
293 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified !== true) return false
295 return value === UserNotificationSettingValue.EMAIL || value === UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
298 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
299 return value === UserNotificationSettingValue.WEB_NOTIFICATION || value === UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
302 static get Instance () {
303 return this.instance || (this.instance = new this())
307 // ---------------------------------------------------------------------------