Skip to content
Open
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
5 changes: 5 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
return [
'routes' => [
[
'name' => 'deep_link#open',
'url' => '/open/{messageId}',
'verb' => 'GET',
],
[
'name' => 'page#index',
'url' => '/',
Expand Down
99 changes: 99 additions & 0 deletions lib/Controller/DeepLinkController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors

* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Controller;

use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Db\MessageMapper;
use OCA\Mail\Service\AccountService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

class DeepLinkController extends Controller {
private MailAccountMapper $mailAccountMapper;
private AccountService $accountService;
private MessageMapper $messageMapper;
private IURLGenerator $urlGenerator;
private IUserSession $userSession;
private LoggerInterface $logger;
Comment on lines +23 to +28
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private MailAccountMapper $mailAccountMapper;
private AccountService $accountService;
private MessageMapper $messageMapper;
private IURLGenerator $urlGenerator;
private IUserSession $userSession;
private LoggerInterface $logger;


public function __construct(
string $appName,
IRequest $request,
MailAccountMapper $mailAccountMapper,
AccountService $accountService,
MessageMapper $messageMapper,
IURLGenerator $urlGenerator,
IUserSession $userSession,
LoggerInterface $logger
Comment on lines +33 to +38
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
MailAccountMapper $mailAccountMapper,
AccountService $accountService,
MessageMapper $messageMapper,
IURLGenerator $urlGenerator,
IUserSession $userSession,
LoggerInterface $logger
private MailAccountMapper $mailAccountMapper,
private AccountService $accountService,
private MessageMapper $messageMapper,
private IURLGenerator $urlGenerator,
private IUserSession $userSession,
private LoggerInterface $logger

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding private/protected/public in a class constructor = shortcut to create a property with the given name and assign the value to it.

) {
parent::__construct($appName, $request);
$this->mailAccountMapper = $mailAccountMapper;
$this->accountService = $accountService;
$this->messageMapper = $messageMapper;
$this->urlGenerator = $urlGenerator;
$this->userSession = $userSession;
$this->logger = $logger;
Comment on lines +41 to +46
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->mailAccountMapper = $mailAccountMapper;
$this->accountService = $accountService;
$this->messageMapper = $messageMapper;
$this->urlGenerator = $urlGenerator;
$this->userSession = $userSession;
$this->logger = $logger;

}

/**
* @NoAdminRequired
* @NoCSRFRequired
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @NoCSRFRequired

* @PublicPage
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @PublicPage

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a public page

*
* @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', []));
}
}
Loading