diff --git a/framework/core/src/Api/Actions/Activity/IndexAction.php b/framework/core/src/Api/Actions/Activity/IndexAction.php index bf4708f98..42c89331a 100644 --- a/framework/core/src/Api/Actions/Activity/IndexAction.php +++ b/framework/core/src/Api/Actions/Activity/IndexAction.php @@ -2,46 +2,77 @@ use Flarum\Core\Repositories\UserRepositoryInterface; use Flarum\Core\Repositories\ActivityRepositoryInterface; -use Flarum\Support\Actor; -use Flarum\Api\Actions\BaseAction; -use Flarum\Api\Actions\ApiParams; -use Flarum\Api\Serializers\ActivitySerializer; +use Flarum\Api\Actions\SerializeCollectionAction; +use Flarum\Api\JsonApiRequest; +use Flarum\Api\JsonApiResponse; -class IndexAction extends BaseAction +class IndexAction extends SerializeCollectionAction { + /** + * @var \Flarum\Core\Repositories\UserRepositoryInterface + */ + protected $users; + + /** + * @var \Flarum\Core\Repositories\ActivityRepositoryInterface + */ + protected $activity; + + /** + * The name of the serializer class to output results with. + * + * @var string + */ + public static $serializer = 'Flarum\Api\Serializers\ActivitySerializer'; + + /** + * The relationships that are available to be included, and which ones are + * included by default. + * + * @var array + */ + public static $include = [ + 'sender' => true, + 'post' => true, + 'post.user' => true, + 'post.discussion' => true, + 'post.discussion.startUser' => true, + 'post.discussion.lastUser' => true + ]; + + /** + * The relations that are linked by default. + * + * @var array + */ + public static $link = ['user']; + /** * Instantiate the action. * - * @param \Flarum\Core\Search\Discussions\UserSearcher $searcher + * @param \Flarum\Core\Repositories\UserRepositoryInterface $users + * @param \Flarum\Core\Repositories\ActivityRepositoryInterface $activity */ - public function __construct(Actor $actor, UserRepositoryInterface $users, ActivityRepositoryInterface $activity) + public function __construct(UserRepositoryInterface $users, ActivityRepositoryInterface $activity) { - $this->actor = $actor; $this->users = $users; $this->activity = $activity; } /** - * Show a user's activity feed. + * Get the activity results, ready to be serialized and assigned to the + * document response. * - * @return \Illuminate\Http\Response + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Illuminate\Database\Eloquent\Collection */ - protected function run(ApiParams $params) + protected function data(JsonApiRequest $request, JsonApiResponse $response) { - $start = $params->start(); - $count = $params->count(20, 50); - $type = $params->get('type'); - $id = $params->get('users'); + $actor = $request->actor->getUser(); - $user = $this->users->findOrFail($id, $this->actor->getUser()); + $user = $this->users->findOrFail($request->get('users'), $actor); - $activity = $this->activity->findByUser($user->id, $this->actor->getUser(), $count, $start, $type); - - // Finally, we can set up the activity serializer and use it to create - // a collection of activity results. - $serializer = new ActivitySerializer(['sender', 'post', 'post.discussion', 'post.user', 'post.discussion.startUser', 'post.discussion.lastUser'], ['user']); - $document = $this->document()->setData($serializer->collection($activity)); - - return $this->respondWithDocument($document); + return $this->activity->findByUser($user->id, $actor, $request->limit, $request->offset, $request->get('type')); } } diff --git a/framework/core/src/Api/Actions/Discussions/CreateAction.php b/framework/core/src/Api/Actions/Discussions/CreateAction.php index d48d370af..e409df8cc 100644 --- a/framework/core/src/Api/Actions/Discussions/CreateAction.php +++ b/framework/core/src/Api/Actions/Discussions/CreateAction.php @@ -32,14 +32,21 @@ class CreateAction extends BaseCreateAction public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer'; /** - * The relations that are included by default. + * The relationships that are available to be included, and which ones are + * included by default. * * @var array */ - public static $include = ['posts', 'startUser', 'lastUser', 'startPost', 'lastPost']; + public static $include = [ + 'posts' => true, + 'startUser' => true, + 'lastUser' => true, + 'startPost' => true, + 'lastPost' => true + ]; /** - * Initialize the action. + * Instantiate the action. * * @param \Illuminate\Contracts\Bus\Dispatcher $bus * @param \Flarum\Core\Models\Forum $forum diff --git a/framework/core/src/Api/Actions/Discussions/DeleteAction.php b/framework/core/src/Api/Actions/Discussions/DeleteAction.php index 637713d27..95d6cbb0c 100644 --- a/framework/core/src/Api/Actions/Discussions/DeleteAction.php +++ b/framework/core/src/Api/Actions/Discussions/DeleteAction.php @@ -16,7 +16,7 @@ class DeleteAction extends BaseDeleteAction protected $bus; /** - * Initialize the action. + * Instantiate the action. * * @param \Illuminate\Contracts\Bus\Dispatcher $bus */ @@ -26,9 +26,10 @@ class DeleteAction extends BaseDeleteAction } /** - * Delete a discussion according to input from the API request. + * Delete a discussion. * * @param \Flarum\Api\Request $request + * @param \Illuminate\Http\Response $response * @return void */ protected function delete(Request $request, Response $response) diff --git a/framework/core/src/Api/Actions/Discussions/IndexAction.php b/framework/core/src/Api/Actions/Discussions/IndexAction.php index 8051bd2f2..d2da7263e 100644 --- a/framework/core/src/Api/Actions/Discussions/IndexAction.php +++ b/framework/core/src/Api/Actions/Discussions/IndexAction.php @@ -23,46 +23,25 @@ class IndexAction extends SerializeCollectionAction public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer'; /** - * The relations that are available to be included. + * The relationships that are available to be included, and which ones are + * included by default. * * @var array */ - public static $includeAvailable = ['startUser', 'lastUser', 'startPost', 'lastPost', 'relevantPosts']; - - /** - * The relations that are included by default. - * - * @var array - */ - public static $include = ['startUser', 'lastUser']; - - /** - * The maximum number of records that can be requested. - * - * @var integer - */ - public static $limitMax = 50; - - /** - * The number of records included by default. - * - * @var integer - */ - public static $limit = 20; + public static $include = [ + 'startUser' => true, + 'lastUser' => true, + 'startPost' => false, + 'lastPost' => false, + 'relevantPosts' => false + ]; /** * The fields that are available to be sorted by. * * @var array */ - public static $sortAvailable = ['lastTime', 'commentsCount', 'startTime']; - - /** - * The default field to sort by. - * - * @var string - */ - public static $sort = ['lastTime' => 'desc']; + public static $sortFields = ['lastTime', 'commentsCount', 'startTime']; /** * Instantiate the action. diff --git a/framework/core/src/Api/Actions/Discussions/ShowAction.php b/framework/core/src/Api/Actions/Discussions/ShowAction.php index 823418a77..dbbbc2302 100644 --- a/framework/core/src/Api/Actions/Discussions/ShowAction.php +++ b/framework/core/src/Api/Actions/Discussions/ShowAction.php @@ -12,16 +12,12 @@ class ShowAction extends SerializeResourceAction use GetsPosts; /** - * The discussion repository. - * - * @var DiscussionRepository + * @var \Flarum\Core\Repositories\DiscussionRepositoryInterface */ protected $discussions; /** - * The post repository. - * - * @var PostRepository + * @var \Flarum\Core\Repositories\PostRepositoryInterface */ protected $posts; @@ -33,23 +29,21 @@ class ShowAction extends SerializeResourceAction public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer'; /** - * The relations that are available to be included. - * - * @var array - */ - public static $includeAvailable = [ - 'startUser', 'lastUser', 'startPost', 'lastPost', 'posts', - 'posts.user', 'posts.user.groups', 'posts.editUser', 'posts.hideUser' - ]; - - /** - * The relations that are included by default. + * The relationships that are available to be included, and which ones are + * included by default. * * @var array */ public static $include = [ - 'startPost', 'lastPost', 'posts', - 'posts.user', 'posts.user.groups', 'posts.editUser', 'posts.hideUser' + 'startUser' => false, + 'lastUser' => false, + 'startPost' => true, + 'lastPost' => true, + 'posts' => true, + 'posts.user' => true, + 'posts.user.groups' => true, + 'posts.editUser' => true, + 'posts.hideUser' => true ]; /** @@ -64,33 +58,20 @@ class ShowAction extends SerializeResourceAction * * @var array */ - public static $sortAvailable = ['time']; + public static $sortFields = ['time']; /** - * The default field to sort by. + * The default sort field and order to user. * * @var string */ public static $sort = ['time' => 'asc']; - /** - * The maximum number of records that can be requested. - * - * @var integer - */ - public static $limitMax = 50; - - /** - * The number of records included by default. - * - * @var integer - */ - public static $limit = 20; - /** * Instantiate the action. * - * @param PostRepository $posts + * @param \Flarum\Core\Repositories\DiscussionRepositoryInterface $discussions + * @param \Flarum\Core\Repositories\PostRepositoryInterface $posts */ public function __construct(DiscussionRepositoryInterface $discussions, PostRepositoryInterface $posts) { diff --git a/framework/core/src/Api/Actions/Discussions/UpdateAction.php b/framework/core/src/Api/Actions/Discussions/UpdateAction.php index 588534747..aabd814d4 100644 --- a/framework/core/src/Api/Actions/Discussions/UpdateAction.php +++ b/framework/core/src/Api/Actions/Discussions/UpdateAction.php @@ -2,7 +2,6 @@ use Flarum\Core\Commands\EditDiscussionCommand; use Flarum\Core\Commands\ReadDiscussionCommand; -use Flarum\Core\Exceptions\PermissionDeniedException; use Flarum\Api\Actions\SerializeResourceAction; use Flarum\Api\Actions\Posts\GetsPosts; use Flarum\Api\JsonApiRequest; @@ -12,8 +11,6 @@ use Illuminate\Contracts\Bus\Dispatcher; class UpdateAction extends SerializeResourceAction { /** - * The command bus. - * * @var \Illuminate\Contracts\Bus\Dispatcher */ protected $bus; @@ -30,10 +27,13 @@ class UpdateAction extends SerializeResourceAction * * @var array */ - public static $include = ['addedPosts', 'addedPosts.user']; + public static $include = [ + 'addedPosts' => true, + 'addedPosts.user' => true + ]; /** - * Initialize the action. + * Instantiate the action. * * @param \Illuminate\Contracts\Bus\Dispatcher $bus */ @@ -46,7 +46,8 @@ class UpdateAction extends SerializeResourceAction * Update a discussion according to input from the API request, and return * it ready to be serialized and assigned to the JsonApi response. * - * @param \Flarum\Api\Request $request + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response * @return \Flarum\Core\Models\Discussion */ protected function data(JsonApiRequest $request, JsonApiResponse $response) @@ -54,18 +55,12 @@ class UpdateAction extends SerializeResourceAction $user = $request->actor->getUser(); $discussionId = $request->get('id'); - // First, we will run the EditDiscussionCommand. This will update the - // discussion's direct properties; by default, this is just the title. - // As usual, however, we will fire an event to allow plugins to update - // additional properties. if ($data = array_except($request->get('data'), ['readNumber'])) { $discussion = $this->bus->dispatch( new EditDiscussionCommand($discussionId, $user, $data) ); } - // Next, if a read number was specified in the request, we will run the - // ReadDiscussionCommand. if ($readNumber = $request->get('data.readNumber')) { $state = $this->bus->dispatch( new ReadDiscussionCommand($discussionId, $user, $readNumber) diff --git a/framework/core/src/Api/Actions/Groups/IndexAction.php b/framework/core/src/Api/Actions/Groups/IndexAction.php index e306356a5..d4989842d 100644 --- a/framework/core/src/Api/Actions/Groups/IndexAction.php +++ b/framework/core/src/Api/Actions/Groups/IndexAction.php @@ -1,18 +1,29 @@ document->setData($serializer->collection($groups)); - - return $this->respondWithDocument(); + return Group::get(); } } diff --git a/framework/core/src/Api/Actions/Notifications/IndexAction.php b/framework/core/src/Api/Actions/Notifications/IndexAction.php index 85b60d7f1..8bc24129a 100644 --- a/framework/core/src/Api/Actions/Notifications/IndexAction.php +++ b/framework/core/src/Api/Actions/Notifications/IndexAction.php @@ -1,50 +1,78 @@ true, + 'subject' => true, + 'subject.discussion' => true + ]; + + /** + * The maximum number of records that can be requested. + * + * @var integer + */ + public static $limitMax = 50; + + /** + * The number of records included by default. + * + * @var integer + */ + public static $limit = 10; + /** * Instantiate the action. * - * @param \Flarum\Core\Search\Discussions\UserSearcher $searcher + * @param \Flarum\Core\Repositories\NotificationRepositoryInterface $notifications */ - public function __construct(Actor $actor, NotificationRepositoryInterface $notifications) + public function __construct(NotificationRepositoryInterface $notifications) { - $this->actor = $actor; $this->notifications = $notifications; } /** - * Show a user's notifications feed. + * Get the notification results, ready to be serialized and assigned to the + * document response. * - * @return \Illuminate\Http\Response + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Illuminate\Database\Eloquent\Collection */ - protected function run(ApiParams $params) + protected function data(JsonApiRequest $request, JsonApiResponse $response) { - $start = $params->start(); - $count = $params->count(10, 50); - - if (! $this->actor->isAuthenticated()) { + if (! $request->actor->isAuthenticated()) { throw new PermissionDeniedException; } - $user = $this->actor->getUser(); - - $notifications = $this->notifications->findByUser($user->id, $count, $start); + $user = $request->actor->getUser(); $user->markNotificationsAsRead()->save(); - // Finally, we can set up the notification serializer and use it to create - // a collection of notification results. - $serializer = new NotificationSerializer(['sender', 'subject', 'subject.discussion']); - $document = $this->document()->setData($serializer->collection($notifications)); - - return $this->respondWithDocument($document); + return $this->notifications->findByUser($user->id, $request->limit, $request->offset); } } diff --git a/framework/core/src/Api/Actions/Notifications/UpdateAction.php b/framework/core/src/Api/Actions/Notifications/UpdateAction.php index bd0d4ccd2..836587309 100644 --- a/framework/core/src/Api/Actions/Notifications/UpdateAction.php +++ b/framework/core/src/Api/Actions/Notifications/UpdateAction.php @@ -1,34 +1,47 @@ get('id'); - $user = $this->actor->getUser(); + $this->bus = $bus; + } - // if ($params->get('notifications.isRead')) { - $command = new ReadNotificationCommand($notificationId, $user); - $notification = $this->dispatch($command, $params); - // } - - // Presumably, the discussion was updated successfully. (One of the command - // handlers would have thrown an exception if not.) We set this - // discussion as our document's primary element. - $serializer = new NotificationSerializer; - $document = $this->document()->setData($serializer->resource($notification)); - - return $this->respondWithDocument($document); + /** + * Mark a notification as read, and return it ready to be serialized and + * assigned to the JsonApi response. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Flarum\Core\Models\Notification + */ + protected function data(JsonApiRequest $request, JsonApiResponse $response) + { + return $this->bus->dispatch( + new ReadNotificationCommand($request->get('id'), $request->actor->getUser()) + ); } } diff --git a/framework/core/src/Api/Actions/Posts/CreateAction.php b/framework/core/src/Api/Actions/Posts/CreateAction.php index a437b8cb8..76ea2249d 100644 --- a/framework/core/src/Api/Actions/Posts/CreateAction.php +++ b/framework/core/src/Api/Actions/Posts/CreateAction.php @@ -2,46 +2,61 @@ use Flarum\Core\Commands\PostReplyCommand; use Flarum\Core\Commands\ReadDiscussionCommand; -use Flarum\Api\Actions\ApiParams; -use Flarum\Api\Actions\BaseAction; -use Flarum\Api\Serializers\PostSerializer; +use Flarum\Api\Actions\CreateAction as BaseCreateAction; +use Flarum\Api\JsonApiRequest; +use Flarum\Api\JsonApiResponse; +use Illuminate\Contracts\Bus\Dispatcher; -class CreateAction extends BaseAction +class CreateAction extends BaseCreateAction { /** - * Reply to a discussion. - * - * @return Response + * @var \Illuminate\Contracts\Bus\Dispatcher */ - protected function run(ApiParams $params) + protected $bus; + + /** + * The name of the serializer class to output results with. + * + * @var string + */ + public static $serializer = 'Flarum\Api\Serializers\PostSerializer'; + + /** + * Instantiate the action. + * + * @param \Illuminate\Contracts\Bus\Dispatcher $bus + */ + public function __construct(Dispatcher $bus) { - $user = $this->actor->getUser(); + $this->bus = $bus; + } - // We've received a request to post a reply. By default, the only - // required attributes of a post is the ID of the discussion to post in, - // the post content, and the author's user account. Let's set up a - // command with this information. We also fire an event to allow plugins - // to add data to the command. - $discussionId = $params->get('data.links.discussion.linkage.id'); - $content = $params->get('data.content'); + /** + * Reply to a discussion according to input from the API request. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Flarum\Core\Models\Post + */ + protected function create(JsonApiRequest $request, JsonApiResponse $response) + { + $user = $request->actor->getUser(); - $command = new PostReplyCommand($discussionId, $content, $user); - $post = $this->dispatch($command, $params); + $discussionId = $request->get('data.links.discussion.linkage.id'); + + $post = $this->bus->dispatch( + new PostReplyCommand($discussionId, $user, $request->get('data')) + ); // After replying, we assume that the user has seen all of the posts // in the discussion; thus, we will mark the discussion as read if // they are logged in. if ($user->exists) { - $command = new ReadDiscussionCommand($discussionId, $user, $post->number); - $this->dispatch($command, $params); + $this->bus->dispatch( + new ReadDiscussionCommand($discussionId, $user, $post->number) + ); } - // Presumably, the post was created successfully. (The command handler - // would have thrown an exception if not.) We set this post as our - // document's primary element. - $serializer = new PostSerializer; - $document = $this->document()->setData($serializer->resource($post)); - - return $this->respondWithDocument($document, 201); + return $post; } } diff --git a/framework/core/src/Api/Actions/Posts/DeleteAction.php b/framework/core/src/Api/Actions/Posts/DeleteAction.php index 66c806b90..d703e5ae1 100644 --- a/framework/core/src/Api/Actions/Posts/DeleteAction.php +++ b/framework/core/src/Api/Actions/Posts/DeleteAction.php @@ -1,23 +1,39 @@ bus = $bus; + } + /** * Delete a post. * - * @return Response + * @param \Flarum\Api\Request $request + * @param \Illuminate\Http\Response $response + * @return void */ - protected function run(ApiParams $params) + protected function delete(Request $request, Response $response) { - $postId = $params->get('id'); - - $command = new DeletePostCommand($postId, $this->actor->getUser()); - $this->dispatch($command, $params); - - return $this->respondWithoutContent(); + $this->bus->dispatch( + new DeletePostCommand($request->get('id'), $request->actor->getUser()) + ); } } diff --git a/framework/core/src/Api/Actions/Posts/GetsPosts.php b/framework/core/src/Api/Actions/Posts/GetsPosts.php index 7470550c8..be99bcf9f 100644 --- a/framework/core/src/Api/Actions/Posts/GetsPosts.php +++ b/framework/core/src/Api/Actions/Posts/GetsPosts.php @@ -1,6 +1,5 @@ true, + 'user.groups' => true, + 'editUser' => true, + 'hideUser' => true, + 'discussion' => true + ]; + /** * Instantiate the action. * - * @param Post $posts + * @param \Flarum\Core\Repositories\PostRepositoryInterface $posts */ - public function __construct(Actor $actor, PostRepositoryInterface $posts) + public function __construct(PostRepositoryInterface $posts) { - $this->actor = $actor; $this->posts = $posts; } /** - * Show posts from a discussion, or by providing an array of IDs. - * - * @return Response - */ - protected function run(ApiParams $params) + * Get the post results, ready to be serialized and assigned to the + * document response. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Illuminate\Database\Eloquent\Collection + */ + protected function data(JsonApiRequest $request, JsonApiResponse $response) { - $postIds = (array) $params->get('ids'); - $include = ['user', 'user.groups', 'editUser', 'hideUser', 'discussion']; - $user = $this->actor->getUser(); + $postIds = (array) $request->get('ids'); + $user = $request->actor->getUser(); if (count($postIds)) { $posts = $this->posts->findByIds($postIds, $user); } else { - if ($discussionId = $params->get('discussions')) { + if ($discussionId = $request->get('discussions')) { $where['discussion_id'] = $discussionId; } - if ($userId = $params->get('users')) { + if ($userId = $request->get('users')) { $where['user_id'] = $userId; } - $posts = $this->getPosts($params, $where, $user); + $posts = $this->getPosts($request, $where); } - if (! count($posts)) { - throw new ModelNotFoundException; - } - - // Finally, we can set up the post serializer and use it to create - // a post resource or collection, depending on how many posts were - // requested. - $serializer = new PostSerializer($include); - $document = $this->document()->setData($serializer->collection($posts->load($include))); - - return $this->respondWithDocument($document); + return $posts; } } diff --git a/framework/core/src/Api/Actions/Posts/ShowAction.php b/framework/core/src/Api/Actions/Posts/ShowAction.php index fa8d2b404..18f96de34 100644 --- a/framework/core/src/Api/Actions/Posts/ShowAction.php +++ b/framework/core/src/Api/Actions/Posts/ShowAction.php @@ -2,41 +2,57 @@ use Illuminate\Database\Eloquent\ModelNotFoundException; use Flarum\Core\Repositories\PostRepositoryInterface; -use Flarum\Support\Actor; -use Flarum\Api\Actions\BaseAction; -use Flarum\Api\Actions\ApiParams; -use Flarum\Api\Serializers\PostSerializer; +use Flarum\Api\Actions\SerializeResourceAction; +use Flarum\Api\JsonApiRequest; +use Flarum\Api\JsonApiResponse; -class ShowAction extends BaseAction +class ShowAction extends SerializeResourceAction { + /** + * @var \Flarum\Core\Repositories\PostRepositoryInterface + */ protected $posts; - public function __construct(Actor $actor, PostRepositoryInterface $posts) + /** + * The name of the serializer class to output results with. + * + * @var string + */ + public static $serializer = 'Flarum\Api\Serializers\PostSerializer'; + + /** + * The relationships that are available to be included, and which ones are + * included by default. + * + * @var array + */ + public static $include = [ + 'user' => true, + 'editUser' => true, + 'hideUser' => true, + 'discussion' => false + ]; + + /** + * Instantiate the action. + * + * @param \Flarum\Core\Repositories\PostRepositoryInterface $posts + */ + public function __construct(PostRepositoryInterface $posts) { - $this->actor = $actor; $this->posts = $posts; } /** - * Show a single post by ID. - * - * @return Response - */ - protected function run(ApiParams $params) + * Get a single post, ready to be serialized and assigned to the JsonApi + * response. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Flarum\Core\Models\Discussion + */ + protected function data(JsonApiRequest $request, JsonApiResponse $response) { - $id = $params->get('id'); - $posts = $this->posts->findOrFail($id, $this->actor->getUser()); - - $include = $params->included(['discussion', 'replyTo']); - $relations = array_merge(['user', 'editUser', 'hideUser'], $include); - $posts->load($relations); - - // Finally, we can set up the post serializer and use it to create - // a post resource or collection, depending on how many posts were - // requested. - $serializer = new PostSerializer($relations); - $document = $this->document()->setData($serializer->resource($posts->first())); - - return $this->respondWithDocument($document); + return $this->posts->findOrFail($request->get('id'), $request->actor->getUser()); } } diff --git a/framework/core/src/Api/Actions/Posts/UpdateAction.php b/framework/core/src/Api/Actions/Posts/UpdateAction.php index 4aeb20baa..7b88040a4 100644 --- a/framework/core/src/Api/Actions/Posts/UpdateAction.php +++ b/framework/core/src/Api/Actions/Posts/UpdateAction.php @@ -1,34 +1,47 @@ get('id'); + $this->bus = $bus; + } - // EditPost is a single command because we don't want to allow partial - // updates (i.e. if we were to run one command and then another, if the - // second one failed, the first one would still have succeeded.) - $command = new EditPostCommand($postId, $this->actor->getUser()); - $this->hydrate($command, $params->get('data')); - $post = $this->dispatch($command, $params); - - // Presumably, the post was updated successfully. (The command handler - // would have thrown an exception if not.) We set this post as our - // document's primary element. - $serializer = new PostSerializer; - $document = $this->document()->setData($serializer->resource($post)); - - return $this->respondWithDocument($document); + /** + * Update a post according to input from the API request, and return it + * ready to be serialized and assigned to the JsonApi response. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Flarum\Core\Models\Post + */ + protected function data(JsonApiRequest $request, JsonApiResponse $response) + { + return $this->bus->dispatch( + new EditPostCommand($request->get('id'), $request->actor->getUser(), $request->get('data')) + ); } } diff --git a/framework/core/src/Api/Actions/SerializeAction.php b/framework/core/src/Api/Actions/SerializeAction.php index 919ece2f4..490bf5ec8 100644 --- a/framework/core/src/Api/Actions/SerializeAction.php +++ b/framework/core/src/Api/Actions/SerializeAction.php @@ -16,21 +16,15 @@ abstract class SerializeAction implements ActionInterface public static $serializer; /** - * The relations that are available to be included. - * - * @var array - */ - public static $includeAvailable = []; - - /** - * The relations that are included by default. + * The relationships that are available to be included (keys), and which + * ones are included by default (boolean values). * * @var array */ public static $include = []; /** - * The relations that are linked by default. + * The relationships that are linked by default. * * @var array */ @@ -41,24 +35,24 @@ abstract class SerializeAction implements ActionInterface * * @var integer */ - public static $limitMax; + public static $limitMax = 50; /** * The number of records included by default. * * @var integer */ - public static $limit; + public static $limit = 20; /** * The fields that are available to be sorted by. * * @var array */ - public static $sortAvailable = []; + public static $sortFields = []; /** - * The default field to sort by. + * The default sort field and order to user. * * @var string */ @@ -110,7 +104,7 @@ abstract class SerializeAction implements ActionInterface */ protected static function buildJsonApiRequest(Request $request) { - $request = new JsonApiRequest($request->input, $request->actor, $request->httpRequest); + $request = new JsonApiRequest($request->input, $request->actor, $request->http); $criteria = new Criteria($request->input); @@ -132,7 +126,7 @@ abstract class SerializeAction implements ActionInterface */ protected static function sanitizeInclude(array $include) { - return array_intersect($include, static::$includeAvailable) ?: static::$include; + return array_intersect($include, array_keys(static::$include)) ?: array_keys(array_filter(static::$include)); } /** @@ -144,7 +138,7 @@ abstract class SerializeAction implements ActionInterface */ protected static function sanitizeSort(array $sort) { - return array_intersect_key($sort, array_flip(static::$sortAvailable)) ?: static::$sort; + return array_intersect_key($sort, array_flip(static::$sortFields)) ?: static::$sort; } /** diff --git a/framework/core/src/Api/Actions/Users/CreateAction.php b/framework/core/src/Api/Actions/Users/CreateAction.php index 5ad6b536c..71d093ec2 100644 --- a/framework/core/src/Api/Actions/Users/CreateAction.php +++ b/framework/core/src/Api/Actions/Users/CreateAction.php @@ -1,36 +1,57 @@ get('data.username'); - $email = $params->get('data.email'); - $password = $params->get('data.password'); + $this->bus = $bus; + $this->forum = $forum; + } - $command = new RegisterUserCommand($username, $email, $password, $this->actor->getUser(), app('flarum.forum')); - $user = $this->dispatch($command, $params); - - // Presumably, the user was created successfully. (The command handler - // would have thrown an exception if not.) We set this post as our - // document's primary element. - $serializer = new UserSerializer; - $document = $this->document()->setData($serializer->resource($user)); - - return $this->respondWithDocument($document, 201); + /** + * Register a user according to input from the API request. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Flarum\Core\Models\User + */ + protected function create(JsonApiRequest $request, JsonApiResponse $response) + { + return $this->bus->dispatch( + new RegisterUserCommand($request->actor->getUser(), $this->forum, $request->get('data')) + ); } } diff --git a/framework/core/src/Api/Actions/Users/DeleteAction.php b/framework/core/src/Api/Actions/Users/DeleteAction.php index 4f9d2a528..c7a9c5efb 100644 --- a/framework/core/src/Api/Actions/Users/DeleteAction.php +++ b/framework/core/src/Api/Actions/Users/DeleteAction.php @@ -1,23 +1,41 @@ bus = $bus; + } + /** * Delete a user. * - * @return Response + * @param \Flarum\Api\Request $request + * @param \Illuminate\Http\Response $response + * @return void */ - protected function run(ApiParams $params) + protected function delete(Request $request, Response $response) { - $userId = $params->get('id'); - - $command = new DeleteUserCommand($userId, $this->actor->getUser()); - $this->dispatch($command, $params); - - return $this->respondWithoutContent(); + $this->bus->dispatch( + new DeleteUserCommand($request->get('id'), $request->actor->getUser()) + ); } } diff --git a/framework/core/src/Api/Actions/Users/IndexAction.php b/framework/core/src/Api/Actions/Users/IndexAction.php index 7ccd8a74e..b4c7e2d4c 100644 --- a/framework/core/src/Api/Actions/Users/IndexAction.php +++ b/framework/core/src/Api/Actions/Users/IndexAction.php @@ -2,12 +2,11 @@ use Flarum\Core\Search\Users\UserSearchCriteria; use Flarum\Core\Search\Users\UserSearcher; -use Flarum\Support\Actor; -use Flarum\Api\Actions\BaseAction; -use Flarum\Api\Actions\ApiParams; -use Flarum\Api\Serializers\UserSerializer; +use Flarum\Api\Actions\SerializeCollectionAction; +use Flarum\Api\JsonApiRequest; +use Flarum\Api\JsonApiResponse; -class IndexAction extends BaseAction +class IndexAction extends SerializeCollectionAction { /** * The user searcher. @@ -21,59 +20,59 @@ class IndexAction extends BaseAction * * @param \Flarum\Core\Search\Discussions\UserSearcher $searcher */ - public function __construct(Actor $actor, UserSearcher $searcher) + public function __construct(UserSearcher $searcher) { - $this->actor = $actor; $this->searcher = $searcher; } /** - * Show a list of users. + * The name of the serializer class to output results with. * - * @return \Illuminate\Http\Response + * @var string */ - protected function run(ApiParams $params) + public static $serializer = 'Flarum\Api\Serializers\UserSerializer'; + + /** + * The relationships that are available to be included, and which ones are + * included by default. + * + * @var array + */ + public static $include = [ + 'groups' => true + ]; + + /** + * The fields that are available to be sorted by. + * + * @var array + */ + public static $sortFields = ['username', 'postsCount', 'discussionsCount', 'lastSeenTime', 'joinTime']; + + /** + * Get the user results, ready to be serialized and assigned to the + * document response. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Illuminate\Database\Eloquent\Collection + */ + protected function data(JsonApiRequest $request, JsonApiResponse $response) { - $query = $params->get('q'); - $start = $params->start(); - $include = $params->included(['groups']); - $count = $params->count(20, 50); - $sort = $params->sort(['', 'username', 'posts', 'discussions', 'lastActive', 'created']); + $criteria = new UserSearchCriteria( + $request->actor->getUser(), + $request->get('q'), + $request->sort + ); - $relations = array_merge(['groups'], $include); - - // Set up the user searcher with our search criteria, and get the - // requested range of results with the necessary relations loaded. - $criteria = new UserSearchCriteria($this->actor->getUser(), $query, $sort['field'], $sort['order']); - - $results = $this->searcher->search($criteria, $count, $start, $relations); - - $document = $this->document(); + $results = $this->searcher->search($criteria, $request->limit, $request->offset, $request->include); if (($total = $results->getTotal()) !== null) { - $document->addMeta('total', $total); + $response->content->addMeta('total', $total); } - // If there are more results, then we need to construct a URL to the - // next results page and add that to the metadata. We do this by - // compacting all of the valid query parameters which have been - // specified. - if ($results->areMoreResults()) { - $start += $count; - $include = implode(',', $include); - $sort = $sort['string']; - $input = array_filter(compact('query', 'sort', 'start', 'count', 'include')); - $moreUrl = $this->buildUrl('users.index', [], $input); - } else { - $moreUrl = ''; - } - $document->addMeta('moreUrl', $moreUrl); + // $response->content->addMeta('moreUrl', $moreUrl); - // Finally, we can set up the discussion serializer and use it to create - // a collection of discussion results. - $serializer = new UserSerializer($relations); - $document->setData($serializer->collection($results->getUsers())); - - return $this->respondWithDocument($document); + return $results->getUsers(); } } diff --git a/framework/core/src/Api/Actions/Users/ShowAction.php b/framework/core/src/Api/Actions/Users/ShowAction.php index f4023d5b5..4350c5f6d 100644 --- a/framework/core/src/Api/Actions/Users/ShowAction.php +++ b/framework/core/src/Api/Actions/Users/ShowAction.php @@ -1,44 +1,60 @@ true + ]; + + /** + * Instantiate the action. + * + * @param \Flarum\Core\Repositories\UserRepositoryInterface $users + */ + public function __construct(UserRepositoryInterface $users) { - $this->actor = $actor; $this->users = $users; } /** - * Show a single user. + * Get a single user, ready to be serialized and assigned to the JsonApi + * response. * - * @return Response + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Flarum\Core\Models\Discussion */ - public function run(ApiParams $params) + protected function data(JsonApiRequest $request, JsonApiResponse $response) { - $id = $params->get('id'); + $id = $request->get('id'); if (! is_numeric($id)) { $id = $this->users->getIdForUsername($id); } - $user = $this->users->findOrFail($id, $this->actor->getUser()); - - // Set up the user serializer, which we will use to create the - // document's primary resource. We will specify that we want the - // 'groups' relation to be included by default. - $serializer = new UserSerializer(['groups']); - $document = $this->document()->setData($serializer->resource($user)); - - return $this->respondWithDocument($document); + return $this->users->findOrFail($id, $request->actor->getUser()); } } diff --git a/framework/core/src/Api/Actions/Users/UpdateAction.php b/framework/core/src/Api/Actions/Users/UpdateAction.php index fe48f0f9a..de518824c 100644 --- a/framework/core/src/Api/Actions/Users/UpdateAction.php +++ b/framework/core/src/Api/Actions/Users/UpdateAction.php @@ -1,35 +1,47 @@ get('id'); + $this->bus = $bus; + } - // EditUser is a single command because we don't want to allow partial - // updates (i.e. if we were to run one command and then another, if the - // second one failed, the first one would still have succeeded.) - $command = new EditUserCommand($userId, $this->actor->getUser()); - $this->hydrate($command, $params->get('data')); - $user = $this->dispatch($command, $params); - - // Presumably, the user was updated successfully. (The command handler - // would have thrown an exception if not.) We set this user as our - // document's primary element. - $serializer = new UserSerializer; - $document = $this->document()->setData($serializer->resource($user)); - - return $this->respondWithDocument($document); + /** + * Update a user according to input from the API request, and return it + * ready to be serialized and assigned to the JsonApi response. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Flarum\Core\Models\Post + */ + protected function data(JsonApiRequest $request, JsonApiResponse $response) + { + return $this->bus->dispatch( + new EditUserCommand($request->get('id'), $request->actor->getUser(), $request->get('data')) + ); } } diff --git a/framework/core/src/Api/Actions/Users/UploadAvatarAction.php b/framework/core/src/Api/Actions/Users/UploadAvatarAction.php index 417a0e9a6..6b6dd7834 100644 --- a/framework/core/src/Api/Actions/Users/UploadAvatarAction.php +++ b/framework/core/src/Api/Actions/Users/UploadAvatarAction.php @@ -1,25 +1,47 @@ file('avatar'); + $this->bus = $bus; + } - $user = $this->dispatch( - new UploadAvatarCommand($userId, $file, $this->actor->getUser()), - $routeParams + /** + * Upload an avatar for a user, and return the user ready to be serialized + * and assigned to the JsonApi response. + * + * @param \Flarum\Api\JsonApiRequest $request + * @param \Flarum\Api\JsonApiResponse $response + * @return \Flarum\Core\Models\User + */ + protected function data(JsonApiRequest $request, JsonApiResponse $response) + { + return $this->bus->dispatch( + new UploadAvatarCommand($request->get('id'), $request->http->file('avatar'), $request->actor->getUser()) ); - - $serializer = new UserSerializer; - $document = $this->document()->setData($serializer->resource($user)); - - return $this->respondWithDocument($document); } } diff --git a/framework/core/src/Api/Events/WillRespondWithDocument.php b/framework/core/src/Api/Events/WillRespondWithDocument.php deleted file mode 100644 index 16adf1d51..000000000 --- a/framework/core/src/Api/Events/WillRespondWithDocument.php +++ /dev/null @@ -1,17 +0,0 @@ -document = $document; - $this->statusCode = $statusCode; - $this->headers = $headers; - } -} diff --git a/framework/core/src/Api/Request.php b/framework/core/src/Api/Request.php index 5ce0e3d08..d5579226a 100644 --- a/framework/core/src/Api/Request.php +++ b/framework/core/src/Api/Request.php @@ -9,17 +9,17 @@ class Request public $actor; - public $httpRequest; + public $http; - public function __construct(array $input, Actor $actor, IlluminateRequest $httpRequest = null) + public function __construct(array $input, Actor $actor, IlluminateRequest $http = null) { $this->input = $input; $this->actor = $actor; - $this->httpRequest = $httpRequest; + $this->http = $http; } public function get($key, $default = null) { - return isset($this->input[$key]) ? $this->input[$key] : $default; + return array_get($this->input, $key, $default); } } diff --git a/framework/core/src/Core/Commands/EditPostCommand.php b/framework/core/src/Core/Commands/EditPostCommand.php index f34104993..f21740056 100644 --- a/framework/core/src/Core/Commands/EditPostCommand.php +++ b/framework/core/src/Core/Commands/EditPostCommand.php @@ -6,13 +6,12 @@ class EditPostCommand public $user; - public $content; + public $data; - public $isHidden; - - public function __construct($postId, $user) + public function __construct($postId, $user, $data) { $this->postId = $postId; $this->user = $user; + $this->data = $data; } } diff --git a/framework/core/src/Core/Commands/EditUserCommand.php b/framework/core/src/Core/Commands/EditUserCommand.php index 9b0ccba38..00ab6d039 100644 --- a/framework/core/src/Core/Commands/EditUserCommand.php +++ b/framework/core/src/Core/Commands/EditUserCommand.php @@ -6,21 +6,12 @@ class EditUserCommand public $user; - public $username; + public $data; - public $email; - - public $password; - - public $bio; - - public $readTime; - - public $preferences; - - public function __construct($userId, $user) + public function __construct($userId, $user, $data) { $this->userId = $userId; $this->user = $user; + $this->data = $data; } } diff --git a/framework/core/src/Core/Commands/PostReplyCommand.php b/framework/core/src/Core/Commands/PostReplyCommand.php index 7fa9e11f5..dd3167838 100644 --- a/framework/core/src/Core/Commands/PostReplyCommand.php +++ b/framework/core/src/Core/Commands/PostReplyCommand.php @@ -4,14 +4,14 @@ class PostReplyCommand { public $discussionId; - public $content; - public $user; - public function __construct($discussionId, $content, $user) + public $data; + + public function __construct($discussionId, $user, $data) { $this->discussionId = $discussionId; - $this->content = $content; $this->user = $user; + $this->data = $data; } } diff --git a/framework/core/src/Core/Commands/RegisterUserCommand.php b/framework/core/src/Core/Commands/RegisterUserCommand.php index e9cdf216b..a881212ba 100644 --- a/framework/core/src/Core/Commands/RegisterUserCommand.php +++ b/framework/core/src/Core/Commands/RegisterUserCommand.php @@ -6,18 +6,12 @@ class RegisterUserCommand public $user; - public $username; + public $data; - public $email; - - public $password; - - public function __construct($username, $email, $password, $user, $forum) + public function __construct($user, $forum, $data) { - $this->username = $username; - $this->email = $email; - $this->password = $password; $this->user = $user; $this->forum = $forum; + $this->data = $data; } } diff --git a/framework/core/src/Core/Handlers/Commands/EditPostCommandHandler.php b/framework/core/src/Core/Handlers/Commands/EditPostCommandHandler.php index 477f741ca..50ed8fdea 100644 --- a/framework/core/src/Core/Handlers/Commands/EditPostCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/EditPostCommandHandler.php @@ -22,14 +22,16 @@ class EditPostCommandHandler $post->assertCan($user, 'edit'); - if (isset($command->content)) { - $post->revise($command->content, $user); + if (isset($command->data['content'])) { + $post->revise($command->data['content'], $user); } - if ($command->isHidden === true) { - $post->hide($user); - } elseif ($command->isHidden === false) { - $post->restore($user); + if (isset($command->data['isHidden'])) { + if ($command->data['isHidden']) { + $post->hide($user); + } else { + $post->restore($user); + } } event(new PostWillBeSaved($post, $command)); diff --git a/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php b/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php index 0ee550d2f..85754f77e 100644 --- a/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php @@ -22,23 +22,28 @@ class EditUserCommandHandler $userToEdit->assertCan($user, 'edit'); - if (isset($command->username)) { - $userToEdit->rename($command->username); + if (isset($command->data['username'])) { + $userToEdit->rename($command->data['username']); } - if (isset($command->email)) { - $userToEdit->changeEmail($command->email); + + if (isset($command->data['email'])) { + $userToEdit->changeEmail($command->data['email']); } - if (isset($command->password)) { - $userToEdit->changePassword($command->password); + + if (isset($command->data['password'])) { + $userToEdit->changePassword($command->data['password']); } - if (isset($command->bio)) { - $userToEdit->changeBio($command->bio); + + if (isset($command->data['bio'])) { + $userToEdit->changeBio($command->data['bio']); } - if (! empty($command->readTime)) { + + if (! empty($command->data['readTime'])) { $userToEdit->markAllAsRead(); } - if (! empty($command->preferences)) { - foreach ($command->preferences as $k => $v) { + + if (! empty($command->data['preferences'])) { + foreach ($command->data['preferences'] as $k => $v) { $userToEdit->setPreference($k, $v); } } diff --git a/framework/core/src/Core/Handlers/Commands/PostReplyCommandHandler.php b/framework/core/src/Core/Handlers/Commands/PostReplyCommandHandler.php index 9ac223b7a..0d496483a 100644 --- a/framework/core/src/Core/Handlers/Commands/PostReplyCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/PostReplyCommandHandler.php @@ -34,7 +34,7 @@ class PostReplyCommandHandler // opportunity to alter the post entity based on data in the command. $post = CommentPost::reply( $command->discussionId, - $command->content, + array_get($command->data, 'content'), $user->id ); diff --git a/framework/core/src/Core/Handlers/Commands/RegisterUserCommandHandler.php b/framework/core/src/Core/Handlers/Commands/RegisterUserCommandHandler.php index 8cc70db32..c83cbf3b6 100644 --- a/framework/core/src/Core/Handlers/Commands/RegisterUserCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/RegisterUserCommandHandler.php @@ -20,9 +20,9 @@ class RegisterUserCommandHandler // Before persistance, though, fire an event to give plugins an // opportunity to alter the post entity based on data in the command. $user = User::register( - $command->username, - $command->email, - $command->password + array_get($command->data, 'username'), + array_get($command->data, 'email'), + array_get($command->data, 'password') ); event(new UserWillBeSaved($user, $command)); diff --git a/framework/core/src/Core/Handlers/Commands/StartDiscussionCommandHandler.php b/framework/core/src/Core/Handlers/Commands/StartDiscussionCommandHandler.php index 262e33137..b4ce16612 100644 --- a/framework/core/src/Core/Handlers/Commands/StartDiscussionCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/StartDiscussionCommandHandler.php @@ -41,7 +41,7 @@ class StartDiscussionCommandHandler // will trigger a domain event that is slightly semantically incorrect // in this situation (PostWasPosted), we may need to reconsider someday. $post = $this->bus->dispatch( - new PostReplyCommand($discussion->id, array_get($command->data, 'content'), $command->user) + new PostReplyCommand($discussion->id, $command->user, $command->data) ); return $post->discussion; diff --git a/framework/core/src/Core/Search/Users/Gambits/FulltextGambit.php b/framework/core/src/Core/Search/Users/Gambits/FulltextGambit.php index ea79d9149..db1043f36 100644 --- a/framework/core/src/Core/Search/Users/Gambits/FulltextGambit.php +++ b/framework/core/src/Core/Search/Users/Gambits/FulltextGambit.php @@ -19,6 +19,6 @@ class FulltextGambit extends GambitAbstract $searcher->query()->whereIn('id', $users); - $searcher->setDefaultSort($users); + $searcher->setDefaultSort(['id' => $users]); } } diff --git a/framework/core/src/Core/Search/Users/UserSearchCriteria.php b/framework/core/src/Core/Search/Users/UserSearchCriteria.php index 016b5b542..eb06f332f 100644 --- a/framework/core/src/Core/Search/Users/UserSearchCriteria.php +++ b/framework/core/src/Core/Search/Users/UserSearchCriteria.php @@ -8,13 +8,10 @@ class UserSearchCriteria public $sort; - public $order; - - public function __construct($user, $query, $sort, $order) + public function __construct($user, $query, $sort) { $this->user = $user; $this->query = $query; $this->sort = $sort; - $this->order = $order; } } diff --git a/framework/core/src/Core/Search/Users/UserSearcher.php b/framework/core/src/Core/Search/Users/UserSearcher.php index bce48e947..16ce9e0b0 100644 --- a/framework/core/src/Core/Search/Users/UserSearcher.php +++ b/framework/core/src/Core/Search/Users/UserSearcher.php @@ -4,23 +4,18 @@ use Flarum\Core\Models\User; use Flarum\Core\Search\SearcherInterface; use Flarum\Core\Search\GambitManager; use Flarum\Core\Repositories\UserRepositoryInterface; +use Flarum\Core\Events\UserSearchWillBePerformed; class UserSearcher implements SearcherInterface { - public $query; + protected $query; - protected $sortMap = [ - 'username' => ['username', 'asc'], - 'posts' => ['comments_count', 'desc'], - 'discussions' => ['discussions_count', 'desc'], - 'lastActive' => ['last_seen_time', 'desc'], - 'created' => ['join_time', 'asc'] - ]; - - protected $defaultSort = 'username'; + protected $gambits; protected $users; + protected $defaultSort = ['username' => 'asc']; + public function __construct(GambitManager $gambits, UserRepositoryInterface $users) { $this->gambits = $gambits; @@ -37,7 +32,7 @@ class UserSearcher implements SearcherInterface return $this->query->getQuery(); } - public function search(UserSearchCriteria $criteria, $count = null, $start = 0, $load = []) + public function search(UserSearchCriteria $criteria, $limit = null, $offset = 0, $load = []) { $this->user = $criteria->user; $this->query = $this->users->query()->whereCan($criteria->user, 'view'); @@ -46,24 +41,23 @@ class UserSearcher implements SearcherInterface $total = $this->query->count(); - $sort = $criteria->sort; - if (empty($sort)) { - $sort = $this->defaultSort; - } - if (is_array($sort)) { - foreach ($sort as $id) { - $this->query->orderByRaw('id != '.(int) $id); + $sort = $criteria->sort ?: $this->defaultSort; + + foreach ($sort as $field => $order) { + if (is_array($order)) { + foreach ($order as $value) { + $this->query->orderByRaw(snake_case($field).' != ?', [$value]); + } + } else { + $this->query->orderBy(snake_case($field), $order); } - } else { - list($column, $order) = $this->sortMap[$sort]; - $this->query->orderBy($column, $criteria->order ?: $order); } - if ($start > 0) { - $this->query->skip($start); + if ($offset > 0) { + $this->query->skip($offset); } - if ($count > 0) { - $this->query->take($count + 1); + if ($limit > 0) { + $this->query->take($limit + 1); } $users = $this->query->get(); diff --git a/framework/core/src/Forum/Actions/IndexAction.php b/framework/core/src/Forum/Actions/IndexAction.php index 537f0f727..a527a3cac 100644 --- a/framework/core/src/Forum/Actions/IndexAction.php +++ b/framework/core/src/Forum/Actions/IndexAction.php @@ -8,6 +8,7 @@ use Config; use View; use DB; use Flarum\Forum\Events\RenderView; +use Flarum\Api\Request as ApiRequest; class IndexAction extends BaseAction { @@ -24,12 +25,13 @@ class IndexAction extends BaseAction 'token' => Cookie::get('flarum_remember') ]; - $response = $this->callAction('Flarum\Api\Actions\Users\ShowAction', ['id' => $user->id]); - $response = $response->getData(); + $response = app('Flarum\Api\Actions\Users\ShowAction') + ->handle(new ApiRequest(['id' => $user->id], $this->actor)) + ->content->toArray(); - $data = [$response->data]; - if (isset($response->included)) { - $data = array_merge($data, $response->included); + $data = [$response['data']]; + if (isset($response['included'])) { + $data = array_merge($data, $response['included']); } }