1 import { MUser, MUserDefault } from '@server/types/models/user'
2 import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist'
3 import { UserNotificationSettingValue } from '../../../shared/models/users'
4 import { logger } from '../../helpers/logger'
5 import { CONFIG } from '../../initializers/config'
6 import { MAbuseFull, MAbuseMessage, MActorFollowFull, MApplication, MPlugin } from '../../types/models'
7 import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../../types/models/video'
8 import { JobQueue } from '../job-queue'
9 import { PeerTubeSocket } from '../peertube-socket'
10 import { Hooks } from '../plugins/hooks'
13 AbuseStateChangeForReporter,
14 AutoFollowForInstance,
18 ImportFinishedForOwner,
19 ImportFinishedForOwnerPayload,
20 NewAbuseForModerators,
21 NewAbuseMessageForModerators,
22 NewAbuseMessageForReporter,
24 NewAutoBlacklistForModerators,
26 NewCommentForVideoOwner,
27 NewPeerTubeVersionForAdmins,
28 NewPluginVersionForAdmins,
29 NewVideoForSubscribers,
30 OwnedPublicationAfterAutoUnblacklist,
31 OwnedPublicationAfterScheduleUpdate,
32 OwnedPublicationAfterTranscoding,
33 RegistrationForModerators,
34 StudioEditionFinishedForOwner,
40 private readonly notificationModels = {
41 newVideo: [ NewVideoForSubscribers ],
42 publicationAfterTranscoding: [ OwnedPublicationAfterTranscoding ],
43 publicationAfterScheduleUpdate: [ OwnedPublicationAfterScheduleUpdate ],
44 publicationAfterAutoUnblacklist: [ OwnedPublicationAfterAutoUnblacklist ],
45 newComment: [ CommentMention, NewCommentForVideoOwner ],
46 newAbuse: [ NewAbuseForModerators ],
47 newBlacklist: [ NewBlacklistForOwner ],
48 unblacklist: [ UnblacklistForOwner ],
49 importFinished: [ ImportFinishedForOwner ],
50 userRegistration: [ RegistrationForModerators ],
51 userFollow: [ FollowForUser ],
52 instanceFollow: [ FollowForInstance ],
53 autoInstanceFollow: [ AutoFollowForInstance ],
54 newAutoBlacklist: [ NewAutoBlacklistForModerators ],
55 abuseStateChange: [ AbuseStateChangeForReporter ],
56 newAbuseMessage: [ NewAbuseMessageForReporter, NewAbuseMessageForModerators ],
57 newPeertubeVersion: [ NewPeerTubeVersionForAdmins ],
58 newPluginVersion: [ NewPluginVersionForAdmins ],
59 videoStudioEditionFinished: [ StudioEditionFinishedForOwner ]
62 private static instance: Notifier
64 private constructor () {
67 notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void {
68 const models = this.notificationModels.newVideo
70 this.sendNotifications(models, video)
71 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
74 notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
75 const models = this.notificationModels.publicationAfterTranscoding
77 this.sendNotifications(models, video)
78 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
81 notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void {
82 const models = this.notificationModels.publicationAfterScheduleUpdate
84 this.sendNotifications(models, video)
85 .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err }))
88 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void {
89 const models = this.notificationModels.publicationAfterAutoUnblacklist
91 this.sendNotifications(models, video)
93 logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })
97 notifyOnNewComment (comment: MCommentOwnerVideo): void {
98 const models = this.notificationModels.newComment
100 this.sendNotifications(models, comment)
101 .catch(err => logger.error('Cannot notify of new comment.', comment.url, { err }))
104 notifyOnNewAbuse (payload: NewAbusePayload): void {
105 const models = this.notificationModels.newAbuse
107 this.sendNotifications(models, payload)
108 .catch(err => logger.error('Cannot notify of new abuse %d.', payload.abuseInstance.id, { err }))
111 notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
112 const models = this.notificationModels.newAutoBlacklist
114 this.sendNotifications(models, videoBlacklist)
115 .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
118 notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
119 const models = this.notificationModels.newBlacklist
121 this.sendNotifications(models, videoBlacklist)
122 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
125 notifyOnVideoUnblacklist (video: MVideoFullLight): void {
126 const models = this.notificationModels.unblacklist
128 this.sendNotifications(models, video)
129 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
132 notifyOnFinishedVideoImport (payload: ImportFinishedForOwnerPayload): void {
133 const models = this.notificationModels.importFinished
135 this.sendNotifications(models, payload)
137 logger.error('Cannot notify owner that its video import %s is finished.', payload.videoImport.getTargetIdentifier(), { err })
141 notifyOnNewUserRegistration (user: MUserDefault): void {
142 const models = this.notificationModels.userRegistration
144 this.sendNotifications(models, user)
145 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
148 notifyOfNewUserFollow (actorFollow: MActorFollowFull): void {
149 const models = this.notificationModels.userFollow
151 this.sendNotifications(models, actorFollow)
154 'Cannot notify owner of channel %s of a new follow by %s.',
155 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
156 actorFollow.ActorFollower.Account.getDisplayName(),
162 notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
163 const models = this.notificationModels.instanceFollow
165 this.sendNotifications(models, actorFollow)
166 .catch(err => logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }))
169 notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void {
170 const models = this.notificationModels.autoInstanceFollow
172 this.sendNotifications(models, actorFollow)
173 .catch(err => logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err }))
176 notifyOnAbuseStateChange (abuse: MAbuseFull): void {
177 const models = this.notificationModels.abuseStateChange
179 this.sendNotifications(models, abuse)
180 .catch(err => logger.error('Cannot notify of abuse %d state change.', abuse.id, { err }))
183 notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void {
184 const models = this.notificationModels.newAbuseMessage
186 this.sendNotifications(models, { abuse, message })
187 .catch(err => logger.error('Cannot notify on new abuse %d message.', abuse.id, { err }))
190 notifyOfNewPeerTubeVersion (application: MApplication, latestVersion: string) {
191 const models = this.notificationModels.newPeertubeVersion
193 this.sendNotifications(models, { application, latestVersion })
194 .catch(err => logger.error('Cannot notify on new PeerTubeb version %s.', latestVersion, { err }))
197 notifyOfNewPluginVersion (plugin: MPlugin) {
198 const models = this.notificationModels.newPluginVersion
200 this.sendNotifications(models, plugin)
201 .catch(err => logger.error('Cannot notify on new plugin version %s.', plugin.name, { err }))
204 notifyOfFinishedVideoStudioEdition (video: MVideoFullLight) {
205 const models = this.notificationModels.videoStudioEditionFinished
207 this.sendNotifications(models, video)
208 .catch(err => logger.error('Cannot notify on finished studio edition %s.', video.url, { err }))
211 private async notify <T> (object: AbstractNotification<T>) {
212 await object.prepare()
214 const users = object.getTargetUsers()
216 if (users.length === 0) return
217 if (await object.isDisabled()) return
221 const toEmails: string[] = []
223 for (const user of users) {
224 const setting = object.getSetting(user)
226 const webNotificationEnabled = this.isWebNotificationEnabled(setting)
227 const emailNotificationEnabled = this.isEmailEnabled(user, setting)
228 const notification = object.createNotification(user)
230 if (webNotificationEnabled) {
231 await notification.save()
233 PeerTubeSocket.Instance.sendNotification(user.id, notification)
236 if (emailNotificationEnabled) {
237 toEmails.push(user.email)
240 Hooks.runAction('action:notifier.notification.created', { webNotificationEnabled, emailNotificationEnabled, user, notification })
243 for (const to of toEmails) {
244 const payload = await object.createEmail(to)
245 JobQueue.Instance.createJobAsync({ type: 'email', payload })
249 private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) {
250 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
252 return value & UserNotificationSettingValue.EMAIL
255 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
256 return value & UserNotificationSettingValue.WEB
259 private async sendNotifications <T> (models: (new (payload: T) => AbstractNotification<T>)[], payload: T) {
260 for (const model of models) {
261 // eslint-disable-next-line new-cap
262 await this.notify(new model(payload))
266 static get Instance () {
267 return this.instance || (this.instance = new this())
271 // ---------------------------------------------------------------------------