diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-30 16:50:12 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-09-04 16:24:58 +0200 |
commit | 8424c4026afd7304880a4ce8138a04ffb3d8c938 (patch) | |
tree | 5b42625a59307b03333aa7d293b40b4c90da8f73 /server/lib/notifier.ts | |
parent | f69ec5f340638ef577e8f5b9b1fb844778656a1f (diff) | |
download | PeerTube-8424c4026afd7304880a4ce8138a04ffb3d8c938.tar.gz PeerTube-8424c4026afd7304880a4ce8138a04ffb3d8c938.tar.zst PeerTube-8424c4026afd7304880a4ce8138a04ffb3d8c938.zip |
Add auto follow back support for instances
Diffstat (limited to 'server/lib/notifier.ts')
-rw-r--r-- | server/lib/notifier.ts | 184 |
1 files changed, 109 insertions, 75 deletions
diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index 23f76a21a..b7cc2607d 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts | |||
@@ -1,30 +1,30 @@ | |||
1 | import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users' | 1 | import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users' |
2 | import { logger } from '../helpers/logger' | 2 | import { logger } from '../helpers/logger' |
3 | import { VideoModel } from '../models/video/video' | ||
4 | import { Emailer } from './emailer' | 3 | import { Emailer } from './emailer' |
5 | import { UserNotificationModel } from '../models/account/user-notification' | 4 | import { UserNotificationModel } from '../models/account/user-notification' |
6 | import { VideoCommentModel } from '../models/video/video-comment' | ||
7 | import { UserModel } from '../models/account/user' | 5 | import { UserModel } from '../models/account/user' |
8 | import { PeerTubeSocket } from './peertube-socket' | 6 | import { PeerTubeSocket } from './peertube-socket' |
9 | import { CONFIG } from '../initializers/config' | 7 | import { CONFIG } from '../initializers/config' |
10 | import { VideoPrivacy, VideoState } from '../../shared/models/videos' | 8 | import { VideoPrivacy, VideoState } from '../../shared/models/videos' |
11 | import { VideoBlacklistModel } from '../models/video/video-blacklist' | ||
12 | import * as Bluebird from 'bluebird' | 9 | import * as Bluebird from 'bluebird' |
13 | import { VideoImportModel } from '../models/video/video-import' | ||
14 | import { AccountBlocklistModel } from '../models/account/account-blocklist' | 10 | import { AccountBlocklistModel } from '../models/account/account-blocklist' |
15 | import { | 11 | import { |
16 | MCommentOwnerVideo, | 12 | MCommentOwnerVideo, |
17 | MVideo, | ||
18 | MVideoAbuseVideo, | 13 | MVideoAbuseVideo, |
19 | MVideoAccountLight, | 14 | MVideoAccountLight, |
15 | MVideoBlacklistLightVideo, | ||
20 | MVideoBlacklistVideo, | 16 | MVideoBlacklistVideo, |
21 | MVideoFullLight | 17 | MVideoFullLight |
22 | } from '../typings/models/video' | 18 | } from '../typings/models/video' |
23 | import { MUser, MUserAccount, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/typings/models/user' | 19 | import { |
24 | import { MActorFollowActors, MActorFollowFull, MActorFollowFollowingFullFollowerAccount } from '../typings/models' | 20 | MUser, |
25 | import { ActorFollowModel } from '../models/activitypub/actor-follow' | 21 | MUserDefault, |
22 | MUserNotifSettingAccount, | ||
23 | MUserWithNotificationSetting, | ||
24 | UserNotificationModelForApi | ||
25 | } from '@server/typings/models/user' | ||
26 | import { MActorFollowFull } from '../typings/models' | ||
26 | import { MVideoImportVideo } from '@server/typings/models/video/video-import' | 27 | import { MVideoImportVideo } from '@server/typings/models/video/video-import' |
27 | import { AccountModel } from '@server/models/account/account' | ||
28 | 28 | ||
29 | class Notifier { | 29 | class Notifier { |
30 | 30 | ||
@@ -77,9 +77,9 @@ class Notifier { | |||
77 | .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 })) |
78 | } | 78 | } |
79 | 79 | ||
80 | notifyOnVideoAutoBlacklist (video: MVideo): void { | 80 | notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void { |
81 | this.notifyModeratorsOfVideoAutoBlacklist(video) | 81 | this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist) |
82 | .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 })) |
83 | } | 83 | } |
84 | 84 | ||
85 | notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void { | 85 | notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void { |
@@ -87,7 +87,7 @@ class Notifier { | |||
87 | .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 })) |
88 | } | 88 | } |
89 | 89 | ||
90 | notifyOnVideoUnblacklist (video: MVideo): void { | 90 | notifyOnVideoUnblacklist (video: MVideoFullLight): void { |
91 | this.notifyVideoOwnerOfUnblacklist(video) | 91 | this.notifyVideoOwnerOfUnblacklist(video) |
92 | .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 })) |
93 | } | 93 | } |
@@ -97,12 +97,12 @@ class Notifier { | |||
97 | .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 })) |
98 | } | 98 | } |
99 | 99 | ||
100 | notifyOnNewUserRegistration (user: MUserAccount): void { | 100 | notifyOnNewUserRegistration (user: MUserDefault): void { |
101 | this.notifyModeratorsOfNewUserRegistration(user) | 101 | this.notifyModeratorsOfNewUserRegistration(user) |
102 | .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 })) |
103 | } | 103 | } |
104 | 104 | ||
105 | notifyOfNewUserFollow (actorFollow: MActorFollowFollowingFullFollowerAccount): void { | 105 | notifyOfNewUserFollow (actorFollow: MActorFollowFull): void { |
106 | this.notifyUserOfNewActorFollow(actorFollow) | 106 | this.notifyUserOfNewActorFollow(actorFollow) |
107 | .catch(err => { | 107 | .catch(err => { |
108 | logger.error( | 108 | logger.error( |
@@ -114,30 +114,37 @@ class Notifier { | |||
114 | }) | 114 | }) |
115 | } | 115 | } |
116 | 116 | ||
117 | notifyOfNewInstanceFollow (actorFollow: MActorFollowActors): void { | 117 | notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void { |
118 | this.notifyAdminsOfNewInstanceFollow(actorFollow) | 118 | this.notifyAdminsOfNewInstanceFollow(actorFollow) |
119 | .catch(err => { | 119 | .catch(err => { |
120 | 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 }) |
121 | }) | 121 | }) |
122 | } | 122 | } |
123 | 123 | ||
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 | |||
124 | private async notifySubscribersOfNewVideo (video: MVideoAccountLight) { | 131 | private async notifySubscribersOfNewVideo (video: MVideoAccountLight) { |
125 | // List all followers that are users | 132 | // List all followers that are users |
126 | const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) | 133 | const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) |
127 | 134 | ||
128 | 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) |
129 | 136 | ||
130 | function settingGetter (user: UserModel) { | 137 | function settingGetter (user: MUserWithNotificationSetting) { |
131 | return user.NotificationSetting.newVideoFromSubscription | 138 | return user.NotificationSetting.newVideoFromSubscription |
132 | } | 139 | } |
133 | 140 | ||
134 | async function notificationCreator (user: UserModel) { | 141 | async function notificationCreator (user: MUserWithNotificationSetting) { |
135 | const notification = await UserNotificationModel.create({ | 142 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
136 | type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, | 143 | type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, |
137 | userId: user.id, | 144 | userId: user.id, |
138 | videoId: video.id | 145 | videoId: video.id |
139 | }) | 146 | }) |
140 | notification.Video = video as VideoModel | 147 | notification.Video = video |
141 | 148 | ||
142 | return notification | 149 | return notification |
143 | } | 150 | } |
@@ -162,17 +169,17 @@ class Notifier { | |||
162 | 169 | ||
163 | 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) |
164 | 171 | ||
165 | function settingGetter (user: UserModel) { | 172 | function settingGetter (user: MUserWithNotificationSetting) { |
166 | return user.NotificationSetting.newCommentOnMyVideo | 173 | return user.NotificationSetting.newCommentOnMyVideo |
167 | } | 174 | } |
168 | 175 | ||
169 | async function notificationCreator (user: UserModel) { | 176 | async function notificationCreator (user: MUserWithNotificationSetting) { |
170 | const notification = await UserNotificationModel.create({ | 177 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
171 | type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, | 178 | type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, |
172 | userId: user.id, | 179 | userId: user.id, |
173 | commentId: comment.id | 180 | commentId: comment.id |
174 | }) | 181 | }) |
175 | notification.Comment = comment as VideoCommentModel | 182 | notification.Comment = comment |
176 | 183 | ||
177 | return notification | 184 | return notification |
178 | } | 185 | } |
@@ -207,19 +214,19 @@ class Notifier { | |||
207 | 214 | ||
208 | 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) |
209 | 216 | ||
210 | function settingGetter (user: UserModel) { | 217 | function settingGetter (user: MUserNotifSettingAccount) { |
211 | if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE | 218 | if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE |
212 | 219 | ||
213 | return user.NotificationSetting.commentMention | 220 | return user.NotificationSetting.commentMention |
214 | } | 221 | } |
215 | 222 | ||
216 | async function notificationCreator (user: UserModel) { | 223 | async function notificationCreator (user: MUserNotifSettingAccount) { |
217 | const notification = await UserNotificationModel.create({ | 224 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
218 | type: UserNotificationType.COMMENT_MENTION, | 225 | type: UserNotificationType.COMMENT_MENTION, |
219 | userId: user.id, | 226 | userId: user.id, |
220 | commentId: comment.id | 227 | commentId: comment.id |
221 | }) | 228 | }) |
222 | notification.Comment = comment as VideoCommentModel | 229 | notification.Comment = comment |
223 | 230 | ||
224 | return notification | 231 | return notification |
225 | } | 232 | } |
@@ -231,7 +238,7 @@ class Notifier { | |||
231 | return this.notify({ users, settingGetter, notificationCreator, emailSender }) | 238 | return this.notify({ users, settingGetter, notificationCreator, emailSender }) |
232 | } | 239 | } |
233 | 240 | ||
234 | private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFollowingFullFollowerAccount) { | 241 | private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) { |
235 | if (actorFollow.ActorFollowing.isOwned() === false) return | 242 | if (actorFollow.ActorFollowing.isOwned() === false) return |
236 | 243 | ||
237 | // Account follows one of our account? | 244 | // Account follows one of our account? |
@@ -253,17 +260,17 @@ class Notifier { | |||
253 | 260 | ||
254 | 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()) |
255 | 262 | ||
256 | function settingGetter (user: UserModel) { | 263 | function settingGetter (user: MUserWithNotificationSetting) { |
257 | return user.NotificationSetting.newFollow | 264 | return user.NotificationSetting.newFollow |
258 | } | 265 | } |
259 | 266 | ||
260 | async function notificationCreator (user: UserModel) { | 267 | async function notificationCreator (user: MUserWithNotificationSetting) { |
261 | const notification = await UserNotificationModel.create({ | 268 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
262 | type: UserNotificationType.NEW_FOLLOW, | 269 | type: UserNotificationType.NEW_FOLLOW, |
263 | userId: user.id, | 270 | userId: user.id, |
264 | actorFollowId: actorFollow.id | 271 | actorFollowId: actorFollow.id |
265 | }) | 272 | }) |
266 | notification.ActorFollow = actorFollow as ActorFollowModel | 273 | notification.ActorFollow = actorFollow |
267 | 274 | ||
268 | return notification | 275 | return notification |
269 | } | 276 | } |
@@ -275,22 +282,22 @@ class Notifier { | |||
275 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | 282 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) |
276 | } | 283 | } |
277 | 284 | ||
278 | private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowActors) { | 285 | private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) { |
279 | const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) | 286 | const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) |
280 | 287 | ||
281 | 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) |
282 | 289 | ||
283 | function settingGetter (user: UserModel) { | 290 | function settingGetter (user: MUserWithNotificationSetting) { |
284 | return user.NotificationSetting.newInstanceFollower | 291 | return user.NotificationSetting.newInstanceFollower |
285 | } | 292 | } |
286 | 293 | ||
287 | async function notificationCreator (user: UserModel) { | 294 | async function notificationCreator (user: MUserWithNotificationSetting) { |
288 | const notification = await UserNotificationModel.create({ | 295 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
289 | type: UserNotificationType.NEW_INSTANCE_FOLLOWER, | 296 | type: UserNotificationType.NEW_INSTANCE_FOLLOWER, |
290 | userId: user.id, | 297 | userId: user.id, |
291 | actorFollowId: actorFollow.id | 298 | actorFollowId: actorFollow.id |
292 | }) | 299 | }) |
293 | notification.ActorFollow = actorFollow as ActorFollowModel | 300 | notification.ActorFollow = actorFollow |
294 | 301 | ||
295 | return notification | 302 | return notification |
296 | } | 303 | } |
@@ -302,18 +309,45 @@ class Notifier { | |||
302 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) | 309 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) |
303 | } | 310 | } |
304 | 311 | ||
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 | |||
305 | private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) { | 339 | private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) { |
306 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) | 340 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) |
307 | if (moderators.length === 0) return | 341 | if (moderators.length === 0) return |
308 | 342 | ||
309 | 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) |
310 | 344 | ||
311 | function settingGetter (user: UserModel) { | 345 | function settingGetter (user: MUserWithNotificationSetting) { |
312 | return user.NotificationSetting.videoAbuseAsModerator | 346 | return user.NotificationSetting.videoAbuseAsModerator |
313 | } | 347 | } |
314 | 348 | ||
315 | async function notificationCreator (user: UserModel) { | 349 | async function notificationCreator (user: MUserWithNotificationSetting) { |
316 | const notification: UserNotificationModelForApi = await UserNotificationModel.create({ | 350 | const notification: UserNotificationModelForApi = await UserNotificationModel.create<UserNotificationModelForApi>({ |
317 | type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS, | 351 | type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS, |
318 | userId: user.id, | 352 | userId: user.id, |
319 | videoAbuseId: videoAbuse.id | 353 | videoAbuseId: videoAbuse.id |
@@ -330,29 +364,29 @@ class Notifier { | |||
330 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | 364 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) |
331 | } | 365 | } |
332 | 366 | ||
333 | private async notifyModeratorsOfVideoAutoBlacklist (video: MVideo) { | 367 | private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) { |
334 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) | 368 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) |
335 | if (moderators.length === 0) return | 369 | if (moderators.length === 0) return |
336 | 370 | ||
337 | 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) |
338 | 372 | ||
339 | function settingGetter (user: UserModel) { | 373 | function settingGetter (user: MUserWithNotificationSetting) { |
340 | return user.NotificationSetting.videoAutoBlacklistAsModerator | 374 | return user.NotificationSetting.videoAutoBlacklistAsModerator |
341 | } | 375 | } |
342 | async function notificationCreator (user: UserModel) { | ||
343 | 376 | ||
344 | const notification = await UserNotificationModel.create({ | 377 | async function notificationCreator (user: MUserWithNotificationSetting) { |
378 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | ||
345 | type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, | 379 | type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, |
346 | userId: user.id, | 380 | userId: user.id, |
347 | videoId: video.id | 381 | videoBlacklistId: videoBlacklist.id |
348 | }) | 382 | }) |
349 | notification.Video = video as VideoModel | 383 | notification.VideoBlacklist = videoBlacklist |
350 | 384 | ||
351 | return notification | 385 | return notification |
352 | } | 386 | } |
353 | 387 | ||
354 | function emailSender (emails: string[]) { | 388 | function emailSender (emails: string[]) { |
355 | return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video) | 389 | return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist) |
356 | } | 390 | } |
357 | 391 | ||
358 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | 392 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) |
@@ -364,17 +398,17 @@ class Notifier { | |||
364 | 398 | ||
365 | 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) |
366 | 400 | ||
367 | function settingGetter (user: UserModel) { | 401 | function settingGetter (user: MUserWithNotificationSetting) { |
368 | return user.NotificationSetting.blacklistOnMyVideo | 402 | return user.NotificationSetting.blacklistOnMyVideo |
369 | } | 403 | } |
370 | 404 | ||
371 | async function notificationCreator (user: UserModel) { | 405 | async function notificationCreator (user: MUserWithNotificationSetting) { |
372 | const notification = await UserNotificationModel.create({ | 406 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
373 | type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, | 407 | type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, |
374 | userId: user.id, | 408 | userId: user.id, |
375 | videoBlacklistId: videoBlacklist.id | 409 | videoBlacklistId: videoBlacklist.id |
376 | }) | 410 | }) |
377 | notification.VideoBlacklist = videoBlacklist as VideoBlacklistModel | 411 | notification.VideoBlacklist = videoBlacklist |
378 | 412 | ||
379 | return notification | 413 | return notification |
380 | } | 414 | } |
@@ -386,23 +420,23 @@ class Notifier { | |||
386 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | 420 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) |
387 | } | 421 | } |
388 | 422 | ||
389 | private async notifyVideoOwnerOfUnblacklist (video: MVideo) { | 423 | private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) { |
390 | const user = await UserModel.loadByVideoId(video.id) | 424 | const user = await UserModel.loadByVideoId(video.id) |
391 | if (!user) return | 425 | if (!user) return |
392 | 426 | ||
393 | 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) |
394 | 428 | ||
395 | function settingGetter (user: UserModel) { | 429 | function settingGetter (user: MUserWithNotificationSetting) { |
396 | return user.NotificationSetting.blacklistOnMyVideo | 430 | return user.NotificationSetting.blacklistOnMyVideo |
397 | } | 431 | } |
398 | 432 | ||
399 | async function notificationCreator (user: UserModel) { | 433 | async function notificationCreator (user: MUserWithNotificationSetting) { |
400 | const notification = await UserNotificationModel.create({ | 434 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
401 | type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, | 435 | type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, |
402 | userId: user.id, | 436 | userId: user.id, |
403 | videoId: video.id | 437 | videoId: video.id |
404 | }) | 438 | }) |
405 | notification.Video = video as VideoModel | 439 | notification.Video = video |
406 | 440 | ||
407 | return notification | 441 | return notification |
408 | } | 442 | } |
@@ -420,17 +454,17 @@ class Notifier { | |||
420 | 454 | ||
421 | 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) |
422 | 456 | ||
423 | function settingGetter (user: UserModel) { | 457 | function settingGetter (user: MUserWithNotificationSetting) { |
424 | return user.NotificationSetting.myVideoPublished | 458 | return user.NotificationSetting.myVideoPublished |
425 | } | 459 | } |
426 | 460 | ||
427 | async function notificationCreator (user: UserModel) { | 461 | async function notificationCreator (user: MUserWithNotificationSetting) { |
428 | const notification = await UserNotificationModel.create({ | 462 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
429 | type: UserNotificationType.MY_VIDEO_PUBLISHED, | 463 | type: UserNotificationType.MY_VIDEO_PUBLISHED, |
430 | userId: user.id, | 464 | userId: user.id, |
431 | videoId: video.id | 465 | videoId: video.id |
432 | }) | 466 | }) |
433 | notification.Video = video as VideoModel | 467 | notification.Video = video |
434 | 468 | ||
435 | return notification | 469 | return notification |
436 | } | 470 | } |
@@ -448,17 +482,17 @@ class Notifier { | |||
448 | 482 | ||
449 | 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()) |
450 | 484 | ||
451 | function settingGetter (user: UserModel) { | 485 | function settingGetter (user: MUserWithNotificationSetting) { |
452 | return user.NotificationSetting.myVideoImportFinished | 486 | return user.NotificationSetting.myVideoImportFinished |
453 | } | 487 | } |
454 | 488 | ||
455 | async function notificationCreator (user: UserModel) { | 489 | async function notificationCreator (user: MUserWithNotificationSetting) { |
456 | const notification = await UserNotificationModel.create({ | 490 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
457 | 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, |
458 | userId: user.id, | 492 | userId: user.id, |
459 | videoImportId: videoImport.id | 493 | videoImportId: videoImport.id |
460 | }) | 494 | }) |
461 | notification.VideoImport = videoImport as VideoImportModel | 495 | notification.VideoImport = videoImport |
462 | 496 | ||
463 | return notification | 497 | return notification |
464 | } | 498 | } |
@@ -472,7 +506,7 @@ class Notifier { | |||
472 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | 506 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) |
473 | } | 507 | } |
474 | 508 | ||
475 | private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserAccount) { | 509 | private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) { |
476 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) | 510 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) |
477 | if (moderators.length === 0) return | 511 | if (moderators.length === 0) return |
478 | 512 | ||
@@ -481,17 +515,17 @@ class Notifier { | |||
481 | moderators.length, registeredUser.username | 515 | moderators.length, registeredUser.username |
482 | ) | 516 | ) |
483 | 517 | ||
484 | function settingGetter (user: UserModel) { | 518 | function settingGetter (user: MUserWithNotificationSetting) { |
485 | return user.NotificationSetting.newUserRegistration | 519 | return user.NotificationSetting.newUserRegistration |
486 | } | 520 | } |
487 | 521 | ||
488 | async function notificationCreator (user: UserModel) { | 522 | async function notificationCreator (user: MUserWithNotificationSetting) { |
489 | const notification = await UserNotificationModel.create({ | 523 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
490 | type: UserNotificationType.NEW_USER_REGISTRATION, | 524 | type: UserNotificationType.NEW_USER_REGISTRATION, |
491 | userId: user.id, | 525 | userId: user.id, |
492 | accountId: registeredUser.Account.id | 526 | accountId: registeredUser.Account.id |
493 | }) | 527 | }) |
494 | notification.Account = registeredUser.Account as AccountModel | 528 | notification.Account = registeredUser.Account |
495 | 529 | ||
496 | return notification | 530 | return notification |
497 | } | 531 | } |
@@ -503,11 +537,11 @@ class Notifier { | |||
503 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | 537 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) |
504 | } | 538 | } |
505 | 539 | ||
506 | private async notify (options: { | 540 | private async notify <T extends MUserWithNotificationSetting> (options: { |
507 | users: MUserWithNotificationSetting[], | 541 | users: T[], |
508 | notificationCreator: (user: MUserWithNotificationSetting) => Promise<UserNotificationModelForApi>, | 542 | notificationCreator: (user: T) => Promise<UserNotificationModelForApi>, |
509 | emailSender: (emails: string[]) => Promise<any> | Bluebird<any>, | 543 | emailSender: (emails: string[]) => Promise<any> | Bluebird<any>, |
510 | settingGetter: (user: MUserWithNotificationSetting) => UserNotificationSettingValue | 544 | settingGetter: (user: T) => UserNotificationSettingValue |
511 | }) { | 545 | }) { |
512 | const emails: string[] = [] | 546 | const emails: string[] = [] |
513 | 547 | ||