1 import { AccountModel } from '@server/models/account/account'
2 import { getServerActor } from '@server/models/application/application'
3 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
8 MUserNotifSettingAccount,
9 MUserWithNotificationSetting,
10 UserNotificationModelForApi
11 } from '@server/types/models/user'
12 import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist'
13 import { MVideoImportVideo } from '@server/types/models/video/video-import'
14 import { UserAbuse } from '@shared/models'
15 import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
16 import { VideoPrivacy, VideoState } from '../../shared/models/videos'
17 import { logger } from '../helpers/logger'
18 import { CONFIG } from '../initializers/config'
19 import { AccountBlocklistModel } from '../models/account/account-blocklist'
20 import { UserModel } from '../models/user/user'
21 import { UserNotificationModel } from '../models/user/user-notification'
22 import { MAbuseFull, MAbuseMessage, MAccountServer, MActorFollowFull, MApplication, MPlugin } from '../types/models'
23 import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../types/models/video'
24 import { isBlockedByServerOrAccount } from './blocklist'
25 import { Emailer } from './emailer'
26 import { PeerTubeSocket } from './peertube-socket'
30 private static instance: Notifier
32 private constructor () {
35 notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void {
36 // Only notify on public and published videos which are not blacklisted
37 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return
39 this.notifySubscribersOfNewVideo(video)
40 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
43 notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
44 // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update
45 if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return
47 this.notifyOwnedVideoHasBeenPublished(video)
48 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
51 notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void {
52 // don't notify if video is still blacklisted or waiting for transcoding
53 if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
55 this.notifyOwnedVideoHasBeenPublished(video)
56 .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err }))
59 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void {
60 // don't notify if video is still waiting for transcoding or scheduled update
61 if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
63 this.notifyOwnedVideoHasBeenPublished(video)
65 logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })
69 notifyOnNewComment (comment: MCommentOwnerVideo): void {
70 this.notifyVideoOwnerOfNewComment(comment)
71 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
73 this.notifyOfCommentMention(comment)
74 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
77 notifyOnNewAbuse (parameters: { abuse: UserAbuse, abuseInstance: MAbuseFull, reporter: string }): void {
78 this.notifyModeratorsOfNewAbuse(parameters)
79 .catch(err => logger.error('Cannot notify of new abuse %d.', parameters.abuseInstance.id, { err }))
82 notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
83 this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist)
84 .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
87 notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
88 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
89 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
92 notifyOnVideoUnblacklist (video: MVideoFullLight): void {
93 this.notifyVideoOwnerOfUnblacklist(video)
94 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
97 notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void {
98 this.notifyOwnerVideoImportIsFinished(videoImport, success)
99 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
102 notifyOnNewUserRegistration (user: MUserDefault): void {
103 this.notifyModeratorsOfNewUserRegistration(user)
104 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
107 notifyOfNewUserFollow (actorFollow: MActorFollowFull): void {
108 this.notifyUserOfNewActorFollow(actorFollow)
111 'Cannot notify owner of channel %s of a new follow by %s.',
112 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
113 actorFollow.ActorFollower.Account.getDisplayName(),
119 notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
120 this.notifyAdminsOfNewInstanceFollow(actorFollow)
122 logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err })
126 notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void {
127 this.notifyAdminsOfAutoInstanceFollowing(actorFollow)
129 logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err })
133 notifyOnAbuseStateChange (abuse: MAbuseFull): void {
134 this.notifyReporterOfAbuseStateChange(abuse)
136 logger.error('Cannot notify reporter of abuse %d state change.', abuse.id, { err })
140 notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void {
141 this.notifyOfNewAbuseMessage(abuse, message)
143 logger.error('Cannot notify on new abuse %d message.', abuse.id, { err })
147 notifyOfNewPeerTubeVersion (application: MApplication, latestVersion: string) {
148 this.notifyAdminsOfNewPeerTubeVersion(application, latestVersion)
150 logger.error('Cannot notify on new PeerTubeb version %s.', latestVersion, { err })
154 notifyOfNewPluginVersion (plugin: MPlugin) {
155 this.notifyAdminsOfNewPluginVersion(plugin)
157 logger.error('Cannot notify on new plugin version %s.', plugin.name, { err })
161 private async notifySubscribersOfNewVideo (video: MVideoAccountLight) {
162 // List all followers that are users
163 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
165 logger.info('Notifying %d users of new video %s.', users.length, video.url)
167 function settingGetter (user: MUserWithNotificationSetting) {
168 return user.NotificationSetting.newVideoFromSubscription
171 async function notificationCreator (user: MUserWithNotificationSetting) {
172 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
173 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
177 notification.Video = video
182 function emailSender (emails: string[]) {
183 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
186 return this.notify({ users, settingGetter, notificationCreator, emailSender })
189 private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) {
190 if (comment.Video.isOwned() === false) return
192 const user = await UserModel.loadByVideoId(comment.videoId)
194 // Not our user or user comments its own video
195 if (!user || comment.Account.userId === user.id) return
197 if (await this.isBlockedByServerOrUser(comment.Account, user)) return
199 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
201 function settingGetter (user: MUserWithNotificationSetting) {
202 return user.NotificationSetting.newCommentOnMyVideo
205 async function notificationCreator (user: MUserWithNotificationSetting) {
206 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
207 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
209 commentId: comment.id
211 notification.Comment = comment
216 function emailSender (emails: string[]) {
217 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
220 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
223 private async notifyOfCommentMention (comment: MCommentOwnerVideo) {
224 const extractedUsernames = comment.extractMentions()
226 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
227 { usernames: extractedUsernames, text: comment.text }
230 let users = await UserModel.listByUsernames(extractedUsernames)
232 if (comment.Video.isOwned()) {
233 const userException = await UserModel.loadByVideoId(comment.videoId)
234 users = users.filter(u => u.id !== userException.id)
237 // Don't notify if I mentioned myself
238 users = users.filter(u => u.Account.id !== comment.accountId)
240 if (users.length === 0) return
242 const serverAccountId = (await getServerActor()).Account.id
243 const sourceAccounts = users.map(u => u.Account.id).concat([ serverAccountId ])
245 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, comment.accountId)
246 const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, comment.Account.Actor.serverId)
248 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
250 function settingGetter (user: MUserNotifSettingAccount) {
251 const accountId = user.Account.id
253 accountMutedHash[accountId] === true || instanceMutedHash[accountId] === true ||
254 accountMutedHash[serverAccountId] === true || instanceMutedHash[serverAccountId] === true
256 return UserNotificationSettingValue.NONE
259 return user.NotificationSetting.commentMention
262 async function notificationCreator (user: MUserNotifSettingAccount) {
263 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
264 type: UserNotificationType.COMMENT_MENTION,
266 commentId: comment.id
268 notification.Comment = comment
273 function emailSender (emails: string[]) {
274 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
277 return this.notify({ users, settingGetter, notificationCreator, emailSender })
280 private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) {
281 if (actorFollow.ActorFollowing.isOwned() === false) return
283 // Account follows one of our account?
284 let followType: 'account' | 'channel' = 'channel'
285 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
287 // Account follows one of our channel?
289 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
290 followType = 'account'
295 const followerAccount = actorFollow.ActorFollower.Account
296 const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower })
298 if (await this.isBlockedByServerOrUser(followerAccountWithActor, user)) return
300 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
302 function settingGetter (user: MUserWithNotificationSetting) {
303 return user.NotificationSetting.newFollow
306 async function notificationCreator (user: MUserWithNotificationSetting) {
307 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
308 type: UserNotificationType.NEW_FOLLOW,
310 actorFollowId: actorFollow.id
312 notification.ActorFollow = actorFollow
317 function emailSender (emails: string[]) {
318 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
321 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
324 private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) {
325 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
327 const follower = Object.assign(actorFollow.ActorFollower.Account, { Actor: actorFollow.ActorFollower })
328 if (await this.isBlockedByServerOrUser(follower)) return
330 logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url)
332 function settingGetter (user: MUserWithNotificationSetting) {
333 return user.NotificationSetting.newInstanceFollower
336 async function notificationCreator (user: MUserWithNotificationSetting) {
337 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
338 type: UserNotificationType.NEW_INSTANCE_FOLLOWER,
340 actorFollowId: actorFollow.id
342 notification.ActorFollow = actorFollow
347 function emailSender (emails: string[]) {
348 return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow)
351 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
354 private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) {
355 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
357 logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url)
359 function settingGetter (user: MUserWithNotificationSetting) {
360 return user.NotificationSetting.autoInstanceFollowing
363 async function notificationCreator (user: MUserWithNotificationSetting) {
364 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
365 type: UserNotificationType.AUTO_INSTANCE_FOLLOWING,
367 actorFollowId: actorFollow.id
369 notification.ActorFollow = actorFollow
374 function emailSender (emails: string[]) {
375 return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow)
378 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
381 private async notifyModeratorsOfNewAbuse (parameters: {
383 abuseInstance: MAbuseFull
386 const { abuse, abuseInstance } = parameters
388 const moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
389 if (moderators.length === 0) return
391 const url = this.getAbuseUrl(abuseInstance)
393 logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url)
395 function settingGetter (user: MUserWithNotificationSetting) {
396 return user.NotificationSetting.abuseAsModerator
399 async function notificationCreator (user: MUserWithNotificationSetting) {
400 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
401 type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS,
405 notification.Abuse = abuseInstance
410 function emailSender (emails: string[]) {
411 return Emailer.Instance.addAbuseModeratorsNotification(emails, parameters)
414 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
417 private async notifyReporterOfAbuseStateChange (abuse: MAbuseFull) {
418 // Only notify our users
419 if (abuse.ReporterAccount.isOwned() !== true) return
421 const url = this.getAbuseUrl(abuse)
423 logger.info('Notifying reporter of abuse % of state change.', url)
425 const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId)
427 function settingGetter (user: MUserWithNotificationSetting) {
428 return user.NotificationSetting.abuseStateChange
431 async function notificationCreator (user: MUserWithNotificationSetting) {
432 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
433 type: UserNotificationType.ABUSE_STATE_CHANGE,
437 notification.Abuse = abuse
442 function emailSender (emails: string[]) {
443 return Emailer.Instance.addAbuseStateChangeNotification(emails, abuse)
446 return this.notify({ users: [ reporter ], settingGetter, notificationCreator, emailSender })
449 private async notifyOfNewAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage) {
450 const url = this.getAbuseUrl(abuse)
451 logger.info('Notifying reporter and moderators of new abuse message on %s.', url)
453 const accountMessage = await AccountModel.load(message.accountId)
455 function settingGetter (user: MUserWithNotificationSetting) {
456 return user.NotificationSetting.abuseNewMessage
459 async function notificationCreator (user: MUserWithNotificationSetting) {
460 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
461 type: UserNotificationType.ABUSE_NEW_MESSAGE,
465 notification.Abuse = abuse
470 function emailSenderReporter (emails: string[]) {
471 return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message, accountMessage })
474 function emailSenderModerators (emails: string[]) {
475 return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message, accountMessage })
478 async function buildReporterOptions () {
479 // Only notify our users
480 if (abuse.ReporterAccount.isOwned() !== true) return undefined
482 const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId)
483 // Don't notify my own message
484 if (reporter.Account.id === message.accountId) return undefined
486 return { users: [ reporter ], settingGetter, notificationCreator, emailSender: emailSenderReporter }
489 async function buildModeratorsOptions () {
490 let moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
491 // Don't notify my own message
492 moderators = moderators.filter(m => m.Account.id !== message.accountId)
494 if (moderators.length === 0) return undefined
496 return { users: moderators, settingGetter, notificationCreator, emailSender: emailSenderModerators }
499 const options = await Promise.all([
500 buildReporterOptions(),
501 buildModeratorsOptions()
506 .filter(opt => !!opt)
507 .map(opt => this.notify(opt))
511 private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) {
512 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
513 if (moderators.length === 0) return
515 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url)
517 function settingGetter (user: MUserWithNotificationSetting) {
518 return user.NotificationSetting.videoAutoBlacklistAsModerator
521 async function notificationCreator (user: MUserWithNotificationSetting) {
522 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
523 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
525 videoBlacklistId: videoBlacklist.id
527 notification.VideoBlacklist = videoBlacklist
532 function emailSender (emails: string[]) {
533 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist)
536 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
539 private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) {
540 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
543 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
545 function settingGetter (user: MUserWithNotificationSetting) {
546 return user.NotificationSetting.blacklistOnMyVideo
549 async function notificationCreator (user: MUserWithNotificationSetting) {
550 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
551 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
553 videoBlacklistId: videoBlacklist.id
555 notification.VideoBlacklist = videoBlacklist
560 function emailSender (emails: string[]) {
561 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
564 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
567 private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) {
568 const user = await UserModel.loadByVideoId(video.id)
571 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
573 function settingGetter (user: MUserWithNotificationSetting) {
574 return user.NotificationSetting.blacklistOnMyVideo
577 async function notificationCreator (user: MUserWithNotificationSetting) {
578 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
579 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
583 notification.Video = video
588 function emailSender (emails: string[]) {
589 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
592 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
595 private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) {
596 const user = await UserModel.loadByVideoId(video.id)
599 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
601 function settingGetter (user: MUserWithNotificationSetting) {
602 return user.NotificationSetting.myVideoPublished
605 async function notificationCreator (user: MUserWithNotificationSetting) {
606 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
607 type: UserNotificationType.MY_VIDEO_PUBLISHED,
611 notification.Video = video
616 function emailSender (emails: string[]) {
617 return Emailer.Instance.myVideoPublishedNotification(emails, video)
620 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
623 private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) {
624 const user = await UserModel.loadByVideoImportId(videoImport.id)
627 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
629 function settingGetter (user: MUserWithNotificationSetting) {
630 return user.NotificationSetting.myVideoImportFinished
633 async function notificationCreator (user: MUserWithNotificationSetting) {
634 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
635 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
637 videoImportId: videoImport.id
639 notification.VideoImport = videoImport
644 function emailSender (emails: string[]) {
646 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
647 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
650 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
653 private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) {
654 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
655 if (moderators.length === 0) return
658 'Notifying %s moderators of new user registration of %s.',
659 moderators.length, registeredUser.username
662 function settingGetter (user: MUserWithNotificationSetting) {
663 return user.NotificationSetting.newUserRegistration
666 async function notificationCreator (user: MUserWithNotificationSetting) {
667 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
668 type: UserNotificationType.NEW_USER_REGISTRATION,
670 accountId: registeredUser.Account.id
672 notification.Account = registeredUser.Account
677 function emailSender (emails: string[]) {
678 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
681 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
684 private async notifyAdminsOfNewPeerTubeVersion (application: MApplication, latestVersion: string) {
685 // Use the debug right to know who is an administrator
686 const admins = await UserModel.listWithRight(UserRight.MANAGE_DEBUG)
687 if (admins.length === 0) return
689 logger.info('Notifying %s admins of new PeerTube version %s.', admins.length, latestVersion)
691 function settingGetter (user: MUserWithNotificationSetting) {
692 return user.NotificationSetting.newPeerTubeVersion
695 async function notificationCreator (user: MUserWithNotificationSetting) {
696 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
697 type: UserNotificationType.NEW_PEERTUBE_VERSION,
699 applicationId: application.id
701 notification.Application = application
706 function emailSender (emails: string[]) {
707 return Emailer.Instance.addNewPeerTubeVersionNotification(emails, latestVersion)
710 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
713 private async notifyAdminsOfNewPluginVersion (plugin: MPlugin) {
714 // Use the debug right to know who is an administrator
715 const admins = await UserModel.listWithRight(UserRight.MANAGE_DEBUG)
716 if (admins.length === 0) return
718 logger.info('Notifying %s admins of new plugin version %s@%s.', admins.length, plugin.name, plugin.latestVersion)
720 function settingGetter (user: MUserWithNotificationSetting) {
721 return user.NotificationSetting.newPluginVersion
724 async function notificationCreator (user: MUserWithNotificationSetting) {
725 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
726 type: UserNotificationType.NEW_PLUGIN_VERSION,
730 notification.Plugin = plugin
735 function emailSender (emails: string[]) {
736 return Emailer.Instance.addNewPlugionVersionNotification(emails, plugin)
739 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
742 private async notify<T extends MUserWithNotificationSetting> (options: {
744 notificationCreator: (user: T) => Promise<UserNotificationModelForApi>
745 emailSender: (emails: string[]) => void
746 settingGetter: (user: T) => UserNotificationSettingValue
748 const emails: string[] = []
750 for (const user of options.users) {
751 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
752 const notification = await options.notificationCreator(user)
754 PeerTubeSocket.Instance.sendNotification(user.id, notification)
757 if (this.isEmailEnabled(user, options.settingGetter(user))) {
758 emails.push(user.email)
762 if (emails.length !== 0) {
763 options.emailSender(emails)
767 private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) {
768 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
770 return value & UserNotificationSettingValue.EMAIL
773 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
774 return value & UserNotificationSettingValue.WEB
777 private isBlockedByServerOrUser (targetAccount: MAccountServer, user?: MUserAccount) {
778 return isBlockedByServerOrAccount(targetAccount, user?.Account)
781 private getAbuseUrl (abuse: MAbuseFull) {
782 return abuse.VideoAbuse?.Video?.url ||
783 abuse.VideoCommentAbuse?.VideoComment?.url ||
784 abuse.FlaggedAccount.Actor.url
787 static get Instance () {
788 return this.instance || (this.instance = new this())
792 // ---------------------------------------------------------------------------