diff options
Diffstat (limited to 'server/lib/notifier.ts')
-rw-r--r-- | server/lib/notifier.ts | 114 |
1 files changed, 100 insertions, 14 deletions
diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index 2fa320cd7..c1e63fa8f 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts | |||
@@ -6,7 +6,7 @@ import { UserNotificationModel } from '../models/account/user-notification' | |||
6 | import { VideoCommentModel } from '../models/video/video-comment' | 6 | import { VideoCommentModel } from '../models/video/video-comment' |
7 | import { UserModel } from '../models/account/user' | 7 | import { UserModel } from '../models/account/user' |
8 | import { PeerTubeSocket } from './peertube-socket' | 8 | import { PeerTubeSocket } from './peertube-socket' |
9 | import { CONFIG } from '../initializers/constants' | 9 | import { CONFIG } from '../initializers/config' |
10 | import { VideoPrivacy, VideoState } from '../../shared/models/videos' | 10 | import { VideoPrivacy, VideoState } from '../../shared/models/videos' |
11 | import { VideoAbuseModel } from '../models/video/video-abuse' | 11 | import { VideoAbuseModel } from '../models/video/video-abuse' |
12 | import { VideoBlacklistModel } from '../models/video/video-blacklist' | 12 | import { VideoBlacklistModel } from '../models/video/video-blacklist' |
@@ -23,19 +23,35 @@ class Notifier { | |||
23 | private constructor () {} | 23 | private constructor () {} |
24 | 24 | ||
25 | notifyOnNewVideo (video: VideoModel): void { | 25 | notifyOnNewVideo (video: VideoModel): void { |
26 | // Only notify on public and published videos | 26 | // Only notify on public and published videos which are not blacklisted |
27 | if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED) return | 27 | if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.VideoBlacklist) return |
28 | 28 | ||
29 | this.notifySubscribersOfNewVideo(video) | 29 | this.notifySubscribersOfNewVideo(video) |
30 | .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) | 30 | .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) |
31 | } | 31 | } |
32 | 32 | ||
33 | notifyOnPendingVideoPublished (video: VideoModel): void { | 33 | notifyOnVideoPublishedAfterTranscoding (video: VideoModel): void { |
34 | // Only notify on public videos that has been published while the user waited transcoding/scheduled update | 34 | // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update |
35 | if (video.waitTranscoding === false && !video.ScheduleVideoUpdate) return | 35 | if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return |
36 | 36 | ||
37 | this.notifyOwnedVideoHasBeenPublished(video) | 37 | this.notifyOwnedVideoHasBeenPublished(video) |
38 | .catch(err => logger.error('Cannot notify owner that its video %s has been published.', video.url, { err })) | 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 | ||
39 | } | 55 | } |
40 | 56 | ||
41 | notifyOnNewComment (comment: VideoCommentModel): void { | 57 | notifyOnNewComment (comment: VideoCommentModel): void { |
@@ -51,6 +67,11 @@ class Notifier { | |||
51 | .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err })) | 67 | .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err })) |
52 | } | 68 | } |
53 | 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 | |||
54 | notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void { | 75 | notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void { |
55 | this.notifyVideoOwnerOfBlacklist(videoBlacklist) | 76 | this.notifyVideoOwnerOfBlacklist(videoBlacklist) |
56 | .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) | 77 | .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) |
@@ -58,7 +79,7 @@ class Notifier { | |||
58 | 79 | ||
59 | notifyOnVideoUnblacklist (video: VideoModel): void { | 80 | notifyOnVideoUnblacklist (video: VideoModel): void { |
60 | this.notifyVideoOwnerOfUnblacklist(video) | 81 | this.notifyVideoOwnerOfUnblacklist(video) |
61 | .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', video.url, { err })) | 82 | .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err })) |
62 | } | 83 | } |
63 | 84 | ||
64 | notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void { | 85 | notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void { |
@@ -71,18 +92,25 @@ class Notifier { | |||
71 | .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) | 92 | .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) |
72 | } | 93 | } |
73 | 94 | ||
74 | notifyOfNewFollow (actorFollow: ActorFollowModel): void { | 95 | notifyOfNewUserFollow (actorFollow: ActorFollowModel): void { |
75 | this.notifyUserOfNewActorFollow(actorFollow) | 96 | this.notifyUserOfNewActorFollow(actorFollow) |
76 | .catch(err => { | 97 | .catch(err => { |
77 | logger.error( | 98 | logger.error( |
78 | 'Cannot notify owner of channel %s of a new follow by %s.', | 99 | 'Cannot notify owner of channel %s of a new follow by %s.', |
79 | actorFollow.ActorFollowing.VideoChannel.getDisplayName(), | 100 | actorFollow.ActorFollowing.VideoChannel.getDisplayName(), |
80 | actorFollow.ActorFollower.Account.getDisplayName(), | 101 | actorFollow.ActorFollower.Account.getDisplayName(), |
81 | err | 102 | { err } |
82 | ) | 103 | ) |
83 | }) | 104 | }) |
84 | } | 105 | } |
85 | 106 | ||
107 | notifyOfNewInstanceFollow (actorFollow: ActorFollowModel): void { | ||
108 | this.notifyAdminsOfNewInstanceFollow(actorFollow) | ||
109 | .catch(err => { | ||
110 | logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) | ||
111 | }) | ||
112 | } | ||
113 | |||
86 | private async notifySubscribersOfNewVideo (video: VideoModel) { | 114 | private async notifySubscribersOfNewVideo (video: VideoModel) { |
87 | // List all followers that are users | 115 | // List all followers that are users |
88 | const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) | 116 | const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) |
@@ -147,10 +175,13 @@ class Notifier { | |||
147 | } | 175 | } |
148 | 176 | ||
149 | private async notifyOfCommentMention (comment: VideoCommentModel) { | 177 | private async notifyOfCommentMention (comment: VideoCommentModel) { |
150 | const usernames = comment.extractMentions() | 178 | const extractedUsernames = comment.extractMentions() |
151 | logger.debug('Extracted %d username from comment %s.', usernames.length, comment.url, { usernames, text: comment.text }) | 179 | logger.debug( |
180 | 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url, | ||
181 | { usernames: extractedUsernames, text: comment.text } | ||
182 | ) | ||
152 | 183 | ||
153 | let users = await UserModel.listByUsernames(usernames) | 184 | let users = await UserModel.listByUsernames(extractedUsernames) |
154 | 185 | ||
155 | if (comment.Video.isOwned()) { | 186 | if (comment.Video.isOwned()) { |
156 | const userException = await UserModel.loadByVideoId(comment.videoId) | 187 | const userException = await UserModel.loadByVideoId(comment.videoId) |
@@ -237,6 +268,33 @@ class Notifier { | |||
237 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | 268 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) |
238 | } | 269 | } |
239 | 270 | ||
271 | private async notifyAdminsOfNewInstanceFollow (actorFollow: ActorFollowModel) { | ||
272 | const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) | ||
273 | |||
274 | logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url) | ||
275 | |||
276 | function settingGetter (user: UserModel) { | ||
277 | return user.NotificationSetting.newInstanceFollower | ||
278 | } | ||
279 | |||
280 | async function notificationCreator (user: UserModel) { | ||
281 | const notification = await UserNotificationModel.create({ | ||
282 | type: UserNotificationType.NEW_INSTANCE_FOLLOWER, | ||
283 | userId: user.id, | ||
284 | actorFollowId: actorFollow.id | ||
285 | }) | ||
286 | notification.ActorFollow = actorFollow | ||
287 | |||
288 | return notification | ||
289 | } | ||
290 | |||
291 | function emailSender (emails: string[]) { | ||
292 | return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow) | ||
293 | } | ||
294 | |||
295 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) | ||
296 | } | ||
297 | |||
240 | private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) { | 298 | private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) { |
241 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) | 299 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) |
242 | if (moderators.length === 0) return | 300 | if (moderators.length === 0) return |
@@ -265,6 +323,34 @@ class Notifier { | |||
265 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | 323 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) |
266 | } | 324 | } |
267 | 325 | ||
326 | private async notifyModeratorsOfVideoAutoBlacklist (video: VideoModel) { | ||
327 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) | ||
328 | if (moderators.length === 0) return | ||
329 | |||
330 | logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, video.url) | ||
331 | |||
332 | function settingGetter (user: UserModel) { | ||
333 | return user.NotificationSetting.videoAutoBlacklistAsModerator | ||
334 | } | ||
335 | async function notificationCreator (user: UserModel) { | ||
336 | |||
337 | const notification = await UserNotificationModel.create({ | ||
338 | type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, | ||
339 | userId: user.id, | ||
340 | videoId: video.id | ||
341 | }) | ||
342 | notification.Video = video | ||
343 | |||
344 | return notification | ||
345 | } | ||
346 | |||
347 | function emailSender (emails: string[]) { | ||
348 | return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video) | ||
349 | } | ||
350 | |||
351 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | ||
352 | } | ||
353 | |||
268 | private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) { | 354 | private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) { |
269 | const user = await UserModel.loadByVideoId(videoBlacklist.videoId) | 355 | const user = await UserModel.loadByVideoId(videoBlacklist.videoId) |
270 | if (!user) return | 356 | if (!user) return |
@@ -436,7 +522,7 @@ class Notifier { | |||
436 | } | 522 | } |
437 | 523 | ||
438 | private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) { | 524 | private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) { |
439 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified !== true) return false | 525 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false |
440 | 526 | ||
441 | return value & UserNotificationSettingValue.EMAIL | 527 | return value & UserNotificationSettingValue.EMAIL |
442 | } | 528 | } |