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 Illuminate\Support\Facades\Redis;
|
|
|
|
use App\Util\Webfinger\WebfingerUrl;
|
|
|
|
use Zttp\Zttp;
|
|
|
|
use App\Util\ActivityPub\Helpers;
|
|
|
|
use App\Transformer\Api\AccountTransformer;
|
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
|
|
|
|
class WebfingerService
|
|
|
|
{
|
|
|
|
public static function lookup($query)
|
|
|
|
{
|
|
|
|
return (new self)->run($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function run($query)
|
|
|
|
{
|
2020-03-17 04:40:03 +00:00
|
|
|
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();
|
|
|
|
}
|
2020-02-06 08:40:12 +00:00
|
|
|
$url = WebfingerUrl::generateWebfingerUrl($query);
|
|
|
|
if(!Helpers::validateUrl($url)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$res = Zttp::get($url);
|
|
|
|
$webfinger = $res->json();
|
|
|
|
if(!isset($webfinger['links'])) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$profile = Helpers::profileFetch($webfinger['links'][0]['href']);
|
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
|
|
|
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
|
2020-02-07 00:11:59 +00:00
|
|
|
$res = $fractal->createData($resource)->toArray();
|
2020-02-06 08:40:12 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
}
|