From ed2b3b7667b8ea2c65bd151b4fe0c75364418eb8 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Mon, 15 Mar 2021 14:50:31 +0100 Subject: [PATCH] fix: fix the adorable url migration (#4963) --- app/Models/Contact/Contact.php | 31 ++++++++++++++++ .../2021_01_10_235600_update_adorable_api.php | 28 --------------- .../2021_01_10_235601_update_adorable_api.php | 35 +++++++++++++++++++ tests/Unit/Models/ContactTest.php | 6 ++-- 4 files changed, 70 insertions(+), 30 deletions(-) delete mode 100644 database/migrations/2021_01_10_235600_update_adorable_api.php create mode 100644 database/migrations/2021_01_10_235601_update_adorable_api.php diff --git a/app/Models/Contact/Contact.php b/app/Models/Contact/Contact.php index 4a0cb3bb42a..93c25f17167 100644 --- a/app/Models/Contact/Contact.php +++ b/app/Models/Contact/Contact.php @@ -1033,6 +1033,37 @@ public function getAvatarDefaultURL() } } + /** + * Get the adorable avatar URL. + * + * @param string|null $value + * @return string|null + */ + public function getAvatarAdorableUrlAttribute(?string $value): ?string + { + if (isset($value) && $value !== '') { + return Str::of($value) + ->ltrim('/') + ->start(Str::finish(config('monica.adorable_api'), '/')); + } + + return null; + } + + /** + * Set the adorable avatar URL. + * + * @param string|null $value + * @return void + */ + public function setAvatarAdorableUrlAttribute(?string $value) + { + if (isset($value) && $value !== '') { + $value = Str::of($value)->replace(Str::finish(config('monica.adorable_api'), '/'), ''); + } + $this->attributes['avatar_adorable_url'] = $value; + } + /** * Returns the URL of the avatar, properly sized. * The avatar can come from 4 sources: diff --git a/database/migrations/2021_01_10_235600_update_adorable_api.php b/database/migrations/2021_01_10_235600_update_adorable_api.php deleted file mode 100644 index 09869795dc0..00000000000 --- a/database/migrations/2021_01_10_235600_update_adorable_api.php +++ /dev/null @@ -1,28 +0,0 @@ -where('avatar_adorable_url', 'like', '%api.adorable.io%') - ->chunk(1000, function ($contacts) use ($adorable_api) { - foreach ($contacts as $contact) { - $contact->update([ - 'avatar_adorable_url' => Str::of($contact->avatar_adorable_url)->replace('https://api.adorable.io/avatars/', $adorable_api), - ]); - } - }); - } -} diff --git a/database/migrations/2021_01_10_235601_update_adorable_api.php b/database/migrations/2021_01_10_235601_update_adorable_api.php new file mode 100644 index 00000000000..85935d7efc9 --- /dev/null +++ b/database/migrations/2021_01_10_235601_update_adorable_api.php @@ -0,0 +1,35 @@ +where('avatar_adorable_url', 'like', 'https://api.adorable.io%') + ->orWhere('avatar_adorable_url', 'like', "$adorable_api%") + ->select('id', 'avatar_adorable_url') + ->chunkById(1000, function ($contacts) use ($adorable_api) { + foreach ($contacts as $contact) { + $adorable_url = Str::of($contact->avatar_adorable_url) + ->replace('https://api.adorable.io/avatars/', '') + ->replace($adorable_api, ''); + DB::table('contacts') + ->where('id', $contact->id) + ->update([ + 'avatar_adorable_url' => (string) $adorable_url, + ]); + } + }); + } +} diff --git a/tests/Unit/Models/ContactTest.php b/tests/Unit/Models/ContactTest.php index fd94e6c84d9..645a6fb52b0 100644 --- a/tests/Unit/Models/ContactTest.php +++ b/tests/Unit/Models/ContactTest.php @@ -515,6 +515,8 @@ public function it_sets_a_default_avatar_color() /** @test */ public function it_returns_the_url_of_the_avatar() { + config(['monica.adorable_api' => 'adorable_api']); + // default $contact = factory(Contact::class)->create([ 'avatar_default_url' => 'defaultURL', @@ -528,12 +530,12 @@ public function it_returns_the_url_of_the_avatar() // adorable $contact = factory(Contact::class)->create([ - 'avatar_adorable_url' => 'adorableURL', + 'avatar_adorable_url' => 'adorable_api/adorableURL', 'avatar_source' => 'adorable', ]); $this->assertEquals( - 'adorableURL', + 'adorable_api/adorableURL', $contact->getAvatarURL() );