]> BookStack Code Mirror - bookstack/blob - app/Activity/Controllers/CommentController.php
Merge pull request #5917 from BookStackApp/copy_references
[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 BookStack\Permissions\Permission;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
13
14 class CommentController extends Controller
15 {
16     public function __construct(
17         protected CommentRepo $commentRepo,
18         protected PageQueries $pageQueries,
19     ) {
20     }
21
22     /**
23      * Save a new comment for a Page.
24      *
25      * @throws ValidationException|\Exception
26      */
27     public function savePageComment(Request $request, int $pageId)
28     {
29         $input = $this->validate($request, [
30             'html'      => ['required', 'string'],
31             'parent_id' => ['nullable', 'integer'],
32             'content_ref' => ['string'],
33         ]);
34
35         $page = $this->pageQueries->findVisibleById($pageId);
36         if ($page === null) {
37             return response('Not found', 404);
38         }
39
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);
44
45         return view('comments.comment-branch', [
46             'readOnly' => false,
47             'branch' => new CommentTreeNode($comment, 0, []),
48         ]);
49     }
50
51     /**
52      * Update an existing comment.
53      *
54      * @throws ValidationException
55      */
56     public function update(Request $request, int $commentId)
57     {
58         $input = $this->validate($request, [
59             'html' => ['required', 'string'],
60         ]);
61
62         $comment = $this->commentRepo->getById($commentId);
63         $this->checkOwnablePermission(Permission::PageView, $comment->entity);
64         $this->checkOwnablePermission(Permission::CommentUpdate, $comment);
65
66         $comment = $this->commentRepo->update($comment, $input['html']);
67
68         return view('comments.comment', [
69             'comment' => $comment,
70             'readOnly' => false,
71         ]);
72     }
73
74     /**
75      * Mark a comment as archived.
76      */
77     public function archive(int $id)
78     {
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();
83         }
84
85         $this->commentRepo->archive($comment);
86
87         $tree = new CommentTree($comment->entity);
88         return view('comments.comment-branch', [
89             'readOnly' => false,
90             'branch' => $tree->getCommentNodeForId($id),
91         ]);
92     }
93
94     /**
95      * Unmark a comment as archived.
96      */
97     public function unarchive(int $id)
98     {
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();
103         }
104
105         $this->commentRepo->unarchive($comment);
106
107         $tree = new CommentTree($comment->entity);
108         return view('comments.comment-branch', [
109             'readOnly' => false,
110             'branch' => $tree->getCommentNodeForId($id),
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(Permission::CommentDelete, $comment);
121
122         $this->commentRepo->delete($comment);
123
124         return response()->json(['message' => trans('entities.comment_deleted')]);
125     }
126 }