]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/notifier.ts
Add abuse messages/states notifications
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
CommitLineData
594d3e48 1import { AbuseMessageModel } from '@server/models/abuse/abuse-message'
696d83fd
C
2import { getServerActor } from '@server/models/application/application'
3import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
4import {
5 MUser,
6 MUserAccount,
7 MUserDefault,
8 MUserNotifSettingAccount,
9 MUserWithNotificationSetting,
10 UserNotificationModelForApi
26d6bf65 11} from '@server/types/models/user'
d95d1559 12import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist'
26d6bf65 13import { MVideoImportVideo } from '@server/types/models/video/video-import'
edbc9325 14import { UserAbuse } from '@shared/models'
cef534ed 15import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
d95d1559 16import { VideoPrivacy, VideoState } from '../../shared/models/videos'
cef534ed 17import { logger } from '../helpers/logger'
6dd9de95 18import { CONFIG } from '../initializers/config'
dc133480 19import { AccountBlocklistModel } from '../models/account/account-blocklist'
696d83fd
C
20import { UserModel } from '../models/account/user'
21import { UserNotificationModel } from '../models/account/user-notification'
594d3e48 22import { MAbuseFull, MAbuseMessage, MAccountServer, MActorFollowFull } from '../types/models'
d95d1559 23import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../types/models/video'
696d83fd
C
24import { isBlockedByServerOrAccount } from './blocklist'
25import { Emailer } from './emailer'
26import { PeerTubeSocket } from './peertube-socket'
cef534ed
C
27
28class Notifier {
29
30 private static instance: Notifier
31
a1587156
C
32 private constructor () {
33 }
cef534ed 34
453e83ea 35 notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void {
7ccddd7b 36 // Only notify on public and published videos which are not blacklisted
5b77537c 37 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return
cef534ed
C
38
39 this.notifySubscribersOfNewVideo(video)
a1587156 40 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
cef534ed
C
41 }
42
453e83ea 43 notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
7ccddd7b
JM
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
dc133480
C
46
47 this.notifyOwnedVideoHasBeenPublished(video)
7ccddd7b
JM
48 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
49 }
50
453e83ea 51 notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void {
7ccddd7b
JM
52 // don't notify if video is still blacklisted or waiting for transcoding
53 if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
54
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 }))
57 }
58
453e83ea 59 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void {
7ccddd7b
JM
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
62
63 this.notifyOwnedVideoHasBeenPublished(video)
a1587156
C
64 .catch(err => {
65 logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })
66 })
dc133480
C
67 }
68
453e83ea 69 notifyOnNewComment (comment: MCommentOwnerVideo): void {
cef534ed 70 this.notifyVideoOwnerOfNewComment(comment)
f7cc67b4
C
71 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
72
73 this.notifyOfCommentMention(comment)
74 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
cef534ed
C
75 }
76
edbc9325 77 notifyOnNewAbuse (parameters: { abuse: UserAbuse, abuseInstance: MAbuseFull, reporter: string }): void {
d95d1559
C
78 this.notifyModeratorsOfNewAbuse(parameters)
79 .catch(err => logger.error('Cannot notify of new abuse %d.', parameters.abuseInstance.id, { err }))
cef534ed
C
80 }
81
8424c402
C
82 notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
83 this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist)
a1587156 84 .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
7ccddd7b
JM
85 }
86
453e83ea 87 notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
cef534ed 88 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
a1587156 89 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
cef534ed
C
90 }
91
8424c402 92 notifyOnVideoUnblacklist (video: MVideoFullLight): void {
cef534ed 93 this.notifyVideoOwnerOfUnblacklist(video)
7ccddd7b 94 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
cef534ed
C
95 }
96
453e83ea 97 notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void {
dc133480 98 this.notifyOwnerVideoImportIsFinished(videoImport, success)
a1587156 99 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
dc133480
C
100 }
101
8424c402 102 notifyOnNewUserRegistration (user: MUserDefault): void {
f7cc67b4
C
103 this.notifyModeratorsOfNewUserRegistration(user)
104 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
105 }
106
8424c402 107 notifyOfNewUserFollow (actorFollow: MActorFollowFull): void {
f7cc67b4 108 this.notifyUserOfNewActorFollow(actorFollow)
a1587156
C
109 .catch(err => {
110 logger.error(
111 'Cannot notify owner of channel %s of a new follow by %s.',
112 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
113 actorFollow.ActorFollower.Account.getDisplayName(),
114 { err }
115 )
116 })
f7cc67b4
C
117 }
118
8424c402 119 notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
883993c8
C
120 this.notifyAdminsOfNewInstanceFollow(actorFollow)
121 .catch(err => {
122 logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err })
123 })
124 }
125
8424c402
C
126 notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void {
127 this.notifyAdminsOfAutoInstanceFollowing(actorFollow)
128 .catch(err => {
129 logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err })
130 })
131 }
132
594d3e48
C
133 notifyOnAbuseStateChange (abuse: MAbuseFull): void {
134 this.notifyReporterOfAbuseStateChange(abuse)
135 .catch(err => {
136 logger.error('Cannot notify reporter of abuse %d state change.', abuse.id, { err })
137 })
138 }
139
140 notifyOnAbuseMessage (abuse: MAbuseFull, message: AbuseMessageModel): void {
141 this.notifyOfNewAbuseMessage(abuse, message)
142 .catch(err => {
143 logger.error('Cannot notify on new abuse %d message.', abuse.id, { err })
144 })
145 }
146
453e83ea 147 private async notifySubscribersOfNewVideo (video: MVideoAccountLight) {
cef534ed
C
148 // List all followers that are users
149 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
150
151 logger.info('Notifying %d users of new video %s.', users.length, video.url)
152
8424c402 153 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
154 return user.NotificationSetting.newVideoFromSubscription
155 }
156
8424c402
C
157 async function notificationCreator (user: MUserWithNotificationSetting) {
158 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
159 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
160 userId: user.id,
161 videoId: video.id
162 })
8424c402 163 notification.Video = video
cef534ed
C
164
165 return notification
166 }
167
168 function emailSender (emails: string[]) {
169 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
170 }
171
172 return this.notify({ users, settingGetter, notificationCreator, emailSender })
173 }
174
453e83ea 175 private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) {
f7cc67b4
C
176 if (comment.Video.isOwned() === false) return
177
cef534ed
C
178 const user = await UserModel.loadByVideoId(comment.videoId)
179
180 // Not our user or user comments its own video
181 if (!user || comment.Account.userId === user.id) return
182
696d83fd 183 if (await this.isBlockedByServerOrUser(comment.Account, user)) return
dc133480 184
cef534ed
C
185 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
186
8424c402 187 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
188 return user.NotificationSetting.newCommentOnMyVideo
189 }
190
8424c402
C
191 async function notificationCreator (user: MUserWithNotificationSetting) {
192 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
193 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
194 userId: user.id,
195 commentId: comment.id
196 })
8424c402 197 notification.Comment = comment
cef534ed
C
198
199 return notification
200 }
201
202 function emailSender (emails: string[]) {
203 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
204 }
205
206 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
207 }
208
453e83ea 209 private async notifyOfCommentMention (comment: MCommentOwnerVideo) {
41d71344
C
210 const extractedUsernames = comment.extractMentions()
211 logger.debug(
212 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
213 { usernames: extractedUsernames, text: comment.text }
214 )
1f6d57e3 215
41d71344 216 let users = await UserModel.listByUsernames(extractedUsernames)
f7cc67b4
C
217
218 if (comment.Video.isOwned()) {
219 const userException = await UserModel.loadByVideoId(comment.videoId)
220 users = users.filter(u => u.id !== userException.id)
221 }
222
223 // Don't notify if I mentioned myself
224 users = users.filter(u => u.Account.id !== comment.accountId)
225
cef534ed
C
226 if (users.length === 0) return
227
dddc8b1f
C
228 const serverAccountId = (await getServerActor()).Account.id
229 const sourceAccounts = users.map(u => u.Account.id).concat([ serverAccountId ])
230
231 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, comment.accountId)
232 const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, comment.Account.Actor.serverId)
f7cc67b4
C
233
234 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
235
8424c402 236 function settingGetter (user: MUserNotifSettingAccount) {
dddc8b1f
C
237 const accountId = user.Account.id
238 if (
239 accountMutedHash[accountId] === true || instanceMutedHash[accountId] === true ||
240 accountMutedHash[serverAccountId] === true || instanceMutedHash[serverAccountId] === true
241 ) {
242 return UserNotificationSettingValue.NONE
243 }
f7cc67b4
C
244
245 return user.NotificationSetting.commentMention
246 }
247
8424c402
C
248 async function notificationCreator (user: MUserNotifSettingAccount) {
249 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
f7cc67b4
C
250 type: UserNotificationType.COMMENT_MENTION,
251 userId: user.id,
252 commentId: comment.id
253 })
8424c402 254 notification.Comment = comment
f7cc67b4
C
255
256 return notification
257 }
258
259 function emailSender (emails: string[]) {
260 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
261 }
262
263 return this.notify({ users, settingGetter, notificationCreator, emailSender })
264 }
265
8424c402 266 private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) {
f7cc67b4
C
267 if (actorFollow.ActorFollowing.isOwned() === false) return
268
269 // Account follows one of our account?
270 let followType: 'account' | 'channel' = 'channel'
271 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
272
273 // Account follows one of our channel?
274 if (!user) {
275 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
276 followType = 'account'
277 }
278
279 if (!user) return
280
f7cc67b4 281 const followerAccount = actorFollow.ActorFollower.Account
dddc8b1f 282 const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower })
f7cc67b4 283
696d83fd 284 if (await this.isBlockedByServerOrUser(followerAccountWithActor, user)) return
f7cc67b4
C
285
286 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
287
8424c402 288 function settingGetter (user: MUserWithNotificationSetting) {
f7cc67b4
C
289 return user.NotificationSetting.newFollow
290 }
291
8424c402
C
292 async function notificationCreator (user: MUserWithNotificationSetting) {
293 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
f7cc67b4
C
294 type: UserNotificationType.NEW_FOLLOW,
295 userId: user.id,
296 actorFollowId: actorFollow.id
297 })
8424c402 298 notification.ActorFollow = actorFollow
f7cc67b4
C
299
300 return notification
301 }
302
303 function emailSender (emails: string[]) {
304 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
305 }
306
307 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
308 }
309
8424c402 310 private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) {
883993c8
C
311 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
312
696d83fd
C
313 const follower = Object.assign(actorFollow.ActorFollower.Account, { Actor: actorFollow.ActorFollower })
314 if (await this.isBlockedByServerOrUser(follower)) return
315
883993c8
C
316 logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url)
317
8424c402 318 function settingGetter (user: MUserWithNotificationSetting) {
883993c8
C
319 return user.NotificationSetting.newInstanceFollower
320 }
321
8424c402
C
322 async function notificationCreator (user: MUserWithNotificationSetting) {
323 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
883993c8
C
324 type: UserNotificationType.NEW_INSTANCE_FOLLOWER,
325 userId: user.id,
326 actorFollowId: actorFollow.id
327 })
8424c402 328 notification.ActorFollow = actorFollow
883993c8
C
329
330 return notification
331 }
332
333 function emailSender (emails: string[]) {
334 return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow)
335 }
336
337 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
338 }
339
8424c402
C
340 private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) {
341 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
342
343 logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url)
344
345 function settingGetter (user: MUserWithNotificationSetting) {
346 return user.NotificationSetting.autoInstanceFollowing
347 }
348
349 async function notificationCreator (user: MUserWithNotificationSetting) {
350 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
351 type: UserNotificationType.AUTO_INSTANCE_FOLLOWING,
352 userId: user.id,
353 actorFollowId: actorFollow.id
354 })
355 notification.ActorFollow = actorFollow
356
357 return notification
358 }
359
360 function emailSender (emails: string[]) {
361 return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow)
362 }
363
364 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
365 }
366
d95d1559 367 private async notifyModeratorsOfNewAbuse (parameters: {
edbc9325 368 abuse: UserAbuse
d95d1559 369 abuseInstance: MAbuseFull
df4c603d
RK
370 reporter: string
371 }) {
d95d1559
C
372 const { abuse, abuseInstance } = parameters
373
374 const moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
f7cc67b4
C
375 if (moderators.length === 0) return
376
594d3e48 377 const url = this.getAbuseUrl(abuseInstance)
d95d1559
C
378
379 logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url)
cef534ed 380
8424c402 381 function settingGetter (user: MUserWithNotificationSetting) {
4f32032f 382 return user.NotificationSetting.abuseAsModerator
cef534ed
C
383 }
384
8424c402 385 async function notificationCreator (user: MUserWithNotificationSetting) {
d95d1559 386 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
310b5219 387 type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS,
cef534ed 388 userId: user.id,
d95d1559 389 abuseId: abuse.id
cef534ed 390 })
d95d1559 391 notification.Abuse = abuseInstance
cef534ed
C
392
393 return notification
394 }
395
396 function emailSender (emails: string[]) {
d95d1559 397 return Emailer.Instance.addAbuseModeratorsNotification(emails, parameters)
cef534ed
C
398 }
399
f7cc67b4 400 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
cef534ed
C
401 }
402
594d3e48
C
403 private async notifyReporterOfAbuseStateChange (abuse: MAbuseFull) {
404 // Only notify our users
405 if (abuse.ReporterAccount.isOwned() !== true) return
406
407 const url = this.getAbuseUrl(abuse)
408
409 logger.info('Notifying reporter of abuse % of state change.', url)
410
411 const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId)
412
413 function settingGetter (user: MUserWithNotificationSetting) {
414 return user.NotificationSetting.abuseStateChange
415 }
416
417 async function notificationCreator (user: MUserWithNotificationSetting) {
418 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
419 type: UserNotificationType.ABUSE_STATE_CHANGE,
420 userId: user.id,
421 abuseId: abuse.id
422 })
423 notification.Abuse = abuse
424
425 return notification
426 }
427
428 function emailSender (emails: string[]) {
429 return Emailer.Instance.addAbuseStateChangeNotification(emails, abuse)
430 }
431
432 return this.notify({ users: [ reporter ], settingGetter, notificationCreator, emailSender })
433 }
434
435 private async notifyOfNewAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage) {
436 const url = this.getAbuseUrl(abuse)
437 logger.info('Notifying reporter and moderators of new abuse message on %s.', url)
438
439 function settingGetter (user: MUserWithNotificationSetting) {
440 return user.NotificationSetting.abuseNewMessage
441 }
442
443 async function notificationCreator (user: MUserWithNotificationSetting) {
444 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
445 type: UserNotificationType.ABUSE_NEW_MESSAGE,
446 userId: user.id,
447 abuseId: abuse.id
448 })
449 notification.Abuse = abuse
450
451 return notification
452 }
453
454 function emailSenderReporter (emails: string[]) {
455 return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message })
456 }
457
458 function emailSenderModerators (emails: string[]) {
459 return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message })
460 }
461
462 async function buildReporterOptions () {
463 // Only notify our users
464 if (abuse.ReporterAccount.isOwned() !== true) return
465
466 const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId)
467 // Don't notify my own message
468 if (reporter.Account.id === message.accountId) return
469
470 return { users: [ reporter ], settingGetter, notificationCreator, emailSender: emailSenderReporter }
471 }
472
473 async function buildModeratorsOptions () {
474 let moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
475 // Don't notify my own message
476 moderators = moderators.filter(m => m.Account.id !== message.accountId)
477
478 if (moderators.length === 0) return
479
480 return { users: moderators, settingGetter, notificationCreator, emailSender: emailSenderModerators }
481 }
482
483 const [ reporterOptions, moderatorsOptions ] = await Promise.all([
484 buildReporterOptions(),
485 buildModeratorsOptions()
486 ])
487
488 return Promise.all([
489 this.notify(reporterOptions),
490 this.notify(moderatorsOptions)
491 ])
492 }
493
8424c402 494 private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) {
7ccddd7b
JM
495 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
496 if (moderators.length === 0) return
497
8424c402 498 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url)
7ccddd7b 499
8424c402 500 function settingGetter (user: MUserWithNotificationSetting) {
7ccddd7b
JM
501 return user.NotificationSetting.videoAutoBlacklistAsModerator
502 }
7ccddd7b 503
8424c402
C
504 async function notificationCreator (user: MUserWithNotificationSetting) {
505 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
7ccddd7b
JM
506 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
507 userId: user.id,
8424c402 508 videoBlacklistId: videoBlacklist.id
7ccddd7b 509 })
8424c402 510 notification.VideoBlacklist = videoBlacklist
7ccddd7b
JM
511
512 return notification
513 }
514
515 function emailSender (emails: string[]) {
8424c402 516 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist)
7ccddd7b
JM
517 }
518
519 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
520 }
521
453e83ea 522 private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) {
cef534ed
C
523 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
524 if (!user) return
525
526 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
527
8424c402 528 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
529 return user.NotificationSetting.blacklistOnMyVideo
530 }
531
8424c402
C
532 async function notificationCreator (user: MUserWithNotificationSetting) {
533 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
534 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
535 userId: user.id,
536 videoBlacklistId: videoBlacklist.id
537 })
8424c402 538 notification.VideoBlacklist = videoBlacklist
cef534ed
C
539
540 return notification
541 }
542
543 function emailSender (emails: string[]) {
544 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
545 }
546
547 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
548 }
549
8424c402 550 private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) {
cef534ed
C
551 const user = await UserModel.loadByVideoId(video.id)
552 if (!user) return
553
554 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
555
8424c402 556 function settingGetter (user: MUserWithNotificationSetting) {
cef534ed
C
557 return user.NotificationSetting.blacklistOnMyVideo
558 }
559
8424c402
C
560 async function notificationCreator (user: MUserWithNotificationSetting) {
561 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
cef534ed
C
562 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
563 userId: user.id,
564 videoId: video.id
565 })
8424c402 566 notification.Video = video
cef534ed
C
567
568 return notification
569 }
570
571 function emailSender (emails: string[]) {
572 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
573 }
574
575 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
576 }
577
453e83ea 578 private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) {
dc133480
C
579 const user = await UserModel.loadByVideoId(video.id)
580 if (!user) return
581
582 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
583
8424c402 584 function settingGetter (user: MUserWithNotificationSetting) {
dc133480
C
585 return user.NotificationSetting.myVideoPublished
586 }
587
8424c402
C
588 async function notificationCreator (user: MUserWithNotificationSetting) {
589 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
dc133480
C
590 type: UserNotificationType.MY_VIDEO_PUBLISHED,
591 userId: user.id,
592 videoId: video.id
593 })
8424c402 594 notification.Video = video
dc133480
C
595
596 return notification
597 }
598
599 function emailSender (emails: string[]) {
600 return Emailer.Instance.myVideoPublishedNotification(emails, video)
601 }
602
603 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
604 }
605
453e83ea 606 private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) {
dc133480
C
607 const user = await UserModel.loadByVideoImportId(videoImport.id)
608 if (!user) return
609
610 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
611
8424c402 612 function settingGetter (user: MUserWithNotificationSetting) {
dc133480
C
613 return user.NotificationSetting.myVideoImportFinished
614 }
615
8424c402
C
616 async function notificationCreator (user: MUserWithNotificationSetting) {
617 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
dc133480
C
618 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
619 userId: user.id,
620 videoImportId: videoImport.id
621 })
8424c402 622 notification.VideoImport = videoImport
dc133480
C
623
624 return notification
625 }
626
627 function emailSender (emails: string[]) {
628 return success
629 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
630 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
631 }
632
633 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
634 }
635
8424c402 636 private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) {
f7cc67b4
C
637 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
638 if (moderators.length === 0) return
639
640 logger.info(
641 'Notifying %s moderators of new user registration of %s.',
453e83ea 642 moderators.length, registeredUser.username
f7cc67b4
C
643 )
644
8424c402 645 function settingGetter (user: MUserWithNotificationSetting) {
f7cc67b4
C
646 return user.NotificationSetting.newUserRegistration
647 }
648
8424c402
C
649 async function notificationCreator (user: MUserWithNotificationSetting) {
650 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
f7cc67b4
C
651 type: UserNotificationType.NEW_USER_REGISTRATION,
652 userId: user.id,
653 accountId: registeredUser.Account.id
654 })
8424c402 655 notification.Account = registeredUser.Account
f7cc67b4
C
656
657 return notification
658 }
659
660 function emailSender (emails: string[]) {
661 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
662 }
663
664 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
665 }
666
a1587156
C
667 private async notify<T extends MUserWithNotificationSetting> (options: {
668 users: T[]
669 notificationCreator: (user: T) => Promise<UserNotificationModelForApi>
670 emailSender: (emails: string[]) => void
8424c402 671 settingGetter: (user: T) => UserNotificationSettingValue
cef534ed
C
672 }) {
673 const emails: string[] = []
674
675 for (const user of options.users) {
676 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
677 const notification = await options.notificationCreator(user)
678
679 PeerTubeSocket.Instance.sendNotification(user.id, notification)
680 }
681
682 if (this.isEmailEnabled(user, options.settingGetter(user))) {
683 emails.push(user.email)
684 }
685 }
686
687 if (emails.length !== 0) {
a1587156 688 options.emailSender(emails)
cef534ed
C
689 }
690 }
691
453e83ea 692 private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) {
1ed9b8ee 693 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
cef534ed 694
2f1548fd 695 return value & UserNotificationSettingValue.EMAIL
cef534ed
C
696 }
697
698 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
2f1548fd 699 return value & UserNotificationSettingValue.WEB
cef534ed
C
700 }
701
696d83fd
C
702 private isBlockedByServerOrUser (targetAccount: MAccountServer, user?: MUserAccount) {
703 return isBlockedByServerOrAccount(targetAccount, user?.Account)
dddc8b1f
C
704 }
705
594d3e48
C
706 private getAbuseUrl (abuse: MAbuseFull) {
707 return abuse.VideoAbuse?.Video?.url ||
708 abuse.VideoCommentAbuse?.VideoComment?.url ||
709 abuse.FlaggedAccount.Actor.url
710 }
711
cef534ed
C
712 static get Instance () {
713 return this.instance || (this.instance = new this())
714 }
715}
716
717// ---------------------------------------------------------------------------
718
719export {
720 Notifier
721}