1
0
Fork 0

Update ApiV1Controller, fix search v2 entities

This commit is contained in:
Daniel Supernault 2022-03-23 05:58:14 -06:00
parent a87f8301b1
commit 9dac861ebe
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
3 changed files with 48 additions and 28 deletions

View File

@ -2513,7 +2513,7 @@ class ApiV1Controller extends Controller
'following' => 'nullable' 'following' => 'nullable'
]); ]);
return $this->json(SearchApiV2Service::query($request)); return $this->json(SearchApiV2Service::query($request, true));
} }
/** /**

View File

@ -19,9 +19,11 @@ use App\Services\StatusService;
class SearchApiV2Service class SearchApiV2Service
{ {
private $query; private $query;
static $mastodonMode = false;
public static function query($query) public static function query($query, $mastodonMode = false)
{ {
self::$mastodonMode = $mastodonMode;
return (new self)->run($query); return (new self)->run($query);
} }
@ -81,6 +83,7 @@ class SearchApiV2Service
protected function accounts() protected function accounts()
{ {
$mastodonMode = self::$mastodonMode;
$user = request()->user(); $user = request()->user();
$limit = $this->query->input('limit') ?? 20; $limit = $this->query->input('limit') ?? 20;
$offset = $this->query->input('offset') ?? 0; $offset = $this->query->input('offset') ?? 0;
@ -93,6 +96,7 @@ class SearchApiV2Service
->where('followers.profile_id', $user->profile_id); ->where('followers.profile_id', $user->profile_id);
}) })
->where('username', 'like', $query) ->where('username', 'like', $query)
->orderBy('domain')
->orderByDesc('profiles.followers_count') ->orderByDesc('profiles.followers_count')
->orderByDesc('followers.created_at') ->orderByDesc('followers.created_at')
->offset($offset) ->offset($offset)
@ -101,8 +105,10 @@ class SearchApiV2Service
->filter(function($profile) use ($banned) { ->filter(function($profile) use ($banned) {
return in_array($profile->domain, $banned) == false; return in_array($profile->domain, $banned) == false;
}) })
->map(function($res) { ->map(function($res) use($mastodonMode) {
return AccountService::get($res['id']); return $mastodonMode ?
AccountService::getMastodon($res['id']) :
AccountService::get($res['id']);
}) })
->filter(function($account) { ->filter(function($account) {
return $account && isset($account['id']); return $account && isset($account['id']);
@ -114,6 +120,7 @@ class SearchApiV2Service
protected function hashtags() protected function hashtags()
{ {
$mastodonMode = self::$mastodonMode;
$limit = $this->query->input('limit') ?? 20; $limit = $this->query->input('limit') ?? 20;
$offset = $this->query->input('offset') ?? 0; $offset = $this->query->input('offset') ?? 0;
$query = '%' . $this->query->input('q') . '%'; $query = '%' . $this->query->input('q') . '%';
@ -122,13 +129,18 @@ class SearchApiV2Service
->offset($offset) ->offset($offset)
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($tag) { ->map(function($tag) use($mastodonMode) {
return [ $res = [
'name' => $tag->name, 'name' => $tag->name,
'url' => $tag->url(), 'url' => $tag->url()
'count' => HashtagService::count($tag->id),
'history' => []
]; ];
if(!$mastodonMode) {
$res['history'] = [];
$res['count'] = HashtagService::count($tag->id);
}
return $res;
}); });
} }
@ -140,6 +152,7 @@ class SearchApiV2Service
protected function statusesById() protected function statusesById()
{ {
$mastodonMode = self::$mastodonMode;
$accountId = $this->query->input('account_id'); $accountId = $this->query->input('account_id');
$limit = $this->query->input('limit', 20); $limit = $this->query->input('limit', 20);
$query = '%' . $this->query->input('q') . '%'; $query = '%' . $this->query->input('q') . '%';
@ -147,8 +160,10 @@ class SearchApiV2Service
->whereProfileId($accountId) ->whereProfileId($accountId)
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($status) { ->map(function($status) use($mastodonMode) {
return StatusService::get($status->id); return $mastodonMode ?
StatusService::getMastodon($status->id) :
StatusService::get($status->id);
}) })
->filter(function($status) { ->filter(function($status) {
return $status && isset($status['account']); return $status && isset($status['account']);
@ -158,6 +173,7 @@ class SearchApiV2Service
protected function resolveQuery() protected function resolveQuery()
{ {
$mastodonMode = self::$mastodonMode;
$query = urldecode($this->query->input('q')); $query = urldecode($this->query->input('q'));
if(Helpers::validateLocalUrl($query)) { if(Helpers::validateLocalUrl($query)) {
if(Str::contains($query, '/p/')) { if(Str::contains($query, '/p/')) {
@ -177,7 +193,7 @@ class SearchApiV2Service
if(Str::substrCount($query, '@') == 2) { if(Str::substrCount($query, '@') == 2) {
try { try {
$res = WebfingerService::lookup($query); $res = WebfingerService::lookup($query, $mastodonMode);
} catch (\Exception $e) { } catch (\Exception $e) {
return $default; return $default;
} }
@ -209,7 +225,9 @@ class SearchApiV2Service
if(!$obj || !isset($obj['id'])) { if(!$obj || !isset($obj['id'])) {
return $default; return $default;
} }
$note = StatusService::get($obj['id']); $note = $mastodonMode ?
StatusService::getMastodon($obj['id']) :
StatusService::get($obj['id']);
if(!$note) { if(!$note) {
return $default; return $default;
} }
@ -225,7 +243,9 @@ class SearchApiV2Service
if(in_array($obj['domain'], $banned)) { if(in_array($obj['domain'], $banned)) {
return $default; return $default;
} }
$default['accounts'][] = AccountService::get($obj['id']); $default['accounts'][] = $mastodonMode ?
AccountService::getMastodon($obj['id']) :
AccountService::get($obj['id']);
return $default; return $default;
break; break;
@ -254,10 +274,7 @@ class SearchApiV2Service
{ {
$query = urldecode($this->query->input('q')); $query = urldecode($this->query->input('q'));
$query = last(explode('/', $query)); $query = last(explode('/', $query));
$status = Status::whereNull('uri') $status = StatusService::getMastodon($query);
->whereScope('public')
->find($query);
if(!$status) { if(!$status) {
return [ return [
'accounts' => [], 'accounts' => [],
@ -266,14 +283,13 @@ class SearchApiV2Service
]; ];
} }
$fractal = new Fractal\Manager(); $res = [
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($status, new StatusTransformer());
return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => $fractal->createData($resource)->toArray() 'statuses' => [$status]
]; ];
return $res;
} }
protected function resolveLocalProfile() protected function resolveLocalProfile()

View File

@ -11,15 +11,17 @@ use App\Services\AccountService;
class WebfingerService class WebfingerService
{ {
public static function lookup($query) public static function lookup($query, $mastodonMode = false)
{ {
return (new self)->run($query); return (new self)->run($query, $mastodonMode);
} }
protected function run($query) protected function run($query, $mastodonMode)
{ {
if($profile = Profile::whereUsername($query)->first()) { if($profile = Profile::whereUsername($query)->first()) {
return AccountService::get($profile->id); return $mastodonMode ?
AccountService::getMastodon($profile->id, true) :
AccountService::get($profile->id);
} }
$url = WebfingerUrl::generateWebfingerUrl($query); $url = WebfingerUrl::generateWebfingerUrl($query);
if(!Helpers::validateUrl($url)) { if(!Helpers::validateUrl($url)) {
@ -54,6 +56,8 @@ class WebfingerService
->first(); ->first();
$profile = Helpers::profileFetch($link); $profile = Helpers::profileFetch($link);
return AccountService::get($profile->id); return $mastodonMode ?
AccountService::getMastodon($profile->id, true) :
AccountService::get($profile->id);
} }
} }