From aff7456639a2ba8813186d0df528730ac4a96cd3 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 22 Jan 2022 19:37:50 -0700 Subject: [PATCH 1/3] Update WebfingerService. Fixes #3167 --- app/Services/WebfingerService.php | 35 +++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/app/Services/WebfingerService.php b/app/Services/WebfingerService.php index 1269dd0eb..3615018bc 100644 --- a/app/Services/WebfingerService.php +++ b/app/Services/WebfingerService.php @@ -6,7 +6,7 @@ use Cache; use App\Profile; use Illuminate\Support\Facades\Redis; use App\Util\Webfinger\WebfingerUrl; -use Zttp\Zttp; +use Illuminate\Support\Facades\Http; use App\Util\ActivityPub\Helpers; use App\Transformer\Api\AccountTransformer; use League\Fractal; @@ -32,16 +32,39 @@ class WebfingerService if(!Helpers::validateUrl($url)) { return []; } - $res = Zttp::get($url); - $webfinger = $res->json(); - if(!isset($webfinger['links'])) { + + $res = Http::retry(3, 500) + ->acceptJson() + ->withHeaders([ + 'User-Agent' => '(Pixelfed/' . config('pixelfed.version') . '; +' . config('app.url') . ')' + ]) + ->timeout(20) + ->get($url); + + if(!$res->successful()) { return []; } - $profile = Helpers::profileFetch($webfinger['links'][0]['href']); + + $webfinger = $res->json(); + if(!isset($webfinger['links']) || !is_array($webfinger['links']) || empty($webfinger['links'])) { + return ['nolinks']; + } + + $link = collect($webfinger['links']) + ->filter(function($link) { + return $link && + isset($link['type']) && + isset($link['href']) && + $link['type'] == 'application/activity+json'; + }) + ->pluck('href') + ->first(); + + $profile = Helpers::profileFetch($link); $fractal = new Fractal\Manager(); $fractal->setSerializer(new ArraySerializer()); $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); $res = $fractal->createData($resource)->toArray(); return $res; } -} \ No newline at end of file +} From 127777261af45263977f0c23e75e05721fe07478 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 22 Jan 2022 19:38:25 -0700 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fe51ec19..d52d82fa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Updated - Updated MediaStorageService, fix remote avatar bug. ([1c20d696](https://github.com/pixelfed/pixelfed/commit/1c20d696)) +- Updated WebfingerService. Fixes #3167. ([aff74566](https://github.com/pixelfed/pixelfed/commit/aff74566)) ## [v0.11.2 (2022-01-09)](https://github.com/pixelfed/pixelfed/compare/v0.11.1...v0.11.2) From 792212b6554aa5990a761920bf78bc6a35962c04 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 22 Jan 2022 19:42:37 -0700 Subject: [PATCH 3/3] Cleanup --- app/Services/WebfingerService.php | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/app/Services/WebfingerService.php b/app/Services/WebfingerService.php index 3615018bc..c7eb9e93f 100644 --- a/app/Services/WebfingerService.php +++ b/app/Services/WebfingerService.php @@ -4,14 +4,10 @@ namespace App\Services; use Cache; use App\Profile; -use Illuminate\Support\Facades\Redis; use App\Util\Webfinger\WebfingerUrl; use Illuminate\Support\Facades\Http; use App\Util\ActivityPub\Helpers; -use App\Transformer\Api\AccountTransformer; -use League\Fractal; -use League\Fractal\Serializer\ArraySerializer; -use League\Fractal\Pagination\IlluminatePaginatorAdapter; +use App\Services\AccountService; class WebfingerService { @@ -23,17 +19,14 @@ class WebfingerService protected function run($query) { if($profile = Profile::whereUsername($query)->first()) { - $fractal = new Fractal\Manager(); - $fractal->setSerializer(new ArraySerializer()); - $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); - return $fractal->createData($resource)->toArray(); + return AccountService::get($profile->id); } $url = WebfingerUrl::generateWebfingerUrl($query); if(!Helpers::validateUrl($url)) { return []; } - $res = Http::retry(3, 500) + $res = Http::retry(3, 100) ->acceptJson() ->withHeaders([ 'User-Agent' => '(Pixelfed/' . config('pixelfed.version') . '; +' . config('app.url') . ')' @@ -47,7 +40,7 @@ class WebfingerService $webfinger = $res->json(); if(!isset($webfinger['links']) || !is_array($webfinger['links']) || empty($webfinger['links'])) { - return ['nolinks']; + return []; } $link = collect($webfinger['links']) @@ -61,10 +54,6 @@ class WebfingerService ->first(); $profile = Helpers::profileFetch($link); - $fractal = new Fractal\Manager(); - $fractal->setSerializer(new ArraySerializer()); - $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); - $res = $fractal->createData($resource)->toArray(); - return $res; + return AccountService::get($profile->id); } }