From f0455b0454367d58584e60b08d36afab9a7b649a Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 10 Jun 2019 21:30:01 -0600 Subject: [PATCH] Update FollowerController, send Follow ActivityPub object to remote instances --- app/Http/Controllers/FollowerController.php | 33 +++++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/FollowerController.php b/app/Http/Controllers/FollowerController.php index 1fcb236d4..cf2ea45a9 100644 --- a/app/Http/Controllers/FollowerController.php +++ b/app/Http/Controllers/FollowerController.php @@ -11,6 +11,7 @@ use App\{ use Auth, Cache; use Illuminate\Http\Request; use App\Jobs\FollowPipeline\FollowPipeline; +use App\Util\ActivityPub\Helpers; class FollowerController extends Controller { @@ -55,13 +56,21 @@ class FollowerController extends Controller $isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->count(); if($private == true && $isFollowing == 0 || $remote == true) { + if($user->following()->count() >= Follower::MAX_FOLLOWING) { + abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts'); + } + + if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) { + abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour'); + } + $follow = FollowRequest::firstOrCreate([ 'follower_id' => $user->id, 'following_id' => $target->id ]); - if($remote == true) { - - } + if($remote == true && config('federation.activitypub.remoteFollow') == true) { + $this->sendFollow($user, $target); + } } elseif ($isFollowing == 0) { if($user->following()->count() >= Follower::MAX_FOLLOWING) { abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts'); @@ -88,4 +97,22 @@ class FollowerController extends Controller Cache::forget('user:account:id:'.$target->user_id); Cache::forget('user:account:id:'.$user->user_id); } + + protected function sendFollow($user, $target) + { + if($target->domain == null || $user->domain != null) { + return; + } + + $payload = [ + '@context' => 'https://www.w3.org/ns/activitystreams', + 'type' => 'Follow', + 'actor' => $user->permalink(), + 'object' => $target->permalink() + ]; + + $inbox = $target->sharedInbox ?? $target->inbox_url; + + Helpers::sendSignedObject($user, $inbox, $payload); + } }