mirror of
https://github.com/pixelfed/pixelfed.git
synced 2025-01-18 02:20:46 +00:00
Update SearchApiV2Service, fix hashtag search
This commit is contained in:
parent
f3220fd21e
commit
83c1a7fd4b
1 changed files with 41 additions and 7 deletions
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue