mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-29 09:43:16 +00:00
Update SearchApiV2Service, use more efficient query
This commit is contained in:
parent
94503a1cf9
commit
cee618e844
1 changed files with 83 additions and 78 deletions
|
@ -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' => [],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue