Преглед на файлове

Merge pull request #5471 from pixelfed/staging

Update SearchApiV2Service, fix hashtag search
daniel преди 6 месеца
родител
ревизия
393e9a2a74
променени са 1 файла, в които са добавени 41 реда и са изтрити 7 реда
  1. 41 7
      app/Services/SearchApiV2Service.php

+ 41 - 7
app/Services/SearchApiV2Service.php

@@ -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)
-            ->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)
             ->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,