]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/notifier.ts
Add the video tags restrictions to the API docs
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
CommitLineData
cef534ed
C
1import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
2import { logger } from '../helpers/logger'
3import { VideoModel } from '../models/video/video'
4import { Emailer } from './emailer'
5import { UserNotificationModel } from '../models/account/user-notification'
6import { VideoCommentModel } from '../models/video/video-comment'
7import { UserModel } from '../models/account/user'
8import { PeerTubeSocket } from './peertube-socket'
9import { CONFIG } from '../initializers/constants'
10import { VideoPrivacy, VideoState } from '../../shared/models/videos'
11import { VideoAbuseModel } from '../models/video/video-abuse'
12import { VideoBlacklistModel } from '../models/video/video-blacklist'
13import * as Bluebird from 'bluebird'
dc133480
C
14import { VideoImportModel } from '../models/video/video-import'
15import { AccountBlocklistModel } from '../models/account/account-blocklist'
f7cc67b4
C
16import { ActorFollowModel } from '../models/activitypub/actor-follow'
17import { AccountModel } from '../models/account/account'
cef534ed
C
18
19class Notifier {
20
21 private static instance: Notifier
22
23 private constructor () {}
24
25 notifyOnNewVideo (video: VideoModel): void {
26 // Only notify on public and published videos
27 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED) return
28
29 this.notifySubscribersOfNewVideo(video)
30 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
31 }
32
dc133480
C
33 notifyOnPendingVideoPublished (video: VideoModel): void {
34 // Only notify on public videos that has been published while the user waited transcoding/scheduled update
35 if (video.waitTranscoding === false && !video.ScheduleVideoUpdate) return
36
37 this.notifyOwnedVideoHasBeenPublished(video)
38 .catch(err => logger.error('Cannot notify owner that its video %s has been published.', video.url, { err }))
39 }
40
cef534ed
C
41 notifyOnNewComment (comment: VideoCommentModel): void {
42 this.notifyVideoOwnerOfNewComment(comment)
f7cc67b4
C
43 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
44
45 this.notifyOfCommentMention(comment)
46 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
cef534ed
C
47 }
48
49 notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void {
50 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
51 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
52 }
53
54 notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void {
55 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
56 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
57 }
58
59 notifyOnVideoUnblacklist (video: VideoModel): void {
60 this.notifyVideoOwnerOfUnblacklist(video)
61 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', video.url, { err }))
62 }
63
dc133480
C
64 notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void {
65 this.notifyOwnerVideoImportIsFinished(videoImport, success)
66 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
67 }
68
f7cc67b4
C
69 notifyOnNewUserRegistration (user: UserModel): void {
70 this.notifyModeratorsOfNewUserRegistration(user)
71 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
72 }
73
74 notifyOfNewFollow (actorFollow: ActorFollowModel): void {
75 this.notifyUserOfNewActorFollow(actorFollow)
76 .catch(err => {
77 logger.error(
78 'Cannot notify owner of channel %s of a new follow by %s.',
79 actorFollow.ActorFollowing.VideoChannel.getDisplayName(),
80 actorFollow.ActorFollower.Account.getDisplayName(),
81 err
82 )
83 })
84 }
85
cef534ed
C
86 private async notifySubscribersOfNewVideo (video: VideoModel) {
87 // List all followers that are users
88 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
89
90 logger.info('Notifying %d users of new video %s.', users.length, video.url)
91
92 function settingGetter (user: UserModel) {
93 return user.NotificationSetting.newVideoFromSubscription
94 }
95
96 async function notificationCreator (user: UserModel) {
97 const notification = await UserNotificationModel.create({
98 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
99 userId: user.id,
100 videoId: video.id
101 })
102 notification.Video = video
103
104 return notification
105 }
106
107 function emailSender (emails: string[]) {
108 return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video)
109 }
110
111 return this.notify({ users, settingGetter, notificationCreator, emailSender })
112 }
113
114 private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) {
f7cc67b4
C
115 if (comment.Video.isOwned() === false) return
116
cef534ed
C
117 const user = await UserModel.loadByVideoId(comment.videoId)
118
119 // Not our user or user comments its own video
120 if (!user || comment.Account.userId === user.id) return
121
dc133480
C
122 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
123 if (accountMuted) return
124
cef534ed
C
125 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
126
127 function settingGetter (user: UserModel) {
128 return user.NotificationSetting.newCommentOnMyVideo
129 }
130
131 async function notificationCreator (user: UserModel) {
132 const notification = await UserNotificationModel.create({
133 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
134 userId: user.id,
135 commentId: comment.id
136 })
137 notification.Comment = comment
138
139 return notification
140 }
141
142 function emailSender (emails: string[]) {
143 return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment)
144 }
145
146 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
147 }
148
f7cc67b4 149 private async notifyOfCommentMention (comment: VideoCommentModel) {
41d71344
C
150 const extractedUsernames = comment.extractMentions()
151 logger.debug(
152 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
153 { usernames: extractedUsernames, text: comment.text }
154 )
1f6d57e3 155
41d71344 156 let users = await UserModel.listByUsernames(extractedUsernames)
f7cc67b4
C
157
158 if (comment.Video.isOwned()) {
159 const userException = await UserModel.loadByVideoId(comment.videoId)
160 users = users.filter(u => u.id !== userException.id)
161 }
162
163 // Don't notify if I mentioned myself
164 users = users.filter(u => u.Account.id !== comment.accountId)
165
cef534ed
C
166 if (users.length === 0) return
167
f7cc67b4
C
168 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
169
170 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
171
172 function settingGetter (user: UserModel) {
173 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
174
175 return user.NotificationSetting.commentMention
176 }
177
178 async function notificationCreator (user: UserModel) {
179 const notification = await UserNotificationModel.create({
180 type: UserNotificationType.COMMENT_MENTION,
181 userId: user.id,
182 commentId: comment.id
183 })
184 notification.Comment = comment
185
186 return notification
187 }
188
189 function emailSender (emails: string[]) {
190 return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
191 }
192
193 return this.notify({ users, settingGetter, notificationCreator, emailSender })
194 }
195
196 private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) {
197 if (actorFollow.ActorFollowing.isOwned() === false) return
198
199 // Account follows one of our account?
200 let followType: 'account' | 'channel' = 'channel'
201 let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
202
203 // Account follows one of our channel?
204 if (!user) {
205 user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
206 followType = 'account'
207 }
208
209 if (!user) return
210
211 if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) {
212 actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel
213 }
214 const followerAccount = actorFollow.ActorFollower.Account
215
216 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
217 if (accountMuted) return
218
219 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
220
221 function settingGetter (user: UserModel) {
222 return user.NotificationSetting.newFollow
223 }
224
225 async function notificationCreator (user: UserModel) {
226 const notification = await UserNotificationModel.create({
227 type: UserNotificationType.NEW_FOLLOW,
228 userId: user.id,
229 actorFollowId: actorFollow.id
230 })
231 notification.ActorFollow = actorFollow
232
233 return notification
234 }
235
236 function emailSender (emails: string[]) {
237 return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
238 }
239
240 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
241 }
242
243 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
244 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
245 if (moderators.length === 0) return
246
247 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
cef534ed
C
248
249 function settingGetter (user: UserModel) {
250 return user.NotificationSetting.videoAbuseAsModerator
251 }
252
253 async function notificationCreator (user: UserModel) {
254 const notification = await UserNotificationModel.create({
255 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
256 userId: user.id,
257 videoAbuseId: videoAbuse.id
258 })
259 notification.VideoAbuse = videoAbuse
260
261 return notification
262 }
263
264 function emailSender (emails: string[]) {
265 return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
266 }
267
f7cc67b4 268 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
cef534ed
C
269 }
270
271 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
272 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
273 if (!user) return
274
275 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
276
277 function settingGetter (user: UserModel) {
278 return user.NotificationSetting.blacklistOnMyVideo
279 }
280
281 async function notificationCreator (user: UserModel) {
282 const notification = await UserNotificationModel.create({
283 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
284 userId: user.id,
285 videoBlacklistId: videoBlacklist.id
286 })
287 notification.VideoBlacklist = videoBlacklist
288
289 return notification
290 }
291
292 function emailSender (emails: string[]) {
293 return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist)
294 }
295
296 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
297 }
298
299 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) {
300 const user = await UserModel.loadByVideoId(video.id)
301 if (!user) return
302
303 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
304
305 function settingGetter (user: UserModel) {
306 return user.NotificationSetting.blacklistOnMyVideo
307 }
308
309 async function notificationCreator (user: UserModel) {
310 const notification = await UserNotificationModel.create({
311 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
312 userId: user.id,
313 videoId: video.id
314 })
315 notification.Video = video
316
317 return notification
318 }
319
320 function emailSender (emails: string[]) {
321 return Emailer.Instance.addVideoUnblacklistNotification(emails, video)
322 }
323
324 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
325 }
326
dc133480
C
327 private async notifyOwnedVideoHasBeenPublished (video: VideoModel) {
328 const user = await UserModel.loadByVideoId(video.id)
329 if (!user) return
330
331 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
332
333 function settingGetter (user: UserModel) {
334 return user.NotificationSetting.myVideoPublished
335 }
336
337 async function notificationCreator (user: UserModel) {
338 const notification = await UserNotificationModel.create({
339 type: UserNotificationType.MY_VIDEO_PUBLISHED,
340 userId: user.id,
341 videoId: video.id
342 })
343 notification.Video = video
344
345 return notification
346 }
347
348 function emailSender (emails: string[]) {
349 return Emailer.Instance.myVideoPublishedNotification(emails, video)
350 }
351
352 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
353 }
354
355 private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) {
356 const user = await UserModel.loadByVideoImportId(videoImport.id)
357 if (!user) return
358
359 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
360
361 function settingGetter (user: UserModel) {
362 return user.NotificationSetting.myVideoImportFinished
363 }
364
365 async function notificationCreator (user: UserModel) {
366 const notification = await UserNotificationModel.create({
367 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
368 userId: user.id,
369 videoImportId: videoImport.id
370 })
371 notification.VideoImport = videoImport
372
373 return notification
374 }
375
376 function emailSender (emails: string[]) {
377 return success
378 ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
379 : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
380 }
381
382 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
383 }
384
f7cc67b4
C
385 private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) {
386 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
387 if (moderators.length === 0) return
388
389 logger.info(
390 'Notifying %s moderators of new user registration of %s.',
391 moderators.length, registeredUser.Account.Actor.preferredUsername
392 )
393
394 function settingGetter (user: UserModel) {
395 return user.NotificationSetting.newUserRegistration
396 }
397
398 async function notificationCreator (user: UserModel) {
399 const notification = await UserNotificationModel.create({
400 type: UserNotificationType.NEW_USER_REGISTRATION,
401 userId: user.id,
402 accountId: registeredUser.Account.id
403 })
404 notification.Account = registeredUser.Account
405
406 return notification
407 }
408
409 function emailSender (emails: string[]) {
410 return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
411 }
412
413 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
414 }
415
cef534ed
C
416 private async notify (options: {
417 users: UserModel[],
418 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
419 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
420 settingGetter: (user: UserModel) => UserNotificationSettingValue
421 }) {
422 const emails: string[] = []
423
424 for (const user of options.users) {
425 if (this.isWebNotificationEnabled(options.settingGetter(user))) {
426 const notification = await options.notificationCreator(user)
427
428 PeerTubeSocket.Instance.sendNotification(user.id, notification)
429 }
430
431 if (this.isEmailEnabled(user, options.settingGetter(user))) {
432 emails.push(user.email)
433 }
434 }
435
436 if (emails.length !== 0) {
437 await options.emailSender(emails)
438 }
439 }
440
441 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
1ed9b8ee 442 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
cef534ed 443
2f1548fd 444 return value & UserNotificationSettingValue.EMAIL
cef534ed
C
445 }
446
447 private isWebNotificationEnabled (value: UserNotificationSettingValue) {
2f1548fd 448 return value & UserNotificationSettingValue.WEB
cef534ed
C
449 }
450
451 static get Instance () {
452 return this.instance || (this.instance = new this())
453 }
454}
455
456// ---------------------------------------------------------------------------
457
458export {
459 Notifier
460}