aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/notifier.ts')
-rw-r--r--server/lib/notifier.ts207
1 files changed, 124 insertions, 83 deletions
diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts
index a7dfb0979..b7cc2607d 100644
--- a/server/lib/notifier.ts
+++ b/server/lib/notifier.ts
@@ -1,20 +1,30 @@
1import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users' 1import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
2import { logger } from '../helpers/logger' 2import { logger } from '../helpers/logger'
3import { VideoModel } from '../models/video/video'
4import { Emailer } from './emailer' 3import { Emailer } from './emailer'
5import { UserNotificationModel } from '../models/account/user-notification' 4import { UserNotificationModel } from '../models/account/user-notification'
6import { VideoCommentModel } from '../models/video/video-comment'
7import { UserModel } from '../models/account/user' 5import { UserModel } from '../models/account/user'
8import { PeerTubeSocket } from './peertube-socket' 6import { PeerTubeSocket } from './peertube-socket'
9import { CONFIG } from '../initializers/config' 7import { CONFIG } from '../initializers/config'
10import { VideoPrivacy, VideoState } from '../../shared/models/videos' 8import { 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' 9import * as Bluebird from 'bluebird'
14import { VideoImportModel } from '../models/video/video-import'
15import { AccountBlocklistModel } from '../models/account/account-blocklist' 10import { AccountBlocklistModel } from '../models/account/account-blocklist'
16import { ActorFollowModel } from '../models/activitypub/actor-follow' 11import {
17import { AccountModel } from '../models/account/account' 12 MCommentOwnerVideo,
13 MVideoAbuseVideo,
14 MVideoAccountLight,
15 MVideoBlacklistLightVideo,
16 MVideoBlacklistVideo,
17 MVideoFullLight
18} from '../typings/models/video'
19import {
20 MUser,
21 MUserDefault,
22 MUserNotifSettingAccount,
23 MUserWithNotificationSetting,
24 UserNotificationModelForApi
25} from '@server/typings/models/user'
26import { MActorFollowFull } from '../typings/models'
27import { MVideoImportVideo } from '@server/typings/models/video/video-import'
18 28
19class Notifier { 29class Notifier {
20 30
@@ -22,7 +32,7 @@ class Notifier {
22 32
23 private constructor () {} 33 private constructor () {}
24 34
25 notifyOnNewVideoIfNeeded (video: VideoModel): void { 35 notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void {
26 // Only notify on public and published videos which are not blacklisted 36 // Only notify on public and published videos which are not blacklisted
27 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return 37 if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return
28 38
@@ -30,7 +40,7 @@ class Notifier {
30 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) 40 .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
31 } 41 }
32 42
33 notifyOnVideoPublishedAfterTranscoding (video: VideoModel): void { 43 notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
34 // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update 44 // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update
35 if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return 45 if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return
36 46
@@ -38,7 +48,7 @@ class Notifier {
38 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err })) 48 .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
39 } 49 }
40 50
41 notifyOnVideoPublishedAfterScheduledUpdate (video: VideoModel): void { 51 notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void {
42 // don't notify if video is still blacklisted or waiting for transcoding 52 // don't notify if video is still blacklisted or waiting for transcoding
43 if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return 53 if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
44 54
@@ -46,7 +56,7 @@ class Notifier {
46 .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err })) 56 .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err }))
47 } 57 }
48 58
49 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: VideoModel): void { 59 notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void {
50 // don't notify if video is still waiting for transcoding or scheduled update 60 // don't notify if video is still waiting for transcoding or scheduled update
51 if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return 61 if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
52 62
@@ -54,7 +64,7 @@ class Notifier {
54 .catch(err => logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })) // tslint:disable-line:max-line-length 64 .catch(err => logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })) // tslint:disable-line:max-line-length
55 } 65 }
56 66
57 notifyOnNewComment (comment: VideoCommentModel): void { 67 notifyOnNewComment (comment: MCommentOwnerVideo): void {
58 this.notifyVideoOwnerOfNewComment(comment) 68 this.notifyVideoOwnerOfNewComment(comment)
59 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err })) 69 .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
60 70
@@ -62,37 +72,37 @@ class Notifier {
62 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err })) 72 .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
63 } 73 }
64 74
65 notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void { 75 notifyOnNewVideoAbuse (videoAbuse: MVideoAbuseVideo): void {
66 this.notifyModeratorsOfNewVideoAbuse(videoAbuse) 76 this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
67 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err })) 77 .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
68 } 78 }
69 79
70 notifyOnVideoAutoBlacklist (video: VideoModel): void { 80 notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
71 this.notifyModeratorsOfVideoAutoBlacklist(video) 81 this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist)
72 .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', video.url, { err })) 82 .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
73 } 83 }
74 84
75 notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void { 85 notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
76 this.notifyVideoOwnerOfBlacklist(videoBlacklist) 86 this.notifyVideoOwnerOfBlacklist(videoBlacklist)
77 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) 87 .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
78 } 88 }
79 89
80 notifyOnVideoUnblacklist (video: VideoModel): void { 90 notifyOnVideoUnblacklist (video: MVideoFullLight): void {
81 this.notifyVideoOwnerOfUnblacklist(video) 91 this.notifyVideoOwnerOfUnblacklist(video)
82 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err })) 92 .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
83 } 93 }
84 94
85 notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void { 95 notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void {
86 this.notifyOwnerVideoImportIsFinished(videoImport, success) 96 this.notifyOwnerVideoImportIsFinished(videoImport, success)
87 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err })) 97 .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
88 } 98 }
89 99
90 notifyOnNewUserRegistration (user: UserModel): void { 100 notifyOnNewUserRegistration (user: MUserDefault): void {
91 this.notifyModeratorsOfNewUserRegistration(user) 101 this.notifyModeratorsOfNewUserRegistration(user)
92 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) 102 .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
93 } 103 }
94 104
95 notifyOfNewUserFollow (actorFollow: ActorFollowModel): void { 105 notifyOfNewUserFollow (actorFollow: MActorFollowFull): void {
96 this.notifyUserOfNewActorFollow(actorFollow) 106 this.notifyUserOfNewActorFollow(actorFollow)
97 .catch(err => { 107 .catch(err => {
98 logger.error( 108 logger.error(
@@ -104,25 +114,32 @@ class Notifier {
104 }) 114 })
105 } 115 }
106 116
107 notifyOfNewInstanceFollow (actorFollow: ActorFollowModel): void { 117 notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
108 this.notifyAdminsOfNewInstanceFollow(actorFollow) 118 this.notifyAdminsOfNewInstanceFollow(actorFollow)
109 .catch(err => { 119 .catch(err => {
110 logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) 120 logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err })
111 }) 121 })
112 } 122 }
113 123
114 private async notifySubscribersOfNewVideo (video: VideoModel) { 124 notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void {
125 this.notifyAdminsOfAutoInstanceFollowing(actorFollow)
126 .catch(err => {
127 logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err })
128 })
129 }
130
131 private async notifySubscribersOfNewVideo (video: MVideoAccountLight) {
115 // List all followers that are users 132 // List all followers that are users
116 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) 133 const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
117 134
118 logger.info('Notifying %d users of new video %s.', users.length, video.url) 135 logger.info('Notifying %d users of new video %s.', users.length, video.url)
119 136
120 function settingGetter (user: UserModel) { 137 function settingGetter (user: MUserWithNotificationSetting) {
121 return user.NotificationSetting.newVideoFromSubscription 138 return user.NotificationSetting.newVideoFromSubscription
122 } 139 }
123 140
124 async function notificationCreator (user: UserModel) { 141 async function notificationCreator (user: MUserWithNotificationSetting) {
125 const notification = await UserNotificationModel.create({ 142 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
126 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, 143 type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,
127 userId: user.id, 144 userId: user.id,
128 videoId: video.id 145 videoId: video.id
@@ -139,7 +156,7 @@ class Notifier {
139 return this.notify({ users, settingGetter, notificationCreator, emailSender }) 156 return this.notify({ users, settingGetter, notificationCreator, emailSender })
140 } 157 }
141 158
142 private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) { 159 private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) {
143 if (comment.Video.isOwned() === false) return 160 if (comment.Video.isOwned() === false) return
144 161
145 const user = await UserModel.loadByVideoId(comment.videoId) 162 const user = await UserModel.loadByVideoId(comment.videoId)
@@ -152,12 +169,12 @@ class Notifier {
152 169
153 logger.info('Notifying user %s of new comment %s.', user.username, comment.url) 170 logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
154 171
155 function settingGetter (user: UserModel) { 172 function settingGetter (user: MUserWithNotificationSetting) {
156 return user.NotificationSetting.newCommentOnMyVideo 173 return user.NotificationSetting.newCommentOnMyVideo
157 } 174 }
158 175
159 async function notificationCreator (user: UserModel) { 176 async function notificationCreator (user: MUserWithNotificationSetting) {
160 const notification = await UserNotificationModel.create({ 177 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
161 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, 178 type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
162 userId: user.id, 179 userId: user.id,
163 commentId: comment.id 180 commentId: comment.id
@@ -174,7 +191,7 @@ class Notifier {
174 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) 191 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
175 } 192 }
176 193
177 private async notifyOfCommentMention (comment: VideoCommentModel) { 194 private async notifyOfCommentMention (comment: MCommentOwnerVideo) {
178 const extractedUsernames = comment.extractMentions() 195 const extractedUsernames = comment.extractMentions()
179 logger.debug( 196 logger.debug(
180 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url, 197 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
@@ -197,14 +214,14 @@ class Notifier {
197 214
198 logger.info('Notifying %d users of new comment %s.', users.length, comment.url) 215 logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
199 216
200 function settingGetter (user: UserModel) { 217 function settingGetter (user: MUserNotifSettingAccount) {
201 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE 218 if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
202 219
203 return user.NotificationSetting.commentMention 220 return user.NotificationSetting.commentMention
204 } 221 }
205 222
206 async function notificationCreator (user: UserModel) { 223 async function notificationCreator (user: MUserNotifSettingAccount) {
207 const notification = await UserNotificationModel.create({ 224 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
208 type: UserNotificationType.COMMENT_MENTION, 225 type: UserNotificationType.COMMENT_MENTION,
209 userId: user.id, 226 userId: user.id,
210 commentId: comment.id 227 commentId: comment.id
@@ -221,7 +238,7 @@ class Notifier {
221 return this.notify({ users, settingGetter, notificationCreator, emailSender }) 238 return this.notify({ users, settingGetter, notificationCreator, emailSender })
222 } 239 }
223 240
224 private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) { 241 private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) {
225 if (actorFollow.ActorFollowing.isOwned() === false) return 242 if (actorFollow.ActorFollowing.isOwned() === false) return
226 243
227 // Account follows one of our account? 244 // Account follows one of our account?
@@ -236,9 +253,6 @@ class Notifier {
236 253
237 if (!user) return 254 if (!user) return
238 255
239 if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) {
240 actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel
241 }
242 const followerAccount = actorFollow.ActorFollower.Account 256 const followerAccount = actorFollow.ActorFollower.Account
243 257
244 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id) 258 const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
@@ -246,12 +260,12 @@ class Notifier {
246 260
247 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) 261 logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
248 262
249 function settingGetter (user: UserModel) { 263 function settingGetter (user: MUserWithNotificationSetting) {
250 return user.NotificationSetting.newFollow 264 return user.NotificationSetting.newFollow
251 } 265 }
252 266
253 async function notificationCreator (user: UserModel) { 267 async function notificationCreator (user: MUserWithNotificationSetting) {
254 const notification = await UserNotificationModel.create({ 268 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
255 type: UserNotificationType.NEW_FOLLOW, 269 type: UserNotificationType.NEW_FOLLOW,
256 userId: user.id, 270 userId: user.id,
257 actorFollowId: actorFollow.id 271 actorFollowId: actorFollow.id
@@ -268,17 +282,17 @@ class Notifier {
268 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) 282 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
269 } 283 }
270 284
271 private async notifyAdminsOfNewInstanceFollow (actorFollow: ActorFollowModel) { 285 private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) {
272 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) 286 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
273 287
274 logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url) 288 logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url)
275 289
276 function settingGetter (user: UserModel) { 290 function settingGetter (user: MUserWithNotificationSetting) {
277 return user.NotificationSetting.newInstanceFollower 291 return user.NotificationSetting.newInstanceFollower
278 } 292 }
279 293
280 async function notificationCreator (user: UserModel) { 294 async function notificationCreator (user: MUserWithNotificationSetting) {
281 const notification = await UserNotificationModel.create({ 295 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
282 type: UserNotificationType.NEW_INSTANCE_FOLLOWER, 296 type: UserNotificationType.NEW_INSTANCE_FOLLOWER,
283 userId: user.id, 297 userId: user.id,
284 actorFollowId: actorFollow.id 298 actorFollowId: actorFollow.id
@@ -295,18 +309,45 @@ class Notifier {
295 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) 309 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
296 } 310 }
297 311
298 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) { 312 private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) {
313 const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
314
315 logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url)
316
317 function settingGetter (user: MUserWithNotificationSetting) {
318 return user.NotificationSetting.autoInstanceFollowing
319 }
320
321 async function notificationCreator (user: MUserWithNotificationSetting) {
322 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
323 type: UserNotificationType.AUTO_INSTANCE_FOLLOWING,
324 userId: user.id,
325 actorFollowId: actorFollow.id
326 })
327 notification.ActorFollow = actorFollow
328
329 return notification
330 }
331
332 function emailSender (emails: string[]) {
333 return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow)
334 }
335
336 return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
337 }
338
339 private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) {
299 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) 340 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
300 if (moderators.length === 0) return 341 if (moderators.length === 0) return
301 342
302 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url) 343 logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
303 344
304 function settingGetter (user: UserModel) { 345 function settingGetter (user: MUserWithNotificationSetting) {
305 return user.NotificationSetting.videoAbuseAsModerator 346 return user.NotificationSetting.videoAbuseAsModerator
306 } 347 }
307 348
308 async function notificationCreator (user: UserModel) { 349 async function notificationCreator (user: MUserWithNotificationSetting) {
309 const notification = await UserNotificationModel.create({ 350 const notification: UserNotificationModelForApi = await UserNotificationModel.create<UserNotificationModelForApi>({
310 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS, 351 type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
311 userId: user.id, 352 userId: user.id,
312 videoAbuseId: videoAbuse.id 353 videoAbuseId: videoAbuse.id
@@ -323,46 +364,46 @@ class Notifier {
323 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) 364 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
324 } 365 }
325 366
326 private async notifyModeratorsOfVideoAutoBlacklist (video: VideoModel) { 367 private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) {
327 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) 368 const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
328 if (moderators.length === 0) return 369 if (moderators.length === 0) return
329 370
330 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, video.url) 371 logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url)
331 372
332 function settingGetter (user: UserModel) { 373 function settingGetter (user: MUserWithNotificationSetting) {
333 return user.NotificationSetting.videoAutoBlacklistAsModerator 374 return user.NotificationSetting.videoAutoBlacklistAsModerator
334 } 375 }
335 async function notificationCreator (user: UserModel) {
336 376
337 const notification = await UserNotificationModel.create({ 377 async function notificationCreator (user: MUserWithNotificationSetting) {
378 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
338 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, 379 type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
339 userId: user.id, 380 userId: user.id,
340 videoId: video.id 381 videoBlacklistId: videoBlacklist.id
341 }) 382 })
342 notification.Video = video 383 notification.VideoBlacklist = videoBlacklist
343 384
344 return notification 385 return notification
345 } 386 }
346 387
347 function emailSender (emails: string[]) { 388 function emailSender (emails: string[]) {
348 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video) 389 return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist)
349 } 390 }
350 391
351 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) 392 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
352 } 393 }
353 394
354 private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) { 395 private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) {
355 const user = await UserModel.loadByVideoId(videoBlacklist.videoId) 396 const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
356 if (!user) return 397 if (!user) return
357 398
358 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url) 399 logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url)
359 400
360 function settingGetter (user: UserModel) { 401 function settingGetter (user: MUserWithNotificationSetting) {
361 return user.NotificationSetting.blacklistOnMyVideo 402 return user.NotificationSetting.blacklistOnMyVideo
362 } 403 }
363 404
364 async function notificationCreator (user: UserModel) { 405 async function notificationCreator (user: MUserWithNotificationSetting) {
365 const notification = await UserNotificationModel.create({ 406 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
366 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, 407 type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,
367 userId: user.id, 408 userId: user.id,
368 videoBlacklistId: videoBlacklist.id 409 videoBlacklistId: videoBlacklist.id
@@ -379,18 +420,18 @@ class Notifier {
379 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) 420 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
380 } 421 }
381 422
382 private async notifyVideoOwnerOfUnblacklist (video: VideoModel) { 423 private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) {
383 const user = await UserModel.loadByVideoId(video.id) 424 const user = await UserModel.loadByVideoId(video.id)
384 if (!user) return 425 if (!user) return
385 426
386 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url) 427 logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url)
387 428
388 function settingGetter (user: UserModel) { 429 function settingGetter (user: MUserWithNotificationSetting) {
389 return user.NotificationSetting.blacklistOnMyVideo 430 return user.NotificationSetting.blacklistOnMyVideo
390 } 431 }
391 432
392 async function notificationCreator (user: UserModel) { 433 async function notificationCreator (user: MUserWithNotificationSetting) {
393 const notification = await UserNotificationModel.create({ 434 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
394 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, 435 type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,
395 userId: user.id, 436 userId: user.id,
396 videoId: video.id 437 videoId: video.id
@@ -407,18 +448,18 @@ class Notifier {
407 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) 448 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
408 } 449 }
409 450
410 private async notifyOwnedVideoHasBeenPublished (video: VideoModel) { 451 private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) {
411 const user = await UserModel.loadByVideoId(video.id) 452 const user = await UserModel.loadByVideoId(video.id)
412 if (!user) return 453 if (!user) return
413 454
414 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url) 455 logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
415 456
416 function settingGetter (user: UserModel) { 457 function settingGetter (user: MUserWithNotificationSetting) {
417 return user.NotificationSetting.myVideoPublished 458 return user.NotificationSetting.myVideoPublished
418 } 459 }
419 460
420 async function notificationCreator (user: UserModel) { 461 async function notificationCreator (user: MUserWithNotificationSetting) {
421 const notification = await UserNotificationModel.create({ 462 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
422 type: UserNotificationType.MY_VIDEO_PUBLISHED, 463 type: UserNotificationType.MY_VIDEO_PUBLISHED,
423 userId: user.id, 464 userId: user.id,
424 videoId: video.id 465 videoId: video.id
@@ -435,18 +476,18 @@ class Notifier {
435 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) 476 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
436 } 477 }
437 478
438 private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) { 479 private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) {
439 const user = await UserModel.loadByVideoImportId(videoImport.id) 480 const user = await UserModel.loadByVideoImportId(videoImport.id)
440 if (!user) return 481 if (!user) return
441 482
442 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier()) 483 logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
443 484
444 function settingGetter (user: UserModel) { 485 function settingGetter (user: MUserWithNotificationSetting) {
445 return user.NotificationSetting.myVideoImportFinished 486 return user.NotificationSetting.myVideoImportFinished
446 } 487 }
447 488
448 async function notificationCreator (user: UserModel) { 489 async function notificationCreator (user: MUserWithNotificationSetting) {
449 const notification = await UserNotificationModel.create({ 490 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
450 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR, 491 type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
451 userId: user.id, 492 userId: user.id,
452 videoImportId: videoImport.id 493 videoImportId: videoImport.id
@@ -465,21 +506,21 @@ class Notifier {
465 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) 506 return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
466 } 507 }
467 508
468 private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) { 509 private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) {
469 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) 510 const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
470 if (moderators.length === 0) return 511 if (moderators.length === 0) return
471 512
472 logger.info( 513 logger.info(
473 'Notifying %s moderators of new user registration of %s.', 514 'Notifying %s moderators of new user registration of %s.',
474 moderators.length, registeredUser.Account.Actor.preferredUsername 515 moderators.length, registeredUser.username
475 ) 516 )
476 517
477 function settingGetter (user: UserModel) { 518 function settingGetter (user: MUserWithNotificationSetting) {
478 return user.NotificationSetting.newUserRegistration 519 return user.NotificationSetting.newUserRegistration
479 } 520 }
480 521
481 async function notificationCreator (user: UserModel) { 522 async function notificationCreator (user: MUserWithNotificationSetting) {
482 const notification = await UserNotificationModel.create({ 523 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
483 type: UserNotificationType.NEW_USER_REGISTRATION, 524 type: UserNotificationType.NEW_USER_REGISTRATION,
484 userId: user.id, 525 userId: user.id,
485 accountId: registeredUser.Account.id 526 accountId: registeredUser.Account.id
@@ -496,11 +537,11 @@ class Notifier {
496 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) 537 return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
497 } 538 }
498 539
499 private async notify (options: { 540 private async notify <T extends MUserWithNotificationSetting> (options: {
500 users: UserModel[], 541 users: T[],
501 notificationCreator: (user: UserModel) => Promise<UserNotificationModel>, 542 notificationCreator: (user: T) => Promise<UserNotificationModelForApi>,
502 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>, 543 emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
503 settingGetter: (user: UserModel) => UserNotificationSettingValue 544 settingGetter: (user: T) => UserNotificationSettingValue
504 }) { 545 }) {
505 const emails: string[] = [] 546 const emails: string[] = []
506 547
@@ -521,7 +562,7 @@ class Notifier {
521 } 562 }
522 } 563 }
523 564
524 private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) { 565 private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) {
525 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false 566 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
526 567
527 return value & UserNotificationSettingValue.EMAIL 568 return value & UserNotificationSettingValue.EMAIL