From 25e5e59b56f1f7d63bde5247e821a68341ad4c9e Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Tue, 16 Nov 2021 23:26:10 +0100 Subject: [PATCH] fix: fix weather get attribute (#5705) --- app/Models/Account/Weather.php | 17 +++++++++++------ .../Geolocalization/GetGPSCoordinate.php | 16 +++++++++++----- resources/views/people/profile.blade.php | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/Models/Account/Weather.php b/app/Models/Account/Weather.php index 80a5a923d8c..fb503f60b61 100644 --- a/app/Models/Account/Weather.php +++ b/app/Models/Account/Weather.php @@ -63,9 +63,9 @@ public function place() /** * Get the weather code. * - * @return string + * @return string|null */ - public function getSummaryCodeAttribute() + public function getSummaryCodeAttribute(): ?string { $json = $this->weather_json; @@ -74,7 +74,7 @@ public function getSummaryCodeAttribute() if (($text = Arr::get($json, 'current.condition.text')) === 'Partly cloudy') { $icon = ((bool) Arr::get($json, 'current.is_day')) ? 'partly-cloudy-day' : 'partly-cloudy-night'; } else { - $icon = Str::of($text)->lower()->replace(' ', '-'); + $icon = (string) Str::of($text)->lower()->replace(' ', '-'); } } @@ -84,17 +84,22 @@ public function getSummaryCodeAttribute() /** * Get the weather summary. * - * @return string + * @return string|null */ public function getSummaryAttribute(): ?string { - return trans('app.weather_'.$this->summary_code); + $summary_code = $this->summary_code; + if (empty($summary_code)) { + return null; + } + + return trans('app.weather_'.$summary_code); } /** * Get the weather location. * - * @return string + * @return string|null */ public function getLocationAttribute(): ?string { diff --git a/app/Services/Instance/Geolocalization/GetGPSCoordinate.php b/app/Services/Instance/Geolocalization/GetGPSCoordinate.php index 390a14de064..28bec7b6e98 100644 --- a/app/Services/Instance/Geolocalization/GetGPSCoordinate.php +++ b/app/Services/Instance/Geolocalization/GetGPSCoordinate.php @@ -61,14 +61,18 @@ private function validateWeatherEnvVariables() * Build the query to send with the API call. * * @param Place $place - * @return string + * @return string|null */ - private function buildQuery(Place $place): string + private function buildQuery(Place $place): ?string { + if (($q = $place->getAddressAsString()) === null) { + return null; + } + $query = http_build_query([ 'format' => 'json', 'key' => config('monica.location_iq_api_key'), - 'q' => $place->getAddressAsString(), + 'q' => $q, ]); return Str::finish(config('location.location_iq_url'), '/').'search.php?'.$query; @@ -82,7 +86,9 @@ private function buildQuery(Place $place): string */ private function query(Place $place): ?Place { - $query = $this->buildQuery($place); + if (($query = $this->buildQuery($place)) === null) { + return null; + } try { $response = Http::get($query); @@ -96,7 +102,7 @@ private function query(Place $place): ?Place } catch (RequestException $e) { if ($e->response->status() === 429 && ($error = $e->response->json('error')) && $error === 'Rate Limited Second') { throw new RateLimitedSecondException($e); - } else { + } elseif ($e->response->status() !== 404 && $e->response->status() !== 400) { Log::error(__CLASS__.' '.__FUNCTION__.': Error making the call: '.$e->getMessage(), [ 'query' => Str::of($query)->replace(config('monica.location_iq_api_key'), '******'), $e, diff --git a/resources/views/people/profile.blade.php b/resources/views/people/profile.blade.php index dba8101e61e..c1507cc02f7 100644 --- a/resources/views/people/profile.blade.php +++ b/resources/views/people/profile.blade.php @@ -42,7 +42,7 @@
- @if (! is_null($weather)) + @if (! is_null($weather) && $weather->summary)