mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-25 15:55:22 +00:00
Update SearchApiV2Service, improve performance and include hashtag post counts when applicable
This commit is contained in:
parent
a37971dd28
commit
fbaed93eda
1 changed files with 24 additions and 13 deletions
|
@ -12,6 +12,9 @@ use League\Fractal\Serializer\ArraySerializer;
|
||||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
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 App\Services\HashtagService;
|
||||||
|
use App\Services\StatusService;
|
||||||
|
|
||||||
class SearchApiV2Service
|
class SearchApiV2Service
|
||||||
{
|
{
|
||||||
|
@ -86,19 +89,27 @@ class SearchApiV2Service
|
||||||
|
|
||||||
protected function accounts()
|
protected function accounts()
|
||||||
{
|
{
|
||||||
|
$user = request()->user();
|
||||||
$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 = '%' . $this->query->input('q') . '%';
|
$query = '%' . $this->query->input('q') . '%';
|
||||||
$results = Profile::whereNull('status')
|
$results = Profile::select('profiles.*', 'followers.profile_id', 'followers.created_at')
|
||||||
|
->whereNull('status')
|
||||||
|
->leftJoin('followers', function($join) use($user) {
|
||||||
|
return $join->on('profiles.id', '=', 'followers.following_id')
|
||||||
|
->where('followers.profile_id', $user->profile_id);
|
||||||
|
})
|
||||||
->where('username', 'like', $query)
|
->where('username', 'like', $query)
|
||||||
|
->orderByDesc('profiles.followers_count')
|
||||||
|
->orderByDesc('followers.created_at')
|
||||||
->offset($offset)
|
->offset($offset)
|
||||||
->limit($limit)
|
->limit($limit)
|
||||||
->get();
|
->get()
|
||||||
|
->map(function($res) {
|
||||||
|
return AccountService::get($res['id']);
|
||||||
|
});
|
||||||
|
|
||||||
$fractal = new Fractal\Manager();
|
return $results;
|
||||||
$fractal->setSerializer(new ArraySerializer());
|
|
||||||
$resource = new Fractal\Resource\Collection($results, new AccountTransformer());
|
|
||||||
return $fractal->createData($resource)->toArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function hashtags()
|
protected function hashtags()
|
||||||
|
@ -115,6 +126,7 @@ class SearchApiV2Service
|
||||||
return [
|
return [
|
||||||
'name' => $tag->name,
|
'name' => $tag->name,
|
||||||
'url' => $tag->url(),
|
'url' => $tag->url(),
|
||||||
|
'count' => HashtagService::count($tag->id),
|
||||||
'history' => []
|
'history' => []
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
@ -134,12 +146,11 @@ class SearchApiV2Service
|
||||||
$results = Status::where('caption', 'like', $query)
|
$results = Status::where('caption', 'like', $query)
|
||||||
->whereProfileId($accountId)
|
->whereProfileId($accountId)
|
||||||
->limit($limit)
|
->limit($limit)
|
||||||
->get();
|
->get()
|
||||||
|
->map(function($status) {
|
||||||
$fractal = new Fractal\Manager();
|
return StatusService::get($status->id);
|
||||||
$fractal->setSerializer(new ArraySerializer());
|
});
|
||||||
$resource = new Fractal\Resource\Collection($results, new StatusTransformer());
|
return $results;
|
||||||
return $fractal->createData($resource)->toArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function resolveQuery()
|
protected function resolveQuery()
|
||||||
|
@ -213,4 +224,4 @@ class SearchApiV2Service
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue