]> BookStack Code Mirror - bookstack/blob - app/Activity/Controllers/CommentController.php
Comments: Fixed a range of TS errors + other
[bookstack] / app / Activity / Controllers / CommentController.php
1 <?php
2
3 namespace BookStack\Activity\Controllers;
4
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 Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
12
13 class CommentController extends Controller
14 {
15     public function __construct(
16         protected CommentRepo $commentRepo,
17         protected PageQueries $pageQueries,
18     ) {
19     }
20
21     /**
22      * Save a new comment for a Page.
23      *
24      * @throws ValidationException
25      */
26     public function savePageComment(Request $request, int $pageId)
27     {
28         $input = $this->validate($request, [
29             'html'      => ['required', 'string'],
30             'parent_id' => ['nullable', 'integer'],
31             'content_ref' => ['string'],
32         ]);
33
34         $page = $this->pageQueries->findVisibleById($pageId);
35         if ($page === null) {
36             return response('Not found', 404);
37         }
38
39         // Prevent adding comments to draft pages
40         if ($page->draft) {
41             return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
42         }
43
44         // Create a new comment.
45         $this->checkPermission('comment-create-all');
46         $contentRef = $input['content_ref'] ?? '';
47         $comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null, $contentRef);
48
49         return view('comments.comment-branch', [
50             'readOnly' => false,
51             'branch' => new CommentTreeNode($comment, 0, []),
52         ]);
53     }
54
55     /**
56      * Update an existing comment.
57      *
58      * @throws ValidationException
59      */
60     public function update(Request $request, int $commentId)
61     {
62         $input = $this->validate($request, [
63             'html' => ['required', 'string'],
64         ]);
65
66         $comment = $this->commentRepo->getById($commentId);
67         $this->checkOwnablePermission('page-view', $comment->entity);
68         $this->checkOwnablePermission('comment-update', $comment);
69
70         $comment = $this->commentRepo->update($comment, $input['html']);
71
72         return view('comments.comment', [
73             'comment' => $comment,
74             'readOnly' => false,
75         ]);
76     }
77
78     /**
79      * Mark a comment as archived.
80      */
81     public function archive(int $id)
82     {
83         $comment = $this->commentRepo->getById($id);
84         $this->checkOwnablePermission('page-view', $comment->entity);
85         if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
86             $this->showPermissionError();
87         }
88
89         $this->commentRepo->archive($comment);
90
91         $tree = new CommentTree($comment->entity);
92         return view('comments.comment-branch', [
93             'readOnly' => false,
94             'branch' => $tree->getCommentNodeForId($id),
95         ]);
96     }
97
98     /**
99      * Unmark a comment as archived.
100      */
101     public function unarchive(int $id)
102     {
103         $comment = $this->commentRepo->getById($id);
104         $this->checkOwnablePermission('page-view', $comment->entity);
105         if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
106             $this->showPermissionError();
107         }
108
109         $this->commentRepo->unarchive($comment);
110
111         $tree = new CommentTree($comment->entity);
112         return view('comments.comment-branch', [
113             'readOnly' => false,
114             'branch' => $tree->getCommentNodeForId($id),
115         ]);
116     }
117
118     /**
119      * Delete a comment from the system.
120      */
121     public function destroy(int $id)
122     {
123         $comment = $this->commentRepo->getById($id);
124         $this->checkOwnablePermission('comment-delete', $comment);
125
126         $this->commentRepo->delete($comment);
127
128         return response()->json(['message' => trans('entities.comment_deleted')]);
129     }
130 }