From 883993c81ecc2388d4a4b37b29b81b6de73d264f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 8 Apr 2019 17:26:01 +0200 Subject: Add notification on new instance follower (server side) --- server/lib/notifier.ts | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'server/lib/notifier.ts') diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index 9fe93ec0d..91b71cc64 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts @@ -92,18 +92,25 @@ class Notifier { .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) } - notifyOfNewFollow (actorFollow: ActorFollowModel): void { + notifyOfNewUserFollow (actorFollow: ActorFollowModel): void { this.notifyUserOfNewActorFollow(actorFollow) .catch(err => { logger.error( 'Cannot notify owner of channel %s of a new follow by %s.', actorFollow.ActorFollowing.VideoChannel.getDisplayName(), actorFollow.ActorFollower.Account.getDisplayName(), - err + { err } ) }) } + notifyOfNewInstanceFollow (actorFollow: ActorFollowModel): void { + this.notifyAdminsOfNewInstanceFollow(actorFollow) + .catch(err => { + logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) + }) + } + private async notifySubscribersOfNewVideo (video: VideoModel) { // List all followers that are users const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) @@ -261,6 +268,33 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } + private async notifyAdminsOfNewInstanceFollow (actorFollow: ActorFollowModel) { + const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) + + logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url) + + function settingGetter (user: UserModel) { + return user.NotificationSetting.newInstanceFollower + } + + async function notificationCreator (user: UserModel) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.NEW_INSTANCE_FOLLOWER, + userId: user.id, + actorFollowId: actorFollow.id + }) + notification.ActorFollow = actorFollow + + return notification + } + + function emailSender (emails: string[]) { + return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow) + } + + return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) + } + private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) { const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) if (moderators.length === 0) return -- cgit v1.2.3