From cce575c1a6dc0ad0c5beeab826748a45e6db828f Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 25 Mar 2020 10:58:03 -0400 Subject: [PATCH] Fix avatar/gravatar call throwing error --- app/Http/Controllers/Api/UserController.php | 2 +- app/Http/Resources/User.php | 13 +++++++++++-- app/Models/User.php | 4 +--- tests/SimBriefTest.php | 6 ++++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index 6e751b7e..60fc0b15 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -90,7 +90,7 @@ class UserController extends Controller public function get($id) { $user = $this->userRepo - ->with(['airline', 'bids']) + ->with(['airline', 'bids', 'rank']) ->find($id); return new UserResource($user); diff --git a/app/Http/Resources/User.php b/app/Http/Resources/User.php index 49649bd1..10867feb 100644 --- a/app/Http/Resources/User.php +++ b/app/Http/Resources/User.php @@ -15,7 +15,6 @@ class User extends Resource $res = [ 'id' => $this->id, 'pilot_id' => $this->pilot_id, - 'avatar' => $this->avatar->url, 'ident' => $this->ident, 'name' => $this->name, 'email' => $this->email, @@ -26,12 +25,22 @@ class User extends Resource 'flight_time' => $this->flight_time, 'timezone' => $this->timezone, 'state' => $this->state, - 'rank' => Rank::make($this->rank), ]; $res['airline'] = Airline::make($this->airline); $res['bids'] = UserBid::collection($this->whenLoaded('bids')); $res['flights'] = new FlightResource($this->whenLoaded('flights')); + $res['rank'] = Rank::make($this->rank); + + /* + * Determine which avatar to send/use + */ + $res['avatar'] = $this->avatar; + if (empty($res['avatar'])) { + $res['avatar'] = $this->gravatar(); + } else { + $res['avatar'] = $res['avatar']->url; + } return $res; } diff --git a/app/Models/User.php b/app/Models/User.php index 671406fa..d7111cd3 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -143,9 +143,7 @@ class User extends Authenticatable public function getAvatarAttribute() { if (!$this->attributes['avatar']) { - return new File([ - 'path' => $this->gravatar(), - ]); + return null; } return new File([ diff --git a/tests/SimBriefTest.php b/tests/SimBriefTest.php index 0176193e..f6d13d13 100644 --- a/tests/SimBriefTest.php +++ b/tests/SimBriefTest.php @@ -85,9 +85,11 @@ class SimBriefTest extends TestCase $this->assertNotNull($flight['simbrief']); $this->assertEquals($briefing->id, $flight['simbrief']['id']); + + $url = str_replace('http://', 'https://', $flight['simbrief']['url']); $this->assertEquals( - 'http://localhost/api/flights/'.$briefing->id.'/briefing', - $flight['simbrief']['url'] + 'https://localhost/api/flights/'.$briefing->id.'/briefing', + $url ); // Retrieve the briefing via API, and then check the doctype