From 36f6be6c3f3d1b941bd37e32fbbb0360fe62c87a Mon Sep 17 00:00:00 2001 From: musa11971 Date: Tue, 10 Sep 2019 18:51:41 +0200 Subject: [PATCH] Using ForumReplyResource to display user specific data --- .../Controllers/ForumThreadController.php | 14 +---------- app/Http/Resources/ForumReplyResource.php | 23 ++++++++++++++++++- public/openapi.json | 3 ++- routes/api.php | 2 +- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/ForumThreadController.php b/app/Http/Controllers/ForumThreadController.php index e958d5b23..773542073 100644 --- a/app/Http/Controllers/ForumThreadController.php +++ b/app/Http/Controllers/ForumThreadController.php @@ -158,23 +158,11 @@ public function replies(Request $request, ForumThread $thread) { // Paginate the replies $replies = $replies->paginate(ForumThread::REPLIES_PER_PAGE); - // Instantiate a CollectionLikeChecker to get the current liked status for the user - $likeChecker = CollectionLikeChecker::retrieve($request->user()->id, $replies); - - // Format the replies - $formattedReplies = []; - - foreach($replies as $reply) { - $formattedReplies[] = array_merge(ForumReplyResource::make($reply)->toArray($request), [ - 'current_like_action' => $likeChecker->getCurrentLikeAction($reply) - ]); - } - // Show successful response return JSONResult::success([ 'page' => $givenPage, 'reply_pages' => $thread->getPageCount(), - 'replies' => $formattedReplies + 'replies' => ForumReplyResource::collection($replies) ]); } diff --git a/app/Http/Resources/ForumReplyResource.php b/app/Http/Resources/ForumReplyResource.php index a14abd63b..97106c4d0 100644 --- a/app/Http/Resources/ForumReplyResource.php +++ b/app/Http/Resources/ForumReplyResource.php @@ -3,6 +3,7 @@ namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; +use Illuminate\Support\Facades\Auth; class ForumReplyResource extends JsonResource { @@ -14,7 +15,7 @@ class ForumReplyResource extends JsonResource */ public function toArray($request) { - return [ + $resource = [ 'id' => $this->id, 'posted_at' => $this->created_at->format('Y-m-d H:i:s'), 'poster' => [ @@ -25,5 +26,25 @@ public function toArray($request) 'score' => $this->likesDiffDislikesCount, 'content' => $this->content ]; + + if(Auth::check()) + $resource = array_merge($resource, $this->getUserSpecificDetails()); + + return $resource; + } + + /** + * Returns the user specific details for the resource. + * + * @return array + */ + protected function getUserSpecificDetails() { + $user = Auth::user(); + + return [ + 'current_user' => [ + 'like_action' => $user->likeAction($this->resource) + ] + ]; } } diff --git a/public/openapi.json b/public/openapi.json index 34bf6de44..633d02288 100644 --- a/public/openapi.json +++ b/public/openapi.json @@ -1471,6 +1471,7 @@ "/forum-threads/{threadID}/replies": { "get": { "security": [ + {}, { "kurozoraBearer": [] } @@ -1478,7 +1479,7 @@ "tags": [ "forum-threads" ], - "summary": "Gets the replies on a thread.", + "summary": "(optional authentication) Gets the replies on a thread.", "description": "This endpoint will retrieve the replies on a thread.", "parameters": [ { diff --git a/routes/api.php b/routes/api.php index a131da5d2..ed57ec84b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -149,7 +149,7 @@ ->middleware('kurozora.userauth'); Route::get('/{thread}/replies', [ForumThreadController::class, 'replies']) - ->middleware('kurozora.userauth'); + ->middleware('kurozora.userauth:optional'); Route::post('/{thread}/replies', [ForumThreadController::class, 'postReply']) ->middleware('kurozora.userauth');