]>
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' | |
d95d1559 C |
21 | import { MAbuseFull, MAbuseVideo, MAccountServer, MActorFollowFull } from '../types/models' |
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 | ||
d95d1559 C |
362 | const url = abuseInstance.VideoAbuse?.Video?.url || abuseInstance.VideoCommentAbuse?.VideoComment?.url |
363 | ||
364 | logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url) | |
cef534ed | 365 | |
8424c402 | 366 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
367 | return user.NotificationSetting.videoAbuseAsModerator |
368 | } | |
369 | ||
8424c402 | 370 | async function notificationCreator (user: MUserWithNotificationSetting) { |
d95d1559 | 371 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
cef534ed C |
372 | type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS, |
373 | userId: user.id, | |
d95d1559 | 374 | abuseId: abuse.id |
cef534ed | 375 | }) |
d95d1559 | 376 | notification.Abuse = abuseInstance |
cef534ed C |
377 | |
378 | return notification | |
379 | } | |
380 | ||
381 | function emailSender (emails: string[]) { | |
d95d1559 | 382 | return Emailer.Instance.addAbuseModeratorsNotification(emails, parameters) |
cef534ed C |
383 | } |
384 | ||
f7cc67b4 | 385 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) |
cef534ed C |
386 | } |
387 | ||
8424c402 | 388 | private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) { |
7ccddd7b JM |
389 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) |
390 | if (moderators.length === 0) return | |
391 | ||
8424c402 | 392 | logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url) |
7ccddd7b | 393 | |
8424c402 | 394 | function settingGetter (user: MUserWithNotificationSetting) { |
7ccddd7b JM |
395 | return user.NotificationSetting.videoAutoBlacklistAsModerator |
396 | } | |
7ccddd7b | 397 | |
8424c402 C |
398 | async function notificationCreator (user: MUserWithNotificationSetting) { |
399 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
7ccddd7b JM |
400 | type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, |
401 | userId: user.id, | |
8424c402 | 402 | videoBlacklistId: videoBlacklist.id |
7ccddd7b | 403 | }) |
8424c402 | 404 | notification.VideoBlacklist = videoBlacklist |
7ccddd7b JM |
405 | |
406 | return notification | |
407 | } | |
408 | ||
409 | function emailSender (emails: string[]) { | |
8424c402 | 410 | return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist) |
7ccddd7b JM |
411 | } |
412 | ||
413 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
414 | } | |
415 | ||
453e83ea | 416 | private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) { |
cef534ed C |
417 | const user = await UserModel.loadByVideoId(videoBlacklist.videoId) |
418 | if (!user) return | |
419 | ||
420 | logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url) | |
421 | ||
8424c402 | 422 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
423 | return user.NotificationSetting.blacklistOnMyVideo |
424 | } | |
425 | ||
8424c402 C |
426 | async function notificationCreator (user: MUserWithNotificationSetting) { |
427 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
428 | type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, |
429 | userId: user.id, | |
430 | videoBlacklistId: videoBlacklist.id | |
431 | }) | |
8424c402 | 432 | notification.VideoBlacklist = videoBlacklist |
cef534ed C |
433 | |
434 | return notification | |
435 | } | |
436 | ||
437 | function emailSender (emails: string[]) { | |
438 | return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist) | |
439 | } | |
440 | ||
441 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
442 | } | |
443 | ||
8424c402 | 444 | private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) { |
cef534ed C |
445 | const user = await UserModel.loadByVideoId(video.id) |
446 | if (!user) return | |
447 | ||
448 | logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url) | |
449 | ||
8424c402 | 450 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
451 | return user.NotificationSetting.blacklistOnMyVideo |
452 | } | |
453 | ||
8424c402 C |
454 | async function notificationCreator (user: MUserWithNotificationSetting) { |
455 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
456 | type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, |
457 | userId: user.id, | |
458 | videoId: video.id | |
459 | }) | |
8424c402 | 460 | notification.Video = video |
cef534ed C |
461 | |
462 | return notification | |
463 | } | |
464 | ||
465 | function emailSender (emails: string[]) { | |
466 | return Emailer.Instance.addVideoUnblacklistNotification(emails, video) | |
467 | } | |
468 | ||
469 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
470 | } | |
471 | ||
453e83ea | 472 | private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) { |
dc133480 C |
473 | const user = await UserModel.loadByVideoId(video.id) |
474 | if (!user) return | |
475 | ||
476 | logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url) | |
477 | ||
8424c402 | 478 | function settingGetter (user: MUserWithNotificationSetting) { |
dc133480 C |
479 | return user.NotificationSetting.myVideoPublished |
480 | } | |
481 | ||
8424c402 C |
482 | async function notificationCreator (user: MUserWithNotificationSetting) { |
483 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
dc133480 C |
484 | type: UserNotificationType.MY_VIDEO_PUBLISHED, |
485 | userId: user.id, | |
486 | videoId: video.id | |
487 | }) | |
8424c402 | 488 | notification.Video = video |
dc133480 C |
489 | |
490 | return notification | |
491 | } | |
492 | ||
493 | function emailSender (emails: string[]) { | |
494 | return Emailer.Instance.myVideoPublishedNotification(emails, video) | |
495 | } | |
496 | ||
497 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
498 | } | |
499 | ||
453e83ea | 500 | private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) { |
dc133480 C |
501 | const user = await UserModel.loadByVideoImportId(videoImport.id) |
502 | if (!user) return | |
503 | ||
504 | logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier()) | |
505 | ||
8424c402 | 506 | function settingGetter (user: MUserWithNotificationSetting) { |
dc133480 C |
507 | return user.NotificationSetting.myVideoImportFinished |
508 | } | |
509 | ||
8424c402 C |
510 | async function notificationCreator (user: MUserWithNotificationSetting) { |
511 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
dc133480 C |
512 | type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR, |
513 | userId: user.id, | |
514 | videoImportId: videoImport.id | |
515 | }) | |
8424c402 | 516 | notification.VideoImport = videoImport |
dc133480 C |
517 | |
518 | return notification | |
519 | } | |
520 | ||
521 | function emailSender (emails: string[]) { | |
522 | return success | |
523 | ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport) | |
524 | : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport) | |
525 | } | |
526 | ||
527 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
528 | } | |
529 | ||
8424c402 | 530 | private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) { |
f7cc67b4 C |
531 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) |
532 | if (moderators.length === 0) return | |
533 | ||
534 | logger.info( | |
535 | 'Notifying %s moderators of new user registration of %s.', | |
453e83ea | 536 | moderators.length, registeredUser.username |
f7cc67b4 C |
537 | ) |
538 | ||
8424c402 | 539 | function settingGetter (user: MUserWithNotificationSetting) { |
f7cc67b4 C |
540 | return user.NotificationSetting.newUserRegistration |
541 | } | |
542 | ||
8424c402 C |
543 | async function notificationCreator (user: MUserWithNotificationSetting) { |
544 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
f7cc67b4 C |
545 | type: UserNotificationType.NEW_USER_REGISTRATION, |
546 | userId: user.id, | |
547 | accountId: registeredUser.Account.id | |
548 | }) | |
8424c402 | 549 | notification.Account = registeredUser.Account |
f7cc67b4 C |
550 | |
551 | return notification | |
552 | } | |
553 | ||
554 | function emailSender (emails: string[]) { | |
555 | return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser) | |
556 | } | |
557 | ||
558 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
559 | } | |
560 | ||
a1587156 C |
561 | private async notify<T extends MUserWithNotificationSetting> (options: { |
562 | users: T[] | |
563 | notificationCreator: (user: T) => Promise<UserNotificationModelForApi> | |
564 | emailSender: (emails: string[]) => void | |
8424c402 | 565 | settingGetter: (user: T) => UserNotificationSettingValue |
cef534ed C |
566 | }) { |
567 | const emails: string[] = [] | |
568 | ||
569 | for (const user of options.users) { | |
570 | if (this.isWebNotificationEnabled(options.settingGetter(user))) { | |
571 | const notification = await options.notificationCreator(user) | |
572 | ||
573 | PeerTubeSocket.Instance.sendNotification(user.id, notification) | |
574 | } | |
575 | ||
576 | if (this.isEmailEnabled(user, options.settingGetter(user))) { | |
577 | emails.push(user.email) | |
578 | } | |
579 | } | |
580 | ||
581 | if (emails.length !== 0) { | |
a1587156 | 582 | options.emailSender(emails) |
cef534ed C |
583 | } |
584 | } | |
585 | ||
453e83ea | 586 | private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) { |
1ed9b8ee | 587 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false |
cef534ed | 588 | |
2f1548fd | 589 | return value & UserNotificationSettingValue.EMAIL |
cef534ed C |
590 | } |
591 | ||
592 | private isWebNotificationEnabled (value: UserNotificationSettingValue) { | |
2f1548fd | 593 | return value & UserNotificationSettingValue.WEB |
cef534ed C |
594 | } |
595 | ||
696d83fd C |
596 | private isBlockedByServerOrUser (targetAccount: MAccountServer, user?: MUserAccount) { |
597 | return isBlockedByServerOrAccount(targetAccount, user?.Account) | |
dddc8b1f C |
598 | } |
599 | ||
cef534ed C |
600 | static get Instance () { |
601 | return this.instance || (this.instance = new this()) | |
602 | } | |
603 | } | |
604 | ||
605 | // --------------------------------------------------------------------------- | |
606 | ||
607 | export { | |
608 | Notifier | |
609 | } |