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\Status;
|
||||||
use App\Transformer\Api\AccountTransformer;
|
use App\Transformer\Api\AccountTransformer;
|
||||||
use App\Util\ActivityPub\Helpers;
|
use App\Util\ActivityPub\Helpers;
|
||||||
|
use DB;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use League\Fractal;
|
use League\Fractal;
|
||||||
use League\Fractal\Serializer\ArraySerializer;
|
use League\Fractal\Serializer\ArraySerializer;
|
||||||
|
@ -131,17 +132,50 @@ class SearchApiV2Service
|
||||||
$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;
|
|
||||||
$operator = config('database.default') === 'pgsql' ? 'ilike' : 'like';
|
|
||||||
|
|
||||||
return Hashtag::where('name', $operator, $query)
|
$query = Str::startsWith($q, '#') ? substr($q, 1) : $q;
|
||||||
->orderByDesc('cached_count')
|
$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)
|
->offset($offset)
|
||||||
->limit($limit)
|
->limit($limit)
|
||||||
->get()
|
->get()
|
||||||
->filter(function ($tag) {
|
|
||||||
return $tag->can_search != false;
|
|
||||||
})
|
|
||||||
->map(function ($tag) use ($mastodonMode) {
|
->map(function ($tag) use ($mastodonMode) {
|
||||||
$res = [
|
$res = [
|
||||||
'name' => $tag->name,
|
'name' => $tag->name,
|
||||||
|
|
Loading…
Reference in a new issue