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