Moved post (un)favoriting to API
This commit is contained in:
parent
db8eab1c5c
commit
2eaab49d35
3 changed files with 48 additions and 21 deletions
|
@ -91,6 +91,8 @@ $postValidation =
|
||||||
\Chibi\Router::register(['PostController', 'flagAction'], 'POST', '/post/{id}/flag', $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', 'hideAction'], 'POST', '/post/{id}/hide', $postValidation);
|
||||||
\Chibi\Router::register(['PostController', 'unhideAction'], 'POST', '/post/{id}/unhide', $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');
|
||||||
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments/{page}', ['page' => '\d+']);
|
\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', 'viewAction'], $method, '/post/{id}', $postValidation);
|
||||||
\Chibi\Router::register(['PostController', 'retrieveAction'], $method, '/post/{name}/retrieve', $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', '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', 'featureAction'], $method, '/post/{id}/feature', $postValidation);
|
||||||
\Chibi\Router::register(['PostController', 'scoreAction'], $method, '/post/{id}/score/{score}', $postValidation);
|
\Chibi\Router::register(['PostController', 'scoreAction'], $method, '/post/{id}/score/{score}', $postValidation);
|
||||||
|
|
||||||
|
|
|
@ -193,29 +193,16 @@ class PostController
|
||||||
|
|
||||||
public function addFavoriteAction($id)
|
public function addFavoriteAction($id)
|
||||||
{
|
{
|
||||||
$context = getContext();
|
Api::run(new TogglePostFavoriteJob(), [
|
||||||
$post = PostModel::findByIdOrName($id);
|
TogglePostFavoriteJob::POST_ID => $id,
|
||||||
Access::assert(Privilege::FavoritePost, Access::getIdentity($post->getUploader()));
|
TogglePostFavoriteJob::STATE => true]);
|
||||||
Access::assertAuthentication();
|
|
||||||
|
|
||||||
if (!InputHelper::get('submit'))
|
|
||||||
return;
|
|
||||||
|
|
||||||
UserModel::updateUserScore(Auth::getCurrentUser(), $post, 1);
|
|
||||||
UserModel::addToUserFavorites(Auth::getCurrentUser(), $post);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeFavoriteAction($id)
|
public function removeFavoriteAction($id)
|
||||||
{
|
{
|
||||||
$context = getContext();
|
Api::run(new TogglePostFavoriteJob(), [
|
||||||
$post = PostModel::findByIdOrName($id);
|
TogglePostFavoriteJob::POST_ID => $id,
|
||||||
Access::assert(Privilege::FavoritePost, Access::getIdentity($post->getUploader()));
|
TogglePostFavoriteJob::STATE => false]);
|
||||||
Access::assertAuthentication();
|
|
||||||
|
|
||||||
if (!InputHelper::get('submit'))
|
|
||||||
return;
|
|
||||||
|
|
||||||
UserModel::removeFromUserFavorites(Auth::getCurrentUser(), $post);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scoreAction($id, $score)
|
public function scoreAction($id, $score)
|
||||||
|
|
40
src/Jobs/TogglePostFavoriteJob.php
Normal file
40
src/Jobs/TogglePostFavoriteJob.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue