From 15beab89f5362b01a9718ae27578b2d1908b1f88 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Sat, 18 Jul 2026 15:00:24 +0200 Subject: [PATCH] fix: avatars in dashboard widgets Signed-off-by: Roberto Guido --- lib/Dashboard/MailWidget.php | 51 +++++++++++++++---- .../Dashboard/ImportantMailWidgetTest.php | 3 ++ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/lib/Dashboard/MailWidget.php b/lib/Dashboard/MailWidget.php index 3d3be48311..9a99b8a168 100644 --- a/lib/Dashboard/MailWidget.php +++ b/lib/Dashboard/MailWidget.php @@ -10,6 +10,7 @@ namespace OCA\Mail\Dashboard; use OCA\Mail\AppInfo\Application; +use OCA\Mail\Contracts\IAvatarService; use OCA\Mail\Contracts\IMailSearch; use OCA\Mail\Db\Message; use OCA\Mail\Exception\ClientException; @@ -40,6 +41,7 @@ public function __construct( IL10N $l10n, IURLGenerator $urlGenerator, IUserManager $userManager, + private IAvatarService $avatarService, protected AccountService $accountService, protected IMailSearch $mailSearch, IInitialState $initialState, @@ -128,6 +130,42 @@ protected function getMailboxIdsToExclude(string $userId): array { return array_values(array_filter($mailboxIdsToExclude)); } + protected function getAvatar($userId, $from) { + $url = null; + + if ($from) { + $email = $from->getEmail(); + + $avatar = $this->avatarService->getAvatar($email, $userId); + if ($avatar) { + if ($avatar->isExternal()) { + $url = $this->urlGenerator->getAbsoluteURL( + $this->urlGenerator->linkToRoute('mail.avatars.image', [ + 'email' => $email, + ]) + ); + } else { + $url = $avatar->getUrl(); + } + } + } + + if ($url == null) { + $url = $this->urlGenerator->getAbsoluteURL( + $this->urlGenerator->linkToRoute('core.GuestAvatar.getAvatar', [ + 'guestName' => $from + ? ($from->getLabel() + ? str_replace('/', '-', $from->getLabel()) + : $from->getEmail()) + : '', + 'size' => 44, + ]) + ); + } + + return $url; + } + /** * @inheritDoc */ @@ -137,7 +175,7 @@ public function getItems(string $userId, ?string $since = null, int $limit = 7): $emails = $this->getEmails($userId, $intSince, $limit); /** @var list */ - return array_map(function (Message $email) { + return array_map(function (Message $email) use ($userId) { $firstFrom = $email->getFrom()->first(); return new WidgetItem( $firstFrom ? $firstFrom->getLabel() : '', @@ -145,16 +183,7 @@ public function getItems(string $userId, ?string $since = null, int $limit = 7): $this->urlGenerator->getAbsoluteURL( $this->urlGenerator->linkToRoute('mail.page.thread', ['mailboxId' => $email->getMailboxId(), 'id' => $email->getId()]) ), - $this->urlGenerator->getAbsoluteURL( - $this->urlGenerator->linkToRoute('core.GuestAvatar.getAvatar', [ - 'guestName' => $firstFrom - ? ($firstFrom->getLabel() - ? str_replace('/', '-', $firstFrom->getLabel()) - : $firstFrom->getEmail()) - : '', - 'size' => 44, - ]) - ), + $this->getAvatar($userId, $firstFrom), (string)$email->getSentAt() ); }, $emails); diff --git a/tests/Unit/Dashboard/ImportantMailWidgetTest.php b/tests/Unit/Dashboard/ImportantMailWidgetTest.php index 9c0c0a3448..2f7ffbe49c 100644 --- a/tests/Unit/Dashboard/ImportantMailWidgetTest.php +++ b/tests/Unit/Dashboard/ImportantMailWidgetTest.php @@ -15,6 +15,7 @@ use OCA\Mail\Db\MailAccount; use OCA\Mail\Db\Message; use OCA\Mail\Service\AccountService; +use OCA\Mail\Service\AvatarService; use OCA\Mail\Service\Search\GlobalSearchQuery; use OCA\Mail\Service\Search\MailSearch; use OCA\Mail\Service\Search\SearchQuery as MailSearchQuery; @@ -41,6 +42,7 @@ protected function setUp(): void { $this->l10n = $this->createMock(IL10N::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->userManager = $this->createMock(IUserManager::class); + $this->avatarService = $this->createMock(AvatarService::class); $this->accountService = $this->createMock(AccountService::class); $this->mailSearch = $this->createMock(MailSearch::class); $this->initialState = $this->createMock(IInitialState::class); @@ -49,6 +51,7 @@ protected function setUp(): void { $this->l10n, $this->urlGenerator, $this->userManager, + $this->avatarService, $this->accountService, $this->mailSearch, $this->initialState,