diff --git a/appinfo/routes.php b/appinfo/routes.php index bda4b2d2fe..c309bebacb 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -10,6 +10,11 @@ */ return [ 'routes' => [ + [ + 'name' => 'deep_link#open', + 'url' => '/open/{messageId}', + 'verb' => 'GET', + ], [ 'name' => 'page#index', 'url' => '/', diff --git a/lib/Controller/DeepLinkController.php b/lib/Controller/DeepLinkController.php new file mode 100644 index 0000000000..d6827d0040 --- /dev/null +++ b/lib/Controller/DeepLinkController.php @@ -0,0 +1,99 @@ +mailAccountMapper = $mailAccountMapper; + $this->accountService = $accountService; + $this->messageMapper = $messageMapper; + $this->urlGenerator = $urlGenerator; + $this->userSession = $userSession; + $this->logger = $logger; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + * + * @param string $messageId + * @return RedirectResponse + */ + public function open(string $messageId): RedirectResponse { + $user = $this->userSession->getUser(); + if ($user === null) { + return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.page.login')); + } + + $userId = $user->getUID(); + + try { + // Ensure formatting: Always wrapped in <> + $cleanedId = '<' . trim($messageId, '<>') . '>'; + + $lightAccounts = $this->mailAccountMapper->findByUserId($userId); + + foreach ($lightAccounts as $lightAccount) { + $accountId = $lightAccount->getId(); + $account = $this->accountService->find($userId, $accountId); + $messages = $this->messageMapper->findByMessageId($account, $cleanedId); + + if (!empty($messages)) { + $message = $messages[0]; + $targetId = $message->getId(); + + // IMPORTANT FIX: Use 'mail.page.thread' instead of 'mail.page#thread' + $url = $this->urlGenerator->linkToRouteAbsolute( + 'mail.page.thread', + ['mailboxId' => $message->getMailboxId(), 'id' => $targetId] + ); + + return new RedirectResponse($url); + } + } + } catch (\Exception $e) { + $this->logger->error('DeepLinkController: An unexpected error occurred.', [ + 'exception' => $e, + 'messageId' => $messageId, + ]); + } + + // Fallback + return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('mail.page.index', [])); + } +}