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