3 namespace BookStack\Activity\Controllers;
5 use BookStack\Activity\CommentRepo;
6 use BookStack\Activity\Tools\CommentTree;
7 use BookStack\Activity\Tools\CommentTreeNode;
8 use BookStack\Entities\Queries\PageQueries;
9 use BookStack\Http\Controller;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
13 class CommentController extends Controller
15 public function __construct(
16 protected CommentRepo $commentRepo,
17 protected PageQueries $pageQueries,
22 * Save a new comment for a Page.
24 * @throws ValidationException
26 public function savePageComment(Request $request, int $pageId)
28 $input = $this->validate($request, [
29 'html' => ['required', 'string'],
30 'parent_id' => ['nullable', 'integer'],
31 'content_ref' => ['string'],
34 $page = $this->pageQueries->findVisibleById($pageId);
36 return response('Not found', 404);
39 // Prevent adding comments to draft pages
41 return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
44 // Create a new comment.
45 $this->checkPermission('comment-create-all');
46 $contentRef = $input['content_ref'] ?? '';
47 $comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null, $contentRef);
49 return view('comments.comment-branch', [
51 'branch' => new CommentTreeNode($comment, 0, []),
56 * Update an existing comment.
58 * @throws ValidationException
60 public function update(Request $request, int $commentId)
62 $input = $this->validate($request, [
63 'html' => ['required', 'string'],
66 $comment = $this->commentRepo->getById($commentId);
67 $this->checkOwnablePermission('page-view', $comment->entity);
68 $this->checkOwnablePermission('comment-update', $comment);
70 $comment = $this->commentRepo->update($comment, $input['html']);
72 return view('comments.comment', [
73 'comment' => $comment,
79 * Mark a comment as archived.
81 public function archive(int $id)
83 $comment = $this->commentRepo->getById($id);
84 $this->checkOwnablePermission('page-view', $comment->entity);
85 if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
86 $this->showPermissionError();
89 $this->commentRepo->archive($comment);
91 $tree = new CommentTree($comment->entity);
92 return view('comments.comment-branch', [
94 'branch' => $tree->getCommentNodeForId($id),
99 * Unmark a comment as archived.
101 public function unarchive(int $id)
103 $comment = $this->commentRepo->getById($id);
104 $this->checkOwnablePermission('page-view', $comment->entity);
105 if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
106 $this->showPermissionError();
109 $this->commentRepo->unarchive($comment);
111 $tree = new CommentTree($comment->entity);
112 return view('comments.comment-branch', [
114 'branch' => $tree->getCommentNodeForId($id),
119 * Delete a comment from the system.
121 public function destroy(int $id)
123 $comment = $this->commentRepo->getById($id);
124 $this->checkOwnablePermission('comment-delete', $comment);
126 $this->commentRepo->delete($comment);
128 return response()->json(['message' => trans('entities.comment_deleted')]);