diff --git a/app/Services/SearchApiV2Service.php b/app/Services/SearchApiV2Service.php index fc87d2c04..f17fe3e84 100644 --- a/app/Services/SearchApiV2Service.php +++ b/app/Services/SearchApiV2Service.php @@ -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,