From 4dc9365acb86ba47697da81ed867453b3123a424 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Fri, 3 Jun 2022 04:18:06 -0600 Subject: [PATCH] Update AccountController, dispatch Accept Follow activity if applicable --- app/FollowRequest.php | 10 +-- app/Http/Controllers/AccountController.php | 10 ++- .../FollowPipeline/FollowAcceptPipeline.php | 69 +++++++++++++++++++ .../ActivityPub/Verb/AcceptFollow.php | 2 +- 4 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 app/Jobs/FollowPipeline/FollowAcceptPipeline.php diff --git a/app/FollowRequest.php b/app/FollowRequest.php index 7852e268..c1b0ac75 100644 --- a/app/FollowRequest.php +++ b/app/FollowRequest.php @@ -11,6 +11,11 @@ class FollowRequest extends Model protected $casts = [ 'activity' => 'array', ]; + + public function actor() + { + return $this->belongsTo(Profile::class, 'follower_id', 'id'); + } public function follower() { @@ -22,11 +27,6 @@ class FollowRequest extends Model return $this->belongsTo(Profile::class, 'following_id', 'id'); } - public function actor() - { - return $this->belongsTo(Profile::class, 'follower_id', 'id'); - } - public function target() { return $this->belongsTo(Profile::class, 'following_id', 'id'); diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index fc3ff69d..66e8655e 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -29,6 +29,7 @@ use App\Transformer\Api\Mastodon\v1\AccountTransformer; use App\Services\AccountService; use App\Services\UserFilterService; use App\Services\RelationshipService; +use App\Jobs\FollowPipeline\FollowAcceptPipeline; class AccountController extends Controller { @@ -394,8 +395,13 @@ class AccountController extends Controller $follow->profile_id = $follower->id; $follow->following_id = $pid; $follow->save(); - FollowPipeline::dispatch($follow); - $followRequest->delete(); + + if($follower->domain != null && $follower->private_key === null) { + FollowAcceptPipeline::dispatch($followRequest); + } else { + FollowPipeline::dispatch($follow); + $followRequest->delete(); + } break; case 'reject': diff --git a/app/Jobs/FollowPipeline/FollowAcceptPipeline.php b/app/Jobs/FollowPipeline/FollowAcceptPipeline.php new file mode 100644 index 00000000..b1c13e2a --- /dev/null +++ b/app/Jobs/FollowPipeline/FollowAcceptPipeline.php @@ -0,0 +1,69 @@ +followRequest = $followRequest; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $follow = $this->followRequest; + $actor = $follow->actor; + $target = $follow->target; + + if($actor->domain == null || $actor->inbox_url == null || !$target->private_key) { + return; + } + + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Item($follow, new AcceptFollow()); + $activity = $fractal->createData($resource)->toArray(); + $url = $actor->sharedInbox ?? $actor->inbox_url; + + Helpers::sendSignedObject($target, $url, $activity); + + $follow->delete(); + + return; + } +} diff --git a/app/Transformer/ActivityPub/Verb/AcceptFollow.php b/app/Transformer/ActivityPub/Verb/AcceptFollow.php index cb5b54a4..014e3cfa 100644 --- a/app/Transformer/ActivityPub/Verb/AcceptFollow.php +++ b/app/Transformer/ActivityPub/Verb/AcceptFollow.php @@ -16,7 +16,7 @@ class AcceptFollow extends Fractal\TransformerAbstract 'actor' => $follow->target->permalink(), 'object' => [ 'type' => 'Follow', - 'id' => $follow->activity ? $follow->activity['id'] : null, + 'id' => $follow->activity && isset($follow->activity['id']) ? $follow->activity['id'] : null, 'actor' => $follow->actor->permalink(), 'object' => $follow->target->permalink() ]