diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
commit | 88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch) | |
tree | b242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/lib/notifier.ts | |
parent | 53a94c7cfa8368da4cd248d65df8346905938f0c (diff) | |
parent | 9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff) | |
download | PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip |
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/lib/notifier.ts')
-rw-r--r-- | server/lib/notifier.ts | 455 |
1 files changed, 455 insertions, 0 deletions
diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts new file mode 100644 index 000000000..d1b331346 --- /dev/null +++ b/server/lib/notifier.ts | |||
@@ -0,0 +1,455 @@ | |||
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 | ||
27 | if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED) return | ||
28 | |||
29 | this.notifySubscribersOfNewVideo(video) | ||
30 | .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) | ||
31 | } | ||
32 | |||
33 | notifyOnPendingVideoPublished (video: VideoModel): void { | ||
34 | // Only notify on public videos that has been published while the user waited transcoding/scheduled update | ||
35 | if (video.waitTranscoding === false && !video.ScheduleVideoUpdate) return | ||
36 | |||
37 | this.notifyOwnedVideoHasBeenPublished(video) | ||
38 | .catch(err => logger.error('Cannot notify owner that its video %s has been published.', video.url, { err })) | ||
39 | } | ||
40 | |||
41 | notifyOnNewComment (comment: VideoCommentModel): void { | ||
42 | this.notifyVideoOwnerOfNewComment(comment) | ||
43 | .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err })) | ||
44 | |||
45 | this.notifyOfCommentMention(comment) | ||
46 | .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err })) | ||
47 | } | ||
48 | |||
49 | notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void { | ||
50 | this.notifyModeratorsOfNewVideoAbuse(videoAbuse) | ||
51 | .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err })) | ||
52 | } | ||
53 | |||
54 | notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void { | ||
55 | this.notifyVideoOwnerOfBlacklist(videoBlacklist) | ||
56 | .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) | ||
57 | } | ||
58 | |||
59 | notifyOnVideoUnblacklist (video: VideoModel): void { | ||
60 | this.notifyVideoOwnerOfUnblacklist(video) | ||
61 | .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', video.url, { err })) | ||
62 | } | ||
63 | |||
64 | notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void { | ||
65 | this.notifyOwnerVideoImportIsFinished(videoImport, success) | ||
66 | .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err })) | ||
67 | } | ||
68 | |||
69 | notifyOnNewUserRegistration (user: UserModel): void { | ||
70 | this.notifyModeratorsOfNewUserRegistration(user) | ||
71 | .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) | ||
72 | } | ||
73 | |||
74 | notifyOfNewFollow (actorFollow: ActorFollowModel): void { | ||
75 | this.notifyUserOfNewActorFollow(actorFollow) | ||
76 | .catch(err => { | ||
77 | logger.error( | ||
78 | 'Cannot notify owner of channel %s of a new follow by %s.', | ||
79 | actorFollow.ActorFollowing.VideoChannel.getDisplayName(), | ||
80 | actorFollow.ActorFollower.Account.getDisplayName(), | ||
81 | err | ||
82 | ) | ||
83 | }) | ||
84 | } | ||
85 | |||
86 | private async notifySubscribersOfNewVideo (video: VideoModel) { | ||
87 | // List all followers that are users | ||
88 | const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) | ||
89 | |||
90 | logger.info('Notifying %d users of new video %s.', users.length, video.url) | ||
91 | |||
92 | function settingGetter (user: UserModel) { | ||
93 | return user.NotificationSetting.newVideoFromSubscription | ||
94 | } | ||
95 | |||
96 | async function notificationCreator (user: UserModel) { | ||
97 | const notification = await UserNotificationModel.create({ | ||
98 | type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, | ||
99 | userId: user.id, | ||
100 | videoId: video.id | ||
101 | }) | ||
102 | notification.Video = video | ||
103 | |||
104 | return notification | ||
105 | } | ||
106 | |||
107 | function emailSender (emails: string[]) { | ||
108 | return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video) | ||
109 | } | ||
110 | |||
111 | return this.notify({ users, settingGetter, notificationCreator, emailSender }) | ||
112 | } | ||
113 | |||
114 | private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) { | ||
115 | if (comment.Video.isOwned() === false) return | ||
116 | |||
117 | const user = await UserModel.loadByVideoId(comment.videoId) | ||
118 | |||
119 | // Not our user or user comments its own video | ||
120 | if (!user || comment.Account.userId === user.id) return | ||
121 | |||
122 | const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId) | ||
123 | if (accountMuted) return | ||
124 | |||
125 | logger.info('Notifying user %s of new comment %s.', user.username, comment.url) | ||
126 | |||
127 | function settingGetter (user: UserModel) { | ||
128 | return user.NotificationSetting.newCommentOnMyVideo | ||
129 | } | ||
130 | |||
131 | async function notificationCreator (user: UserModel) { | ||
132 | const notification = await UserNotificationModel.create({ | ||
133 | type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, | ||
134 | userId: user.id, | ||
135 | commentId: comment.id | ||
136 | }) | ||
137 | notification.Comment = comment | ||
138 | |||
139 | return notification | ||
140 | } | ||
141 | |||
142 | function emailSender (emails: string[]) { | ||
143 | return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment) | ||
144 | } | ||
145 | |||
146 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | ||
147 | } | ||
148 | |||
149 | private async notifyOfCommentMention (comment: VideoCommentModel) { | ||
150 | const usernames = comment.extractMentions() | ||
151 | let users = await UserModel.listByUsernames(usernames) | ||
152 | |||
153 | if (comment.Video.isOwned()) { | ||
154 | const userException = await UserModel.loadByVideoId(comment.videoId) | ||
155 | users = users.filter(u => u.id !== userException.id) | ||
156 | } | ||
157 | |||
158 | // Don't notify if I mentioned myself | ||
159 | users = users.filter(u => u.Account.id !== comment.accountId) | ||
160 | |||
161 | if (users.length === 0) return | ||
162 | |||
163 | const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId) | ||
164 | |||
165 | logger.info('Notifying %d users of new comment %s.', users.length, comment.url) | ||
166 | |||
167 | function settingGetter (user: UserModel) { | ||
168 | if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE | ||
169 | |||
170 | return user.NotificationSetting.commentMention | ||
171 | } | ||
172 | |||
173 | async function notificationCreator (user: UserModel) { | ||
174 | const notification = await UserNotificationModel.create({ | ||
175 | type: UserNotificationType.COMMENT_MENTION, | ||
176 | userId: user.id, | ||
177 | commentId: comment.id | ||
178 | }) | ||
179 | notification.Comment = comment | ||
180 | |||
181 | return notification | ||
182 | } | ||
183 | |||
184 | function emailSender (emails: string[]) { | ||
185 | return Emailer.Instance.addNewCommentMentionNotification(emails, comment) | ||
186 | } | ||
187 | |||
188 | return this.notify({ users, settingGetter, notificationCreator, emailSender }) | ||
189 | } | ||
190 | |||
191 | private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) { | ||
192 | if (actorFollow.ActorFollowing.isOwned() === false) return | ||
193 | |||
194 | // Account follows one of our account? | ||
195 | let followType: 'account' | 'channel' = 'channel' | ||
196 | let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id) | ||
197 | |||
198 | // Account follows one of our channel? | ||
199 | if (!user) { | ||
200 | user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id) | ||
201 | followType = 'account' | ||
202 | } | ||
203 | |||
204 | if (!user) return | ||
205 | |||
206 | if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) { | ||
207 | actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel | ||
208 | } | ||
209 | const followerAccount = actorFollow.ActorFollower.Account | ||
210 | |||
211 | const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id) | ||
212 | if (accountMuted) return | ||
213 | |||
214 | logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) | ||
215 | |||
216 | function settingGetter (user: UserModel) { | ||
217 | return user.NotificationSetting.newFollow | ||
218 | } | ||
219 | |||
220 | async function notificationCreator (user: UserModel) { | ||
221 | const notification = await UserNotificationModel.create({ | ||
222 | type: UserNotificationType.NEW_FOLLOW, | ||
223 | userId: user.id, | ||
224 | actorFollowId: actorFollow.id | ||
225 | }) | ||
226 | notification.ActorFollow = actorFollow | ||
227 | |||
228 | return notification | ||
229 | } | ||
230 | |||
231 | function emailSender (emails: string[]) { | ||
232 | return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType) | ||
233 | } | ||
234 | |||
235 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | ||
236 | } | ||
237 | |||
238 | private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) { | ||
239 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) | ||
240 | if (moderators.length === 0) return | ||
241 | |||
242 | logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url) | ||
243 | |||
244 | function settingGetter (user: UserModel) { | ||
245 | return user.NotificationSetting.videoAbuseAsModerator | ||
246 | } | ||
247 | |||
248 | async function notificationCreator (user: UserModel) { | ||
249 | const notification = await UserNotificationModel.create({ | ||
250 | type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS, | ||
251 | userId: user.id, | ||
252 | videoAbuseId: videoAbuse.id | ||
253 | }) | ||
254 | notification.VideoAbuse = videoAbuse | ||
255 | |||
256 | return notification | ||
257 | } | ||
258 | |||
259 | function emailSender (emails: string[]) { | ||
260 | return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse) | ||
261 | } | ||
262 | |||
263 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | ||
264 | } | ||
265 | |||
266 | private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) { | ||
267 | const user = await UserModel.loadByVideoId(videoBlacklist.videoId) | ||
268 | if (!user) return | ||
269 | |||
270 | logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url) | ||
271 | |||
272 | function settingGetter (user: UserModel) { | ||
273 | return user.NotificationSetting.blacklistOnMyVideo | ||
274 | } | ||
275 | |||
276 | async function notificationCreator (user: UserModel) { | ||
277 | const notification = await UserNotificationModel.create({ | ||
278 | type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, | ||
279 | userId: user.id, | ||
280 | videoBlacklistId: videoBlacklist.id | ||
281 | }) | ||
282 | notification.VideoBlacklist = videoBlacklist | ||
283 | |||
284 | return notification | ||
285 | } | ||
286 | |||
287 | function emailSender (emails: string[]) { | ||
288 | return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist) | ||
289 | } | ||
290 | |||
291 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | ||
292 | } | ||
293 | |||
294 | private async notifyVideoOwnerOfUnblacklist (video: VideoModel) { | ||
295 | const user = await UserModel.loadByVideoId(video.id) | ||
296 | if (!user) return | ||
297 | |||
298 | logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url) | ||
299 | |||
300 | function settingGetter (user: UserModel) { | ||
301 | return user.NotificationSetting.blacklistOnMyVideo | ||
302 | } | ||
303 | |||
304 | async function notificationCreator (user: UserModel) { | ||
305 | const notification = await UserNotificationModel.create({ | ||
306 | type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, | ||
307 | userId: user.id, | ||
308 | videoId: video.id | ||
309 | }) | ||
310 | notification.Video = video | ||
311 | |||
312 | return notification | ||
313 | } | ||
314 | |||
315 | function emailSender (emails: string[]) { | ||
316 | return Emailer.Instance.addVideoUnblacklistNotification(emails, video) | ||
317 | } | ||
318 | |||
319 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | ||
320 | } | ||
321 | |||
322 | private async notifyOwnedVideoHasBeenPublished (video: VideoModel) { | ||
323 | const user = await UserModel.loadByVideoId(video.id) | ||
324 | if (!user) return | ||
325 | |||
326 | logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url) | ||
327 | |||
328 | function settingGetter (user: UserModel) { | ||
329 | return user.NotificationSetting.myVideoPublished | ||
330 | } | ||
331 | |||
332 | async function notificationCreator (user: UserModel) { | ||
333 | const notification = await UserNotificationModel.create({ | ||
334 | type: UserNotificationType.MY_VIDEO_PUBLISHED, | ||
335 | userId: user.id, | ||
336 | videoId: video.id | ||
337 | }) | ||
338 | notification.Video = video | ||
339 | |||
340 | return notification | ||
341 | } | ||
342 | |||
343 | function emailSender (emails: string[]) { | ||
344 | return Emailer.Instance.myVideoPublishedNotification(emails, video) | ||
345 | } | ||
346 | |||
347 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | ||
348 | } | ||
349 | |||
350 | private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) { | ||
351 | const user = await UserModel.loadByVideoImportId(videoImport.id) | ||
352 | if (!user) return | ||
353 | |||
354 | logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier()) | ||
355 | |||
356 | function settingGetter (user: UserModel) { | ||
357 | return user.NotificationSetting.myVideoImportFinished | ||
358 | } | ||
359 | |||
360 | async function notificationCreator (user: UserModel) { | ||
361 | const notification = await UserNotificationModel.create({ | ||
362 | type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR, | ||
363 | userId: user.id, | ||
364 | videoImportId: videoImport.id | ||
365 | }) | ||
366 | notification.VideoImport = videoImport | ||
367 | |||
368 | return notification | ||
369 | } | ||
370 | |||
371 | function emailSender (emails: string[]) { | ||
372 | return success | ||
373 | ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport) | ||
374 | : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport) | ||
375 | } | ||
376 | |||
377 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | ||
378 | } | ||
379 | |||
380 | private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) { | ||
381 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) | ||
382 | if (moderators.length === 0) return | ||
383 | |||
384 | logger.info( | ||
385 | 'Notifying %s moderators of new user registration of %s.', | ||
386 | moderators.length, registeredUser.Account.Actor.preferredUsername | ||
387 | ) | ||
388 | |||
389 | function settingGetter (user: UserModel) { | ||
390 | return user.NotificationSetting.newUserRegistration | ||
391 | } | ||
392 | |||
393 | async function notificationCreator (user: UserModel) { | ||
394 | const notification = await UserNotificationModel.create({ | ||
395 | type: UserNotificationType.NEW_USER_REGISTRATION, | ||
396 | userId: user.id, | ||
397 | accountId: registeredUser.Account.id | ||
398 | }) | ||
399 | notification.Account = registeredUser.Account | ||
400 | |||
401 | return notification | ||
402 | } | ||
403 | |||
404 | function emailSender (emails: string[]) { | ||
405 | return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser) | ||
406 | } | ||
407 | |||
408 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | ||
409 | } | ||
410 | |||
411 | private async notify (options: { | ||
412 | users: UserModel[], | ||
413 | notificationCreator: (user: UserModel) => Promise<UserNotificationModel>, | ||
414 | emailSender: (emails: string[]) => Promise<any> | Bluebird<any>, | ||
415 | settingGetter: (user: UserModel) => UserNotificationSettingValue | ||
416 | }) { | ||
417 | const emails: string[] = [] | ||
418 | |||
419 | for (const user of options.users) { | ||
420 | if (this.isWebNotificationEnabled(options.settingGetter(user))) { | ||
421 | const notification = await options.notificationCreator(user) | ||
422 | |||
423 | PeerTubeSocket.Instance.sendNotification(user.id, notification) | ||
424 | } | ||
425 | |||
426 | if (this.isEmailEnabled(user, options.settingGetter(user))) { | ||
427 | emails.push(user.email) | ||
428 | } | ||
429 | } | ||
430 | |||
431 | if (emails.length !== 0) { | ||
432 | await options.emailSender(emails) | ||
433 | } | ||
434 | } | ||
435 | |||
436 | private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) { | ||
437 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified !== true) return false | ||
438 | |||
439 | return value & UserNotificationSettingValue.EMAIL | ||
440 | } | ||
441 | |||
442 | private isWebNotificationEnabled (value: UserNotificationSettingValue) { | ||
443 | return value & UserNotificationSettingValue.WEB | ||
444 | } | ||
445 | |||
446 | static get Instance () { | ||
447 | return this.instance || (this.instance = new this()) | ||
448 | } | ||
449 | } | ||
450 | |||
451 | // --------------------------------------------------------------------------- | ||
452 | |||
453 | export { | ||
454 | Notifier | ||
455 | } | ||