Skip to content

Commit

Permalink
fix: fix the adorable url migration (#4963)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Mar 15, 2021
1 parent 19117ed commit ed2b3b7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 30 deletions.
31 changes: 31 additions & 0 deletions app/Models/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 0 additions & 28 deletions database/migrations/2021_01_10_235600_update_adorable_api.php

This file was deleted.

35 changes: 35 additions & 0 deletions database/migrations/2021_01_10_235601_update_adorable_api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;

class UpdateAdorableAPI extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$adorable_api = Str::finish(config('monica.adorable_api'), '/');

DB::table('contacts')
->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,
]);
}
});
}
}
6 changes: 4 additions & 2 deletions tests/Unit/Models/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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()
);

Expand Down

0 comments on commit ed2b3b7

Please sign in to comment.