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