3 declare(strict_types=1);
5 namespace BookStack\Activity\Controllers;
7 use BookStack\Activity\CommentRepo;
8 use BookStack\Activity\Models\Comment;
9 use BookStack\Http\ApiController;
10 use Illuminate\Http\JsonResponse;
13 * The comment data model has a 'local_id' property, which is a unique integer ID
14 * scoped to the page which the comment is on. The 'parent_id' is used for replies
15 * and refers to the 'local_id' of the parent comment on the same page, not the main
16 * globally unique 'id'.
18 class CommentApiController extends ApiController
20 // TODO - Add tree-style comment listing to page-show responses.
25 // TODO - Test visibility controls
26 // TODO - Test permissions of each action
28 public function __construct(
29 protected CommentRepo $commentRepo,
34 * Get a listing of comments visible to the user.
36 public function list(): JsonResponse
38 $query = $this->commentRepo->getQueryForVisible();
40 return $this->apiListingResponse($query, [
41 'id', 'commentable_id', 'commentable_type', 'parent_id', 'local_id', 'content_ref', 'created_by', 'updated_by', 'created_at', 'updated_at'
46 * Read the details of a single comment, along with its direct replies.
48 public function read(string $id): JsonResponse
50 $comment = $this->commentRepo->getQueryForVisible()
51 ->where('id', '=', $id)->firstOrFail();
53 $replies = $this->commentRepo->getQueryForVisible()
54 ->where('parent_id', '=', $comment->local_id)
55 ->where('commentable_id', '=', $comment->commentable_id)
56 ->where('commentable_type', '=', $comment->commentable_type)
59 /** @var Comment[] $toProcess */
60 $toProcess = [$comment, ...$replies];
61 foreach ($toProcess as $commentToProcess) {
62 $commentToProcess->setAttribute('html', $commentToProcess->safeHtml());
63 $commentToProcess->makeVisible('html');
66 $comment->setRelation('replies', $replies);
68 return response()->json($comment);