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/activitypub/actor.ts | 2 ++ server/lib/activitypub/process/process-follow.ts | 19 +++++++---- server/lib/emailer.ts | 18 ++++++++++ .../lib/job-queue/handlers/activitypub-follow.ts | 2 +- server/lib/notifier.ts | 38 ++++++++++++++++++++-- server/lib/user.ts | 3 +- 6 files changed, 71 insertions(+), 11 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index 63e810642..c0ad07a52 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -342,6 +342,8 @@ function saveActorAndServerAndModelIfNotExist ( actorCreated.VideoChannel.Account = ownerActor.Account } + actorCreated.Server = server + return actorCreated } } diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index 140bbe9f1..276a57e60 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts @@ -24,14 +24,16 @@ export { // --------------------------------------------------------------------------- async function processFollow (actor: ActorModel, targetActorURL: string) { - const { actorFollow, created } = await sequelizeTypescript.transaction(async t => { + const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => { const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) if (!targetActor) throw new Error('Unknown actor') if (targetActor.isOwned() === false) throw new Error('This is not a local actor.') const serverActor = await getServerActor() - if (targetActor.id === serverActor.id && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) { + const isFollowingInstance = targetActor.id === serverActor.id + + if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) { logger.info('Rejecting %s because instance followers are disabled.', targetActor.url) return sendReject(actor, targetActor) @@ -50,9 +52,6 @@ async function processFollow (actor: ActorModel, targetActorURL: string) { transaction: t }) - actorFollow.ActorFollower = actor - actorFollow.ActorFollowing = targetActor - if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) { actorFollow.state = 'accepted' await actorFollow.save({ transaction: t }) @@ -64,10 +63,16 @@ async function processFollow (actor: ActorModel, targetActorURL: string) { // Target sends to actor he accepted the follow request if (actorFollow.state === 'accepted') await sendAccept(actorFollow) - return { actorFollow, created } + return { actorFollow, created, isFollowingInstance } }) - if (created) Notifier.Instance.notifyOfNewFollow(actorFollow) + // Rejected + if (!actorFollow) return + + if (created) { + if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow) + else Notifier.Instance.notifyOfNewUserFollow(actorFollow) + } logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url) } diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index eec97c27e..aa9083362 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -129,6 +129,24 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } + addNewInstanceFollowerNotification (to: string[], actorFollow: ActorFollowModel) { + const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : '' + + const text = `Hi dear admin,\n\n` + + `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` + + `\n\n` + + `Cheers,\n` + + `PeerTube.` + + const emailPayload: EmailPayload = { + to, + subject: 'New instance follower', + text + } + + return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) + } + myVideoPublishedNotification (to: string[], video: VideoModel) { const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() diff --git a/server/lib/job-queue/handlers/activitypub-follow.ts b/server/lib/job-queue/handlers/activitypub-follow.ts index b4d381062..e7e5ff950 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts @@ -73,5 +73,5 @@ async function follow (fromActor: ActorModel, targetActor: ActorModel) { return actorFollow }) - if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewFollow(actorFollow) + if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollow) } 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 diff --git a/server/lib/user.ts b/server/lib/user.ts index 5588b0f76..6fbe3ed03 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -110,7 +110,8 @@ function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Tr blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, newUserRegistration: UserNotificationSettingValue.WEB, commentMention: UserNotificationSettingValue.WEB, - newFollow: UserNotificationSettingValue.WEB + newFollow: UserNotificationSettingValue.WEB, + newInstanceFollower: UserNotificationSettingValue.WEB } return UserNotificationSettingModel.create(values, { transaction: t }) -- cgit v1.2.3