currentUserId = $UserId; $this->mailManager = $mailManager; $this->preferences = $preferences; $this->mailSearch = $mailSearch; $this->accountService = $accountService; $this->urlGenerator = $urlGenerator; $this->mimeTypeDetector = $mimeTypeDetector; $this->clientFactory = $clientFactory; $this->syncService = $syncService; $this->mailRepository = $mailRepository; } /** * @NoAdminRequired * @NoCSRFRequired * */ public function loadEmailboxMessage(): JSONResponse { $mailboxId = $this->mailRepository->getCurrentUserMailBoxId($this->currentUserId); if($mailboxId == null){ return new JSONResponse([ "success" => false, "message" => "MAILBOX_NOT_FOUND", ], Http::STATUS_OK); } $lastMessageTimestamp = null; $init = true; $sortOrder = 'newest'; $query = null; try { $mailbox = $this->mailManager->getMailbox($this->currentUserId, $mailboxId); $account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId()); // try { // if (boolval($this->request->getParam('withSync'))) { // $this->syncService->syncMailbox( // $account, // $mailbox, // Horde_Imap_Client::SYNC_NEWMSGSUIDS | Horde_Imap_Client::SYNC_FLAGSUIDS | Horde_Imap_Client::SYNC_VANISHEDUIDS, // null, // $lastMessageTimestamp, // $init, // $sortOrder, // $query // ); // } // } catch (\Throwable $th) { // // on ignioir car il n'est pas est deja synchronise // } } catch (DoesNotExistException $e) { return new JSONResponse([], Http::STATUS_FORBIDDEN); } $order = $this->preferences->getPreference($this->currentUserId, 'sort-order', 'newest') === 'newest' ? 'DESC': 'ASC'; return new JSONResponse( $this->mailSearch->findMessages( $account, $mailbox, $order, null, null, 400, ) ); } /** * @NoAdminRequired * @NoCSRFRequired * */ public function loadEmailboxAttachement(int $messageId = 0): Response { $data = []; try { $message = $this->mailManager->getMessage($this->currentUserId, $messageId); $mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId()); $account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId()); // $data["messageObject"] = $message ?? null; } catch (DoesNotExistException $e) { return new JSONResponse([], Http::STATUS_FORBIDDEN); } $client = $this->clientFactory->getClient($account); try { $imapMessage = $this->mailManager->getImapMessage( $client, $account, $mailbox, $message->getUid(), true ); $json = $imapMessage->getFullMessage($messageId); } finally { $client->logout(); } $data['fullMessage'] = $json; $data['attachments'] = array_map(function ($a) use ($messageId) { return $this->enrichDownloadUrl( $messageId, $a ); }, $json['attachments']); if(isset( $json['attachments'])) { unset($json['attachments']); } return new JSONResponse(($data)); } /** * @param int $id * @param array $attachment * * @return array */ private function enrichDownloadUrl(int $id, array $attachment) { $downloadUrl = $this->urlGenerator->linkToRoute('mail.messages.downloadAttachment', [ 'id' => $id, 'attachmentId' => $attachment['id'], ]); $downloadUrl = $this->urlGenerator->getAbsoluteURL($downloadUrl); $attachment['downloadUrl'] = $downloadUrl; $attachment['mimeUrl'] = $this->mimeTypeDetector->mimeTypeIcon($attachment['mime']); $attachment['isImage'] = $this->attachmentIsImage($attachment); $attachment['isCalendarEvent'] = $this->attachmentIsCalendarEvent($attachment); return $attachment; } /** * Determines if the content of this attachment is an image * * @param array $attachment * * @return boolean */ private function attachmentIsImage(array $attachment): bool { return in_array( $attachment['mime'], [ 'image/jpeg', 'image/png', 'image/gif' ]); } /** * @param array $attachment * * @return boolean */ private function attachmentIsCalendarEvent(array $attachment): bool { return in_array($attachment['mime'], ['text/calendar', 'application/ics'], true); } }