refactor CommentService a bit, add BoardReferenceProvider and CommentReferenceProvider (no widgets but resolving)

Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
This commit is contained in:
Julien Veyssier
2023-02-01 13:11:55 +01:00
parent 264be93a74
commit 9658ccd843
4 changed files with 364 additions and 13 deletions

View File

@@ -76,6 +76,42 @@ class CommentService {
return new DataResponse($result);
}
/**
* @param int $cardId
* @param int $commentId
* @return IComment
* @throws NoPermissionException
* @throws NotFoundException
*/
private function get(int $cardId, int $commentId): IComment {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
try {
$comment = $this->commentsManager->get($commentId);
if ($comment->getObjectType() !== Application::COMMENT_ENTITY_TYPE || (int) $comment->getObjectId() !== $cardId) {
throw new CommentNotFoundException();
}
} catch (CommentNotFoundException $e) {
throw new NotFoundException('No comment found.');
}
if ($comment->getParentId() !== '0') {
$this->permissionService->checkPermission($this->cardMapper, $comment->getParentId(), Acl::PERMISSION_READ);
}
return $comment;
}
/**
* @param int $cardId
* @param int $commentId
* @return array
* @throws NoPermissionException
* @throws NotFoundException
*/
public function getFormatted(int $cardId, int $commentId): array {
$comment = $this->get($cardId, $commentId);
return $this->formatComment($comment);
}
/**
* @param string $cardId
* @param string $message
@@ -126,21 +162,10 @@ class CommentService {
if (!is_numeric($commentId)) {
throw new BadRequestException('A valid comment id must be provided');
}
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
try {
$comment = $this->commentsManager->get($commentId);
if ($comment->getObjectType() !== Application::COMMENT_ENTITY_TYPE || $comment->getObjectId() !== $cardId) {
throw new CommentNotFoundException();
}
} catch (CommentNotFoundException $e) {
throw new NotFoundException('No comment found.');
}
$comment = $this->get((int) $cardId, (int) $commentId);
if ($comment->getActorType() !== 'users' || $comment->getActorId() !== $this->userId) {
throw new NoPermissionException('Only authors are allowed to edit their comment.');
}
if ($comment->getParentId() !== '0') {
$this->permissionService->checkPermission($this->cardMapper, $comment->getParentId(), Acl::PERMISSION_READ);
}
$comment->setMessage($message);
$this->commentsManager->save($comment);