]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/notifier.ts
add quarantine videos feature (#1637)
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
1 import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
2 import { logger } from '../helpers/logger'
3 import { VideoModel } from '../models/video/video'
4 import { Emailer } from './emailer'
5 import { UserNotificationModel } from '../models/account/user-notification'
6 import { VideoCommentModel } from '../models/video/video-comment'
7 import { UserModel } from '../models/account/user'
8 import { PeerTubeSocket } from './peertube-socket'
9 import { CONFIG } from '../initializers/constants'
10 import { VideoPrivacy, VideoState } from '../../shared/models/videos'
11 import { VideoAbuseModel } from '../models/video/video-abuse'
12 import { VideoBlacklistModel } from '../models/video/video-blacklist'
13 import * as Bluebird from 'bluebird'
14 import { VideoImportModel } from '../models/video/video-import'
15 import { AccountBlocklistModel } from '../models/account/account-blocklist'
16 import { ActorFollowModel } from '../models/activitypub/actor-follow'
17 import { AccountModel } from '../models/account/account'
18
19 class Notifier {
20
21 private static instance: Notifier
22
23 private constructor () {}
24
25 notifyOnNewVideo (video: VideoModel): void {
26 // Only notify on public and published videos which are not blacklisted
27 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.VideoBlacklist) return
28
29 this.notifySubscribersOfNewVideo(video)
30 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
31 }
32
33 notifyOnVideoPublishedAfterTranscoding (video: VideoModel): void {
34 // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update
35 if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return
36
37 this.notifyOwnedVideoHasBeenPublished(video)
38 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
39 }
40
41 notifyOnVideoPublishedAfterScheduledUpdate (video: VideoModel): void {
42 // don't notify if video is still blacklisted or waiting for transcoding
43 if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
44
45 this.notifyOwnedVideoHasBeenPublished(video)
46 .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err }))
47 }
48
49 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: VideoModel): void {
50 // don't notify if video is still waiting for transcoding or scheduled update
51 if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
52
53 this.notifyOwnedVideoHasBeenPublished(video)
54 .catch(err => logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })) // tslint:disable-line:max-line-length
55 }
56
57 notifyOnNewComment (comment: VideoCommentModel): void {
58 this.notifyVideoOwnerOfNewComment(comment)
59 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
60
61 this.notifyOfCommentMention(comment)
62 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
63 }
64
65 notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void {
66 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
67 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
68 }
69
70 notifyOnVideoAutoBlacklist (video: VideoModel): void {
71 this.notifyModeratorsOfVideoAutoBlacklist(video)
72 .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', video.url, { err }))
73 }
74
75 notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void {
76 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
77 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
78 }
79
80 notifyOnVideoUnblacklist (video: VideoModel): void {
81 this.notifyVideoOwnerOfUnblacklist(video)
82 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
83 }
84
85 notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void {
86 this.notifyOwnerVideoImportIsFinished(videoImport, success)
87 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
88 }
89
90 notifyOnNewUserRegistration (user: UserModel): void {
91 this.notifyModeratorsOfNewUserRegistration(user)
92 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
93 }
94
95 notifyOfNewFollow (actorFollow: ActorFollowModel): void {
96 this.notifyUserOfNewActorFollow(actorFollow)
97 .catch(err => {
98 logger.error(
99 'Cannot notify owner of channel %s of a new follow by %s.',
100 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
101 actorFollow.ActorFollower.Account.getDisplayName(),
102 err
103 )
104 })
105 }
106
107 private async notifySubscribersOfNewVideo (video: VideoModel) {
108 // List all followers that are users
109 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
110
111 logger.info('Notifying %d users of new video %s.', users.length, video.url)
112
113 function settingGetter (user: UserModel) {
114 return user.NotificationSetting.newVideoFromSubscription
115 }
116
117 async function notificationCreator (user: UserModel) {
118 const notification = await UserNotificationModel.create({
119 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
120 userId: user.id,
121 videoId: video.id
122 })
123 notification.Video = video
124
125 return notification
126 }
127
128 function emailSender (emails: string[]) {
129 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
130 }
131
132 return this.notify({ users, settingGetter, notificationCreator, emailSender })
133 }
134
135 private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) {
136 if (comment.Video.isOwned() === false) return
137
138 const user = await UserModel.loadByVideoId(comment.videoId)
139
140 // Not our user or user comments its own video
141 if (!user || comment.Account.userId === user.id) return
142
143 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
144 if (accountMuted) return
145
146 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
147
148 function settingGetter (user: UserModel) {
149 return user.NotificationSetting.newCommentOnMyVideo
150 }
151
152 async function notificationCreator (user: UserModel) {
153 const notification = await UserNotificationModel.create({
154 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
155 userId: user.id,
156 commentId: comment.id
157 })
158 notification.Comment = comment
159
160 return notification
161 }
162
163 function emailSender (emails: string[]) {
164 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
165 }
166
167 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
168 }
169
170 private async notifyOfCommentMention (comment: VideoCommentModel) {
171 const extractedUsernames = comment.extractMentions()
172 logger.debug(
173 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
174 { usernames: extractedUsernames, text: comment.text }
175 )
176
177 let users = await UserModel.listByUsernames(extractedUsernames)
178
179 if (comment.Video.isOwned()) {
180 const userException = await UserModel.loadByVideoId(comment.videoId)
181 users = users.filter(u => u.id !== userException.id)
182 }
183
184 // Don't notify if I mentioned myself
185 users = users.filter(u => u.Account.id !== comment.accountId)
186
187 if (users.length === 0) return
188
189 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
190
191 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
192
193 function settingGetter (user: UserModel) {
194 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
195
196 return user.NotificationSetting.commentMention
197 }
198
199 async function notificationCreator (user: UserModel) {
200 const notification = await UserNotificationModel.create({
201 type: UserNotificationType.COMMENT_MENTION,
202 userId: user.id,
203 commentId: comment.id
204 })
205 notification.Comment = comment
206
207 return notification
208 }
209
210 function emailSender (emails: string[]) {
211 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
212 }
213
214 return this.notify({ users, settingGetter, notificationCreator, emailSender })
215 }
216
217 private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) {
218 if (actorFollow.ActorFollowing.isOwned() === false) return
219
220 // Account follows one of our account?
221 let followType: 'account' | 'channel' = 'channel'
222 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
223
224 // Account follows one of our channel?
225 if (!user) {
226 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
227 followType = 'account'
228 }
229
230 if (!user) return
231
232 if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) {
233 actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel
234 }
235 const followerAccount = actorFollow.ActorFollower.Account
236
237 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
238 if (accountMuted) return
239
240 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
241
242 function settingGetter (user: UserModel) {
243 return user.NotificationSetting.newFollow
244 }
245
246 async function notificationCreator (user: UserModel) {
247 const notification = await UserNotificationModel.create({
248 type: UserNotificationType.NEW_FOLLOW,
249 userId: user.id,
250 actorFollowId: actorFollow.id
251 })
252 notification.ActorFollow = actorFollow
253
254 return notification
255 }
256
257 function emailSender (emails: string[]) {
258 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
259 }
260
261 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
262 }
263
264 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
265 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
266 if (moderators.length === 0) return
267
268 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
269
270 function settingGetter (user: UserModel) {
271 return user.NotificationSetting.videoAbuseAsModerator
272 }
273
274 async function notificationCreator (user: UserModel) {
275 const notification = await UserNotificationModel.create({
276 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
277 userId: user.id,
278 videoAbuseId: videoAbuse.id
279 })
280 notification.VideoAbuse = videoAbuse
281
282 return notification
283 }
284
285 function emailSender (emails: string[]) {
286 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
287 }
288
289 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
290 }
291
292 private async notifyModeratorsOfVideoAutoBlacklist (video: VideoModel) {
293 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
294 if (moderators.length === 0) return
295
296 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, video.url)
297
298 function settingGetter (user: UserModel) {
299 return user.NotificationSetting.videoAutoBlacklistAsModerator
300 }
301 async function notificationCreator (user: UserModel) {
302
303 const notification = await UserNotificationModel.create({
304 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
305 userId: user.id,
306 videoId: video.id
307 })
308 notification.Video = video
309
310 return notification
311 }
312
313 function emailSender (emails: string[]) {
314 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video)
315 }
316
317 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
318 }
319
320 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
321 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
322 if (!user) return
323
324 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
325
326 function settingGetter (user: UserModel) {
327 return user.NotificationSetting.blacklistOnMyVideo
328 }
329
330 async function notificationCreator (user: UserModel) {
331 const notification = await UserNotificationModel.create({
332 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
333 userId: user.id,
334 videoBlacklistId: videoBlacklist.id
335 })
336 notification.VideoBlacklist = videoBlacklist
337
338 return notification
339 }
340
341 function emailSender (emails: string[]) {
342 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
343 }
344
345 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
346 }
347
348 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) {
349 const user = await UserModel.loadByVideoId(video.id)
350 if (!user) return
351
352 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
353
354 function settingGetter (user: UserModel) {
355 return user.NotificationSetting.blacklistOnMyVideo
356 }
357
358 async function notificationCreator (user: UserModel) {
359 const notification = await UserNotificationModel.create({
360 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
361 userId: user.id,
362 videoId: video.id
363 })
364 notification.Video = video
365
366 return notification
367 }
368
369 function emailSender (emails: string[]) {
370 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
371 }
372
373 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
374 }
375
376 private async notifyOwnedVideoHasBeenPublished (video: VideoModel) {
377 const user = await UserModel.loadByVideoId(video.id)
378 if (!user) return
379
380 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
381
382 function settingGetter (user: UserModel) {
383 return user.NotificationSetting.myVideoPublished
384 }
385
386 async function notificationCreator (user: UserModel) {
387 const notification = await UserNotificationModel.create({
388 type: UserNotificationType.MY_VIDEO_PUBLISHED,
389 userId: user.id,
390 videoId: video.id
391 })
392 notification.Video = video
393
394 return notification
395 }
396
397 function emailSender (emails: string[]) {
398 return Emailer.Instance.myVideoPublishedNotification(emails, video)
399 }
400
401 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
402 }
403
404 private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) {
405 const user = await UserModel.loadByVideoImportId(videoImport.id)
406 if (!user) return
407
408 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
409
410 function settingGetter (user: UserModel) {
411 return user.NotificationSetting.myVideoImportFinished
412 }
413
414 async function notificationCreator (user: UserModel) {
415 const notification = await UserNotificationModel.create({
416 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
417 userId: user.id,
418 videoImportId: videoImport.id
419 })
420 notification.VideoImport = videoImport
421
422 return notification
423 }
424
425 function emailSender (emails: string[]) {
426 return success
427 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
428 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
429 }
430
431 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
432 }
433
434 private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) {
435 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
436 if (moderators.length === 0) return
437
438 logger.info(
439 'Notifying %s moderators of new user registration of %s.',
440 moderators.length, registeredUser.Account.Actor.preferredUsername
441 )
442
443 function settingGetter (user: UserModel) {
444 return user.NotificationSetting.newUserRegistration
445 }
446
447 async function notificationCreator (user: UserModel) {
448 const notification = await UserNotificationModel.create({
449 type: UserNotificationType.NEW_USER_REGISTRATION,
450 userId: user.id,
451 accountId: registeredUser.Account.id
452 })
453 notification.Account = registeredUser.Account
454
455 return notification
456 }
457
458 function emailSender (emails: string[]) {
459 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
460 }
461
462 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
463 }
464
465 private async notify (options: {
466 users: UserModel[],
467 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
468 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
469 settingGetter: (user: UserModel) => UserNotificationSettingValue
470 }) {
471 const emails: string[] = []
472
473 for (const user of options.users) {
474 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
475 const notification = await options.notificationCreator(user)
476
477 PeerTubeSocket.Instance.sendNotification(user.id, notification)
478 }
479
480 if (this.isEmailEnabled(user, options.settingGetter(user))) {
481 emails.push(user.email)
482 }
483 }
484
485 if (emails.length !== 0) {
486 await options.emailSender(emails)
487 }
488 }
489
490 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
491 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
492
493 return value & UserNotificationSettingValue.EMAIL
494 }
495
496 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
497 return value & UserNotificationSettingValue.WEB
498 }
499
500 static get Instance () {
501 return this.instance || (this.instance = new this())
502 }
503 }
504
505 // ---------------------------------------------------------------------------
506
507 export {
508 Notifier
509 }