]> BookStack Code Mirror - bookstack/blob - app/Activity/Controllers/CommentController.php
7a290ebabc526969ae38232be72982c8af953f30
[bookstack] / app / Activity / Controllers / CommentController.php
1 <?php
2
3 namespace BookStack\Activity\Controllers;
4
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;
10
11 class CommentController extends Controller
12 {
13     public function __construct(
14         protected CommentRepo $commentRepo,
15         protected PageQueries $pageQueries,
16     ) {
17     }
18
19     /**
20      * Save a new comment for a Page.
21      *
22      * @throws ValidationException
23      */
24     public function savePageComment(Request $request, int $pageId)
25     {
26         $input = $this->validate($request, [
27             'html'      => ['required', 'string'],
28             'parent_id' => ['nullable', 'integer'],
29             'content_ref' => ['string'],
30         ]);
31
32         $page = $this->pageQueries->findVisibleById($pageId);
33         if ($page === null) {
34             return response('Not found', 404);
35         }
36
37         // Prevent adding comments to draft pages
38         if ($page->draft) {
39             return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
40         }
41
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']);
45
46         return view('comments.comment-branch', [
47             'readOnly' => false,
48             'branch' => [
49                 'comment' => $comment,
50                 'children' => [],
51             ]
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         if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
85             $this->showPermissionError();
86         }
87
88         $this->commentRepo->archive($comment);
89
90         return view('comments.comment', [
91             'comment' => $comment,
92             'readOnly' => false,
93         ]);
94     }
95
96     /**
97      * Unmark a comment as archived.
98      */
99     public function unarchive(int $id)
100     {
101         $comment = $this->commentRepo->getById($id);
102         if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
103             $this->showPermissionError();
104         }
105
106         $this->commentRepo->unarchive($comment);
107
108         return view('comments.comment', [
109             'comment' => $comment,
110             'readOnly' => false,
111         ]);
112     }
113
114     /**
115      * Delete a comment from the system.
116      */
117     public function destroy(int $id)
118     {
119         $comment = $this->commentRepo->getById($id);
120         $this->checkOwnablePermission('comment-delete', $comment);
121
122         $this->commentRepo->delete($comment);
123
124         return response()->json(['message' => trans('entities.comment_deleted')]);
125     }
126 }