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'],
29 'content_ref' => ['string'],
32 $page = $this->pageQueries->findVisibleById($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, $input['html'], $input['parent_id'] ?? null, $input['content_ref']);
46 return view('comments.comment-branch', [
49 'comment' => $comment,
56 * Update an existing comment.
58 * @throws ValidationException
60 public function update(Request $request, int $commentId)
62 $input = $this->validate($request, [
63 'html' => ['required', 'string'],
66 $comment = $this->commentRepo->getById($commentId);
67 $this->checkOwnablePermission('page-view', $comment->entity);
68 $this->checkOwnablePermission('comment-update', $comment);
70 $comment = $this->commentRepo->update($comment, $input['html']);
72 return view('comments.comment', [
73 'comment' => $comment,
79 * Mark a comment as archived.
81 public function archive(int $id)
83 $comment = $this->commentRepo->getById($id);
84 if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
85 $this->showPermissionError();
88 $this->commentRepo->archive($comment);
90 return view('comments.comment', [
91 'comment' => $comment,
97 * Unmark a comment as archived.
99 public function unarchive(int $id)
101 $comment = $this->commentRepo->getById($id);
102 if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
103 $this->showPermissionError();
106 $this->commentRepo->unarchive($comment);
108 return view('comments.comment', [
109 'comment' => $comment,
115 * Delete a comment from the system.
117 public function destroy(int $id)
119 $comment = $this->commentRepo->getById($id);
120 $this->checkOwnablePermission('comment-delete', $comment);
122 $this->commentRepo->delete($comment);
124 return response()->json(['message' => trans('entities.comment_deleted')]);