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 $comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null, $input['content_ref']);
48 return view('comments.comment-branch', [
50 'branch' => new CommentTreeNode($comment, 0, []),
55 * Update an existing comment.
57 * @throws ValidationException
59 public function update(Request $request, int $commentId)
61 $input = $this->validate($request, [
62 'html' => ['required', 'string'],
65 $comment = $this->commentRepo->getById($commentId);
66 $this->checkOwnablePermission('page-view', $comment->entity);
67 $this->checkOwnablePermission('comment-update', $comment);
69 $comment = $this->commentRepo->update($comment, $input['html']);
71 return view('comments.comment', [
72 'comment' => $comment,
78 * Mark a comment as archived.
80 public function archive(int $id)
82 $comment = $this->commentRepo->getById($id);
83 $this->checkOwnablePermission('page-view', $comment->entity);
84 if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
85 $this->showPermissionError();
88 $this->commentRepo->archive($comment);
90 $tree = new CommentTree($comment->entity);
91 return view('comments.comment-branch', [
93 'branch' => $tree->getCommentNodeForId($id),
98 * Unmark a comment as archived.
100 public function unarchive(int $id)
102 $comment = $this->commentRepo->getById($id);
103 $this->checkOwnablePermission('page-view', $comment->entity);
104 if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
105 $this->showPermissionError();
108 $this->commentRepo->unarchive($comment);
110 $tree = new CommentTree($comment->entity);
111 return view('comments.comment-branch', [
113 'branch' => $tree->getCommentNodeForId($id),
118 * Delete a comment from the system.
120 public function destroy(int $id)
122 $comment = $this->commentRepo->getById($id);
123 $this->checkOwnablePermission('comment-delete', $comment);
125 $this->commentRepo->delete($comment);
127 return response()->json(['message' => trans('entities.comment_deleted')]);