Skip to content

Commit

Permalink
feat: add discussion sortmap to container binding (#3377)
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland authored Apr 4, 2022
1 parent 9d9594a commit 1d27f62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
17 changes: 1 addition & 16 deletions framework/core/src/Forum/Content/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __invoke(Document $document, Request $request)
$page = max(1, intval(Arr::pull($queryParams, 'page')));
$filters = Arr::pull($queryParams, 'filter', []);

$sortMap = $this->getSortMap();
$sortMap = resolve('flarum.forum.discussions.sortmap');

$params = [
'sort' => $sort && isset($sortMap[$sort]) ? $sortMap[$sort] : '',
Expand All @@ -96,21 +96,6 @@ public function __invoke(Document $document, Request $request)
return $document;
}

/**
* Get a map of sort query param values and their API sort params.
*
* @return array
*/
private function getSortMap()
{
return [
'latest' => '-lastPostedAt',
'top' => '-commentCount',
'newest' => '-createdAt',
'oldest' => 'createdAt'
];
}

/**
* Get the result of an API request to list discussions.
*
Expand Down
9 changes: 9 additions & 0 deletions framework/core/src/Forum/ForumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ public function register()
$this->container->bind('flarum.frontend.forum', function (Container $container) {
return $container->make('flarum.frontend.factory')('forum');
});

$this->container->singleton('flarum.forum.discussions.sortmap', function () {
return [
'latest' => '-lastPostedAt',
'top' => '-commentCount',
'newest' => '-createdAt',
'oldest' => 'createdAt'
];
});
}

public function boot(Container $container, Dispatcher $events, Factory $view)
Expand Down

0 comments on commit 1d27f62

Please sign in to comment.