1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-22 15:55:14 +00:00
pixelfed/app/Services/WebfingerService.php

97 lines
2.9 KiB
PHP
Raw Normal View History

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