]> BookStack Code Mirror - bookstack/blob - app/Activity/Controllers/CommentController.php
Comments: Started archive display, created mode for tree node
[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         $comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null, $input['content_ref']);
47
48         return view('comments.comment-branch', [
49             'readOnly' => false,
50             'branch' => new CommentTreeNode($comment, 0, []),
51         ]);
52     }
53
54     /**
55      * Update an existing comment.
56      *
57      * @throws ValidationException
58      */
59     public function update(Request $request, int $commentId)
60     {
61         $input = $this->validate($request, [
62             'html' => ['required', 'string'],
63         ]);
64
65         $comment = $this->commentRepo->getById($commentId);
66         $this->checkOwnablePermission('page-view', $comment->entity);
67         $this->checkOwnablePermission('comment-update', $comment);
68
69         $comment = $this->commentRepo->update($comment, $input['html']);
70
71         return view('comments.comment', [
72             'comment' => $comment,
73             'readOnly' => false,
74         ]);
75     }
76
77     /**
78      * Mark a comment as archived.
79      */
80     public function archive(int $id)
81     {
82         $comment = $this->commentRepo->getById($id);
83         $this->checkOwnablePermission('page-view', $comment->entity);
84         if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
85             $this->showPermissionError();
86         }
87
88         $this->commentRepo->archive($comment);
89
90         $tree = new CommentTree($comment->entity);
91         return view('comments.comment-branch', [
92             'readOnly' => false,
93             'branch' => $tree->getCommentNodeForId($id),
94         ]);
95     }
96
97     /**
98      * Unmark a comment as archived.
99      */
100     public function unarchive(int $id)
101     {
102         $comment = $this->commentRepo->getById($id);
103         $this->checkOwnablePermission('page-view', $comment->entity);
104         if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
105             $this->showPermissionError();
106         }
107
108         $this->commentRepo->unarchive($comment);
109
110         $tree = new CommentTree($comment->entity);
111         return view('comments.comment-branch', [
112             'readOnly' => false,
113             'branch' => $tree->getCommentNodeForId($id),
114         ]);
115     }
116
117     /**
118      * Delete a comment from the system.
119      */
120     public function destroy(int $id)
121     {
122         $comment = $this->commentRepo->getById($id);
123         $this->checkOwnablePermission('comment-delete', $comment);
124
125         $this->commentRepo->delete($comment);
126
127         return response()->json(['message' => trans('entities.comment_deleted')]);
128     }
129 }