Skip to content

Commit

Permalink
refactor: allow to setup your own adorable api url (#4899)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Feb 20, 2021
1 parent c1eb71c commit ab4019e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 2 additions & 3 deletions app/Services/Contact/Avatar/GetAdorableAvatarURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace App\Services\Contact\Avatar;

use Illuminate\Support\Str;
use App\Services\BaseService;

class GetAdorableAvatarURL extends BaseService
{
private const ADORABLE_API = 'https://api.hello-avatar.com/adorables/';

/**
* Get the validation rules that apply to the service.
*
Expand All @@ -34,7 +33,7 @@ public function execute(array $data)

$size = $this->size($data);

return self::ADORABLE_API.$size.'/'.$data['uuid'].'.png';
return Str::finish(config('monica.adorable_api'), '/').$size.'/'.$data['uuid'].'.png';
}

/**
Expand Down
13 changes: 13 additions & 0 deletions config/monica.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,17 @@
| The default avatar size.
*/
'avatar_size' => 200,

/*
|--------------------------------------------------------------------------
| Default adorable api url
|--------------------------------------------------------------------------
|
| The default adorable api url.
|
| You can host your own version, see https://github.com/itsthatguy/avatars-api-middleware
| or https://hub.docker.com/r/aldrio/adorable-avatars.
*/
'adorable_api' => env('ADORABLE_API', 'https://api.hello-avatar.com/adorables/'),

];
10 changes: 7 additions & 3 deletions database/migrations/2021_01_10_235600_update_adorable_api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Str;
use App\Models\Contact\Contact;
use Illuminate\Database\Migrations\Migration;

Expand All @@ -12,12 +13,15 @@ class UpdateAdorableAPI extends Migration
*/
public function up()
{
$adorable_api = Str::finish(config('monica.adorable_api'), '/');

Contact::without(['account', 'avatarPhoto', 'gender'])
->where('avatar_adorable_url', 'like', '%api.adorable.io%')
->chunk(1000, function ($contacts) {
->chunk(1000, function ($contacts) use ($adorable_api) {
foreach ($contacts as $contact) {
$contact->avatar_adorable_url = str_replace('api.adorable.io/avatars', 'api.hello-avatar.com/adorables', $contact->avatar_adorable_url);
$contact->save();
$contact->update([
'avatar_adorable_url' => Str::of($contact->avatar_adorable_url)->replace('https://api.adorable.io/avatars/', $adorable_api),
]);
}
});
}
Expand Down

0 comments on commit ab4019e

Please sign in to comment.