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 $this->validate($request, [
26 'text' => ['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, $request->get('text'), $request->get('parent_id'));
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 $this->validate($request, [
61 'text' => ['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, $request->get('text'));
70 return view('comments.comment', ['comment' => $comment, 'readOnly' => false]);
74 * Delete a comment from the system.
76 public function destroy(int $id)
78 $comment = $this->commentRepo->getById($id);
79 $this->checkOwnablePermission('comment-delete', $comment);
81 $this->commentRepo->delete($comment);
83 return response()->json(['message' => trans('entities.comment_deleted')]);