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