diff --git a/app/Http/Controllers/ForumSectionController.php b/app/Http/Controllers/ForumSectionController.php index fd298b4da..21be099fe 100644 --- a/app/Http/Controllers/ForumSectionController.php +++ b/app/Http/Controllers/ForumSectionController.php @@ -8,6 +8,7 @@ use App\Helpers\JSONResult; use App\Http\Requests\PostThread; use App\Http\Resources\ForumSectionResource; +use App\Http\Resources\ForumThreadResource; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -41,7 +42,7 @@ public function details(ForumSection $section) { } /** - * Returns the threads of a section + * Returns the threads of a section. * * @param Request $request * @param ForumSection $section @@ -72,30 +73,11 @@ public function threads(Request $request, ForumSection $section) { $threads = $threads->paginate(ForumSection::THREADS_PER_PAGE); - // Format the threads - $displayThreads = []; - - foreach($threads as $thread) - $displayThreads[] = [ - 'id' => $thread->id, - 'title' => $thread->title, - 'content_teaser' => - substr(strip_tags($thread->content), 0, 100) . - ((strlen($thread->content) > 100) ? '...' : '') - , - 'locked' => (bool) $thread->locked, - 'poster_user_id' => $thread->user->id, - 'poster_username' => $thread->user->username, - 'creation_date' => $thread->created_at->format('Y-m-d H:i:s'), - 'reply_count' => $thread->replies->count(), - 'score' => $thread->likesDiffDislikesCount - ]; - // Show threads in response return JSONResult::success([ 'page' => (int) $givenPage, 'thread_pages' => $section->getPageCount(), - 'threads' => $displayThreads + 'threads' => ForumThreadResource::collection($threads) ]); } diff --git a/app/Http/Resources/ForumThreadResource.php b/app/Http/Resources/ForumThreadResource.php new file mode 100644 index 000000000..358847602 --- /dev/null +++ b/app/Http/Resources/ForumThreadResource.php @@ -0,0 +1,50 @@ + $this->id, + 'title' => $this->title, + 'content' => $this->content, + 'locked' => (bool) $this->locked, + 'poster_user_id' => $this->user->id, + 'poster_username' => $this->user->username, + 'creation_date' => $this->created_at->format('Y-m-d H:i:s'), + 'reply_count' => $this->replies->count(), + 'score' => $this->likesDiffDislikesCount + ]; + + 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 40a300c43..d6d4c2da5 100644 --- a/public/openapi.json +++ b/public/openapi.json @@ -1104,6 +1104,12 @@ }, "/forum-sections/{sectionID}/threads": { "get": { + "security": [ + {}, + { + "kurozoraBearer": [] + } + ], "tags": [ "forum-sections" ], diff --git a/routes/api.php b/routes/api.php index fa74c07f2..69bcd968f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -132,7 +132,8 @@ Route::get('/{section}', [ForumSectionController::class, 'details']); - Route::get('/{section}/threads', [ForumSectionController::class, 'threads']); + Route::get('/{section}/threads', [ForumSectionController::class, 'threads']) + ->middleware('kurozora.userauth:optional'); Route::post('/{section}/threads', [ForumSectionController::class, 'postThread']) ->middleware('kurozora.userauth');