diff --git a/app/Helpers/EntryHelpers.php b/app/Helpers/EntryHelpers.php index 25228a3..928bb6a 100644 --- a/app/Helpers/EntryHelpers.php +++ b/app/Helpers/EntryHelpers.php @@ -6,6 +6,8 @@ use App\Models\Entry; use App\Models\EntryFile; use App\Models\News; use App\Services\XenforoApiService; +use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\RequestException; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; @@ -74,25 +76,29 @@ class EntryHelpers { $cacheKey = "news_comments_{$entry->id}"; else $cacheKey = "entry_comments_{$entry->id}"; - return Cache::remember($cacheKey, now()->addDays(1), function () use ($entry, $limit) { + return Cache::flexible($cacheKey, [now()->addMinutes(30), now()->addMinutes(60)], function () use ($entry, $limit) { - $service = app(XenforoApiService::class); + try { + $service = app(XenforoApiService::class); - // Get thread infos and pagination. - $paginationInfos = $service->getThreadPosts($entry->comments_thread_id, 1); - $lastPage = $paginationInfos['pagination']['last_page'] ?? 1; + // Get thread infos and pagination. + $paginationInfos = $service->getThreadPosts($entry->comments_thread_id, 1); + $lastPage = $paginationInfos['pagination']['last_page'] ?? 1; - // Get last threads - $lastPageData = $lastPage > 1 ? $service->getThreadPosts($entry->comments_thread_id, $lastPage) : $paginationInfos; - $posts = $lastPageData['posts'] ?? []; + // Get last threads + $lastPageData = $lastPage > 1 ? $service->getThreadPosts($entry->comments_thread_id, $lastPage) : $paginationInfos; + $posts = $lastPageData['posts'] ?? []; - if( count( $posts ) < $limit && $lastPage > 1 ){ - $previousPageData = $service->getThreadPosts($entry->comments_thread_id, $lastPage - 1 ); - $posts = array_merge( $posts, $previousPageData['posts'] ?? [] ); + if (count($posts) < $limit && $lastPage > 1) { + $previousPageData = $service->getThreadPosts($entry->comments_thread_id, $lastPage - 1); + $posts = array_merge($posts, $previousPageData['posts'] ?? []); + } + + return collect($posts)->slice(-$limit)->reverse()->values()->toArray(); + } catch (ConnectException|RequestException $e ){ + return []; } - return collect( $posts )->slice(-$limit)->reverse()->values()->toArray(); - }); } diff --git a/app/Services/XenforoApiService.php b/app/Services/XenforoApiService.php index ed99cb9..04ffeb8 100644 --- a/app/Services/XenforoApiService.php +++ b/app/Services/XenforoApiService.php @@ -25,7 +25,7 @@ class XenforoApiService { */ private function get(string $endpoint, ?int $customUserId = null ): mixed { - $response = Http::timeout(30)->withOptions(['verify' => false])->withHeaders([ + $response = Http::timeout(8)->connectTimeout(4)->withOptions(['verify' => false])->withHeaders([ 'XF-Api-Key' => $this->apiKey, 'XF-Api-User' => $customUserId ?? $this->superUserId, ])->get("{$this->apiUrl}/{$endpoint}"); @@ -40,7 +40,7 @@ class XenforoApiService { private function post(string $endpoint, ?int $customUserId = null, array $data = [] ): mixed { - $response = Http::timeout(30)->withOptions(['verify' => false])->withHeaders([ + $response = Http::timeout(8)->connectTimeout(4)->withOptions(['verify' => false])->withHeaders([ 'XF-Api-Key' => $this->apiKey, 'XF-Api-User' => $customUserId ?? $this->superUserId, ])->post("{$this->apiUrl}/{$endpoint}", $data); @@ -55,7 +55,7 @@ class XenforoApiService { private function delete(string $endpoint, ?int $customUserId = null, array $data = [] ): mixed { - $response = Http::timeout(30)->withOptions(['verify' => false])->withHeaders([ + $response = Http::timeout(8)->connectTimeout(4)->withOptions(['verify' => false])->withHeaders([ 'XF-Api-Key' => $this->apiKey, 'XF-Api-User' => $customUserId ?? $this->superUserId, ])->delete("{$this->apiUrl}/{$endpoint}", $data);