2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
namespace BookStack\Activity\Controllers;
|
2017-01-14 00:15:48 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Activity\CommentRepo;
|
2020-11-22 08:17:45 +08:00
|
|
|
use BookStack\Entities\Models\Page;
|
2023-05-19 03:53:39 +08:00
|
|
|
use BookStack\Http\Controller;
|
2017-01-14 00:15:48 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-10-05 19:55:01 +08:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2017-01-14 00:15:48 +08:00
|
|
|
|
|
|
|
class CommentController extends Controller
|
|
|
|
{
|
2023-06-08 22:03:38 +08:00
|
|
|
public function __construct(
|
|
|
|
protected CommentRepo $commentRepo
|
|
|
|
) {
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
2017-04-19 03:51:45 +08:00
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
/**
|
2021-06-26 23:23:15 +08:00
|
|
|
* Save a new comment for a Page.
|
|
|
|
*
|
2019-10-05 19:55:01 +08:00
|
|
|
* @throws ValidationException
|
2017-09-03 23:37:51 +08:00
|
|
|
*/
|
2020-05-02 06:24:11 +08:00
|
|
|
public function savePageComment(Request $request, int $pageId)
|
2017-04-19 03:51:45 +08:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
2021-11-05 08:26:55 +08:00
|
|
|
'text' => ['required', 'string'],
|
|
|
|
'parent_id' => ['nullable', 'integer'],
|
2017-04-19 03:51:45 +08:00
|
|
|
]);
|
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
$page = Page::visible()->find($pageId);
|
|
|
|
if ($page === null) {
|
2017-04-19 03:51:45 +08:00
|
|
|
return response('Not found', 404);
|
|
|
|
}
|
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
// Prevent adding comments to draft pages
|
|
|
|
if ($page->draft) {
|
|
|
|
return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
|
2017-04-19 03:51:45 +08:00
|
|
|
}
|
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
// Create a new comment.
|
|
|
|
$this->checkPermission('comment-create-all');
|
2020-05-02 06:24:11 +08:00
|
|
|
$comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id'));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2023-06-08 22:03:38 +08:00
|
|
|
return view('comments.comment-branch', [
|
|
|
|
'branch' => [
|
|
|
|
'comment' => $comment,
|
|
|
|
'children' => [],
|
|
|
|
]
|
|
|
|
]);
|
2017-09-03 23:37:51 +08:00
|
|
|
}
|
2017-05-30 11:32:47 +08:00
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
/**
|
|
|
|
* Update an existing comment.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-10-05 19:55:01 +08:00
|
|
|
* @throws ValidationException
|
2017-09-03 23:37:51 +08:00
|
|
|
*/
|
2019-10-05 19:55:01 +08:00
|
|
|
public function update(Request $request, int $commentId)
|
2017-09-03 23:37:51 +08:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
2021-11-05 08:26:55 +08:00
|
|
|
'text' => ['required', 'string'],
|
2017-04-19 03:51:45 +08:00
|
|
|
]);
|
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
$comment = $this->commentRepo->getById($commentId);
|
|
|
|
$this->checkOwnablePermission('page-view', $comment->entity);
|
|
|
|
$this->checkOwnablePermission('comment-update', $comment);
|
|
|
|
|
2020-05-02 06:24:11 +08:00
|
|
|
$comment = $this->commentRepo->update($comment, $request->get('text'));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-04-07 19:00:09 +08:00
|
|
|
return view('comments.comment', ['comment' => $comment]);
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
/**
|
|
|
|
* Delete a comment from the system.
|
|
|
|
*/
|
2019-10-05 19:55:01 +08:00
|
|
|
public function destroy(int $id)
|
2017-09-03 23:37:51 +08:00
|
|
|
{
|
|
|
|
$comment = $this->commentRepo->getById($id);
|
2017-04-19 03:51:45 +08:00
|
|
|
$this->checkOwnablePermission('comment-delete', $comment);
|
2019-10-05 19:55:01 +08:00
|
|
|
|
2017-06-04 21:22:44 +08:00
|
|
|
$this->commentRepo->delete($comment);
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
return response()->json(['message' => trans('entities.comment_deleted')]);
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
|
|
|
}
|