Skip to content

Commit

Permalink
Update SearchApiV2Service, improve performance and include hashtag po…
Browse files Browse the repository at this point in the history
…st counts when applicable
  • Loading branch information
dansup committed Dec 5, 2021
1 parent a37971d commit fbaed93
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions app/Services/SearchApiV2Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Util\ActivityPub\Helpers;
use Illuminate\Support\Str;
use App\Services\AccountService;
use App\Services\HashtagService;
use App\Services\StatusService;

class SearchApiV2Service
{
Expand Down Expand Up @@ -86,19 +89,27 @@ protected function resolve()

protected function accounts()
{
$user = request()->user();
$limit = $this->query->input('limit') ?? 20;
$offset = $this->query->input('offset') ?? 0;
$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)
->orderByDesc('profiles.followers_count')
->orderByDesc('followers.created_at')
->offset($offset)
->limit($limit)
->get();
->get()
->map(function($res) {
return AccountService::get($res['id']);
});

$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Collection($results, new AccountTransformer());
return $fractal->createData($resource)->toArray();
return $results;
}

protected function hashtags()
Expand All @@ -115,6 +126,7 @@ protected function hashtags()
return [
'name' => $tag->name,
'url' => $tag->url(),
'count' => HashtagService::count($tag->id),
'history' => []
];
});
Expand All @@ -134,12 +146,11 @@ protected function statusesById()
$results = Status::where('caption', 'like', $query)
->whereProfileId($accountId)
->limit($limit)
->get();

$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Collection($results, new StatusTransformer());
return $fractal->createData($resource)->toArray();
->get()
->map(function($status) {
return StatusService::get($status->id);
});
return $results;
}

protected function resolveQuery()
Expand Down Expand Up @@ -213,4 +224,4 @@ protected function resolveLocalProfile()
];
}

}
}

0 comments on commit fbaed93

Please sign in to comment.