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