WebfingerService.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use App\Profile;
  5. use App\Util\Webfinger\WebfingerUrl;
  6. use Illuminate\Support\Facades\Http;
  7. use App\Util\ActivityPub\Helpers;
  8. use App\Services\AccountService;
  9. class WebfingerService
  10. {
  11. public static function lookup($query)
  12. {
  13. return (new self)->run($query);
  14. }
  15. protected function run($query)
  16. {
  17. if($profile = Profile::whereUsername($query)->first()) {
  18. return AccountService::get($profile->id);
  19. }
  20. $url = WebfingerUrl::generateWebfingerUrl($query);
  21. if(!Helpers::validateUrl($url)) {
  22. return [];
  23. }
  24. $res = Http::retry(3, 100)
  25. ->acceptJson()
  26. ->withHeaders([
  27. 'User-Agent' => '(Pixelfed/' . config('pixelfed.version') . '; +' . config('app.url') . ')'
  28. ])
  29. ->timeout(20)
  30. ->get($url);
  31. if(!$res->successful()) {
  32. return [];
  33. }
  34. $webfinger = $res->json();
  35. if(!isset($webfinger['links']) || !is_array($webfinger['links']) || empty($webfinger['links'])) {
  36. return [];
  37. }
  38. $link = collect($webfinger['links'])
  39. ->filter(function($link) {
  40. return $link &&
  41. isset($link['type']) &&
  42. isset($link['href']) &&
  43. $link['type'] == 'application/activity+json';
  44. })
  45. ->pluck('href')
  46. ->first();
  47. $profile = Helpers::profileFetch($link);
  48. return AccountService::get($profile->id);
  49. }
  50. }