diff options
author | Richard Steinmetz <richard@steinmetz.cloud> | 2021-12-17 19:11:27 +0300 |
---|---|---|
committer | Richard Steinmetz <richard@steinmetz.cloud> | 2022-03-28 12:44:36 +0300 |
commit | 6efad68afae361eee60b3850c29680038edb4657 (patch) | |
tree | 887d475301c5d3a8431535aa313a1264dac24560 /lib | |
parent | fdfce08e2a05f401e55cb30776366af6f1870e9f (diff) |
Load itineraries asynchronouslyenh/4823/async-itineraries
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/Controller/MessagesController.php | 32 | ||||
-rw-r--r-- | lib/Service/ItineraryService.php | 28 |
2 files changed, 44 insertions, 16 deletions
diff --git a/lib/Controller/MessagesController.php b/lib/Controller/MessagesController.php index e3cc7a3e7..f2f083d38 100755 --- a/lib/Controller/MessagesController.php +++ b/lib/Controller/MessagesController.php @@ -11,6 +11,7 @@ declare(strict_types=1); * @author Lukas Reschke <lukas@owncloud.com> * @author Thomas Imbreckx <zinks@iozero.be> * @author Thomas Müller <thomas.mueller@tmit.eu> + * @author Richard Steinmetz <richard@steinmetz.cloud> * * Mail * @@ -248,11 +249,10 @@ class MessagesController extends Controller { $message->getUid(), true )->getFullMessage($id); - $json['itineraries'] = $this->itineraryService->extract( - $account, - $mailbox->getName(), - $message->getUid() - ); + $itineraries = $this->itineraryService->getCached($account, $mailbox, $message->getUid()); + if ($itineraries) { + $json['itineraries'] = $itineraries; + } $json['attachments'] = array_map(function ($a) use ($id) { return $this->enrichDownloadUrl( $id, @@ -272,6 +272,28 @@ class MessagesController extends Controller { return $response; } + /** + * @NoAdminRequired + * @TrapError + * + * @param int $id + * + * @return JSONResponse + * + * @throws ClientException + */ + public function getItineraries(int $id): JSONResponse { + try { + $message = $this->mailManager->getMessage($this->currentUserId, $id); + $mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId()); + $account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId()); + } catch (DoesNotExistException $e) { + return new JSONResponse([], Http::STATUS_FORBIDDEN); + } + + return new JsonResponse($this->itineraryService->extract($account, $mailbox, $message->getUid())); + } + private function isSenderTrusted(Message $message): bool { $from = $message->getFrom(); $first = $from->first(); diff --git a/lib/Service/ItineraryService.php b/lib/Service/ItineraryService.php index a4c0ee7bd..b7fd29acc 100644 --- a/lib/Service/ItineraryService.php +++ b/lib/Service/ItineraryService.php @@ -6,6 +6,7 @@ declare(strict_types=1); * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> * * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * @author 2021 Richard Steinmetz <richard@steinmetz.cloud> * * @license GNU AGPL version 3 or any later version * @@ -27,7 +28,7 @@ namespace OCA\Mail\Service; use ChristophWurst\KItinerary\Itinerary; use OCA\Mail\Account; -use OCA\Mail\Db\MailboxMapper; +use OCA\Mail\Db\Mailbox; use OCA\Mail\IMAP\IMAPClientFactory; use OCA\Mail\IMAP\MessageMapper; use OCA\Mail\Integration\KItinerary\ItineraryExtractor; @@ -43,9 +44,6 @@ class ItineraryService { /** @var IMAPClientFactory */ private $clientFactory; - /** @var MailboxMapper */ - private $mailboxMapper; - /** @var MessageMapper */ private $messageMapper; @@ -59,27 +57,34 @@ class ItineraryService { private $logger; public function __construct(IMAPClientFactory $clientFactory, - MailboxMapper $mailboxMapper, MessageMapper $messageMapper, ItineraryExtractor $extractor, ICacheFactory $cacheFactory, LoggerInterface $logger) { $this->clientFactory = $clientFactory; - $this->mailboxMapper = $mailboxMapper; $this->messageMapper = $messageMapper; $this->extractor = $extractor; $this->cache = $cacheFactory->createLocal(); $this->logger = $logger; } - public function extract(Account $account, string $mailbox, int $id): Itinerary { - $mailbox = $this->mailboxMapper->find($account, $mailbox); + private function buildCacheKey(Account $account, Mailbox $mailbox, int $id): string { + return 'mail_itinerary_' . $account->getId() . '_' . $mailbox->getName() . '_' . $id; + } - $cacheKey = 'mail_itinerary_' . $account->getId() . '_' . $mailbox->getName() . '_' . $id; - if ($cached = ($this->cache->get($cacheKey))) { + public function getCached(Account $account, Mailbox $mailbox, int $id): ?Itinerary { + if ($cached = ($this->cache->get($this->buildCacheKey($account, $mailbox, $id)))) { return Itinerary::fromJson($cached); } + return null; + } + + public function extract(Account $account, Mailbox $mailbox, int $id): Itinerary { + if ($cached = ($this->getCached($account, $mailbox, $id))) { + return $cached; + } + $client = $this->clientFactory->getClient($account); $itinerary = new Itinerary(); @@ -104,7 +109,8 @@ class ItineraryService { $final = $this->extractor->extract(json_encode($itinerary)); $this->logger->debug('Reduced ' . count($itinerary) . ' itinerary entries to ' . count($final) . ' entries'); - $this->cache->set($cacheKey, json_encode($final)); + $cache_key = $this->buildCacheKey($account, $mailbox, $id); + $this->cache->set($cache_key, json_encode($final)); return $final; } |