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'
17 private static instance: Notifier
19 private constructor () {}
21 notifyOnNewVideo (video: VideoModel): void {
22 // Only notify on public and published videos
23 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED) return
25 this.notifySubscribersOfNewVideo(video)
26 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
29 notifyOnNewComment (comment: VideoCommentModel): void {
30 this.notifyVideoOwnerOfNewComment(comment)
31 .catch(err => logger.error('Cannot notify of new comment %s.', comment.url, { err }))
34 notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void {
35 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
36 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
39 notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void {
40 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
41 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
44 notifyOnVideoUnblacklist (video: VideoModel): void {
45 this.notifyVideoOwnerOfUnblacklist(video)
46 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', video.url, { err }))
49 private async notifySubscribersOfNewVideo (video: VideoModel) {
50 // List all followers that are users
51 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
53 logger.info('Notifying %d users of new video %s.', users.length, video.url)
55 function settingGetter (user: UserModel) {
56 return user.NotificationSetting.newVideoFromSubscription
59 async function notificationCreator (user: UserModel) {
60 const notification = await UserNotificationModel.create({
61 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
65 notification.Video = video
70 function emailSender (emails: string[]) {
71 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
74 return this.notify({ users, settingGetter, notificationCreator, emailSender })
77 private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) {
78 const user = await UserModel.loadByVideoId(comment.videoId)
80 // Not our user or user comments its own video
81 if (!user || comment.Account.userId === user.id) return
83 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
85 function settingGetter (user: UserModel) {
86 return user.NotificationSetting.newCommentOnMyVideo
89 async function notificationCreator (user: UserModel) {
90 const notification = await UserNotificationModel.create({
91 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
95 notification.Comment = comment
100 function emailSender (emails: string[]) {
101 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
104 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
107 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
108 const users = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
109 if (users.length === 0) return
111 logger.info('Notifying %s user/moderators of new video abuse %s.', users.length, videoAbuse.Video.url)
113 function settingGetter (user: UserModel) {
114 return user.NotificationSetting.videoAbuseAsModerator
117 async function notificationCreator (user: UserModel) {
118 const notification = await UserNotificationModel.create({
119 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
121 videoAbuseId: videoAbuse.id
123 notification.VideoAbuse = videoAbuse
128 function emailSender (emails: string[]) {
129 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
132 return this.notify({ users, settingGetter, notificationCreator, emailSender })
135 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
136 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
139 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
141 function settingGetter (user: UserModel) {
142 return user.NotificationSetting.blacklistOnMyVideo
145 async function notificationCreator (user: UserModel) {
146 const notification = await UserNotificationModel.create({
147 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
149 videoBlacklistId: videoBlacklist.id
151 notification.VideoBlacklist = videoBlacklist
156 function emailSender (emails: string[]) {
157 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
160 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
163 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) {
164 const user = await UserModel.loadByVideoId(video.id)
167 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
169 function settingGetter (user: UserModel) {
170 return user.NotificationSetting.blacklistOnMyVideo
173 async function notificationCreator (user: UserModel) {
174 const notification = await UserNotificationModel.create({
175 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
179 notification.Video = video
184 function emailSender (emails: string[]) {
185 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
188 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
191 private async notify (options: {
193 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
194 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
195 settingGetter: (user: UserModel) => UserNotificationSettingValue
197 const emails: string[] = []
199 for (const user of options.users) {
200 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
201 const notification = await options.notificationCreator(user)
203 PeerTubeSocket.Instance.sendNotification(user.id, notification)
206 if (this.isEmailEnabled(user, options.settingGetter(user))) {
207 emails.push(user.email)
211 if (emails.length !== 0) {
212 await options.emailSender(emails)
216 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
217 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified !== true) return false
219 return value === UserNotificationSettingValue.EMAIL || value === UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
222 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
223 return value === UserNotificationSettingValue.WEB_NOTIFICATION || value === UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
226 static get Instance () {
227 return this.instance || (this.instance = new this())
231 // ---------------------------------------------------------------------------