diff --git a/src/Api/Controllers/ListPollsController.php b/src/Api/Controllers/ListPollsController.php index b3c4c763..5420482b 100644 --- a/src/Api/Controllers/ListPollsController.php +++ b/src/Api/Controllers/ListPollsController.php @@ -16,6 +16,7 @@ use Flarum\Http\UrlGenerator; use FoF\Polls\Api\Serializers\PollSerializer; use FoF\Polls\Poll; +use FoF\Polls\PollRepository; use Illuminate\Database\Eloquent\Collection; use Psr\Http\Message\ServerRequestInterface; use Tobscure\JsonApi\Document; @@ -31,13 +32,19 @@ class ListPollsController extends AbstractListController 'myVotes.option', ]; + /** + * @var PollRepository + */ + protected $polls; + /** * @var UrlGenerator */ protected $url; - public function __construct(UrlGenerator $url) + public function __construct(PollRepository $polls, UrlGenerator $url) { + $this->polls = $polls; $this->url = $url; } @@ -47,17 +54,16 @@ public function data(ServerRequestInterface $request, Document $document): Colle // Not yet needed, but here if/when we do. // $filters = $this->extractFilter($request); - // $sort = $this->extractSort($request); - // $sortIsDefault = $this->sortIsDefault($request); + $sort = $this->extractSort($request); + $sortIsDefault = $this->sortIsDefault($request); $limit = $this->extractLimit($request); $offset = $this->extractOffset($request); $include = $this->extractInclude($request); - $results = Poll::query() + $results = $this->polls->queryVisibleTo($actor) ->select('polls.*') - ->whereVisibleTo($actor) - ->orderBy('id') + ->orderBy($sortIsDefault ? 'id' : $sort, 'desc') ->skip($offset) ->take($limit);