diff --git a/extensions/tags/src/Content/Tag.php b/extensions/tags/src/Content/Tag.php index 1b683e763..b7c7500fa 100644 --- a/extensions/tags/src/Content/Tag.php +++ b/extensions/tags/src/Content/Tag.php @@ -10,6 +10,7 @@ namespace Flarum\Tags\Content; use Flarum\Api\Client; +use Flarum\Api\Resource\DiscussionResource; use Flarum\Frontend\Document; use Flarum\Http\RequestUtil; use Flarum\Http\SlugManager; @@ -27,7 +28,8 @@ class Tag protected Factory $view, protected TagRepository $tags, protected TranslatorInterface $translator, - protected SlugManager $slugger + protected SlugManager $slugger, + protected DiscussionResource $resource ) { } @@ -42,7 +44,7 @@ class Tag $page = Arr::pull($queryParams, 'page', 1); $filters = Arr::pull($queryParams, 'filter', []); - $sortMap = $this->getSortMap(); + $sortMap = $this->resource->sortMap(); $tag = $this->slugger->forResource(TagModel::class)->fromSlug($slug, $actor); @@ -78,14 +80,6 @@ class Tag return $document; } - /** - * Get a map of sort query param values and their API sort params. - */ - protected function getSortMap(): array - { - return resolve('flarum.forum.discussions.sortmap'); - } - /** * Get the result of an API request to list discussions. */ diff --git a/framework/core/src/Api/Resource/AbstractDatabaseResource.php b/framework/core/src/Api/Resource/AbstractDatabaseResource.php index b1429967c..c9faa998a 100644 --- a/framework/core/src/Api/Resource/AbstractDatabaseResource.php +++ b/framework/core/src/Api/Resource/AbstractDatabaseResource.php @@ -12,6 +12,7 @@ use Flarum\Api\Resource\Contracts\{Findable, Deletable}; use Flarum\Api\Resource\Concerns\Bootable; use Flarum\Api\Resource\Concerns\Extendable; +use Flarum\Api\Resource\Concerns\HasSortMap; use Flarum\Foundation\DispatchEventsTrait; use Flarum\User\User; use RuntimeException; @@ -30,6 +31,7 @@ abstract class AbstractDatabaseResource extends BaseResource implements { use Bootable; use Extendable; + use HasSortMap; use DispatchEventsTrait { dispatchEventsFor as traitDispatchEventsFor; } diff --git a/framework/core/src/Api/Resource/AbstractResource.php b/framework/core/src/Api/Resource/AbstractResource.php index deee2bccd..7ad30b8c6 100644 --- a/framework/core/src/Api/Resource/AbstractResource.php +++ b/framework/core/src/Api/Resource/AbstractResource.php @@ -4,6 +4,7 @@ namespace Flarum\Api\Resource; use Flarum\Api\Resource\Concerns\Bootable; use Flarum\Api\Resource\Concerns\Extendable; +use Flarum\Api\Resource\Concerns\HasSortMap; use Flarum\Api\Resource\Contracts\Collection; use Flarum\Api\Resource\Contracts\Resource; use Tobyz\JsonApiServer\Resource\AbstractResource as BaseResource; @@ -12,4 +13,5 @@ abstract class AbstractResource extends BaseResource implements Resource, Collec { use Bootable; use Extendable; + use HasSortMap; } diff --git a/framework/core/src/Api/Resource/Concerns/HasSortMap.php b/framework/core/src/Api/Resource/Concerns/HasSortMap.php new file mode 100644 index 000000000..4c197d40d --- /dev/null +++ b/framework/core/src/Api/Resource/Concerns/HasSortMap.php @@ -0,0 +1,22 @@ +resolveSorts(); + + $map = []; + + foreach ($sorts as $sort) { + $map = array_merge($map, $sort->sortMap()); + } + + return $map; + } +} diff --git a/framework/core/src/Api/Resource/DiscussionResource.php b/framework/core/src/Api/Resource/DiscussionResource.php index eceda452c..7ee42c1c7 100644 --- a/framework/core/src/Api/Resource/DiscussionResource.php +++ b/framework/core/src/Api/Resource/DiscussionResource.php @@ -246,9 +246,13 @@ class DiscussionResource extends AbstractDatabaseResource public function sorts(): array { return [ - SortColumn::make('lastPostedAt'), - SortColumn::make('commentCount'), - SortColumn::make('createdAt'), + SortColumn::make('lastPostedAt') + ->descendingAlias('latest'), + SortColumn::make('commentCount') + ->descendingAlias('top'), + SortColumn::make('createdAt') + ->ascendingAlias('oldest') + ->descendingAlias('newest'), ]; } diff --git a/framework/core/src/Api/Sort/SortColumn.php b/framework/core/src/Api/Sort/SortColumn.php index 492e348d0..e0a11c312 100644 --- a/framework/core/src/Api/Sort/SortColumn.php +++ b/framework/core/src/Api/Sort/SortColumn.php @@ -6,5 +6,36 @@ use Tobyz\JsonApiServer\Laravel\Sort\SortColumn as BaseSortColumn; class SortColumn extends BaseSortColumn { - // + protected array $alias = [ + 'asc' => null, + 'desc' => null, + ]; + + public function ascendingAlias(?string $alias): static + { + $this->alias['asc'] = $alias; + + return $this; + } + + public function descendingAlias(?string $alias): static + { + $this->alias['desc'] = $alias; + + return $this; + } + + public function sortMap(): array + { + $map = []; + + foreach ($this->alias as $direction => $alias) { + if ($alias) { + $sort = ($direction === 'asc' ? '' : '-') . $this->name; + $map[$alias] = $sort; + } + } + + return $map; + } } diff --git a/framework/core/src/Forum/Content/Index.php b/framework/core/src/Forum/Content/Index.php index ff67aa3ea..6120494ef 100644 --- a/framework/core/src/Forum/Content/Index.php +++ b/framework/core/src/Forum/Content/Index.php @@ -10,6 +10,7 @@ namespace Flarum\Forum\Content; use Flarum\Api\Client; +use Flarum\Api\Resource\DiscussionResource; use Flarum\Frontend\Document; use Flarum\Http\UrlGenerator; use Flarum\Locale\TranslatorInterface; @@ -26,6 +27,7 @@ class Index protected SettingsRepositoryInterface $settings, protected UrlGenerator $url, protected TranslatorInterface $translator, + protected DiscussionResource $resource, ) { } @@ -37,7 +39,7 @@ class Index $q = Arr::pull($queryParams, 'q'); $page = max(1, intval(Arr::pull($queryParams, 'page'))); - $sortMap = resolve('flarum.forum.discussions.sortmap'); + $sortMap = $this->resource->sortMap(); $params = [ ...$queryParams,