]>
Commit | Line | Data |
---|---|---|
1 | import { AccountModel } from '@server/models/account/account' | |
2 | import { getServerActor } from '@server/models/application/application' | |
3 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' | |
4 | import { | |
5 | MUser, | |
6 | MUserAccount, | |
7 | MUserDefault, | |
8 | MUserNotifSettingAccount, | |
9 | MUserWithNotificationSetting, | |
10 | UserNotificationModelForApi | |
11 | } from '@server/types/models/user' | |
12 | import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist' | |
13 | import { MVideoImportVideo } from '@server/types/models/video/video-import' | |
14 | import { UserAbuse } from '@shared/models' | |
15 | import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users' | |
16 | import { VideoPrivacy, VideoState } from '../../shared/models/videos' | |
17 | import { logger } from '../helpers/logger' | |
18 | import { CONFIG } from '../initializers/config' | |
19 | import { AccountBlocklistModel } from '../models/account/account-blocklist' | |
20 | import { UserModel } from '../models/account/user' | |
21 | import { UserNotificationModel } from '../models/account/user-notification' | |
22 | import { MAbuseFull, MAbuseMessage, MAccountServer, MActorFollowFull } from '../types/models' | |
23 | import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../types/models/video' | |
24 | import { isBlockedByServerOrAccount } from './blocklist' | |
25 | import { Emailer } from './emailer' | |
26 | import { PeerTubeSocket } from './peertube-socket' | |
27 | ||
28 | class Notifier { | |
29 | ||
30 | private static instance: Notifier | |
31 | ||
32 | private constructor () { | |
33 | } | |
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 => { | |
65 | logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err }) | |
66 | }) | |
67 | } | |
68 | ||
69 | notifyOnNewComment (comment: MCommentOwnerVideo): void { | |
70 | this.notifyVideoOwnerOfNewComment(comment) | |
71 | .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err })) | |
72 | ||
73 | this.notifyOfCommentMention(comment) | |
74 | .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err })) | |
75 | } | |
76 | ||
77 | notifyOnNewAbuse (parameters: { abuse: UserAbuse, abuseInstance: MAbuseFull, reporter: string }): void { | |
78 | this.notifyModeratorsOfNewAbuse(parameters) | |
79 | .catch(err => logger.error('Cannot notify of new abuse %d.', parameters.abuseInstance.id, { err })) | |
80 | } | |
81 | ||
82 | notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void { | |
83 | this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist) | |
84 | .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err })) | |
85 | } | |
86 | ||
87 | notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void { | |
88 | this.notifyVideoOwnerOfBlacklist(videoBlacklist) | |
89 | .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) | |
90 | } | |
91 | ||
92 | notifyOnVideoUnblacklist (video: MVideoFullLight): void { | |
93 | this.notifyVideoOwnerOfUnblacklist(video) | |
94 | .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err })) | |
95 | } | |
96 | ||
97 | notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void { | |
98 | this.notifyOwnerVideoImportIsFinished(videoImport, success) | |
99 | .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err })) | |
100 | } | |
101 | ||
102 | notifyOnNewUserRegistration (user: MUserDefault): void { | |
103 | this.notifyModeratorsOfNewUserRegistration(user) | |
104 | .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) | |
105 | } | |
106 | ||
107 | notifyOfNewUserFollow (actorFollow: MActorFollowFull): void { | |
108 | this.notifyUserOfNewActorFollow(actorFollow) | |
109 | .catch(err => { | |
110 | logger.error( | |
111 | 'Cannot notify owner of channel %s of a new follow by %s.', | |
112 | actorFollow.ActorFollowing.VideoChannel.getDisplayName(), | |
113 | actorFollow.ActorFollower.Account.getDisplayName(), | |
114 | { err } | |
115 | ) | |
116 | }) | |
117 | } | |
118 | ||
119 | notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void { | |
120 | this.notifyAdminsOfNewInstanceFollow(actorFollow) | |
121 | .catch(err => { | |
122 | logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) | |
123 | }) | |
124 | } | |
125 | ||
126 | notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void { | |
127 | this.notifyAdminsOfAutoInstanceFollowing(actorFollow) | |
128 | .catch(err => { | |
129 | logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err }) | |
130 | }) | |
131 | } | |
132 | ||
133 | notifyOnAbuseStateChange (abuse: MAbuseFull): void { | |
134 | this.notifyReporterOfAbuseStateChange(abuse) | |
135 | .catch(err => { | |
136 | logger.error('Cannot notify reporter of abuse %d state change.', abuse.id, { err }) | |
137 | }) | |
138 | } | |
139 | ||
140 | notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void { | |
141 | this.notifyOfNewAbuseMessage(abuse, message) | |
142 | .catch(err => { | |
143 | logger.error('Cannot notify on new abuse %d message.', abuse.id, { err }) | |
144 | }) | |
145 | } | |
146 | ||
147 | private async notifySubscribersOfNewVideo (video: MVideoAccountLight) { | |
148 | // List all followers that are users | |
149 | const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) | |
150 | ||
151 | logger.info('Notifying %d users of new video %s.', users.length, video.url) | |
152 | ||
153 | function settingGetter (user: MUserWithNotificationSetting) { | |
154 | return user.NotificationSetting.newVideoFromSubscription | |
155 | } | |
156 | ||
157 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
158 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
159 | type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, | |
160 | userId: user.id, | |
161 | videoId: video.id | |
162 | }) | |
163 | notification.Video = video | |
164 | ||
165 | return notification | |
166 | } | |
167 | ||
168 | function emailSender (emails: string[]) { | |
169 | return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video) | |
170 | } | |
171 | ||
172 | return this.notify({ users, settingGetter, notificationCreator, emailSender }) | |
173 | } | |
174 | ||
175 | private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) { | |
176 | if (comment.Video.isOwned() === false) return | |
177 | ||
178 | const user = await UserModel.loadByVideoId(comment.videoId) | |
179 | ||
180 | // Not our user or user comments its own video | |
181 | if (!user || comment.Account.userId === user.id) return | |
182 | ||
183 | if (await this.isBlockedByServerOrUser(comment.Account, user)) return | |
184 | ||
185 | logger.info('Notifying user %s of new comment %s.', user.username, comment.url) | |
186 | ||
187 | function settingGetter (user: MUserWithNotificationSetting) { | |
188 | return user.NotificationSetting.newCommentOnMyVideo | |
189 | } | |
190 | ||
191 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
192 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
193 | type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, | |
194 | userId: user.id, | |
195 | commentId: comment.id | |
196 | }) | |
197 | notification.Comment = comment | |
198 | ||
199 | return notification | |
200 | } | |
201 | ||
202 | function emailSender (emails: string[]) { | |
203 | return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment) | |
204 | } | |
205 | ||
206 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
207 | } | |
208 | ||
209 | private async notifyOfCommentMention (comment: MCommentOwnerVideo) { | |
210 | const extractedUsernames = comment.extractMentions() | |
211 | logger.debug( | |
212 | 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url, | |
213 | { usernames: extractedUsernames, text: comment.text } | |
214 | ) | |
215 | ||
216 | let users = await UserModel.listByUsernames(extractedUsernames) | |
217 | ||
218 | if (comment.Video.isOwned()) { | |
219 | const userException = await UserModel.loadByVideoId(comment.videoId) | |
220 | users = users.filter(u => u.id !== userException.id) | |
221 | } | |
222 | ||
223 | // Don't notify if I mentioned myself | |
224 | users = users.filter(u => u.Account.id !== comment.accountId) | |
225 | ||
226 | if (users.length === 0) return | |
227 | ||
228 | const serverAccountId = (await getServerActor()).Account.id | |
229 | const sourceAccounts = users.map(u => u.Account.id).concat([ serverAccountId ]) | |
230 | ||
231 | const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, comment.accountId) | |
232 | const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, comment.Account.Actor.serverId) | |
233 | ||
234 | logger.info('Notifying %d users of new comment %s.', users.length, comment.url) | |
235 | ||
236 | function settingGetter (user: MUserNotifSettingAccount) { | |
237 | const accountId = user.Account.id | |
238 | if ( | |
239 | accountMutedHash[accountId] === true || instanceMutedHash[accountId] === true || | |
240 | accountMutedHash[serverAccountId] === true || instanceMutedHash[serverAccountId] === true | |
241 | ) { | |
242 | return UserNotificationSettingValue.NONE | |
243 | } | |
244 | ||
245 | return user.NotificationSetting.commentMention | |
246 | } | |
247 | ||
248 | async function notificationCreator (user: MUserNotifSettingAccount) { | |
249 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
250 | type: UserNotificationType.COMMENT_MENTION, | |
251 | userId: user.id, | |
252 | commentId: comment.id | |
253 | }) | |
254 | notification.Comment = comment | |
255 | ||
256 | return notification | |
257 | } | |
258 | ||
259 | function emailSender (emails: string[]) { | |
260 | return Emailer.Instance.addNewCommentMentionNotification(emails, comment) | |
261 | } | |
262 | ||
263 | return this.notify({ users, settingGetter, notificationCreator, emailSender }) | |
264 | } | |
265 | ||
266 | private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) { | |
267 | if (actorFollow.ActorFollowing.isOwned() === false) return | |
268 | ||
269 | // Account follows one of our account? | |
270 | let followType: 'account' | 'channel' = 'channel' | |
271 | let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id) | |
272 | ||
273 | // Account follows one of our channel? | |
274 | if (!user) { | |
275 | user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id) | |
276 | followType = 'account' | |
277 | } | |
278 | ||
279 | if (!user) return | |
280 | ||
281 | const followerAccount = actorFollow.ActorFollower.Account | |
282 | const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower }) | |
283 | ||
284 | if (await this.isBlockedByServerOrUser(followerAccountWithActor, user)) return | |
285 | ||
286 | logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) | |
287 | ||
288 | function settingGetter (user: MUserWithNotificationSetting) { | |
289 | return user.NotificationSetting.newFollow | |
290 | } | |
291 | ||
292 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
293 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
294 | type: UserNotificationType.NEW_FOLLOW, | |
295 | userId: user.id, | |
296 | actorFollowId: actorFollow.id | |
297 | }) | |
298 | notification.ActorFollow = actorFollow | |
299 | ||
300 | return notification | |
301 | } | |
302 | ||
303 | function emailSender (emails: string[]) { | |
304 | return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType) | |
305 | } | |
306 | ||
307 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
308 | } | |
309 | ||
310 | private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) { | |
311 | const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) | |
312 | ||
313 | const follower = Object.assign(actorFollow.ActorFollower.Account, { Actor: actorFollow.ActorFollower }) | |
314 | if (await this.isBlockedByServerOrUser(follower)) return | |
315 | ||
316 | logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url) | |
317 | ||
318 | function settingGetter (user: MUserWithNotificationSetting) { | |
319 | return user.NotificationSetting.newInstanceFollower | |
320 | } | |
321 | ||
322 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
323 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
324 | type: UserNotificationType.NEW_INSTANCE_FOLLOWER, | |
325 | userId: user.id, | |
326 | actorFollowId: actorFollow.id | |
327 | }) | |
328 | notification.ActorFollow = actorFollow | |
329 | ||
330 | return notification | |
331 | } | |
332 | ||
333 | function emailSender (emails: string[]) { | |
334 | return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow) | |
335 | } | |
336 | ||
337 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) | |
338 | } | |
339 | ||
340 | private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) { | |
341 | const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) | |
342 | ||
343 | logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url) | |
344 | ||
345 | function settingGetter (user: MUserWithNotificationSetting) { | |
346 | return user.NotificationSetting.autoInstanceFollowing | |
347 | } | |
348 | ||
349 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
350 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
351 | type: UserNotificationType.AUTO_INSTANCE_FOLLOWING, | |
352 | userId: user.id, | |
353 | actorFollowId: actorFollow.id | |
354 | }) | |
355 | notification.ActorFollow = actorFollow | |
356 | ||
357 | return notification | |
358 | } | |
359 | ||
360 | function emailSender (emails: string[]) { | |
361 | return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow) | |
362 | } | |
363 | ||
364 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) | |
365 | } | |
366 | ||
367 | private async notifyModeratorsOfNewAbuse (parameters: { | |
368 | abuse: UserAbuse | |
369 | abuseInstance: MAbuseFull | |
370 | reporter: string | |
371 | }) { | |
372 | const { abuse, abuseInstance } = parameters | |
373 | ||
374 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES) | |
375 | if (moderators.length === 0) return | |
376 | ||
377 | const url = this.getAbuseUrl(abuseInstance) | |
378 | ||
379 | logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url) | |
380 | ||
381 | function settingGetter (user: MUserWithNotificationSetting) { | |
382 | return user.NotificationSetting.abuseAsModerator | |
383 | } | |
384 | ||
385 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
386 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
387 | type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS, | |
388 | userId: user.id, | |
389 | abuseId: abuse.id | |
390 | }) | |
391 | notification.Abuse = abuseInstance | |
392 | ||
393 | return notification | |
394 | } | |
395 | ||
396 | function emailSender (emails: string[]) { | |
397 | return Emailer.Instance.addAbuseModeratorsNotification(emails, parameters) | |
398 | } | |
399 | ||
400 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
401 | } | |
402 | ||
403 | private async notifyReporterOfAbuseStateChange (abuse: MAbuseFull) { | |
404 | // Only notify our users | |
405 | if (abuse.ReporterAccount.isOwned() !== true) return | |
406 | ||
407 | const url = this.getAbuseUrl(abuse) | |
408 | ||
409 | logger.info('Notifying reporter of abuse % of state change.', url) | |
410 | ||
411 | const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId) | |
412 | ||
413 | function settingGetter (user: MUserWithNotificationSetting) { | |
414 | return user.NotificationSetting.abuseStateChange | |
415 | } | |
416 | ||
417 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
418 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
419 | type: UserNotificationType.ABUSE_STATE_CHANGE, | |
420 | userId: user.id, | |
421 | abuseId: abuse.id | |
422 | }) | |
423 | notification.Abuse = abuse | |
424 | ||
425 | return notification | |
426 | } | |
427 | ||
428 | function emailSender (emails: string[]) { | |
429 | return Emailer.Instance.addAbuseStateChangeNotification(emails, abuse) | |
430 | } | |
431 | ||
432 | return this.notify({ users: [ reporter ], settingGetter, notificationCreator, emailSender }) | |
433 | } | |
434 | ||
435 | private async notifyOfNewAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage) { | |
436 | const url = this.getAbuseUrl(abuse) | |
437 | logger.info('Notifying reporter and moderators of new abuse message on %s.', url) | |
438 | ||
439 | const accountMessage = await AccountModel.load(message.accountId) | |
440 | ||
441 | function settingGetter (user: MUserWithNotificationSetting) { | |
442 | return user.NotificationSetting.abuseNewMessage | |
443 | } | |
444 | ||
445 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
446 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
447 | type: UserNotificationType.ABUSE_NEW_MESSAGE, | |
448 | userId: user.id, | |
449 | abuseId: abuse.id | |
450 | }) | |
451 | notification.Abuse = abuse | |
452 | ||
453 | return notification | |
454 | } | |
455 | ||
456 | function emailSenderReporter (emails: string[]) { | |
457 | return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message, accountMessage }) | |
458 | } | |
459 | ||
460 | function emailSenderModerators (emails: string[]) { | |
461 | return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message, accountMessage }) | |
462 | } | |
463 | ||
464 | async function buildReporterOptions () { | |
465 | // Only notify our users | |
466 | if (abuse.ReporterAccount.isOwned() !== true) return undefined | |
467 | ||
468 | const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId) | |
469 | // Don't notify my own message | |
470 | if (reporter.Account.id === message.accountId) return undefined | |
471 | ||
472 | return { users: [ reporter ], settingGetter, notificationCreator, emailSender: emailSenderReporter } | |
473 | } | |
474 | ||
475 | async function buildModeratorsOptions () { | |
476 | let moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES) | |
477 | // Don't notify my own message | |
478 | moderators = moderators.filter(m => m.Account.id !== message.accountId) | |
479 | ||
480 | if (moderators.length === 0) return undefined | |
481 | ||
482 | return { users: moderators, settingGetter, notificationCreator, emailSender: emailSenderModerators } | |
483 | } | |
484 | ||
485 | const options = await Promise.all([ | |
486 | buildReporterOptions(), | |
487 | buildModeratorsOptions() | |
488 | ]) | |
489 | ||
490 | return Promise.all( | |
491 | options | |
492 | .filter(opt => !!opt) | |
493 | .map(opt => this.notify(opt)) | |
494 | ) | |
495 | } | |
496 | ||
497 | private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) { | |
498 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) | |
499 | if (moderators.length === 0) return | |
500 | ||
501 | logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url) | |
502 | ||
503 | function settingGetter (user: MUserWithNotificationSetting) { | |
504 | return user.NotificationSetting.videoAutoBlacklistAsModerator | |
505 | } | |
506 | ||
507 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
508 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
509 | type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, | |
510 | userId: user.id, | |
511 | videoBlacklistId: videoBlacklist.id | |
512 | }) | |
513 | notification.VideoBlacklist = videoBlacklist | |
514 | ||
515 | return notification | |
516 | } | |
517 | ||
518 | function emailSender (emails: string[]) { | |
519 | return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist) | |
520 | } | |
521 | ||
522 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
523 | } | |
524 | ||
525 | private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) { | |
526 | const user = await UserModel.loadByVideoId(videoBlacklist.videoId) | |
527 | if (!user) return | |
528 | ||
529 | logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url) | |
530 | ||
531 | function settingGetter (user: MUserWithNotificationSetting) { | |
532 | return user.NotificationSetting.blacklistOnMyVideo | |
533 | } | |
534 | ||
535 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
536 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
537 | type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, | |
538 | userId: user.id, | |
539 | videoBlacklistId: videoBlacklist.id | |
540 | }) | |
541 | notification.VideoBlacklist = videoBlacklist | |
542 | ||
543 | return notification | |
544 | } | |
545 | ||
546 | function emailSender (emails: string[]) { | |
547 | return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist) | |
548 | } | |
549 | ||
550 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
551 | } | |
552 | ||
553 | private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) { | |
554 | const user = await UserModel.loadByVideoId(video.id) | |
555 | if (!user) return | |
556 | ||
557 | logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url) | |
558 | ||
559 | function settingGetter (user: MUserWithNotificationSetting) { | |
560 | return user.NotificationSetting.blacklistOnMyVideo | |
561 | } | |
562 | ||
563 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
564 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
565 | type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, | |
566 | userId: user.id, | |
567 | videoId: video.id | |
568 | }) | |
569 | notification.Video = video | |
570 | ||
571 | return notification | |
572 | } | |
573 | ||
574 | function emailSender (emails: string[]) { | |
575 | return Emailer.Instance.addVideoUnblacklistNotification(emails, video) | |
576 | } | |
577 | ||
578 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
579 | } | |
580 | ||
581 | private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) { | |
582 | const user = await UserModel.loadByVideoId(video.id) | |
583 | if (!user) return | |
584 | ||
585 | logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url) | |
586 | ||
587 | function settingGetter (user: MUserWithNotificationSetting) { | |
588 | return user.NotificationSetting.myVideoPublished | |
589 | } | |
590 | ||
591 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
592 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
593 | type: UserNotificationType.MY_VIDEO_PUBLISHED, | |
594 | userId: user.id, | |
595 | videoId: video.id | |
596 | }) | |
597 | notification.Video = video | |
598 | ||
599 | return notification | |
600 | } | |
601 | ||
602 | function emailSender (emails: string[]) { | |
603 | return Emailer.Instance.myVideoPublishedNotification(emails, video) | |
604 | } | |
605 | ||
606 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
607 | } | |
608 | ||
609 | private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) { | |
610 | const user = await UserModel.loadByVideoImportId(videoImport.id) | |
611 | if (!user) return | |
612 | ||
613 | logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier()) | |
614 | ||
615 | function settingGetter (user: MUserWithNotificationSetting) { | |
616 | return user.NotificationSetting.myVideoImportFinished | |
617 | } | |
618 | ||
619 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
620 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
621 | type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR, | |
622 | userId: user.id, | |
623 | videoImportId: videoImport.id | |
624 | }) | |
625 | notification.VideoImport = videoImport | |
626 | ||
627 | return notification | |
628 | } | |
629 | ||
630 | function emailSender (emails: string[]) { | |
631 | return success | |
632 | ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport) | |
633 | : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport) | |
634 | } | |
635 | ||
636 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
637 | } | |
638 | ||
639 | private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) { | |
640 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) | |
641 | if (moderators.length === 0) return | |
642 | ||
643 | logger.info( | |
644 | 'Notifying %s moderators of new user registration of %s.', | |
645 | moderators.length, registeredUser.username | |
646 | ) | |
647 | ||
648 | function settingGetter (user: MUserWithNotificationSetting) { | |
649 | return user.NotificationSetting.newUserRegistration | |
650 | } | |
651 | ||
652 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
653 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
654 | type: UserNotificationType.NEW_USER_REGISTRATION, | |
655 | userId: user.id, | |
656 | accountId: registeredUser.Account.id | |
657 | }) | |
658 | notification.Account = registeredUser.Account | |
659 | ||
660 | return notification | |
661 | } | |
662 | ||
663 | function emailSender (emails: string[]) { | |
664 | return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser) | |
665 | } | |
666 | ||
667 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
668 | } | |
669 | ||
670 | private async notify<T extends MUserWithNotificationSetting> (options: { | |
671 | users: T[] | |
672 | notificationCreator: (user: T) => Promise<UserNotificationModelForApi> | |
673 | emailSender: (emails: string[]) => void | |
674 | settingGetter: (user: T) => UserNotificationSettingValue | |
675 | }) { | |
676 | const emails: string[] = [] | |
677 | ||
678 | for (const user of options.users) { | |
679 | if (this.isWebNotificationEnabled(options.settingGetter(user))) { | |
680 | const notification = await options.notificationCreator(user) | |
681 | ||
682 | PeerTubeSocket.Instance.sendNotification(user.id, notification) | |
683 | } | |
684 | ||
685 | if (this.isEmailEnabled(user, options.settingGetter(user))) { | |
686 | emails.push(user.email) | |
687 | } | |
688 | } | |
689 | ||
690 | if (emails.length !== 0) { | |
691 | options.emailSender(emails) | |
692 | } | |
693 | } | |
694 | ||
695 | private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) { | |
696 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false | |
697 | ||
698 | return value & UserNotificationSettingValue.EMAIL | |
699 | } | |
700 | ||
701 | private isWebNotificationEnabled (value: UserNotificationSettingValue) { | |
702 | return value & UserNotificationSettingValue.WEB | |
703 | } | |
704 | ||
705 | private isBlockedByServerOrUser (targetAccount: MAccountServer, user?: MUserAccount) { | |
706 | return isBlockedByServerOrAccount(targetAccount, user?.Account) | |
707 | } | |
708 | ||
709 | private getAbuseUrl (abuse: MAbuseFull) { | |
710 | return abuse.VideoAbuse?.Video?.url || | |
711 | abuse.VideoCommentAbuse?.VideoComment?.url || | |
712 | abuse.FlaggedAccount.Actor.url | |
713 | } | |
714 | ||
715 | static get Instance () { | |
716 | return this.instance || (this.instance = new this()) | |
717 | } | |
718 | } | |
719 | ||
720 | // --------------------------------------------------------------------------- | |
721 | ||
722 | export { | |
723 | Notifier | |
724 | } |