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 protected $commentRepo;
15 public function __construct(CommentRepo $commentRepo)
17 $this->commentRepo = $commentRepo;
21 * Save a new comment for a Page.
23 * @throws ValidationException
25 public function savePageComment(Request $request, int $pageId)
27 $this->validate($request, [
28 'text' => ['required', 'string'],
29 'parent_id' => ['nullable', 'integer'],
32 $page = Page::visible()->find($pageId);
34 return response('Not found', 404);
37 // Prevent adding comments to draft pages
39 return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
42 // Create a new comment.
43 $this->checkPermission('comment-create-all');
44 $comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id'));
46 return view('comments.comment', ['comment' => $comment]);
50 * Update an existing comment.
52 * @throws ValidationException
54 public function update(Request $request, int $commentId)
56 $this->validate($request, [
57 'text' => ['required', 'string'],
60 $comment = $this->commentRepo->getById($commentId);
61 $this->checkOwnablePermission('page-view', $comment->entity);
62 $this->checkOwnablePermission('comment-update', $comment);
64 $comment = $this->commentRepo->update($comment, $request->get('text'));
66 return view('comments.comment', ['comment' => $comment]);
70 * Delete a comment from the system.
72 public function destroy(int $id)
74 $comment = $this->commentRepo->getById($id);
75 $this->checkOwnablePermission('comment-delete', $comment);
77 $this->commentRepo->delete($comment);
79 return response()->json(['message' => trans('entities.comment_deleted')]);