Skip to content

Commit

Permalink
Don’t fetch contact avatars from outside
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Aug 11, 2024
1 parent 5027f4a commit 0fb5ac6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Changed

- Contact avatars are now fetched from unavatar.io instead of gravatar.com, and a fallback SVG with initials is used if none is found.
- Contact avatars are no longer fetched from gravatar.com. Instead, a synced user profile photo is used, if one exists, falling back to an SVG with initials.

## 3.4.2 - 2024-08-02

Expand Down
56 changes: 40 additions & 16 deletions src/elements/ContactElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ public static function hasStatuses(): bool
return true;
}

/**
* @inheritdoc
*/
public static function hasThumbs(): bool
{
return true;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -895,32 +903,48 @@ public function getStatus(): ?string
}

/**
* Returns an image from unavatar.io, with an SVG behind it as a fallback.
*
* @see User::getThumbHtml()
* @inheritdoc
*/
public function getThumbHtml(int $size): ?string
protected function thumbUrl(int $size): ?string
{
$value = strtolower(trim($this->email));
$image = Html::img('https://unavatar.io/' . $value . '?fallback=false', [
'width' => $size,
'height' => $size,
]);
$user = $this->getUser();

if ($user) {
return $user->thumbUrl($size);
}

return null;
}

/**
* @inheritdoc
*/
protected function thumbSvg(): ?string
{
$initials = mb_strtoupper(mb_substr($this->email, 0, 2));

$initials = mb_strtoupper(mb_substr(str_replace('@', '', $value), 0, 2));
$svg = <<<XML
return <<<XML
<svg version="1.1" baseProfile="full" width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="50" fill="var(--gray-100)"/>
<text x="50" y="66" font-size="46" font-weight="500" font-family="sans-serif" text-anchor="middle" fill="var(--gray-700)">$initials</text>
</svg>
XML;
}

$options = [
'class' => 'thumb rounded',
];
/**
* @inheritdoc
*/
protected function thumbAlt(): ?string
{
return $this->email;
}

return Html::tag('div', $svg, $options)
. Html::tag('div', $image, $options + ['style' => 'position: absolute']);
/**
* @inheritdoc
*/
protected function hasRoundedThumb(): bool
{
return true;
}

/**
Expand Down

0 comments on commit 0fb5ac6

Please sign in to comment.