-
Notifications
You must be signed in to change notification settings - Fork 271
perf: reduce number of avatar requests #10486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
use OCA\Mail\Db\Message; | ||
use OCA\Mail\Db\MessageMapper as DbMapper; | ||
use OCA\Mail\IMAP\MessageMapper as ImapMapper; | ||
use OCA\Mail\Service\Avatar\Avatar; | ||
use OCA\Mail\Service\AvatarService; | ||
use Psr\Log\LoggerInterface; | ||
use function array_key_exists; | ||
use function array_map; | ||
|
@@ -34,22 +36,27 @@ class PreviewEnhancer { | |
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
/** @var AvatarService */ | ||
private $avatarService; | ||
|
||
public function __construct(IMAPClientFactory $clientFactory, | ||
ImapMapper $imapMapper, | ||
DbMapper $dbMapper, | ||
LoggerInterface $logger) { | ||
LoggerInterface $logger, | ||
AvatarService $avatarService) { | ||
$this->clientFactory = $clientFactory; | ||
$this->imapMapper = $imapMapper; | ||
$this->mapper = $dbMapper; | ||
$this->logger = $logger; | ||
$this->avatarService = $avatarService; | ||
} | ||
|
||
/** | ||
* @param Message[] $messages | ||
* | ||
* @return Message[] | ||
*/ | ||
public function process(Account $account, Mailbox $mailbox, array $messages): array { | ||
public function process(Account $account, Mailbox $mailbox, array $messages, bool $preLoadAvatars = false, ?string $userId = null): array { | ||
$needAnalyze = array_reduce($messages, static function (array $carry, Message $message) { | ||
if ($message->getStructureAnalyzed()) { | ||
// Nothing to do | ||
|
@@ -59,6 +66,22 @@ public function process(Account $account, Mailbox $mailbox, array $messages): ar | |
return array_merge($carry, [$message->getUid()]); | ||
}, []); | ||
|
||
if ($preLoadAvatars) { | ||
foreach ($messages as $message) { | ||
$from = $message->getFrom()->first(); | ||
if ($message->getAvatar() === null && $from !== null && $from->getEmail() !== null && $userId !== null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: negate the condition and use |
||
$avatar = $this->avatarService->getCachedAvatar($from->getEmail(), $userId); | ||
if ($avatar === null) { | ||
$message->setFetchAvatarFromClient(true); | ||
} | ||
if ($avatar instanceof Avatar) { | ||
$message->setAvatar($avatar); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
if ($needAnalyze === []) { | ||
// Nothing to enhance | ||
return $messages; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Unit\IMAP; | ||
|
||
use ChristophWurst\Nextcloud\Testing\TestCase; | ||
use OCA\Mail\Address; | ||
use OCA\Mail\AddressList; | ||
use OCA\Mail\Db\Message; | ||
use OCA\Mail\Db\MessageMapper as DbMapper; | ||
use OCA\Mail\IMAP\IMAPClientFactory; | ||
use OCA\Mail\IMAP\MessageMapper as ImapMapper; | ||
use OCA\Mail\IMAP\PreviewEnhancer; | ||
use OCA\Mail\Service\Avatar\Avatar; | ||
use OCA\Mail\Service\AvatarService; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class PreviewEnhancerTest extends TestCase { | ||
|
||
|
||
/** @var IMAPClientFactory|MockObject */ | ||
private $imapClientFactory; | ||
/** @var ImapMapper|MockObject */ | ||
private $imapMapper; | ||
/** @var DbMapper|MockObject */ | ||
private $dbMapper; | ||
/** @var LoggerInterface|MockObject */ | ||
private $logger; | ||
/** @var AvatarService|MockObject */ | ||
private $avatarService; | ||
/** @var PreviewEnhancer */ | ||
private $previewEnhancer; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->imapClientFactory = $this->createMock(IMAPClientFactory::class); | ||
$this->imapMapper = $this->createMock(ImapMapper::class); | ||
$this->dbMapper = $this->createMock(DbMapper::class); | ||
$this->logger = $this->createMock(LoggerInterface::class); | ||
$this->avatarService = $this->createMock(AvatarService::class); | ||
|
||
$this->previewEnhancer = new previewEnhancer($this->imapClientFactory, | ||
$this->imapMapper, | ||
$this->dbMapper, | ||
$this->logger, | ||
$this->avatarService); | ||
} | ||
|
||
public function testAvatars(): void { | ||
|
||
$message1 = new Message(); | ||
$message1->setId(1); | ||
$message1->setStructureAnalyzed(true); | ||
$message1->setFrom(new AddressList([Address::fromRaw('Alice', 'alice@example.com')])); | ||
$message2 = new Message(); | ||
$message2->setId(2); | ||
$message2->setStructureAnalyzed(true); | ||
$message2->setFrom(new AddressList([Address::fromRaw('Bob', 'bob@example.com')])); | ||
$messages = [$message1, $message2]; | ||
$message2Avatar = new Avatar('example.com', 'image/png', true); | ||
$this->avatarService->expects($this->exactly(2)) | ||
->method('getCachedAvatar') | ||
->withConsecutive( | ||
['alice@example.com', 'testuser'], | ||
['bob@example.com', 'testuser'] | ||
) | ||
->willReturnOnConsecutiveCalls( | ||
null, | ||
$message2Avatar | ||
); | ||
$this->previewEnhancer->process( | ||
$this->createMock(\OCA\Mail\Account::class), | ||
$this->createMock(\OCA\Mail\Db\Mailbox::class), | ||
$messages, | ||
true, | ||
'testuser' | ||
); | ||
$this->assertTrue($message1->jsonSerialize()['fetchAvatarFromClient']); | ||
$this->assertFalse($message2->jsonSerialize()['fetchAvatarFromClient']); | ||
$this->assertNull($message1->getAvatar()); | ||
$this->assertSame($message2Avatar, $message2->getAvatar()); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.