Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions lib/Dashboard/MailWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +41,7 @@ public function __construct(
IL10N $l10n,
IURLGenerator $urlGenerator,
IUserManager $userManager,
private IAvatarService $avatarService,
protected AccountService $accountService,
protected IMailSearch $mailSearch,
IInitialState $initialState,
Expand Down Expand Up @@ -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
*/
Expand All @@ -137,24 +175,15 @@ public function getItems(string $userId, ?string $since = null, int $limit = 7):
$emails = $this->getEmails($userId, $intSince, $limit);

/** @var list<WidgetItem> */
return array_map(function (Message $email) {
return array_map(function (Message $email) use ($userId) {
$firstFrom = $email->getFrom()->first();
return new WidgetItem(
$firstFrom ? $firstFrom->getLabel() : '',
$email->getSubject(),
$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);
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/Dashboard/ImportantMailWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -49,6 +51,7 @@ protected function setUp(): void {
$this->l10n,
$this->urlGenerator,
$this->userManager,
$this->avatarService,
$this->accountService,
$this->mailSearch,
$this->initialState,
Expand Down
Loading