]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/CommentController.php
#47 - Adds functionality to delete a comment. Also reduces the number of watchers.
[bookstack] / app / Http / Controllers / CommentController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Repos\CommentRepo;
4 use BookStack\Repos\EntityRepo;
5 use BookStack\Comment;
6 use Illuminate\Http\Request;
7
8 // delete  -checkOwnablePermission \
9 class CommentController extends Controller
10 {
11     protected $entityRepo;
12
13     public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo, Comment $comment)
14     {
15         $this->entityRepo = $entityRepo;
16         $this->commentRepo = $commentRepo;
17         $this->comment = $comment;
18         parent::__construct();
19     }
20
21     public function save(Request $request, $pageId, $commentId = null)
22     {
23         $this->validate($request, [
24             'text' => 'required|string',
25             'html' => 'required|string',
26         ]);
27
28         try {
29             $page = $this->entityRepo->getById('page', $pageId, true);
30         } catch (ModelNotFoundException $e) {
31             return response('Not found', 404);
32         }
33
34         if($page->draft) {
35             // cannot add comments to drafts.
36             return response()->json([
37                 'status' => 'error',
38                 'message' => trans('errors.cannot_add_comment_to_draft'),
39             ], 400);
40         }
41
42         $this->checkOwnablePermission('page-view', $page);
43         if (empty($commentId)) {
44             // create a new comment.
45             $this->checkPermission('comment-create-all');
46             $comment = $this->commentRepo->create($page, $request->only(['text', 'html', 'parent_id']));
47             $respMsg = trans('entities.comment_created');
48         } else {
49             // update existing comment
50             // get comment by ID and check if this user has permission to update.
51             $comment = $this->comment->findOrFail($commentId);
52             $this->checkOwnablePermission('comment-update', $comment);
53             $this->commentRepo->update($comment, $request->all());
54             $respMsg = trans('entities.comment_updated');
55         }
56
57         $comment = $this->commentRepo->getCommentById($comment->id);
58
59         return response()->json([
60             'status'    => 'success',
61             'message'   => $respMsg,
62             'comment'   => $comment
63         ]);
64
65     }
66
67     public function destroy($id) {
68         $comment = $this->comment->findOrFail($id);
69         $this->checkOwnablePermission('comment-delete', $comment);
70         $this->commentRepo->delete($comment);
71         $comment = $this->commentRepo->getCommentById($comment->id);
72
73         return response()->json([
74             'success' => true,
75             'message' => trans('entities.comment_deleted'),
76             'comment' => $comment
77         ]);
78     }
79
80
81     public function getPageComments($pageId) {
82         try {
83             $page = $this->entityRepo->getById('page', $pageId, true);
84         } catch (ModelNotFoundException $e) {
85             return response('Not found', 404);
86         }
87
88         if($page->draft) {
89             // cannot add comments to drafts.
90             return response()->json([
91                 'status' => 'error',
92                 'message' => trans('errors.no_comments_for_draft'),
93             ], 400);
94         }
95
96         $this->checkOwnablePermission('page-view', $page);
97
98         $comments = $this->commentRepo->getPageComments($pageId);
99         return response()->json(['success' => true, 'comments'=> $comments['comments'],
100             'total' => $comments['total'], 'permissions' => [
101                 'comment_create' => $this->currentUser->can('comment-create-all'),
102                 'comment_update_own' => $this->currentUser->can('comment-update-own'),
103                 'comment_update_all' => $this->currentUser->can('comment-update-all'),
104                 'comment_delete_all' => $this->currentUser->can('comment-delete-all'),
105                 'comment_delete_own' => $this->currentUser->can('comment-delete-own'),
106             ], 'user_id' => $this->currentUser->id]);
107     }
108 }