Added comment API
This commit is contained in:
parent
e43f3b54d7
commit
16c5740277
11 changed files with 310 additions and 2 deletions
|
@ -56,6 +56,13 @@ changePostRelations = regularUser, powerUser, moderator, administrator
|
|||
|
||||
listTags = anonymous, regularUser, powerUser, moderator, administrator
|
||||
|
||||
listComments = anonymous, regularUser, powerUser, moderator, administrator
|
||||
addComments = regularUser, powerUser, moderator, administrator
|
||||
editOwnComments = regularUser, powerUser, moderator, administrator
|
||||
editAllComments = moderator, administrator
|
||||
deleteOwnComments = regularUser, powerUser, moderator, administrator
|
||||
deleteAllComments = moderator, administrator
|
||||
|
||||
viewHistory = anonymous, regularUser, powerUser, moderator, administrator
|
||||
|
||||
[users]
|
||||
|
|
|
@ -33,6 +33,13 @@ App.Auth = function(_, jQuery, util, api, appState, promise) {
|
|||
changePostThumbnail: 'changePostThumbnail',
|
||||
changePostRelations: 'changePostRelations',
|
||||
|
||||
listComments: 'listComments',
|
||||
addComments: 'addComments',
|
||||
editOwnComments: 'editOwnComments',
|
||||
editAllComments: 'editAllComments',
|
||||
deleteOwnComments: 'deleteOwnComments',
|
||||
deleteAllComments: 'deleteAllComments',
|
||||
|
||||
listTags: 'listTags',
|
||||
|
||||
viewHistory: 'viewHistory',
|
||||
|
|
128
src/Controllers/CommentController.php
Normal file
128
src/Controllers/CommentController.php
Normal file
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
namespace Szurubooru\Controllers;
|
||||
|
||||
class CommentController extends AbstractController
|
||||
{
|
||||
private $privilegeService;
|
||||
private $authService;
|
||||
private $postService;
|
||||
private $commentService;
|
||||
private $commentViewProxy;
|
||||
private $postViewProxy;
|
||||
private $inputReader;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Services\PrivilegeService $privilegeService,
|
||||
\Szurubooru\Services\AuthService $authService,
|
||||
\Szurubooru\Services\PostService $postService,
|
||||
\Szurubooru\Services\CommentService $commentService,
|
||||
\Szurubooru\Controllers\ViewProxies\CommentViewProxy $commentViewProxy,
|
||||
\Szurubooru\Controllers\ViewProxies\PostViewProxy $postViewProxy,
|
||||
\Szurubooru\Helpers\InputReader $inputReader)
|
||||
{
|
||||
$this->privilegeService = $privilegeService;
|
||||
$this->authService = $authService;
|
||||
$this->postService = $postService;
|
||||
$this->commentService = $commentService;
|
||||
$this->commentViewProxy = $commentViewProxy;
|
||||
$this->postViewProxy = $postViewProxy;
|
||||
$this->inputReader = $inputReader;
|
||||
}
|
||||
|
||||
public function registerRoutes(\Szurubooru\Router $router)
|
||||
{
|
||||
$router->get('/api/comments', [$this, 'getComments']);
|
||||
$router->get('/api/comments/:postNameOrId', [$this, 'getPostComments']);
|
||||
$router->post('/api/comments/:postNameOrId', [$this, 'addComment']);
|
||||
$router->put('/api/comments/:commentId', [$this, 'editComment']);
|
||||
$router->delete('/api/comments/:commentId', [$this, 'deleteComment']);
|
||||
}
|
||||
|
||||
public function getComments()
|
||||
{
|
||||
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::LIST_COMMENTS);
|
||||
|
||||
$filter = new \Szurubooru\SearchServices\Filters\PostFilter();
|
||||
$filter->setPageSize(10);
|
||||
$filter->setPageNumber($this->inputReader->page);
|
||||
$filter->setOrder([
|
||||
\Szurubooru\SearchServices\Filters\PostFilter::ORDER_LAST_COMMENT_TIME =>
|
||||
\Szurubooru\SearchServices\Filters\PostFilter::ORDER_DESC]);
|
||||
|
||||
$requirement = new \Szurubooru\SearchServices\Requirements\Requirement();
|
||||
$requirement->setValue(new \Szurubooru\SearchServices\Requirements\RequirementRangedValue());
|
||||
$requirement->getValue()->setMinValue(1);
|
||||
$requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_COMMENT_COUNT);
|
||||
$filter->addRequirement($requirement);
|
||||
|
||||
$result = $this->postService->getFiltered($filter);
|
||||
$posts = $result->getEntities();
|
||||
|
||||
$data = [];
|
||||
foreach ($posts as $post)
|
||||
{
|
||||
$data[] = [
|
||||
'post' => $this->postViewProxy->fromEntity($post),
|
||||
'comments' => $this->commentViewProxy->fromArray($this->commentService->getByPost($post))];
|
||||
}
|
||||
|
||||
return [
|
||||
'data' => $data,
|
||||
'pageSize' => $result->getPageSize(),
|
||||
'totalRecords' => $result->getTotalRecords()];
|
||||
}
|
||||
|
||||
public function getPostComments($postNameOrId)
|
||||
{
|
||||
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::LIST_COMMENTS);
|
||||
$post = $this->postService->getByNameOrId($postNameOrId);
|
||||
|
||||
$filter = new \Szurubooru\SearchServices\Filters\CommentFilter();
|
||||
$filter->setOrder([
|
||||
\Szurubooru\SearchServices\Filters\CommentFilter::ORDER_ID =>
|
||||
\Szurubooru\SearchServices\Filters\CommentFilter::ORDER_ASC]);
|
||||
|
||||
$requirement = new \Szurubooru\SearchServices\Requirements\Requirement();
|
||||
$requirement->setValue(new \Szurubooru\SearchServices\Requirements\RequirementSingleValue($post->getId()));
|
||||
$requirement->setType(\Szurubooru\SearchServices\Filters\CommentFilter::REQUIREMENT_POST_ID);
|
||||
$filter->addRequirement($requirement);
|
||||
|
||||
$result = $this->commentService->getFiltered($filter);
|
||||
$entities = $this->commentViewProxy->fromArray($result->getEntities());
|
||||
return ['data' => $entities];
|
||||
}
|
||||
|
||||
public function addComment($postNameOrId)
|
||||
{
|
||||
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::ADD_COMMENTS);
|
||||
|
||||
$post = $this->postService->getByNameOrId($postNameOrId);
|
||||
$comment = $this->commentService->createComment($post, $this->inputReader->text);
|
||||
return $this->commentViewProxy->fromEntity($comment);
|
||||
}
|
||||
|
||||
public function editComment($commentId)
|
||||
{
|
||||
$comment = $this->commentService->getById($commentId);
|
||||
|
||||
$this->privilegeService->assertPrivilege(
|
||||
($comment->getUser() and $this->privilegeService->isLoggedIn($comment->getUser()))
|
||||
? \Szurubooru\Privilege::EDIT_OWN_COMMENTS
|
||||
: \Szurubooru\Privilege::EDIT_ALL_COMMENTS);
|
||||
|
||||
$comment = $this->commentService->updateComment($comment, $this->inputReader->text);
|
||||
return $this->commentViewProxy->fromEntity($comment);
|
||||
}
|
||||
|
||||
public function deleteComment($commentId)
|
||||
{
|
||||
$comment = $this->commentService->getById($commentId);
|
||||
|
||||
$this->privilegeService->assertPrivilege(
|
||||
$this->privilegeService->isLoggedIn($comment->getUser())
|
||||
? \Szurubooru\Privilege::DELETE_OWN_COMMENTS
|
||||
: \Szurubooru\Privilege::DELETE_ALL_COMMENTS);
|
||||
|
||||
return $this->commentService->deleteComment($comment);
|
||||
}
|
||||
}
|
32
src/Controllers/ViewProxies/CommentViewProxy.php
Normal file
32
src/Controllers/ViewProxies/CommentViewProxy.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
namespace Szurubooru\Controllers\ViewProxies;
|
||||
|
||||
class CommentViewProxy extends AbstractViewProxy
|
||||
{
|
||||
private $postViewProxy;
|
||||
private $userViewProxy;
|
||||
|
||||
public function __construct(
|
||||
PostViewProxy $postViewProxy,
|
||||
UserViewProxy $userViewProxy)
|
||||
{
|
||||
$this->postViewProxy = $postViewProxy;
|
||||
$this->userViewProxy = $userViewProxy;
|
||||
}
|
||||
|
||||
public function fromEntity($comment, $config = [])
|
||||
{
|
||||
$result = new \StdClass;
|
||||
if ($comment)
|
||||
{
|
||||
$result->id = $comment->getId();
|
||||
$result->creationTime = $comment->getCreationTime();
|
||||
$result->lastEditTime = $comment->getLastEditTime();
|
||||
$result->text = $comment->getText();
|
||||
$result->postId = $comment->getPostId();
|
||||
$result->user = $this->userViewProxy->fromEntity($comment->getUser());
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -67,10 +67,10 @@ class Comment extends Entity
|
|||
return $this->lazyLoad(self::LAZY_LOADER_USER, null);
|
||||
}
|
||||
|
||||
public function setUser(\Szurubooru\Entities\User $user)
|
||||
public function setUser(\Szurubooru\Entities\User $user = null)
|
||||
{
|
||||
$this->lazySave(self::LAZY_LOADER_USER, $user);
|
||||
$this->userId = $user->getId();
|
||||
$this->userId = $user ? $user->getId() : null;
|
||||
}
|
||||
|
||||
public function getPost()
|
||||
|
|
|
@ -35,5 +35,12 @@ class Privilege
|
|||
|
||||
const LIST_TAGS = 'listTags';
|
||||
|
||||
const LIST_COMMENTS = 'listComments';
|
||||
const ADD_COMMENTS = 'addComments';
|
||||
const EDIT_OWN_COMMENTS = 'editOwnComments';
|
||||
const EDIT_ALL_COMMENTS = 'editAllComments';
|
||||
const DELETE_OWN_COMMENTS = 'deleteOwnComments';
|
||||
const DELETE_ALL_COMMENTS = 'deleteAllComments';
|
||||
|
||||
const VIEW_HISTORY = 'viewHistory';
|
||||
}
|
||||
|
|
14
src/SearchServices/Filters/CommentFilter.php
Normal file
14
src/SearchServices/Filters/CommentFilter.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
namespace Szurubooru\SearchServices\Filters;
|
||||
|
||||
class CommentFilter extends BasicFilter implements IFilter
|
||||
{
|
||||
const ORDER_ID = 'id';
|
||||
|
||||
const REQUIREMENT_POST_ID = 'postId';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setOrder([self::ORDER_ID => self::ORDER_DESC]);
|
||||
}
|
||||
}
|
|
@ -20,6 +20,7 @@ class PostFilter extends BasicFilter implements IFilter
|
|||
const REQUIREMENT_HASH = 'name';
|
||||
const REQUIREMENT_TAG_COUNT = 'tagCount';
|
||||
const REQUIREMENT_FAV_COUNT = 'favCount';
|
||||
const REQUIREMENT_COMMENT_COUNT = 'commentCount';
|
||||
const REQUIREMENT_SCORE = 'score';
|
||||
const REQUIREMENT_UPLOADER = 'uploader.name';
|
||||
const REQUIREMENT_SAFETY = 'safety';
|
||||
|
|
|
@ -41,6 +41,9 @@ class PostSearchParser extends AbstractSearchParser
|
|||
elseif ($token->getKey() === 'fav_count')
|
||||
$this->addFavCountRequirement($filter, $token);
|
||||
|
||||
elseif ($token->getKey() === 'comment_count')
|
||||
$this->addCommentCountRequirement($filter, $token);
|
||||
|
||||
elseif ($token->getKey() === 'score')
|
||||
$this->addScoreRequirement($filter, $token);
|
||||
|
||||
|
@ -180,6 +183,15 @@ class PostSearchParser extends AbstractSearchParser
|
|||
self::ALLOW_COMPOSITE | self::ALLOW_RANGES);
|
||||
}
|
||||
|
||||
private function addCommentCountRequirement($filter, $token)
|
||||
{
|
||||
$this->addRequirementFromToken(
|
||||
$filter,
|
||||
$token,
|
||||
\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_COMMENT_COUNT,
|
||||
self::ALLOW_COMPOSITE | self::ALLOW_RANGES);
|
||||
}
|
||||
|
||||
private function addScoreRequirement($filter, $token)
|
||||
{
|
||||
$this->addRequirementFromToken(
|
||||
|
|
99
src/Services/CommentService.php
Normal file
99
src/Services/CommentService.php
Normal file
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
namespace Szurubooru\Services;
|
||||
|
||||
class CommentService
|
||||
{
|
||||
private $validator;
|
||||
private $commentDao;
|
||||
private $transactionManager;
|
||||
private $authService;
|
||||
private $timeService;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Validator $validator,
|
||||
\Szurubooru\Dao\CommentDao $commentDao,
|
||||
\Szurubooru\Dao\TransactionManager $transactionManager,
|
||||
\Szurubooru\Services\AuthService $authService,
|
||||
\Szurubooru\Services\TimeService $timeService)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
$this->commentDao = $commentDao;
|
||||
$this->transactionManager = $transactionManager;
|
||||
$this->authService = $authService;
|
||||
$this->timeService = $timeService;
|
||||
}
|
||||
|
||||
public function getById($commentId)
|
||||
{
|
||||
$transactionFunc = function() use ($commentId)
|
||||
{
|
||||
$comment = $this->commentDao->findById($commentId);
|
||||
if (!$comment)
|
||||
throw new \InvalidArgumentException('Comment with ID "' . $commentId . '" was not found.');
|
||||
return $comment;
|
||||
};
|
||||
return $this->transactionManager->rollback($transactionFunc);
|
||||
}
|
||||
|
||||
public function getByPost(\Szurubooru\Entities\Post $post)
|
||||
{
|
||||
$transactionFunc = function() use ($post)
|
||||
{
|
||||
return $this->commentDao->findByPost($post);
|
||||
};
|
||||
return $this->transactionManager->rollback($transactionFunc);
|
||||
}
|
||||
|
||||
public function getFiltered(\Szurubooru\SearchServices\Filters\CommentFilter $filter)
|
||||
{
|
||||
$transactionFunc = function() use ($filter)
|
||||
{
|
||||
return $this->commentDao->findFiltered($filter);
|
||||
};
|
||||
return $this->transactionManager->rollback($transactionFunc);
|
||||
}
|
||||
|
||||
public function createComment(\Szurubooru\Entities\Post $post, $text)
|
||||
{
|
||||
$transactionFunc = function() use ($post, $text)
|
||||
{
|
||||
$comment = new \Szurubooru\Entities\Comment();
|
||||
$comment->setCreationTime($this->timeService->getCurrentTime());
|
||||
$comment->setLastEditTime($this->timeService->getCurrentTime());
|
||||
$comment->setUser($this->authService->isLoggedIn() ? $this->authService->getLoggedInUser() : null);
|
||||
$comment->setPost($post);
|
||||
|
||||
$this->updateCommentText($comment, $text);
|
||||
|
||||
return $this->commentDao->save($comment);
|
||||
};
|
||||
return $this->transactionManager->commit($transactionFunc);
|
||||
}
|
||||
|
||||
public function updateComment(\Szurubooru\Entities\Comment $comment, $newText)
|
||||
{
|
||||
$transactionFunc = function() use ($comment, $newText)
|
||||
{
|
||||
$comment->setLastEditTime($this->timeService->getCurrentTime());
|
||||
|
||||
$this->updateCommentText($comment, $newText);
|
||||
return $this->commentDao->save($comment);
|
||||
};
|
||||
return $this->transactionManager->commit($transactionFunc);
|
||||
}
|
||||
|
||||
public function deleteComment(\Szurubooru\Entities\Comment $comment)
|
||||
{
|
||||
$transactionFunc = function() use ($comment)
|
||||
{
|
||||
$this->commentDao->deleteById($comment->getId());
|
||||
};
|
||||
$this->transactionManager->commit($transactionFunc);
|
||||
}
|
||||
|
||||
private function updateCommentText(\Szurubooru\Entities\Comment $comment, $text)
|
||||
{
|
||||
$this->validator->validateLength($text, 5, 2000, 'Comment text');
|
||||
$comment->setText($text);
|
||||
}
|
||||
}
|
|
@ -44,6 +44,7 @@ return [
|
|||
$container->get(\Szurubooru\Controllers\HistoryController::class),
|
||||
$container->get(\Szurubooru\Controllers\FavoritesController::class),
|
||||
$container->get(\Szurubooru\Controllers\PostScoreController::class),
|
||||
$container->get(\Szurubooru\Controllers\CommentController::class),
|
||||
];
|
||||
}),
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue