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

@ -18,317 +18,355 @@ use App\Services\StatusService;
class SearchApiV2Service class SearchApiV2Service
{ {
private $query; private $query;
static $mastodonMode = false; static $mastodonMode = false;
public static function query($query, $mastodonMode = false) public static function query($query, $mastodonMode = false)
{ {
self::$mastodonMode = $mastodonMode; self::$mastodonMode = $mastodonMode;
return (new self)->run($query); return (new self)->run($query);
} }
protected function run($query) protected function run($query)
{ {
$this->query = $query; $this->query = $query;
$q = urldecode($query->input('q')); $q = urldecode($query->input('q'));
if($query->has('resolve') && if($query->has('resolve') &&
( Str::startsWith($q, 'https://') || ( Str::startsWith($q, 'https://') ||
Str::substrCount($q, '@') >= 1) Str::substrCount($q, '@') >= 1)
) { ) {
return $this->resolveQuery(); return $this->resolveQuery();
} }
if($query->has('type')) { if($query->has('type')) {
switch ($query->input('type')) { switch ($query->input('type')) {
case 'accounts': case 'accounts':
return [ return [
'accounts' => $this->accounts(), 'accounts' => $this->accounts(),
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => []
]; ];
break; break;
case 'hashtags': case 'hashtags':
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => $this->hashtags(), 'hashtags' => $this->hashtags(),
'statuses' => [] 'statuses' => []
]; ];
break; break;
case 'statuses': case 'statuses':
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => $this->statuses() 'statuses' => $this->statuses()
]; ];
break; break;
} }
} }
if($query->has('account_id')) { if($query->has('account_id')) {
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => $this->statusesById() 'statuses' => $this->statusesById()
]; ];
} }
return [ return [
'accounts' => $this->accounts(), 'accounts' => $this->accounts(),
'hashtags' => $this->hashtags(), 'hashtags' => $this->hashtags(),
'statuses' => $this->statuses() 'statuses' => $this->statuses()
]; ];
} }
protected function accounts($initalQuery = false) protected function accounts($initalQuery = false)
{ {
$mastodonMode = self::$mastodonMode; $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;
$rawQuery = $initalQuery ? $initalQuery : $this->query->input('q'); $rawQuery = $initalQuery ? $initalQuery : $this->query->input('q');
$query = $rawQuery . '%'; $query = $rawQuery . '%';
$webfingerQuery = $query; $webfingerQuery = $query;
if(Str::substrCount($rawQuery, '@') == 1 && substr($rawQuery, 0, 1) !== '@') { if(Str::substrCount($rawQuery, '@') == 1 && substr($rawQuery, 0, 1) !== '@') {
$query = '@' . $query; $query = '@' . $query;
} }
if(substr($webfingerQuery, 0, 1) !== '@') { if(substr($webfingerQuery, 0, 1) !== '@') {
$webfingerQuery = '@' . $webfingerQuery; $webfingerQuery = '@' . $webfingerQuery;
} }
$banned = InstanceService::getBannedDomains(); $banned = InstanceService::getBannedDomains();
$operator = config('database.default') === 'pgsql' ? 'ilike' : 'like'; $operator = config('database.default') === 'pgsql' ? 'ilike' : 'like';
$results = Profile::select('username', 'id', 'followers_count', 'domain') $results = Profile::select('username', 'id', 'followers_count', 'domain')
->where('username', $operator, $query) ->where('username', $operator, $query)
->orWhere('webfinger', $operator, $webfingerQuery) ->orWhere('webfinger', $operator, $webfingerQuery)
->orderByDesc('profiles.followers_count') ->orderByDesc('profiles.followers_count')
->offset($offset) ->offset($offset)
->limit($limit) ->limit($limit)
->get() ->get()
->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) use($mastodonMode) { ->map(function($res) use($mastodonMode) {
return $mastodonMode ? return $mastodonMode ?
AccountService::getMastodon($res['id']) : AccountService::getMastodon($res['id']) :
AccountService::get($res['id']); AccountService::get($res['id']);
}) })
->filter(function($account) { ->filter(function($account) {
return $account && isset($account['id']); return $account && isset($account['id']);
}) })
->values(); ->values();
return $results; return $results;
} }
protected function hashtags() protected function hashtags()
{ {
$mastodonMode = self::$mastodonMode; $mastodonMode = self::$mastodonMode;
$q = $this->query->input('q'); $q = $this->query->input('q');
$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 = Str::startsWith($q, '#') ? '%' . substr($q, 1) . '%' : '%' . $q . '%'; $query = Str::startsWith($q, '#') ? '%' . substr($q, 1) . '%' : '%' . $q . '%';
$operator = config('database.default') === 'pgsql' ? 'ilike' : 'like'; $operator = config('database.default') === 'pgsql' ? 'ilike' : 'like';
return Hashtag::where('name', $operator, $query) return Hashtag::where('name', $operator, $query)
->orWhere('slug', $operator, $query) ->orWhere('slug', $operator, $query)
->where(function($q) { ->where(function($q) {
return $q->where('can_search', true) return $q->where('can_search', true)
->orWhereNull('can_search'); ->orWhereNull('can_search');
}) })
->orderByDesc('cached_count') ->orderByDesc('cached_count')
->offset($offset) ->offset($offset)
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($tag) use($mastodonMode) { ->map(function($tag) use($mastodonMode) {
$res = [ $res = [
'name' => $tag->name, 'name' => $tag->name,
'url' => $tag->url() 'url' => $tag->url()
]; ];
if(!$mastodonMode) { if(!$mastodonMode) {
$res['history'] = []; $res['history'] = [];
$res['count'] = HashtagService::count($tag->id); $res['count'] = HashtagService::count($tag->id);
} }
return $res; return $res;
}); });
} }
protected function statuses() protected function statuses()
{ {
// Removed until we provide more relevent sorting/results // Removed until we provide more relevent sorting/results
return []; return [];
} }
protected function statusesById() protected function statusesById()
{ {
// Removed until we provide more relevent sorting/results // Removed until we provide more relevent sorting/results
return []; return [];
} }
protected function resolveQuery() protected function resolveQuery()
{ {
$default = [ $default = [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => [], 'statuses' => [],
]; ];
$mastodonMode = self::$mastodonMode; $mastodonMode = self::$mastodonMode;
$query = urldecode($this->query->input('q')); $query = urldecode($this->query->input('q'));
if(substr($query, 0, 1) === '@' && !Str::contains($query, '.')) { if(substr($query, 0, 1) === '@' && !Str::contains($query, '.')) {
$default['accounts'] = $this->accounts(substr($query, 1)); $default['accounts'] = $this->accounts(substr($query, 1));
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 { } else if(Str::contains($query, 'i/web/profile/')) {
return $this->resolveLocalProfile(); return $this->resolveLocalProfileId();
} } else {
} else { return $this->resolveLocalProfile();
if(!Helpers::validateUrl($query) && strpos($query, '@') == -1) { }
return $default; } else {
} if(!Helpers::validateUrl($query) && strpos($query, '@') == -1) {
return $default;
}
if(!Str::startsWith($query, 'http') && Str::substrCount($query, '@') == 1 && strpos($query, '@') !== 0) { if(!Str::startsWith($query, 'http') && Str::substrCount($query, '@') == 1 && strpos($query, '@') !== 0) {
try { try {
$res = WebfingerService::lookup('@' . $query, $mastodonMode); $res = WebfingerService::lookup('@' . $query, $mastodonMode);
} catch (\Exception $e) { } catch (\Exception $e) {
return $default; return $default;
} }
if($res && isset($res['id'])) { if($res && isset($res['id'])) {
$default['accounts'][] = $res; $default['accounts'][] = $res;
return $default; return $default;
} else { } else {
return $default; return $default;
} }
} }
if(Str::substrCount($query, '@') == 2) { if(Str::substrCount($query, '@') == 2) {
try { try {
$res = WebfingerService::lookup($query, $mastodonMode); $res = WebfingerService::lookup($query, $mastodonMode);
} catch (\Exception $e) { } catch (\Exception $e) {
return $default; return $default;
} }
if($res && isset($res['id'])) { if($res && isset($res['id'])) {
$default['accounts'][] = $res; $default['accounts'][] = $res;
return $default; return $default;
} else { } else {
return $default; return $default;
} }
} }
try { if($sid = Status::whereUri($query)->first()) {
$res = ActivityPubFetchService::get($query); $s = StatusService::get($sid->id, false);
$banned = InstanceService::getBannedDomains(); if(in_array($s['visibility'], ['public', 'unlisted'])) {
if($res) { $default['statuses'][] = $s;
$json = json_decode($res, true); return $default;
}
}
if(!$json || !isset($json['@context']) || !isset($json['type']) || !in_array($json['type'], ['Note', 'Person'])) { try {
return [ $res = ActivityPubFetchService::get($query);
'accounts' => [], $banned = InstanceService::getBannedDomains();
'hashtags' => [], if($res) {
'statuses' => [], $json = json_decode($res, true);
];
}
switch($json['type']) { if(!$json || !isset($json['@context']) || !isset($json['type']) || !in_array($json['type'], ['Note', 'Person'])) {
case 'Note': return [
$obj = Helpers::statusFetch($query); 'accounts' => [],
if(!$obj || !isset($obj['id'])) { 'hashtags' => [],
return $default; 'statuses' => [],
} ];
$note = $mastodonMode ? }
StatusService::getMastodon($obj['id']) :
StatusService::get($obj['id']);
if(!$note) {
return $default;
}
$default['statuses'][] = $note;
return $default;
break;
case 'Person': switch($json['type']) {
$obj = Helpers::profileFetch($query); case 'Note':
if(!$obj) { $obj = Helpers::statusFetch($query);
return $default; if(!$obj || !isset($obj['id'])) {
} return $default;
if(in_array($obj['domain'], $banned)) { }
return $default; $note = $mastodonMode ?
} StatusService::getMastodon($obj['id'], false) :
$default['accounts'][] = $mastodonMode ? StatusService::get($obj['id'], false);
AccountService::getMastodon($obj['id']) : if(!$note) {
AccountService::get($obj['id']); return $default;
return $default; }
break; if(!isset($note['visibility']) || !in_array($note['visibility'], ['public', 'unlisted'])) {
return $default;
}
$default['statuses'][] = $note;
return $default;
break;
default: case 'Person':
return [ $obj = Helpers::profileFetch($query);
'accounts' => [], if(!$obj) {
'hashtags' => [], return $default;
'statuses' => [], }
]; if(in_array($obj['domain'], $banned)) {
break; return $default;
} }
} $default['accounts'][] = $mastodonMode ?
} catch (\Exception $e) { AccountService::getMastodon($obj['id'], true) :
return [ AccountService::get($obj['id'], true);
'accounts' => [], return $default;
'hashtags' => [], break;
'statuses' => [],
];
}
return $default; default:
} return [
} 'accounts' => [],
'hashtags' => [],
'statuses' => [],
];
break;
}
}
} catch (\Exception $e) {
return [
'accounts' => [],
'hashtags' => [],
'statuses' => [],
];
}
protected function resolveLocalStatus() return $default;
{ }
$query = urldecode($this->query->input('q')); }
$query = last(explode('/', $query));
$status = StatusService::getMastodon($query);
if(!$status) {
return [
'accounts' => [],
'hashtags' => [],
'statuses' => []
];
}
$res = [ protected function resolveLocalStatus()
'accounts' => [], {
'hashtags' => [], $query = urldecode($this->query->input('q'));
'statuses' => [$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' => [],
'statuses' => []
];
}
return $res; $res = [
} 'accounts' => [],
'hashtags' => [],
'statuses' => [$status]
];
protected function resolveLocalProfile() return $res;
{ }
$query = urldecode($this->query->input('q'));
$query = last(explode('/', $query));
$profile = Profile::whereNull('status')
->whereNull('domain')
->whereUsername($query)
->first();
if(!$profile) { protected function resolveLocalProfile()
return [ {
'accounts' => [], $query = urldecode($this->query->input('q'));
'hashtags' => [], $query = last(explode('/', parse_url($query, PHP_URL_PATH)));
'statuses' => [] $profile = Profile::whereNull('status')
]; ->whereNull('domain')
} ->whereUsername($query)
->first();
$fractal = new Fractal\Manager(); if(!$profile) {
$fractal->setSerializer(new ArraySerializer()); return [
$resource = new Fractal\Resource\Item($profile, new AccountTransformer()); 'accounts' => [],
return [ 'hashtags' => [],
'accounts' => $fractal->createData($resource)->toArray(), 'statuses' => []
'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' => []
];
}
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' => []
];
}
} }