3 namespace BookStack\Activity\Controllers;
5 use BookStack\Activity\CommentRepo;
6 use BookStack\Entities\Models\Page;
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
19 * Save a new comment for a Page.
21 * @throws ValidationException
23 public function savePageComment(Request $request, int $pageId)
25 $input = $this->validate($request, [
26 'html' => ['required', 'string'],
27 'parent_id' => ['nullable', 'integer'],
30 $page = Page::visible()->find($pageId);
32 return response('Not found', 404);
35 // Prevent adding comments to draft pages
37 return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
40 // Create a new comment.
41 $this->checkPermission('comment-create-all');
42 $comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null);
44 return view('comments.comment-branch', [
47 'comment' => $comment,
54 * Update an existing comment.
56 * @throws ValidationException
58 public function update(Request $request, int $commentId)
60 $input = $this->validate($request, [
61 'html' => ['required', 'string'],
64 $comment = $this->commentRepo->getById($commentId);
65 $this->checkOwnablePermission('page-view', $comment->entity);
66 $this->checkOwnablePermission('comment-update', $comment);
68 $comment = $this->commentRepo->update($comment, $input['html']);
70 return view('comments.comment', [
71 'comment' => $comment,
77 * Delete a comment from the system.
79 public function destroy(int $id)
81 $comment = $this->commentRepo->getById($id);
82 $this->checkOwnablePermission('comment-delete', $comment);
84 $this->commentRepo->delete($comment);
86 return response()->json(['message' => trans('entities.comment_deleted')]);