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