Moved comment removal to API

This commit is contained in:
Marcin Kurczewski 2014-05-02 09:40:23 +02:00
parent 16c5d6961b
commit 6a28be5e3e
3 changed files with 48 additions and 14 deletions

View file

@ -46,7 +46,7 @@ class CommentController
if (InputHelper::get('sender') == 'preview') if (InputHelper::get('sender') == 'preview')
return $this->previewAction(); return $this->previewAction();
$comment = Api::run( Api::run(
new AddCommentJob(), new AddCommentJob(),
[ [
JobArgs::POST_ID => InputHelper::get('post-id'), JobArgs::POST_ID => InputHelper::get('post-id'),
@ -64,7 +64,7 @@ class CommentController
if (InputHelper::get('sender') == 'preview') if (InputHelper::get('sender') == 'preview')
return $this->previewAction(); return $this->previewAction();
$comment = Api::run( Api::run(
new EditCommentJob(), new EditCommentJob(),
[ [
JobArgs::COMMENT_ID => $id, JobArgs::COMMENT_ID => $id,
@ -74,15 +74,10 @@ class CommentController
public function deleteAction($id) public function deleteAction($id)
{ {
$comment = CommentModel::findById($id); $comment = Api::run(
new DeleteCommentJob(),
Access::assert( [
Privilege::DeleteComment, JobArgs::COMMENT_ID => $id,
Access::getIdentity($comment->getCommenter())); ]);
CommentModel::remove($comment);
LogHelper::log('{user} removed comment from {post}', [
'post' => TextHelper::reprPost($comment->getPost())]);
} }
} }

View file

@ -0,0 +1,40 @@
<?php
class DeleteCommentJob extends AbstractJob
{
protected $comment;
public function prepare()
{
$this->comment = CommentModel::findById($this->getArgument(JobArgs::COMMENT_ID));
}
public function execute()
{
$post = $this->comment->getPost();
CommentModel::remove($this->comment);
LogHelper::log('{user} removed comment from {post}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($post)]);
}
public function requiresPrivilege()
{
return
[
Privilege::DeleteComment,
Access::getIdentity($this->comment->getCommenter())
];
}
public function requiresAuthentication()
{
return true;
}
public function requiresConfirmedEmail()
{
return getConfig()->registration->needEmailForCommenting;
}
}

View file

@ -10,7 +10,6 @@ class EditCommentJob extends AbstractJob
public function execute() public function execute()
{ {
$user = Auth::getCurrentUser();
$comment = $this->comment; $comment = $this->comment;
$comment->commentDate = time(); $comment->commentDate = time();
@ -18,7 +17,7 @@ class EditCommentJob extends AbstractJob
CommentModel::save($comment); CommentModel::save($comment);
LogHelper::log('{user} edited comment in {post}', [ LogHelper::log('{user} edited comment in {post}', [
'user' => TextHelper::reprUser($user), 'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($comment->getPost())]); 'post' => TextHelper::reprPost($comment->getPost())]);
return $comment; return $comment;