Skip to content
Draft
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
62 changes: 37 additions & 25 deletions lib/IMAP/PreviewEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
use Psr\Log\LoggerInterface;
use function array_key_exists;
use function array_map;
use function array_merge;
use function array_reduce;

class PreviewEnhancer {
/** @var IMAPClientFactory */
Expand Down Expand Up @@ -60,44 +58,30 @@
*
* @return Message[]
*/
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
return $carry;
}
public function process(Account $account, Mailbox $mailbox, array $messages, bool $preLoadAvatars = false): array {
$needAnalyze = [];

return array_merge($carry, [$message->getUid()]);
}, []);
$client = $this->clientFactory->getClient($account);

foreach ($messages as $message) {
if ($message->getStructureAnalyzed() === false || $message->getStructureAnalyzed() === null) {
$needAnalyze[] = $message->getUid();
}

$attachments = $this->attachmentService->getAttachmentNames($account, $mailbox, $message, $client);
$message->setAttachments($attachments);
}

if ($preLoadAvatars) {
foreach ($messages as $message) {
$from = $message->getFrom()->first();
if ($message->getAvatar() === null && $from !== null && $from->getEmail() !== null && $userId !== null) {
$avatar = $this->avatarService->getCachedAvatar($from->getEmail(), $userId);
if ($avatar === null) {
$message->setFetchAvatarFromClient(true);
}
if ($avatar instanceof Avatar) {
$message->setAvatar($avatar);
}

}
if ($preLoadAvatars) {
$this->preLoadAvatar($message, $account->getUserId());
}
}

if ($needAnalyze === []) {
// Nothing to enhance
$client->logout();
return $messages;
}


try {
$data = $this->imapMapper->getBodyStructureData(
$client,
Expand Down Expand Up @@ -133,4 +117,32 @@
return $message;
}, $messages));
}

private function preLoadAvatar(Message $message, string $userId): void {
if ($message->getAvatar() !== null) {
return;
}

// Only allow client-side fetching once we confirm a cache miss.
$message->setFetchAvatarFromClient(false);

$from = $message->getFrom()->first();
if ($from === null) {
return;
}

try {
$email = $from->getEmail();
} catch (\Exception) {
return;
}

$avatar = $this->avatarService->getCachedAvatar($email, $userId);

Check failure on line 140 in lib/IMAP/PreviewEnhancer.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable33

PossiblyNullArgument

lib/IMAP/PreviewEnhancer.php:140:51: PossiblyNullArgument: Argument 1 of OCA\Mail\Service\AvatarService::getCachedAvatar cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 140 in lib/IMAP/PreviewEnhancer.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable32

PossiblyNullArgument

lib/IMAP/PreviewEnhancer.php:140:51: PossiblyNullArgument: Argument 1 of OCA\Mail\Service\AvatarService::getCachedAvatar cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 140 in lib/IMAP/PreviewEnhancer.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/IMAP/PreviewEnhancer.php:140:51: PossiblyNullArgument: Argument 1 of OCA\Mail\Service\AvatarService::getCachedAvatar cannot be null, possibly null value provided (see https://psalm.dev/078)
if ($avatar instanceof Avatar) {
$message->setAvatar($avatar);
return;
}

$message->setFetchAvatarFromClient(true);
}
}
1 change: 0 additions & 1 deletion lib/Service/Search/MailSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public function findMessages(Account $account,
$sortOrder,
),
true,
$userId
);
}

Expand Down
Loading