]>
Commit | Line | Data |
---|---|---|
cef534ed C |
1 | import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users' |
2 | import { logger } from '../helpers/logger' | |
cef534ed C |
3 | import { Emailer } from './emailer' |
4 | import { UserNotificationModel } from '../models/account/user-notification' | |
cef534ed C |
5 | import { UserModel } from '../models/account/user' |
6 | import { PeerTubeSocket } from './peertube-socket' | |
6dd9de95 | 7 | import { CONFIG } from '../initializers/config' |
cef534ed | 8 | import { VideoPrivacy, VideoState } from '../../shared/models/videos' |
dc133480 | 9 | import { AccountBlocklistModel } from '../models/account/account-blocklist' |
453e83ea C |
10 | import { |
11 | MCommentOwnerVideo, | |
453e83ea C |
12 | MVideoAbuseVideo, |
13 | MVideoAccountLight, | |
8424c402 | 14 | MVideoBlacklistLightVideo, |
453e83ea C |
15 | MVideoBlacklistVideo, |
16 | MVideoFullLight | |
17 | } from '../typings/models/video' | |
8424c402 | 18 | import { |
a1587156 C |
19 | MUser, |
20 | MUserAccount, | |
8424c402 C |
21 | MUserDefault, |
22 | MUserNotifSettingAccount, | |
23 | MUserWithNotificationSetting, | |
24 | UserNotificationModelForApi | |
25 | } from '@server/typings/models/user' | |
dddc8b1f | 26 | import { MAccountDefault, MActorFollowFull } from '../typings/models' |
453e83ea | 27 | import { MVideoImportVideo } from '@server/typings/models/video/video-import' |
dddc8b1f C |
28 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' |
29 | import { getServerActor } from '@server/helpers/utils' | |
cef534ed C |
30 | |
31 | class Notifier { | |
32 | ||
33 | private static instance: Notifier | |
34 | ||
a1587156 C |
35 | private constructor () { |
36 | } | |
cef534ed | 37 | |
453e83ea | 38 | notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void { |
7ccddd7b | 39 | // Only notify on public and published videos which are not blacklisted |
5b77537c | 40 | if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return |
cef534ed C |
41 | |
42 | this.notifySubscribersOfNewVideo(video) | |
a1587156 | 43 | .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) |
cef534ed C |
44 | } |
45 | ||
453e83ea | 46 | notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void { |
7ccddd7b JM |
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 | |
dc133480 C |
49 | |
50 | this.notifyOwnedVideoHasBeenPublished(video) | |
7ccddd7b JM |
51 | .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err })) |
52 | } | |
53 | ||
453e83ea | 54 | notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void { |
7ccddd7b JM |
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 | ||
453e83ea | 62 | notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void { |
7ccddd7b JM |
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) | |
a1587156 C |
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 | }) | |
dc133480 C |
70 | } |
71 | ||
453e83ea | 72 | notifyOnNewComment (comment: MCommentOwnerVideo): void { |
cef534ed | 73 | this.notifyVideoOwnerOfNewComment(comment) |
f7cc67b4 C |
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 })) | |
cef534ed C |
78 | } |
79 | ||
453e83ea | 80 | notifyOnNewVideoAbuse (videoAbuse: MVideoAbuseVideo): void { |
cef534ed | 81 | this.notifyModeratorsOfNewVideoAbuse(videoAbuse) |
a1587156 | 82 | .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err })) |
cef534ed C |
83 | } |
84 | ||
8424c402 C |
85 | notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void { |
86 | this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist) | |
a1587156 | 87 | .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err })) |
7ccddd7b JM |
88 | } |
89 | ||
453e83ea | 90 | notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void { |
cef534ed | 91 | this.notifyVideoOwnerOfBlacklist(videoBlacklist) |
a1587156 | 92 | .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) |
cef534ed C |
93 | } |
94 | ||
8424c402 | 95 | notifyOnVideoUnblacklist (video: MVideoFullLight): void { |
cef534ed | 96 | this.notifyVideoOwnerOfUnblacklist(video) |
7ccddd7b | 97 | .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err })) |
cef534ed C |
98 | } |
99 | ||
453e83ea | 100 | notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void { |
dc133480 | 101 | this.notifyOwnerVideoImportIsFinished(videoImport, success) |
a1587156 | 102 | .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err })) |
dc133480 C |
103 | } |
104 | ||
8424c402 | 105 | notifyOnNewUserRegistration (user: MUserDefault): void { |
f7cc67b4 C |
106 | this.notifyModeratorsOfNewUserRegistration(user) |
107 | .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) | |
108 | } | |
109 | ||
8424c402 | 110 | notifyOfNewUserFollow (actorFollow: MActorFollowFull): void { |
f7cc67b4 | 111 | this.notifyUserOfNewActorFollow(actorFollow) |
a1587156 C |
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 | }) | |
f7cc67b4 C |
120 | } |
121 | ||
8424c402 | 122 | notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void { |
883993c8 C |
123 | this.notifyAdminsOfNewInstanceFollow(actorFollow) |
124 | .catch(err => { | |
125 | logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) | |
126 | }) | |
127 | } | |
128 | ||
8424c402 C |
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 | ||
453e83ea | 136 | private async notifySubscribersOfNewVideo (video: MVideoAccountLight) { |
cef534ed C |
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 | ||
8424c402 | 142 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
143 | return user.NotificationSetting.newVideoFromSubscription |
144 | } | |
145 | ||
8424c402 C |
146 | async function notificationCreator (user: MUserWithNotificationSetting) { |
147 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
148 | type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, |
149 | userId: user.id, | |
150 | videoId: video.id | |
151 | }) | |
8424c402 | 152 | notification.Video = video |
cef534ed C |
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 | ||
453e83ea | 164 | private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) { |
f7cc67b4 C |
165 | if (comment.Video.isOwned() === false) return |
166 | ||
cef534ed C |
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 | ||
dddc8b1f | 172 | if (await this.isBlockedByServerOrAccount(user, comment.Account)) return |
dc133480 | 173 | |
cef534ed C |
174 | logger.info('Notifying user %s of new comment %s.', user.username, comment.url) |
175 | ||
8424c402 | 176 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
177 | return user.NotificationSetting.newCommentOnMyVideo |
178 | } | |
179 | ||
8424c402 C |
180 | async function notificationCreator (user: MUserWithNotificationSetting) { |
181 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
182 | type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, |
183 | userId: user.id, | |
184 | commentId: comment.id | |
185 | }) | |
8424c402 | 186 | notification.Comment = comment |
cef534ed C |
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 | ||
453e83ea | 198 | private async notifyOfCommentMention (comment: MCommentOwnerVideo) { |
41d71344 C |
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 | ) | |
1f6d57e3 | 204 | |
41d71344 | 205 | let users = await UserModel.listByUsernames(extractedUsernames) |
f7cc67b4 C |
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 | ||
cef534ed C |
215 | if (users.length === 0) return |
216 | ||
dddc8b1f C |
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) | |
f7cc67b4 C |
222 | |
223 | logger.info('Notifying %d users of new comment %s.', users.length, comment.url) | |
224 | ||
8424c402 | 225 | function settingGetter (user: MUserNotifSettingAccount) { |
dddc8b1f C |
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 | } | |
f7cc67b4 C |
233 | |
234 | return user.NotificationSetting.commentMention | |
235 | } | |
236 | ||
8424c402 C |
237 | async function notificationCreator (user: MUserNotifSettingAccount) { |
238 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
f7cc67b4 C |
239 | type: UserNotificationType.COMMENT_MENTION, |
240 | userId: user.id, | |
241 | commentId: comment.id | |
242 | }) | |
8424c402 | 243 | notification.Comment = comment |
f7cc67b4 C |
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 | ||
8424c402 | 255 | private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) { |
f7cc67b4 C |
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 | ||
f7cc67b4 | 270 | const followerAccount = actorFollow.ActorFollower.Account |
dddc8b1f | 271 | const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower }) |
f7cc67b4 | 272 | |
dddc8b1f | 273 | if (await this.isBlockedByServerOrAccount(user, followerAccountWithActor)) return |
f7cc67b4 C |
274 | |
275 | logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) | |
276 | ||
8424c402 | 277 | function settingGetter (user: MUserWithNotificationSetting) { |
f7cc67b4 C |
278 | return user.NotificationSetting.newFollow |
279 | } | |
280 | ||
8424c402 C |
281 | async function notificationCreator (user: MUserWithNotificationSetting) { |
282 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
f7cc67b4 C |
283 | type: UserNotificationType.NEW_FOLLOW, |
284 | userId: user.id, | |
285 | actorFollowId: actorFollow.id | |
286 | }) | |
8424c402 | 287 | notification.ActorFollow = actorFollow |
f7cc67b4 C |
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 | ||
8424c402 | 299 | private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) { |
883993c8 C |
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 | ||
8424c402 | 304 | function settingGetter (user: MUserWithNotificationSetting) { |
883993c8 C |
305 | return user.NotificationSetting.newInstanceFollower |
306 | } | |
307 | ||
8424c402 C |
308 | async function notificationCreator (user: MUserWithNotificationSetting) { |
309 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
883993c8 C |
310 | type: UserNotificationType.NEW_INSTANCE_FOLLOWER, |
311 | userId: user.id, | |
312 | actorFollowId: actorFollow.id | |
313 | }) | |
8424c402 | 314 | notification.ActorFollow = actorFollow |
883993c8 C |
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 | ||
8424c402 C |
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 | ||
453e83ea | 353 | private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) { |
f7cc67b4 C |
354 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) |
355 | if (moderators.length === 0) return | |
356 | ||
357 | logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url) | |
cef534ed | 358 | |
8424c402 | 359 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
360 | return user.NotificationSetting.videoAbuseAsModerator |
361 | } | |
362 | ||
8424c402 C |
363 | async function notificationCreator (user: MUserWithNotificationSetting) { |
364 | const notification: UserNotificationModelForApi = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
365 | type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS, |
366 | userId: user.id, | |
367 | videoAbuseId: videoAbuse.id | |
368 | }) | |
369 | notification.VideoAbuse = videoAbuse | |
370 | ||
371 | return notification | |
372 | } | |
373 | ||
374 | function emailSender (emails: string[]) { | |
375 | return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse) | |
376 | } | |
377 | ||
f7cc67b4 | 378 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) |
cef534ed C |
379 | } |
380 | ||
8424c402 | 381 | private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) { |
7ccddd7b JM |
382 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) |
383 | if (moderators.length === 0) return | |
384 | ||
8424c402 | 385 | logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url) |
7ccddd7b | 386 | |
8424c402 | 387 | function settingGetter (user: MUserWithNotificationSetting) { |
7ccddd7b JM |
388 | return user.NotificationSetting.videoAutoBlacklistAsModerator |
389 | } | |
7ccddd7b | 390 | |
8424c402 C |
391 | async function notificationCreator (user: MUserWithNotificationSetting) { |
392 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
7ccddd7b JM |
393 | type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, |
394 | userId: user.id, | |
8424c402 | 395 | videoBlacklistId: videoBlacklist.id |
7ccddd7b | 396 | }) |
8424c402 | 397 | notification.VideoBlacklist = videoBlacklist |
7ccddd7b JM |
398 | |
399 | return notification | |
400 | } | |
401 | ||
402 | function emailSender (emails: string[]) { | |
8424c402 | 403 | return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist) |
7ccddd7b JM |
404 | } |
405 | ||
406 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
407 | } | |
408 | ||
453e83ea | 409 | private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) { |
cef534ed C |
410 | const user = await UserModel.loadByVideoId(videoBlacklist.videoId) |
411 | if (!user) return | |
412 | ||
413 | logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url) | |
414 | ||
8424c402 | 415 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
416 | return user.NotificationSetting.blacklistOnMyVideo |
417 | } | |
418 | ||
8424c402 C |
419 | async function notificationCreator (user: MUserWithNotificationSetting) { |
420 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
421 | type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, |
422 | userId: user.id, | |
423 | videoBlacklistId: videoBlacklist.id | |
424 | }) | |
8424c402 | 425 | notification.VideoBlacklist = videoBlacklist |
cef534ed C |
426 | |
427 | return notification | |
428 | } | |
429 | ||
430 | function emailSender (emails: string[]) { | |
431 | return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist) | |
432 | } | |
433 | ||
434 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
435 | } | |
436 | ||
8424c402 | 437 | private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) { |
cef534ed C |
438 | const user = await UserModel.loadByVideoId(video.id) |
439 | if (!user) return | |
440 | ||
441 | logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url) | |
442 | ||
8424c402 | 443 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
444 | return user.NotificationSetting.blacklistOnMyVideo |
445 | } | |
446 | ||
8424c402 C |
447 | async function notificationCreator (user: MUserWithNotificationSetting) { |
448 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
449 | type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, |
450 | userId: user.id, | |
451 | videoId: video.id | |
452 | }) | |
8424c402 | 453 | notification.Video = video |
cef534ed C |
454 | |
455 | return notification | |
456 | } | |
457 | ||
458 | function emailSender (emails: string[]) { | |
459 | return Emailer.Instance.addVideoUnblacklistNotification(emails, video) | |
460 | } | |
461 | ||
462 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
463 | } | |
464 | ||
453e83ea | 465 | private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) { |
dc133480 C |
466 | const user = await UserModel.loadByVideoId(video.id) |
467 | if (!user) return | |
468 | ||
469 | logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url) | |
470 | ||
8424c402 | 471 | function settingGetter (user: MUserWithNotificationSetting) { |
dc133480 C |
472 | return user.NotificationSetting.myVideoPublished |
473 | } | |
474 | ||
8424c402 C |
475 | async function notificationCreator (user: MUserWithNotificationSetting) { |
476 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
dc133480 C |
477 | type: UserNotificationType.MY_VIDEO_PUBLISHED, |
478 | userId: user.id, | |
479 | videoId: video.id | |
480 | }) | |
8424c402 | 481 | notification.Video = video |
dc133480 C |
482 | |
483 | return notification | |
484 | } | |
485 | ||
486 | function emailSender (emails: string[]) { | |
487 | return Emailer.Instance.myVideoPublishedNotification(emails, video) | |
488 | } | |
489 | ||
490 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
491 | } | |
492 | ||
453e83ea | 493 | private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) { |
dc133480 C |
494 | const user = await UserModel.loadByVideoImportId(videoImport.id) |
495 | if (!user) return | |
496 | ||
497 | logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier()) | |
498 | ||
8424c402 | 499 | function settingGetter (user: MUserWithNotificationSetting) { |
dc133480 C |
500 | return user.NotificationSetting.myVideoImportFinished |
501 | } | |
502 | ||
8424c402 C |
503 | async function notificationCreator (user: MUserWithNotificationSetting) { |
504 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
dc133480 C |
505 | type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR, |
506 | userId: user.id, | |
507 | videoImportId: videoImport.id | |
508 | }) | |
8424c402 | 509 | notification.VideoImport = videoImport |
dc133480 C |
510 | |
511 | return notification | |
512 | } | |
513 | ||
514 | function emailSender (emails: string[]) { | |
515 | return success | |
516 | ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport) | |
517 | : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport) | |
518 | } | |
519 | ||
520 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
521 | } | |
522 | ||
8424c402 | 523 | private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) { |
f7cc67b4 C |
524 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) |
525 | if (moderators.length === 0) return | |
526 | ||
527 | logger.info( | |
528 | 'Notifying %s moderators of new user registration of %s.', | |
453e83ea | 529 | moderators.length, registeredUser.username |
f7cc67b4 C |
530 | ) |
531 | ||
8424c402 | 532 | function settingGetter (user: MUserWithNotificationSetting) { |
f7cc67b4 C |
533 | return user.NotificationSetting.newUserRegistration |
534 | } | |
535 | ||
8424c402 C |
536 | async function notificationCreator (user: MUserWithNotificationSetting) { |
537 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
f7cc67b4 C |
538 | type: UserNotificationType.NEW_USER_REGISTRATION, |
539 | userId: user.id, | |
540 | accountId: registeredUser.Account.id | |
541 | }) | |
8424c402 | 542 | notification.Account = registeredUser.Account |
f7cc67b4 C |
543 | |
544 | return notification | |
545 | } | |
546 | ||
547 | function emailSender (emails: string[]) { | |
548 | return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser) | |
549 | } | |
550 | ||
551 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
552 | } | |
553 | ||
a1587156 C |
554 | private async notify<T extends MUserWithNotificationSetting> (options: { |
555 | users: T[] | |
556 | notificationCreator: (user: T) => Promise<UserNotificationModelForApi> | |
557 | emailSender: (emails: string[]) => void | |
8424c402 | 558 | settingGetter: (user: T) => UserNotificationSettingValue |
cef534ed C |
559 | }) { |
560 | const emails: string[] = [] | |
561 | ||
562 | for (const user of options.users) { | |
563 | if (this.isWebNotificationEnabled(options.settingGetter(user))) { | |
564 | const notification = await options.notificationCreator(user) | |
565 | ||
566 | PeerTubeSocket.Instance.sendNotification(user.id, notification) | |
567 | } | |
568 | ||
569 | if (this.isEmailEnabled(user, options.settingGetter(user))) { | |
570 | emails.push(user.email) | |
571 | } | |
572 | } | |
573 | ||
574 | if (emails.length !== 0) { | |
a1587156 | 575 | options.emailSender(emails) |
cef534ed C |
576 | } |
577 | } | |
578 | ||
453e83ea | 579 | private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) { |
1ed9b8ee | 580 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false |
cef534ed | 581 | |
2f1548fd | 582 | return value & UserNotificationSettingValue.EMAIL |
cef534ed C |
583 | } |
584 | ||
585 | private isWebNotificationEnabled (value: UserNotificationSettingValue) { | |
2f1548fd | 586 | return value & UserNotificationSettingValue.WEB |
cef534ed C |
587 | } |
588 | ||
dddc8b1f C |
589 | private async isBlockedByServerOrAccount (user: MUserAccount, targetAccount: MAccountDefault) { |
590 | const serverAccountId = (await getServerActor()).Account.id | |
591 | const sourceAccounts = [ serverAccountId, user.Account.id ] | |
592 | ||
593 | const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, targetAccount.id) | |
594 | if (accountMutedHash[serverAccountId] || accountMutedHash[user.Account.id]) return true | |
595 | ||
596 | const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, targetAccount.Actor.serverId) | |
597 | if (instanceMutedHash[serverAccountId] || instanceMutedHash[user.Account.id]) return true | |
598 | ||
599 | return false | |
600 | } | |
601 | ||
cef534ed C |
602 | static get Instance () { |
603 | return this.instance || (this.instance = new this()) | |
604 | } | |
605 | } | |
606 | ||
607 | // --------------------------------------------------------------------------- | |
608 | ||
609 | export { | |
610 | Notifier | |
611 | } |