Skip to content

Commit

Permalink
fixup! perf: reduce number of avatar requests
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Dec 13, 2024
1 parent d4f5514 commit 147633a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
28 changes: 14 additions & 14 deletions lib/IMAP/PreviewEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,26 @@ public function __construct(IMAPClientFactory $clientFactory,
*
* @return Message[]
*/
public function process(Account $account, Mailbox $mailbox, array $messages): array {
$needAnalyze = array_reduce($messages, function (array $carry, Message $message) {
public function process(Account $account, Mailbox $mailbox, array $messages, string $mode = 'sync'): array {

Check warning on line 63 in lib/IMAP/PreviewEnhancer.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/PreviewEnhancer.php#L63

Added line #L63 was not covered by tests
$needAnalyze = array_reduce($messages, static function (array $carry, Message $message) {
if ($message->getStructureAnalyzed()) {
// Try fetching the avatar if it's not set
// Nothing to do
return $carry;
}

return array_merge($carry, [$message->getUid()]);
}, []);

// If we are in the API call, we need to fetch the avatar for the sender
if ($mode === 'apiCall') {
foreach ($messages as $message) {
$from = $message->getFrom()->first() ;
if ($message->getAvatar() === null && $from !== null && $from->getEmail() !== null && $this->userId !== null) {
$avatar = $this->avatarService->getAvatar($from->getEmail(), $this->userId);
$message->setAvatar($avatar);

Check warning on line 79 in lib/IMAP/PreviewEnhancer.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/PreviewEnhancer.php#L74-L79

Added lines #L74 - L79 were not covered by tests
}
return $carry;
}

return array_merge($carry, [$message->getUid()]);
}, []);
}

if ($needAnalyze === []) {
// Nothing to enhance
Expand All @@ -99,7 +105,7 @@ public function process(Account $account, Mailbox $mailbox, array $messages): ar
$client->logout();
}

return $this->mapper->updatePreviewDataBulk(...array_map(function (Message $message) use ($data) {
return $this->mapper->updatePreviewDataBulk(...array_map(static function (Message $message) use ($data) {
if (!array_key_exists($message->getUid(), $data)) {
// Nothing to do
return $message;
Expand All @@ -113,12 +119,6 @@ public function process(Account $account, Mailbox $mailbox, array $messages): ar
$message->setEncrypted($structureData->isEncrypted());
$message->setMentionsMe($structureData->getMentionsMe());

$from = $message->getFrom()->first() ;
if ($message->getAvatar() === null && $from !== null && $from->getEmail() !== null && $this->userId !== null) {
$avatar = $this->avatarService->getAvatar($from->getEmail(), $this->userId);
$message->setAvatar($avatar);
}

return $message;
}, $messages));
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Service/Search/MailSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function findMessages(Account $account,
$this->messageMapper->findByIds($account->getUserId(),
$this->getIdsLocally($account, $mailbox, $query, $sortOrder, $limit),
$sortOrder,
)
),
'apiCall'
);
}

Expand Down

0 comments on commit 147633a

Please sign in to comment.