Merge pull request #4992 from pixelfed/staging

Update SearchApiV2Service, use more efficient query
This commit is contained in:
daniel 2024-03-07 03:34:10 -07:00 committed by GitHub
commit 7c2ecd8706
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 88 additions and 78 deletions

View file

@ -5,6 +5,11 @@
### Updates ### Updates
- Update SoftwareUpdateService, add command to refresh latest versions ([632f2cb6](https://github.com/pixelfed/pixelfed/commit/632f2cb6)) - Update SoftwareUpdateService, add command to refresh latest versions ([632f2cb6](https://github.com/pixelfed/pixelfed/commit/632f2cb6))
- Update Post.vue, fix cache bug ([3a27e637](https://github.com/pixelfed/pixelfed/commit/3a27e637))
- Update StatusHashtagService, use more efficient cached count ([592c8412](https://github.com/pixelfed/pixelfed/commit/592c8412))
- Update DiscoverController, handle discover hashtag redirects ([18382e8a](https://github.com/pixelfed/pixelfed/commit/18382e8a))
- Update ApiV1Controller, use admin filter service ([94503a1c](https://github.com/pixelfed/pixelfed/commit/94503a1c))
- Update SearchApiV2Service, use more efficient query ([cee618e8](https://github.com/pixelfed/pixelfed/commit/cee618e8))
- ([](https://github.com/pixelfed/pixelfed/commit/)) - ([](https://github.com/pixelfed/pixelfed/commit/))
## [v0.11.13 (2024-03-05)](https://github.com/pixelfed/pixelfed/compare/v0.11.12...v0.11.13) ## [v0.11.13 (2024-03-05)](https://github.com/pixelfed/pixelfed/compare/v0.11.12...v0.11.13)

View file

@ -2,28 +2,26 @@
namespace App\Services; namespace App\Services;
use Cache; use App\Hashtag;
use Illuminate\Support\Facades\Redis; use App\Profile;
use App\{Hashtag, Profile, Status}; use App\Status;
use App\Transformer\Api\AccountTransformer; use App\Transformer\Api\AccountTransformer;
use App\Transformer\Api\StatusTransformer;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Util\ActivityPub\Helpers; use App\Util\ActivityPub\Helpers;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Services\AccountService; use League\Fractal;
use App\Services\HashtagService; use League\Fractal\Serializer\ArraySerializer;
use App\Services\StatusService;
class SearchApiV2Service class SearchApiV2Service
{ {
private $query; private $query;
static $mastodonMode = false;
public static $mastodonMode = false;
public static function query($query, $mastodonMode = false) public static function query($query, $mastodonMode = false)
{ {
self::$mastodonMode = $mastodonMode; self::$mastodonMode = $mastodonMode;
return (new self)->run($query); return (new self)->run($query);
} }
@ -45,21 +43,21 @@ class SearchApiV2Service
return [ return [
'accounts' => $this->accounts(), 'accounts' => $this->accounts(),
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => [],
]; ];
break; break;
case 'hashtags': case 'hashtags':
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => $this->hashtags(), 'hashtags' => $this->hashtags(),
'statuses' => [] 'statuses' => [],
]; ];
break; break;
case 'statuses': case 'statuses':
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => $this->statuses() 'statuses' => $this->statuses(),
]; ];
break; break;
} }
@ -69,14 +67,14 @@ class SearchApiV2Service
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => $this->statusesById() 'statuses' => $this->statusesById(),
]; ];
} }
return [ return [
'accounts' => $this->accounts(), 'accounts' => $this->accounts(),
'hashtags' => $this->hashtags(), 'hashtags' => $this->hashtags(),
'statuses' => $this->statuses() 'statuses' => $this->statuses(),
]; ];
} }
@ -134,31 +132,31 @@ 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 . '%'; $query = Str::startsWith($q, '#') ? substr($q, 1).'%' : $q;
$operator = config('database.default') === 'pgsql' ? 'ilike' : 'like'; $operator = config('database.default') === 'pgsql' ? 'ilike' : 'like';
return Hashtag::where('name', $operator, $query) return Hashtag::where('name', $operator, $query)
->orWhere('slug', $operator, $query)
->where(function($q) {
return $q->where('can_search', true)
->orWhereNull('can_search');
})
->orderByDesc('cached_count') ->orderByDesc('cached_count')
->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,
'url' => $tag->url() 'url' => $tag->url(),
]; ];
if (! $mastodonMode) { if (! $mastodonMode) {
$res['history'] = []; $res['history'] = [];
$res['count'] = HashtagService::count($tag->id); $res['count'] = $tag->cached_count ?? 0;
} }
return $res; return $res;
}); })
->values();
} }
protected function statuses() protected function statuses()
@ -194,6 +192,7 @@ class SearchApiV2Service
} }
if (substr($query, 0, 1) === '@' && ! Str::contains($query, '.')) { if (substr($query, 0, 1) === '@' && ! Str::contains($query, '.')) {
$default['accounts'] = $this->accounts(substr($query, 1)); $default['accounts'] = $this->accounts(substr($query, 1));
return $default; return $default;
} }
if (Helpers::validateLocalUrl($query)) { if (Helpers::validateLocalUrl($query)) {
@ -221,6 +220,7 @@ class SearchApiV2Service
return $default; return $default;
} }
$default['accounts'][] = $res; $default['accounts'][] = $res;
return $default; return $default;
} else { } else {
return $default; return $default;
@ -239,6 +239,7 @@ class SearchApiV2Service
return $default; return $default;
} }
$default['accounts'][] = $res; $default['accounts'][] = $res;
return $default; return $default;
} else { } else {
return $default; return $default;
@ -252,6 +253,7 @@ class SearchApiV2Service
} }
if (in_array($s['visibility'], ['public', 'unlisted'])) { if (in_array($s['visibility'], ['public', 'unlisted'])) {
$default['statuses'][] = $s; $default['statuses'][] = $s;
return $default; return $default;
} }
} }
@ -286,6 +288,7 @@ class SearchApiV2Service
return $default; return $default;
} }
$default['statuses'][] = $note; $default['statuses'][] = $note;
return $default; return $default;
break; break;
@ -300,6 +303,7 @@ class SearchApiV2Service
$default['accounts'][] = $mastodonMode ? $default['accounts'][] = $mastodonMode ?
AccountService::getMastodon($obj['id'], true) : AccountService::getMastodon($obj['id'], true) :
AccountService::get($obj['id'], true); AccountService::get($obj['id'], true);
return $default; return $default;
break; break;
@ -333,14 +337,14 @@ class SearchApiV2Service
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => [],
]; ];
} }
$res = [ $res = [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => [$status] 'statuses' => [$status],
]; ];
return $res; return $res;
@ -359,17 +363,18 @@ class SearchApiV2Service
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => [],
]; ];
} }
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($profile, new AccountTransformer()); $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return [ return [
'accounts' => [$fractal->createData($resource)->toArray()], 'accounts' => [$fractal->createData($resource)->toArray()],
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => [],
]; ];
} }
@ -384,18 +389,18 @@ class SearchApiV2Service
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => [],
]; ];
} }
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($profile, new AccountTransformer()); $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return [ return [
'accounts' => [$fractal->createData($resource)->toArray()], 'accounts' => [$fractal->createData($resource)->toArray()],
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => [],
]; ];
} }
} }