Merge pull request #5471 from pixelfed/staging

Update SearchApiV2Service, fix hashtag search
This commit is contained in:
daniel 2025-01-14 19:00:04 -07:00 committed by GitHub
commit 393e9a2a74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,7 @@ use App\Profile;
use App\Status;
use App\Transformer\Api\AccountTransformer;
use App\Util\ActivityPub\Helpers;
use DB;
use Illuminate\Support\Str;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
@ -131,17 +132,50 @@ class SearchApiV2Service
$q = $this->query->input('q');
$limit = $this->query->input('limit') ?? 20;
$offset = $this->query->input('offset') ?? 0;
$query = Str::startsWith($q, '#') ? substr($q, 1).'%' : $q;
$operator = config('database.default') === 'pgsql' ? 'ilike' : 'like';
return Hashtag::where('name', $operator, $query)
->orderByDesc('cached_count')
$query = Str::startsWith($q, '#') ? substr($q, 1) : $q;
$query = $query.'%';
if (config('database.default') === 'pgsql') {
$baseQuery = Hashtag::query()
->where('name', 'ilike', $query)
->where('is_banned', false)
->where(function ($q) {
$q->where('can_search', true)
->orWhereNull('can_search');
})
->orderByDesc(DB::raw('COALESCE(cached_count, 0)'))
->offset($offset)
->limit($limit)
->get();
return $baseQuery
->map(function ($tag) use ($mastodonMode) {
$res = [
'name' => $tag->name,
'url' => $tag->url(),
];
if (! $mastodonMode) {
$res['history'] = [];
$res['count'] = $tag->cached_count ?? 0;
}
return $res;
})
->values();
}
return Hashtag::where('name', 'like', $query)
->where('is_banned', false)
->where(function ($q) {
$q->where('can_search', true)
->orWhereNull('can_search');
})
->orderBy(DB::raw('COALESCE(cached_count, 0)'), 'desc')
->offset($offset)
->limit($limit)
->get()
->filter(function ($tag) {
return $tag->can_search != false;
})
->map(function ($tag) use ($mastodonMode) {
$res = [
'name' => $tag->name,