From 2eaab49d35cff054e6f13d05d8be2757cea78828 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 3 May 2014 19:39:27 +0200 Subject: [PATCH] Moved post (un)favoriting to API --- public_html/dispatch.php | 4 +-- src/Controllers/PostController.php | 25 +++++-------------- src/Jobs/TogglePostFavoriteJob.php | 40 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 src/Jobs/TogglePostFavoriteJob.php diff --git a/public_html/dispatch.php b/public_html/dispatch.php index fdae65c2..a8fb4753 100644 --- a/public_html/dispatch.php +++ b/public_html/dispatch.php @@ -91,6 +91,8 @@ $postValidation = \Chibi\Router::register(['PostController', 'flagAction'], 'POST', '/post/{id}/flag', $postValidation); \Chibi\Router::register(['PostController', 'hideAction'], 'POST', '/post/{id}/hide', $postValidation); \Chibi\Router::register(['PostController', 'unhideAction'], 'POST', '/post/{id}/unhide', $postValidation); +\Chibi\Router::register(['PostController', 'removeFavoriteAction'], 'POST', '/post/{id}/rem-fav', $postValidation); +\Chibi\Router::register(['PostController', 'addFavoriteAction'], 'POST', '/post/{id}/add-fav', $postValidation); \Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments'); \Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments/{page}', ['page' => '\d+']); @@ -104,8 +106,6 @@ foreach (['GET', 'POST'] as $method) \Chibi\Router::register(['PostController', 'viewAction'], $method, '/post/{id}', $postValidation); \Chibi\Router::register(['PostController', 'retrieveAction'], $method, '/post/{name}/retrieve', $postValidation); \Chibi\Router::register(['PostController', 'thumbAction'], $method, '/post/{name}/thumb', $postValidation); - \Chibi\Router::register(['PostController', 'removeFavoriteAction'], $method, '/post/{id}/rem-fav', $postValidation); - \Chibi\Router::register(['PostController', 'addFavoriteAction'], $method, '/post/{id}/add-fav', $postValidation); \Chibi\Router::register(['PostController', 'featureAction'], $method, '/post/{id}/feature', $postValidation); \Chibi\Router::register(['PostController', 'scoreAction'], $method, '/post/{id}/score/{score}', $postValidation); diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 0e45f9de..31103b2e 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -193,29 +193,16 @@ class PostController public function addFavoriteAction($id) { - $context = getContext(); - $post = PostModel::findByIdOrName($id); - Access::assert(Privilege::FavoritePost, Access::getIdentity($post->getUploader())); - Access::assertAuthentication(); - - if (!InputHelper::get('submit')) - return; - - UserModel::updateUserScore(Auth::getCurrentUser(), $post, 1); - UserModel::addToUserFavorites(Auth::getCurrentUser(), $post); + Api::run(new TogglePostFavoriteJob(), [ + TogglePostFavoriteJob::POST_ID => $id, + TogglePostFavoriteJob::STATE => true]); } public function removeFavoriteAction($id) { - $context = getContext(); - $post = PostModel::findByIdOrName($id); - Access::assert(Privilege::FavoritePost, Access::getIdentity($post->getUploader())); - Access::assertAuthentication(); - - if (!InputHelper::get('submit')) - return; - - UserModel::removeFromUserFavorites(Auth::getCurrentUser(), $post); + Api::run(new TogglePostFavoriteJob(), [ + TogglePostFavoriteJob::POST_ID => $id, + TogglePostFavoriteJob::STATE => false]); } public function scoreAction($id, $score) diff --git a/src/Jobs/TogglePostFavoriteJob.php b/src/Jobs/TogglePostFavoriteJob.php new file mode 100644 index 00000000..92b8505b --- /dev/null +++ b/src/Jobs/TogglePostFavoriteJob.php @@ -0,0 +1,40 @@ +post; + $favorite = boolval($this->getArgument(self::STATE)); + + if ($favorite) + { + UserModel::updateUserScore(Auth::getCurrentUser(), $post, 1); + UserModel::addToUserFavorites(Auth::getCurrentUser(), $post); + } + else + { + UserModel::removeFromUserFavorites(Auth::getCurrentUser(), $post); + } + + return $post; + } + + public function requiresPrivilege() + { + return + [ + Privilege::FavoritePost, + Access::getIdentity($this->post->getUploader()) + ]; + } + + public function requiresAuthentication() + { + return true; + } + + public function requiresConfirmedEmail() + { + return false; + } +}