fix: user avatar sometimes shows up as gravatar

This commit is contained in:
Phan An 2024-06-04 11:43:26 +02:00
parent 1cb1125183
commit ee4c97168b
3 changed files with 16 additions and 12 deletions

View file

@ -99,6 +99,19 @@ function gravatar(string $email, int $size = 192): string
return sprintf("https://www.gravatar.com/avatar/%s?s=$size&d=robohash", md5(Str::lower($email)));
}
function avatar_or_gravatar(?string $avatar, string $email): string
{
if (!$avatar) {
return gravatar($email);
}
if (Str::startsWith($avatar, ['http://', 'https://'])) {
return $avatar;
}
return user_avatar_url($avatar);
}
/**
* A quick check to determine if a mailer is configured.
* This is not bulletproof but should work in most cases.

View file

@ -24,7 +24,7 @@ class CollaborativeSongResource extends SongResource
PlaylistCollaborator::make(
$this->song->collaborator_id,
$this->song->collaborator_name,
gravatar($this->song->collaborator_email),
avatar_or_gravatar($this->song->collaborator_avatar, $this->song->collaborator_email),
),
),
'added_at' => $this->song->added_at,

View file

@ -16,7 +16,6 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Sanctum\PersonalAccessToken;
@ -109,20 +108,12 @@ class User extends Authenticatable
protected function avatar(): Attribute
{
return Attribute::get(function (): string {
$avatar = Arr::get($this->attributes, 'avatar');
if (Str::startsWith($avatar, ['http://', 'https://'])) {
return $avatar;
}
return $avatar ? user_avatar_url($avatar) : gravatar($this->email);
});
return Attribute::get(fn (): string => avatar_or_gravatar(Arr::get($this->attributes, 'avatar'), $this->email));
}
protected function hasCustomAvatar(): Attribute
{
return Attribute::get(fn (): bool => (bool) $this->attributes['avatar']);
return Attribute::get(fn (): bool => (bool) Arr::get($this->attributes, 'avatar'));
}
protected function isProspect(): Attribute