Update SearchApiV2Service, improve resolve query logic to better handle remote posts/profiles and local posts/profiles

This commit is contained in:
Daniel Supernault 2023-07-14 01:22:49 -06:00
parent 0704c7e05e
commit c61d0b915f
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -179,8 +179,10 @@ class SearchApiV2Service
return $default; return $default;
} }
if(Helpers::validateLocalUrl($query)) { if(Helpers::validateLocalUrl($query)) {
if(Str::contains($query, '/p/')) { if(Str::contains($query, '/p/') || Str::contains($query, 'i/web/post/')) {
return $this->resolveLocalStatus(); return $this->resolveLocalStatus();
} else if(Str::contains($query, 'i/web/profile/')) {
return $this->resolveLocalProfileId();
} else { } else {
return $this->resolveLocalProfile(); return $this->resolveLocalProfile();
} }
@ -217,6 +219,14 @@ class SearchApiV2Service
} }
} }
if($sid = Status::whereUri($query)->first()) {
$s = StatusService::get($sid->id, false);
if(in_array($s['visibility'], ['public', 'unlisted'])) {
$default['statuses'][] = $s;
return $default;
}
}
try { try {
$res = ActivityPubFetchService::get($query); $res = ActivityPubFetchService::get($query);
$banned = InstanceService::getBannedDomains(); $banned = InstanceService::getBannedDomains();
@ -238,11 +248,14 @@ class SearchApiV2Service
return $default; return $default;
} }
$note = $mastodonMode ? $note = $mastodonMode ?
StatusService::getMastodon($obj['id']) : StatusService::getMastodon($obj['id'], false) :
StatusService::get($obj['id']); StatusService::get($obj['id'], false);
if(!$note) { if(!$note) {
return $default; return $default;
} }
if(!isset($note['visibility']) || !in_array($note['visibility'], ['public', 'unlisted'])) {
return $default;
}
$default['statuses'][] = $note; $default['statuses'][] = $note;
return $default; return $default;
break; break;
@ -256,8 +269,8 @@ class SearchApiV2Service
return $default; return $default;
} }
$default['accounts'][] = $mastodonMode ? $default['accounts'][] = $mastodonMode ?
AccountService::getMastodon($obj['id']) : AccountService::getMastodon($obj['id'], true) :
AccountService::get($obj['id']); AccountService::get($obj['id'], true);
return $default; return $default;
break; break;
@ -285,9 +298,9 @@ class SearchApiV2Service
protected function resolveLocalStatus() protected function resolveLocalStatus()
{ {
$query = urldecode($this->query->input('q')); $query = urldecode($this->query->input('q'));
$query = last(explode('/', $query)); $query = last(explode('/', parse_url($query, PHP_URL_PATH)));
$status = StatusService::getMastodon($query); $status = StatusService::getMastodon($query, false);
if(!$status) { if(!$status || !in_array($status['visibility'], ['public', 'unlisted'])) {
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
@ -307,7 +320,7 @@ class SearchApiV2Service
protected function resolveLocalProfile() protected function resolveLocalProfile()
{ {
$query = urldecode($this->query->input('q')); $query = urldecode($this->query->input('q'));
$query = last(explode('/', $query)); $query = last(explode('/', parse_url($query, PHP_URL_PATH)));
$profile = Profile::whereNull('status') $profile = Profile::whereNull('status')
->whereNull('domain') ->whereNull('domain')
->whereUsername($query) ->whereUsername($query)
@ -325,7 +338,32 @@ class SearchApiV2Service
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($profile, new AccountTransformer()); $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return [ return [
'accounts' => $fractal->createData($resource)->toArray(), 'accounts' => [$fractal->createData($resource)->toArray()],
'hashtags' => [],
'statuses' => []
];
}
protected function resolveLocalProfileId()
{
$query = urldecode($this->query->input('q'));
$query = last(explode('/', parse_url($query, PHP_URL_PATH)));
$profile = Profile::whereNull('status')
->find($query);
if(!$profile) {
return [
'accounts' => [],
'hashtags' => [],
'statuses' => []
];
}
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return [
'accounts' => [$fractal->createData($resource)->toArray()],
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => []
]; ];