Moved tag toggling to API
This commit is contained in:
parent
7c1b8ca4d5
commit
162b131435
6 changed files with 93 additions and 42 deletions
|
@ -81,6 +81,7 @@ $postValidation =
|
|||
\Chibi\Router::register(['PostController', 'favoritesView'], 'GET', '/favorites/{page}', $postValidation);
|
||||
\Chibi\Router::register(['PostController', 'upvotedView'], 'GET', '/upvoted', $postValidation);
|
||||
\Chibi\Router::register(['PostController', 'upvotedView'], 'GET', '/upvoted/{page}', $postValidation);
|
||||
\Chibi\Router::register(['PostController', 'toggleTagAction'], 'POST', '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation);
|
||||
|
||||
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments');
|
||||
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments/{page}', ['page' => '\d+']);
|
||||
|
@ -92,7 +93,6 @@ $postValidation =
|
|||
foreach (['GET', 'POST'] as $method)
|
||||
{
|
||||
\Chibi\Router::register(['PostController', 'uploadAction'], $method, '/posts/upload', $postValidation);
|
||||
\Chibi\Router::register(['PostController', 'toggleTagAction'], $method, '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation);
|
||||
\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);
|
||||
|
|
|
@ -14,7 +14,7 @@ $(function()
|
|||
var enable = !aDom.parents('.post').hasClass('tagged');
|
||||
var url = $(this).attr('href') + '?json';
|
||||
url = url.replace('_enable_', enable ? '1' : '0');
|
||||
$.get(url, {submit: 1}).success(function(data)
|
||||
$.post(url).success(function(data)
|
||||
{
|
||||
aDom.removeClass('inactive');
|
||||
aDom.parents('.post').removeClass('tagged');
|
||||
|
|
|
@ -71,49 +71,17 @@ class PostController
|
|||
|
||||
public function toggleTagAction($id, $tag, $enable)
|
||||
{
|
||||
$context = getContext();
|
||||
$tagName = $tag;
|
||||
$post = PostModel::findByIdOrName($id);
|
||||
$context->transport->post = $post;
|
||||
|
||||
if (!InputHelper::get('submit'))
|
||||
return;
|
||||
|
||||
Access::assert(
|
||||
Privilege::MassTag,
|
||||
Access::getIdentity($post->getUploader()));
|
||||
Access::getIdentity(PostModel::findById($id)->getUploader()));
|
||||
|
||||
$tags = $post->getTags();
|
||||
|
||||
if (!$enable)
|
||||
{
|
||||
foreach ($tags as $i => $tag)
|
||||
if ($tag->name == $tagName)
|
||||
unset($tags[$i]);
|
||||
|
||||
LogHelper::log('{user} untagged {post} with {tag}', [
|
||||
'post' => TextHelper::reprPost($post),
|
||||
'tag' => TextHelper::reprTag($tag)]);
|
||||
}
|
||||
elseif ($enable)
|
||||
{
|
||||
$tag = TagModel::findByName($tagName, false);
|
||||
if ($tag === null)
|
||||
{
|
||||
$tag = TagModel::spawn();
|
||||
$tag->name = $tagName;
|
||||
TagModel::save($tag);
|
||||
}
|
||||
|
||||
$tags []= $tag;
|
||||
LogHelper::log('{user} tagged {post} with {tag}', [
|
||||
'post' => TextHelper::reprPost($post),
|
||||
'tag' => TextHelper::reprTag($tag)]);
|
||||
}
|
||||
|
||||
$post->setTags($tags);
|
||||
|
||||
PostModel::save($post);
|
||||
Api::run(
|
||||
new TogglePostTagJob(),
|
||||
[
|
||||
JobArgs::POST_ID => $id,
|
||||
JobArgs::TAG_NAME => $tag,
|
||||
JobArgs::STATE => $enable,
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadAction()
|
||||
|
|
11
src/Jobs/Abstraction/AbstractPostEditJob.php
Normal file
11
src/Jobs/Abstraction/AbstractPostEditJob.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
abstract class AbstractPostEditJob extends AbstractJob
|
||||
{
|
||||
protected $post;
|
||||
|
||||
public function prepare()
|
||||
{
|
||||
$postId = $this->getArgument(JobArgs::POST_ID);
|
||||
$this->post = PostModel::findByIdOrName($postId);
|
||||
}
|
||||
}
|
|
@ -3,8 +3,10 @@ class JobArgs
|
|||
{
|
||||
const COMMENT_ID = 'comment-id';
|
||||
const POST_ID = 'post-id';
|
||||
const TAG_NAME = 'tag-name';
|
||||
const TEXT = 'text';
|
||||
const PAGE_NUMBER = 'page-number';
|
||||
const QUERY = 'query';
|
||||
const LOG_ID = 'log-id';
|
||||
const STATE = 'state';
|
||||
}
|
||||
|
|
70
src/Jobs/TogglePostTagJob.php
Normal file
70
src/Jobs/TogglePostTagJob.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
class TogglePostTagJob extends AbstractPostEditJob
|
||||
{
|
||||
public function execute()
|
||||
{
|
||||
$tagName = $this->getArgument(JobArgs::TAG_NAME);
|
||||
$enable = boolval($this->getArgument(JobArgs::STATE));
|
||||
$post = $this->post;
|
||||
|
||||
$tags = $post->getTags();
|
||||
|
||||
if ($enable)
|
||||
{
|
||||
$tag = TagModel::findByName($tagName, false);
|
||||
if ($tag === null)
|
||||
{
|
||||
$tag = TagModel::spawn();
|
||||
$tag->name = $tagName;
|
||||
TagModel::save($tag);
|
||||
}
|
||||
|
||||
$tags []= $tag;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($tags as $i => $tag)
|
||||
if ($tag->name == $tagName)
|
||||
unset($tags[$i]);
|
||||
}
|
||||
|
||||
$post->setTags($tags);
|
||||
PostModel::save($post);
|
||||
|
||||
if ($enable)
|
||||
{
|
||||
LogHelper::log('{user} tagged {post} with {tag}', [
|
||||
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
|
||||
'post' => TextHelper::reprPost($post),
|
||||
'tag' => TextHelper::reprTag($tag)]);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogHelper::log('{user} untagged {post} with {tag}', [
|
||||
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
|
||||
'post' => TextHelper::reprPost($post),
|
||||
'tag' => TextHelper::reprTag($tag)]);
|
||||
}
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
public function requiresPrivilege()
|
||||
{
|
||||
return
|
||||
[
|
||||
Privilege::EditPostTags,
|
||||
Access::getIdentity($this->post->getUploader())
|
||||
];
|
||||
}
|
||||
|
||||
public function requiresAuthentication()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function requiresConfirmedEmail()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue