diff --git a/app/Helpers/CollectionLikeChecker.php b/app/Helpers/CollectionLikeChecker.php new file mode 100644 index 000000000..0d6404447 --- /dev/null +++ b/app/Helpers/CollectionLikeChecker.php @@ -0,0 +1,107 @@ +getQueueableClass(); + + // Create the instance + $instance = new CollectionLikeChecker(); + $instance->collectionType = $collectionType; + $instance->collection = $collection; + $instance->userID = $userID; + + // Fetch the data for the instance + $instance->fetchData(); + + // Return the instance + return $instance; + } + + /** + * Returns an array of IDs of the items in the collection. + * + * @return array + */ + protected function getCollectionIDs() { + $IDs = []; + + foreach($this->collection as $item) + $IDs[] = $item->id; + + return $IDs; + } + + /** + * Fetches the data by making a query and stores it in the instance. + */ + protected function fetchData() { + $this->queryData = DB::table('love_likes') + ->where('user_id', $this->userID) + ->where('likeable_type', $this->collectionType) + ->whereIn('likeable_id', $this->getCollectionIDs()) + ->get(); + } + + /** + * Checks if an item has been liked. + * + * @param $item + * @return bool + */ + function hasLiked($item) { + foreach($this->queryData as $queryItem) + if($queryItem->likeable_id == $item->id && $queryItem->type_id == 'LIKE') + return true; + + return false; + } + + /** + * Checks if an item has been disliked. + * + * @param $item + * @return bool + */ + function hasDisliked($item) { + foreach($this->queryData as $queryItem) + if($queryItem->likeable_id == $item->id && $queryItem->type_id == 'DISLIKE') + return true; + + return false; + } + + /** + * Returns the current like action for an item. + * -1 = disliked + * 0 = neutral + * 1 = liked + * + * @param $item + * @return int + */ + function getCurrentLikeAction($item) { + if($this->hasLiked($item)) + return 1; + else if($this->hasDisliked($item)) + return -1; + else + return 0; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/ForumThreadController.php b/app/Http/Controllers/ForumThreadController.php index 0b1a21d48..81471e8a1 100644 --- a/app/Http/Controllers/ForumThreadController.php +++ b/app/Http/Controllers/ForumThreadController.php @@ -5,7 +5,9 @@ use App\ForumReply; use App\ForumSectionBan; use App\ForumThread; +use App\Helpers\CollectionLikeChecker; use App\Helpers\JSONResult; +use App\Http\Resources\ForumReplyResource; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -152,30 +154,26 @@ public function replies(Request $request, ForumThread $thread) { else if($givenOrder == 'top') $replies = $replies->orderByLikesCount(); + // 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 - $displayReplies = []; + $formattedReplies = []; foreach($replies as $reply) { - $displayReplies[] = [ - 'id' => $reply->id, - 'posted_at' => $reply->created_at->format('Y-m-d H:i:s'), - 'user' => [ - 'id' => $reply->user->id, - 'username' => $reply->user->username, - 'avatar' => $reply->user->getAvatarURL() - ], - 'score' => $reply->likesDiffDislikesCount, - 'content' => $reply->content - ]; + $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' => $displayReplies + 'replies' => $formattedReplies ]); } diff --git a/app/Http/Resources/ForumReplyResource.php b/app/Http/Resources/ForumReplyResource.php new file mode 100644 index 000000000..a14abd63b --- /dev/null +++ b/app/Http/Resources/ForumReplyResource.php @@ -0,0 +1,29 @@ + $this->id, + 'posted_at' => $this->created_at->format('Y-m-d H:i:s'), + 'poster' => [ + 'id' => $this->user->id, + 'username' => $this->user->username, + 'avatar' => $this->user->getAvatarURL() + ], + 'score' => $this->likesDiffDislikesCount, + 'content' => $this->content + ]; + } +} diff --git a/public/openapi.json b/public/openapi.json index c657b800e..dca755c7a 100644 --- a/public/openapi.json +++ b/public/openapi.json @@ -1451,6 +1451,11 @@ }, "/forum-threads/{threadID}/replies": { "get": { + "security": [ + { + "kurozoraBearer": [] + } + ], "tags": [ "forum-threads" ], diff --git a/routes/api.php b/routes/api.php index 1985818a1..ef073ca73 100644 --- a/routes/api.php +++ b/routes/api.php @@ -144,7 +144,8 @@ Route::post('/{thread}/vote', [ForumThreadController::class, 'vote']) ->middleware('kurozora.userauth'); - Route::get('/{thread}/replies', [ForumThreadController::class, 'replies']); + Route::get('/{thread}/replies', [ForumThreadController::class, 'replies']) + ->middleware('kurozora.userauth'); Route::post('/{thread}/replies', [ForumThreadController::class, 'postReply']) ->middleware('kurozora.userauth');