diff --git a/lib/IMAP/PreviewEnhancer.php b/lib/IMAP/PreviewEnhancer.php index f115307d1a..d87f861c18 100644 --- a/lib/IMAP/PreviewEnhancer.php +++ b/lib/IMAP/PreviewEnhancer.php @@ -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 { + $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); } - return $carry; } - - return array_merge($carry, [$message->getUid()]); - }, []); + } if ($needAnalyze === []) { // Nothing to enhance @@ -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; @@ -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)); } diff --git a/lib/Service/Search/MailSearch.php b/lib/Service/Search/MailSearch.php index 917c7eccc0..3efb8e32b5 100644 --- a/lib/Service/Search/MailSearch.php +++ b/lib/Service/Search/MailSearch.php @@ -118,7 +118,8 @@ public function findMessages(Account $account, $this->messageMapper->findByIds($account->getUserId(), $this->getIdsLocally($account, $mailbox, $query, $sortOrder, $limit), $sortOrder, - ) + ), + 'apiCall' ); }