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