Skip to content
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

perf(imap): FETCH only flags for partial message updates #8453

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
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
59 changes: 55 additions & 4 deletions lib/IMAP/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use Psr\Log\LoggerInterface;
use function array_filter;
use function array_map;
use function array_values;
use function count;
use function fclose;
use function in_array;
Expand Down Expand Up @@ -236,6 +237,32 @@ static function (int $uid) use ($highestKnownUid) {
];
}

/**
* @param Horde_Imap_Client_Base $client
* @param string $mailbox
* @param int[]|Horde_Imap_Client_Ids $ids
* @param string $userId
* @param bool $loadBody
* @return IMAPMessage[]
*
* @throws DoesNotExistException
* @throws Horde_Imap_Client_Exception
* @throws Horde_Imap_Client_Exception_NoSupportExtension
* @throws Horde_Mime_Exception
* @throws ServiceException
*/
public function findFlagsByIds(Horde_Imap_Client_Base $client,
string $mailbox,
array|Horde_Imap_Client_Ids $ids,
string $userId,
bool $loadBody = false): array {
$query = new Horde_Imap_Client_Fetch_Query();
$query->uid();
$query->flags();

return $this->findByQuery($ids, $query, $mailbox, $client, $loadBody, $userId);
}

/**
* @param Horde_Imap_Client_Base $client
* @param string $mailbox
Expand All @@ -252,7 +279,7 @@ static function (int $uid) use ($highestKnownUid) {
*/
public function findByIds(Horde_Imap_Client_Base $client,
string $mailbox,
$ids,
array|Horde_Imap_Client_Ids $ids,
string $userId,
bool $loadBody = false): array {
$query = new Horde_Imap_Client_Fetch_Query();
Expand All @@ -267,6 +294,30 @@ public function findByIds(Horde_Imap_Client_Base $client,
]
);

return $this->findByQuery($ids, $query, $mailbox, $client, $loadBody, $userId);
}

/**
* @param array|Horde_Imap_Client_Ids $ids
* @param Horde_Imap_Client_Fetch_Query $query
* @param string $mailbox
* @param Horde_Imap_Client_Base $client
* @param bool $loadBody
* @param string $userId
*
* @return array|IMAPMessage[]
* @throws DoesNotExistException
* @throws Horde_Imap_Client_Exception
* @throws Horde_Imap_Client_Exception_NoSupportExtension
* @throws Horde_Mime_Exception
* @throws ServiceException
*/
private function findByQuery(array|Horde_Imap_Client_Ids $ids,
Horde_Imap_Client_Fetch_Query $query,
string $mailbox,
Horde_Imap_Client_Base $client,
bool $loadBody,
string $userId): array {
if (is_array($ids)) {
// Chunk to prevent overly long IMAP commands
/** @var Horde_Imap_Client_Data_Fetch[] $fetchResults */
Expand All @@ -283,11 +334,11 @@ public function findByIds(Horde_Imap_Client_Base $client,
}

$fetchResults = array_values(array_filter($fetchResults, static function (Horde_Imap_Client_Data_Fetch $fetchResult) {
return $fetchResult->exists(Horde_Imap_Client::FETCH_ENVELOPE);
return $fetchResult->exists(Horde_Imap_Client::FETCH_UID);
}));

if ($fetchResults === []) {
$this->logger->debug("findByIds in $mailbox got " . count($ids) . " UIDs but found none");
$this->logger->debug("findByQuery in $mailbox got " . count($ids) . " UIDs but found none");
} else {
$minFetched = $fetchResults[0]->getUid();
$maxFetched = $fetchResults[count($fetchResults) - 1]->getUid();
Expand All @@ -296,7 +347,7 @@ public function findByIds(Horde_Imap_Client_Base $client,
} else {
$range = 'literals';
}
$this->logger->debug("findByIds in $mailbox got " . count($ids) . " UIDs ($range) and found " . count($fetchResults) . ". minFetched=$minFetched maxFetched=$maxFetched");
$this->logger->debug("findByQuery in $mailbox got " . count($ids) . " UIDs ($range) and found " . count($fetchResults) . ". minFetched=$minFetched maxFetched=$maxFetched");
}

return array_map(function (Horde_Imap_Client_Data_Fetch $fetchResult) use ($client, $mailbox, $loadBody, $userId) {
Expand Down
2 changes: 1 addition & 1 deletion lib/IMAP/Sync/Synchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function sync(Horde_Imap_Client_Base $imapClient,
}

$newMessages = $this->messageMapper->findByIds($imapClient, $request->getMailbox(), $newUids, $userId);
$changedMessages = $this->messageMapper->findByIds($imapClient, $request->getMailbox(), $changedUids, $userId);
$changedMessages = $this->messageMapper->findFlagsByIds($imapClient, $request->getMailbox(), $changedUids, $userId);
$vanishedMessageUids = $vanishedUids;

return new Response($newMessages, $changedMessages, $vanishedMessageUids, null);
Expand Down
Loading