55namespace BookStack \Activity \Controllers ;
66
77use BookStack \Activity \CommentRepo ;
8+ use BookStack \Activity \Models \Comment ;
89use BookStack \Http \ApiController ;
910use Illuminate \Http \JsonResponse ;
1011
12+ /**
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'.
17+ */
1118class CommentApiController extends ApiController
1219{
1320 // TODO - Add tree-style comment listing to page-show responses.
14- // TODO - list
1521 // TODO - create
16- // TODO - read
1722 // TODO - update
1823 // TODO - delete
1924
2025 // TODO - Test visibility controls
2126 // TODO - Test permissions of each action
2227
23- // TODO - Support intro block for API docs so we can explain the
24- // properties for comments in a shared kind of way?
25-
2628 public function __construct (
2729 protected CommentRepo $ commentRepo ,
2830 ) {
2931 }
3032
31-
3233 /**
3334 * Get a listing of comments visible to the user.
3435 */
@@ -40,4 +41,30 @@ public function list(): JsonResponse
4041 'id ' , 'commentable_id ' , 'commentable_type ' , 'parent_id ' , 'local_id ' , 'content_ref ' , 'created_by ' , 'updated_by ' , 'created_at ' , 'updated_at '
4142 ]);
4243 }
44+
45+ /**
46+ * Read the details of a single comment, along with its direct replies.
47+ */
48+ public function read (string $ id ): JsonResponse
49+ {
50+ $ comment = $ this ->commentRepo ->getQueryForVisible ()
51+ ->where ('id ' , '= ' , $ id )->firstOrFail ();
52+
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 )
57+ ->get ();
58+
59+ /** @var Comment[] $toProcess */
60+ $ toProcess = [$ comment , ...$ replies ];
61+ foreach ($ toProcess as $ commentToProcess ) {
62+ $ commentToProcess ->setAttribute ('html ' , $ commentToProcess ->safeHtml ());
63+ $ commentToProcess ->makeVisible ('html ' );
64+ }
65+
66+ $ comment ->setRelation ('replies ' , $ replies );
67+
68+ return response ()->json ($ comment );
69+ }
4370}
0 commit comments