1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Services/WebfingerService.php

69 lines
1.6 KiB
PHP
Raw Normal View History

2020-02-06 08:40:12 +00:00
<?php
namespace App\Services;
use Cache;
2020-03-17 04:40:03 +00:00
use App\Profile;
2020-02-06 08:40:12 +00:00
use App\Util\Webfinger\WebfingerUrl;
2022-01-23 02:37:50 +00:00
use Illuminate\Support\Facades\Http;
2020-02-06 08:40:12 +00:00
use App\Util\ActivityPub\Helpers;
2022-01-23 02:42:37 +00:00
use App\Services\AccountService;
2020-02-06 08:40:12 +00:00
class WebfingerService
{
public static function lookup($query, $mastodonMode = false)
2020-02-06 08:40:12 +00:00
{
return (new self)->run($query, $mastodonMode);
2020-02-06 08:40:12 +00:00
}
protected function run($query, $mastodonMode)
2020-02-06 08:40:12 +00:00
{
2020-03-17 04:40:03 +00:00
if($profile = Profile::whereUsername($query)->first()) {
return $mastodonMode ?
AccountService::getMastodon($profile->id, true) :
AccountService::get($profile->id);
2020-03-17 04:40:03 +00:00
}
2020-02-06 08:40:12 +00:00
$url = WebfingerUrl::generateWebfingerUrl($query);
if(!Helpers::validateUrl($url)) {
return [];
}
2022-01-23 02:37:50 +00:00
2022-01-23 02:42:37 +00:00
$res = Http::retry(3, 100)
2022-01-23 02:37:50 +00:00
->acceptJson()
->withHeaders([
'User-Agent' => '(Pixelfed/' . config('pixelfed.version') . '; +' . config('app.url') . ')'
])
->timeout(20)
->get($url);
if(!$res->successful()) {
2020-02-06 08:40:12 +00:00
return [];
}
2022-01-23 02:37:50 +00:00
$webfinger = $res->json();
if(!isset($webfinger['links']) || !is_array($webfinger['links']) || empty($webfinger['links'])) {
2022-01-23 02:42:37 +00:00
return [];
2022-01-23 02:37:50 +00:00
}
$link = collect($webfinger['links'])
->filter(function($link) {
return $link &&
isset($link['rel']) &&
2022-01-23 02:37:50 +00:00
isset($link['type']) &&
isset($link['href']) &&
$link['rel'] == 'self' &&
$link['type'] == 'application/activity+json' || $link['type'] == 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
2022-01-23 02:37:50 +00:00
})
->pluck('href')
->first();
$profile = Helpers::profileFetch($link);
if(!$profile) {
return;
}
return $mastodonMode ?
AccountService::getMastodon($profile->id, true) :
AccountService::get($profile->id);
2020-02-06 08:40:12 +00:00
}
2022-01-23 02:37:50 +00:00
}