Update SearchApiV2Service, resolve remote queries

This commit is contained in:
Daniel Supernault 2022-01-09 23:15:45 -07:00
parent 9030c77800
commit c8a667f20e
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
1 changed files with 74 additions and 14 deletions

View File

@ -28,12 +28,14 @@ class SearchApiV2Service
protected function run($query)
{
$this->query = $query;
$q = urldecode($query->input('q'));
if($query->has('resolve') &&
$query->resolve == true &&
Helpers::validateUrl(urldecode($query->input('q')))
( Str::startsWith($q, 'https://') ||
Str::substrCount($q, '@') == 2)
) {
return $this->resolve();
return $this->resolveQuery();
}
if($query->has('type')) {
@ -77,16 +79,6 @@ class SearchApiV2Service
];
}
protected function resolve()
{
$query = urldecode($this->query->input('q'));
if(Str::startsWith($query, '@') == true) {
return WebfingerService::lookup($this->query->input('q'));
} else if (Str::startsWith($query, 'https://') == true) {
return $this->resolveQuery();
}
}
protected function accounts()
{
$user = request()->user();
@ -163,11 +155,79 @@ class SearchApiV2Service
return $this->resolveLocalProfile();
}
} else {
return [
$default = [
'accounts' => [],
'hashtags' => [],
'statuses' => []
'statuses' => [],
];
if(!Helpers::validateUrl($query) && strpos($query, '@') == -1) {
return $default;
}
if(Str::substrCount($query, '@') == 2) {
try {
$res = WebfingerService::lookup($query);
} catch (\Exception $e) {
return $default;
}
if($res && isset($res['id'])) {
$default['accounts'][] = $res;
return $default;
} else {
return $default;
}
}
try {
$res = ActivityPubFetchService::get($query);
if($res) {
$json = json_decode($res, true);
if(!$json || !isset($json['@context']) || !isset($json['type']) || !in_array($json['type'], ['Note', 'Person'])) {
return [
'accounts' => [],
'hashtags' => [],
'statuses' => [],
];
}
switch($json['type']) {
case 'Note':
$obj = Helpers::statusFetch($query);
if(!$obj) {
return $default;
}
$default['statuses'][] = StatusService::get($obj['id']);
return $default;
break;
case 'Person':
$obj = Helpers::profileFetch($query);
if(!$obj) {
return $default;
}
$default['accounts'][] = AccountService::get($obj['id']);
return $default;
break;
default:
return [
'accounts' => [],
'hashtags' => [],
'statuses' => [],
];
break;
}
}
} catch (\Exception $e) {
return [
'accounts' => [],
'hashtags' => [],
'statuses' => [],
];
}
return $default;
}
}