Skip to content

Commit

Permalink
feat: re-activate adorable avatars with permanent solution (#5872)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jan 11, 2022
1 parent 0396c28 commit ccf6d4f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 38 deletions.
34 changes: 17 additions & 17 deletions app/Models/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Prunable;
use App\Models\ModelBindingHasher as Model;
use LaravelAdorable\Facades\LaravelAdorable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Filesystem\Filesystem;
Expand Down Expand Up @@ -1122,27 +1123,26 @@ public function getAvatarDefaultURL()
public function getAvatarAdorableUrlAttribute(?string $value): ?string
{
if (isset($value) && $value !== '') {
return Str::of($value)
$url = Str::of($value)
->after('https://api.adorable.io/avatars/')
->ltrim('/')
->start(Str::finish(config('monica.adorable_api'), '/'));
}
->ltrim('/');

return null;
}
$data = Str::of($url)->split('/\//');

/**
* 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'), '/'), '');
if (! ctype_digit($data[0]) || count($data) === 1) {
$size = config('monica.avatar_size');
$hash = $data[0];
} else {
$size = (int) $data[0];
$hash = $data[1];
}

$hash = Str::of($hash)->split('/\.png/')[0];

return LaravelAdorable::get($size, $hash);
}
$this->attributes['avatar_adorable_url'] = $value;

return null;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions app/Services/Contact/Avatar/GetAdorableAvatarURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Services\Contact\Avatar;

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

class GetAdorableAvatarURL extends BaseService
Expand Down Expand Up @@ -33,7 +32,7 @@ public function execute(array $data)

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

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

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ext-gmp": "*",
"ext-intl": "*",
"ext-redis": "*",
"asbiin/laravel-adorable": "^1.0",
"asbiin/laravel-webauthn": "^1.0",
"bacon/bacon-qr-code": "^2.0",
"creativeorange/gravatar": "^1.0",
Expand Down
79 changes: 77 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions config/monica.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,4 @@
*/
'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 tests/Unit/Models/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use App\Models\User\User;
use Mockery\MockInterface;
use Tests\FeatureTestCase;
use App\Helpers\DateHelper;
use App\Models\Contact\Debt;
Expand All @@ -24,6 +25,7 @@
use App\Models\Relationship\Relationship;
use App\Jobs\StayInTouch\ScheduleStayInTouch;
use App\Models\Relationship\RelationshipType;
use LaravelAdorable\Adorable\LaravelAdorable;
use App\Models\Relationship\RelationshipTypeGroup;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Notification as NotificationFacade;
Expand Down Expand Up @@ -515,7 +517,9 @@ public function it_sets_a_default_avatar_color()
/** @test */
public function it_returns_the_url_of_the_avatar()
{
config(['monica.adorable_api' => 'adorable_api']);
$this->mock(LaravelAdorable::class, function (MockInterface $mock) {
$mock->shouldReceive('get')->andReturn('adorableURL');
});

// default
$contact = factory(Contact::class)->create([
Expand All @@ -530,12 +534,12 @@ public function it_returns_the_url_of_the_avatar()

// adorable
$contact = factory(Contact::class)->create([
'avatar_adorable_url' => 'adorable_api/adorableURL',
'avatar_adorable_url' => 'adorableURL',
'avatar_source' => 'adorable',
]);

$this->assertEquals(
'adorable_api/adorableURL',
'adorableURL',
$contact->getAvatarURL()
);

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Services/Contact/Avatar/GetAdorableAvatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function it_returns_an_url()
$url = app(GetAdorableAvatarURL::class)->execute($request);

$this->assertEquals(
'https://api.hello-avatar.com/adorables/400/matt@wordpress.com.png',
'400/matt@wordpress.com.png',
$url
);
}
Expand All @@ -38,7 +38,7 @@ public function it_returns_an_url_with_a_default_avatar_size()

// should return an avatar of 200 px wide
$this->assertEquals(
'https://api.hello-avatar.com/adorables/200/matt@wordpress.com.png',
'200/matt@wordpress.com.png',
$url
);
}
Expand Down

0 comments on commit ccf6d4f

Please sign in to comment.