3 namespace BookStack\Activity\Controllers;
5 use BookStack\Activity\CommentRepo;
6 use BookStack\Activity\Tools\CommentTree;
7 use BookStack\Activity\Tools\CommentTreeNode;
8 use BookStack\Entities\Queries\PageQueries;
9 use BookStack\Http\Controller;
10 use BookStack\Permissions\Permission;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
14 class CommentController extends Controller
16 public function __construct(
17 protected CommentRepo $commentRepo,
18 protected PageQueries $pageQueries,
23 * Save a new comment for a Page.
25 * @throws ValidationException|\Exception
27 public function savePageComment(Request $request, int $pageId)
29 $input = $this->validate($request, [
30 'html' => ['required', 'string'],
31 'parent_id' => ['nullable', 'integer'],
32 'content_ref' => ['string'],
35 $page = $this->pageQueries->findVisibleById($pageId);
37 return response('Not found', 404);
40 // Create a new comment.
41 $this->checkPermission(Permission::CommentCreateAll);
42 $contentRef = $input['content_ref'] ?? '';
43 $comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null, $contentRef);
45 return view('comments.comment-branch', [
47 'branch' => new CommentTreeNode($comment, 0, []),
52 * Update an existing comment.
54 * @throws ValidationException
56 public function update(Request $request, int $commentId)
58 $input = $this->validate($request, [
59 'html' => ['required', 'string'],
62 $comment = $this->commentRepo->getById($commentId);
63 $this->checkOwnablePermission(Permission::PageView, $comment->entity);
64 $this->checkOwnablePermission(Permission::CommentUpdate, $comment);
66 $comment = $this->commentRepo->update($comment, $input['html']);
68 return view('comments.comment', [
69 'comment' => $comment,
75 * Mark a comment as archived.
77 public function archive(int $id)
79 $comment = $this->commentRepo->getById($id);
80 $this->checkOwnablePermission(Permission::PageView, $comment->entity);
81 if (!userCan(Permission::CommentUpdate, $comment) && !userCan(Permission::CommentDelete, $comment)) {
82 $this->showPermissionError();
85 $this->commentRepo->archive($comment);
87 $tree = new CommentTree($comment->entity);
88 return view('comments.comment-branch', [
90 'branch' => $tree->getCommentNodeForId($id),
95 * Unmark a comment as archived.
97 public function unarchive(int $id)
99 $comment = $this->commentRepo->getById($id);
100 $this->checkOwnablePermission(Permission::PageView, $comment->entity);
101 if (!userCan(Permission::CommentUpdate, $comment) && !userCan(Permission::CommentDelete, $comment)) {
102 $this->showPermissionError();
105 $this->commentRepo->unarchive($comment);
107 $tree = new CommentTree($comment->entity);
108 return view('comments.comment-branch', [
110 'branch' => $tree->getCommentNodeForId($id),
115 * Delete a comment from the system.
117 public function destroy(int $id)
119 $comment = $this->commentRepo->getById($id);
120 $this->checkOwnablePermission(Permission::CommentDelete, $comment);
122 $this->commentRepo->delete($comment);
124 return response()->json(['message' => trans('entities.comment_deleted')]);