2017-04-19 03:51:45 +08:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2017-01-14 00:15:48 +08:00
|
|
|
|
2017-04-19 03:51:45 +08:00
|
|
|
use BookStack\Repos\CommentRepo;
|
|
|
|
use BookStack\Repos\EntityRepo;
|
2017-05-16 03:10:14 +08:00
|
|
|
use BookStack\Comment;
|
2017-01-14 00:15:48 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2017-04-19 03:51:45 +08:00
|
|
|
// delete -checkOwnablePermission \
|
2017-01-14 00:15:48 +08:00
|
|
|
class CommentController extends Controller
|
|
|
|
{
|
2017-04-19 03:51:45 +08:00
|
|
|
protected $entityRepo;
|
|
|
|
|
2017-05-16 03:10:14 +08:00
|
|
|
public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo, Comment $comment)
|
2017-04-19 03:51:45 +08:00
|
|
|
{
|
|
|
|
$this->entityRepo = $entityRepo;
|
|
|
|
$this->commentRepo = $commentRepo;
|
2017-05-16 03:10:14 +08:00
|
|
|
$this->comment = $comment;
|
2017-04-19 03:51:45 +08:00
|
|
|
parent::__construct();
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
2017-04-19 03:51:45 +08:00
|
|
|
|
2017-05-03 05:12:04 +08:00
|
|
|
public function save(Request $request, $pageId, $commentId = null)
|
2017-04-19 03:51:45 +08:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'text' => 'required|string',
|
|
|
|
'html' => 'required|string',
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
return response('Not found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($page->draft) {
|
|
|
|
// cannot add comments to drafts.
|
|
|
|
return response()->json([
|
|
|
|
'status' => 'error',
|
|
|
|
'message' => trans('errors.cannot_add_comment_to_draft'),
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
|
|
|
if (empty($commentId)) {
|
|
|
|
// create a new comment.
|
|
|
|
$this->checkPermission('comment-create-all');
|
|
|
|
$comment = $this->commentRepo->create($page, $request->only(['text', 'html', 'parent_id']));
|
2017-05-16 03:10:14 +08:00
|
|
|
$respMsg = trans('entities.comment_created');
|
2017-04-19 03:51:45 +08:00
|
|
|
} else {
|
|
|
|
// update existing comment
|
2017-05-16 03:10:14 +08:00
|
|
|
// get comment by ID and check if this user has permission to update.
|
2017-04-19 03:51:45 +08:00
|
|
|
$comment = $this->comment->findOrFail($commentId);
|
|
|
|
$this->checkOwnablePermission('comment-update', $comment);
|
|
|
|
$this->commentRepo->update($comment, $request->all());
|
|
|
|
$respMsg = trans('entities.comment_updated');
|
|
|
|
}
|
|
|
|
|
2017-05-30 11:32:47 +08:00
|
|
|
$comment = $this->commentRepo->getCommentById($comment->id);
|
|
|
|
|
2017-04-19 03:51:45 +08:00
|
|
|
return response()->json([
|
|
|
|
'status' => 'success',
|
2017-05-30 11:32:47 +08:00
|
|
|
'message' => $respMsg,
|
|
|
|
'comment' => $comment
|
2017-04-19 03:51:45 +08:00
|
|
|
]);
|
|
|
|
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-01-14 00:15:48 +08:00
|
|
|
public function destroy($id) {
|
2017-04-19 03:51:45 +08:00
|
|
|
$comment = $this->comment->findOrFail($id);
|
|
|
|
$this->checkOwnablePermission('comment-delete', $comment);
|
2017-06-04 21:22:44 +08:00
|
|
|
$this->commentRepo->delete($comment);
|
|
|
|
$comment = $this->commentRepo->getCommentById($comment->id);
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'success' => true,
|
|
|
|
'message' => trans('entities.comment_deleted'),
|
|
|
|
'comment' => $comment
|
|
|
|
]);
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
2017-04-19 03:51:45 +08:00
|
|
|
|
2017-05-30 11:32:47 +08:00
|
|
|
|
|
|
|
public function getPageComments($pageId) {
|
2017-04-19 03:51:45 +08:00
|
|
|
try {
|
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
return response('Not found', 404);
|
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-04-19 03:51:45 +08:00
|
|
|
if($page->draft) {
|
|
|
|
// cannot add comments to drafts.
|
|
|
|
return response()->json([
|
|
|
|
'status' => 'error',
|
|
|
|
'message' => trans('errors.no_comments_for_draft'),
|
|
|
|
], 400);
|
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-04-19 03:51:45 +08:00
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-05-30 11:32:47 +08:00
|
|
|
$comments = $this->commentRepo->getPageComments($pageId);
|
2017-06-04 13:47:14 +08:00
|
|
|
return response()->json(['success' => true, 'comments'=> $comments['comments'],
|
|
|
|
'total' => $comments['total'], 'permissions' => [
|
|
|
|
'comment_create' => $this->currentUser->can('comment-create-all'),
|
|
|
|
'comment_update_own' => $this->currentUser->can('comment-update-own'),
|
|
|
|
'comment_update_all' => $this->currentUser->can('comment-update-all'),
|
|
|
|
'comment_delete_all' => $this->currentUser->can('comment-delete-all'),
|
|
|
|
'comment_delete_own' => $this->currentUser->can('comment-delete-own'),
|
|
|
|
], 'user_id' => $this->currentUser->id]);
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
|
|
|
}
|