forked from mirror/pixelfed
Update SearchApiV2Service, improve resolve query logic to better handle remote posts/profiles and local posts/profiles
This commit is contained in:
parent
0704c7e05e
commit
c61d0b915f
1 changed files with 322 additions and 284 deletions
|
@ -179,8 +179,10 @@ class SearchApiV2Service
|
|||
return $default;
|
||||
}
|
||||
if(Helpers::validateLocalUrl($query)) {
|
||||
if(Str::contains($query, '/p/')) {
|
||||
if(Str::contains($query, '/p/') || Str::contains($query, 'i/web/post/')) {
|
||||
return $this->resolveLocalStatus();
|
||||
} else if(Str::contains($query, 'i/web/profile/')) {
|
||||
return $this->resolveLocalProfileId();
|
||||
} else {
|
||||
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 {
|
||||
$res = ActivityPubFetchService::get($query);
|
||||
$banned = InstanceService::getBannedDomains();
|
||||
|
@ -238,11 +248,14 @@ class SearchApiV2Service
|
|||
return $default;
|
||||
}
|
||||
$note = $mastodonMode ?
|
||||
StatusService::getMastodon($obj['id']) :
|
||||
StatusService::get($obj['id']);
|
||||
StatusService::getMastodon($obj['id'], false) :
|
||||
StatusService::get($obj['id'], false);
|
||||
if(!$note) {
|
||||
return $default;
|
||||
}
|
||||
if(!isset($note['visibility']) || !in_array($note['visibility'], ['public', 'unlisted'])) {
|
||||
return $default;
|
||||
}
|
||||
$default['statuses'][] = $note;
|
||||
return $default;
|
||||
break;
|
||||
|
@ -256,8 +269,8 @@ class SearchApiV2Service
|
|||
return $default;
|
||||
}
|
||||
$default['accounts'][] = $mastodonMode ?
|
||||
AccountService::getMastodon($obj['id']) :
|
||||
AccountService::get($obj['id']);
|
||||
AccountService::getMastodon($obj['id'], true) :
|
||||
AccountService::get($obj['id'], true);
|
||||
return $default;
|
||||
break;
|
||||
|
||||
|
@ -285,9 +298,9 @@ class SearchApiV2Service
|
|||
protected function resolveLocalStatus()
|
||||
{
|
||||
$query = urldecode($this->query->input('q'));
|
||||
$query = last(explode('/', $query));
|
||||
$status = StatusService::getMastodon($query);
|
||||
if(!$status) {
|
||||
$query = last(explode('/', parse_url($query, PHP_URL_PATH)));
|
||||
$status = StatusService::getMastodon($query, false);
|
||||
if(!$status || !in_array($status['visibility'], ['public', 'unlisted'])) {
|
||||
return [
|
||||
'accounts' => [],
|
||||
'hashtags' => [],
|
||||
|
@ -307,7 +320,7 @@ class SearchApiV2Service
|
|||
protected function resolveLocalProfile()
|
||||
{
|
||||
$query = urldecode($this->query->input('q'));
|
||||
$query = last(explode('/', $query));
|
||||
$query = last(explode('/', parse_url($query, PHP_URL_PATH)));
|
||||
$profile = Profile::whereNull('status')
|
||||
->whereNull('domain')
|
||||
->whereUsername($query)
|
||||
|
@ -325,7 +338,32 @@ class SearchApiV2Service
|
|||
$fractal->setSerializer(new ArraySerializer());
|
||||
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
|
||||
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' => [],
|
||||
'statuses' => []
|
||||
];
|
||||
|
|
Loading…
Add table
Reference in a new issue