3 namespace BookStack\Activity\Controllers;
5 use BookStack\Activity\CommentRepo;
6 use BookStack\Entities\Queries\PageQueries;
7 use BookStack\Http\Controller;
8 use Illuminate\Http\Request;
9 use Illuminate\Validation\ValidationException;
11 class CommentController extends Controller
13 public function __construct(
14 protected CommentRepo $commentRepo,
15 protected PageQueries $pageQueries,
20 * Save a new comment for a Page.
22 * @throws ValidationException
24 public function savePageComment(Request $request, int $pageId)
26 $input = $this->validate($request, [
27 'html' => ['required', 'string'],
28 'parent_id' => ['nullable', 'integer'],
31 $page = $this->pageQueries->findVisibleById($pageId);
33 return response('Not found', 404);
36 // Prevent adding comments to draft pages
38 return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
41 // Create a new comment.
42 $this->checkPermission('comment-create-all');
43 $comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null);
45 return view('comments.comment-branch', [
48 'comment' => $comment,
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 * Delete a comment from the system.
80 public function destroy(int $id)
82 $comment = $this->commentRepo->getById($id);
83 $this->checkOwnablePermission('comment-delete', $comment);
85 $this->commentRepo->delete($comment);
87 return response()->json(['message' => trans('entities.comment_deleted')]);