Moved post (un)favoriting to API

This commit is contained in:
Marcin Kurczewski 2014-05-03 19:39:27 +02:00
parent db8eab1c5c
commit 2eaab49d35
3 changed files with 48 additions and 21 deletions

View file

@ -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);

View file

@ -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)

View file

@ -0,0 +1,40 @@
<?php
class TogglePostFavoriteJob extends AbstractPostEditJob
{
public function execute()
{
$post = $this->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;
}
}