Skip to content

Commit 04063bb

Browse files
committed
feat: add OCS routes for mailbox and mail listing
Signed-off-by: Jana Peper <[email protected]>
1 parent 94dac17 commit 04063bb

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Mail\Controller;
11+
12+
use OCA\Mail\Contracts\IMailManager;
13+
use OCA\Mail\Contracts\IMailSearch;
14+
use OCA\Mail\Contracts\IUserPreferences;
15+
use OCA\Mail\ResponseDefinitions;
16+
use OCA\Mail\Service\AccountService;
17+
use OCA\Mail\Service\AliasesService;
18+
use OCP\AppFramework\Db\DoesNotExistException;
19+
use OCP\AppFramework\Http;
20+
use OCP\AppFramework\Http\Attribute\ApiRoute;
21+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
22+
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
23+
use OCP\AppFramework\Http\DataResponse;
24+
use OCP\AppFramework\OCSController;
25+
use OCP\IRequest;
26+
27+
/**
28+
* @psalm-import-type MailAccountListResponse from ResponseDefinitions
29+
*/
30+
class MailboxesApiController extends OCSController {
31+
public function __construct(
32+
string $appName,
33+
IRequest $request,
34+
private readonly ?string $userId,
35+
private IMailManager $mailManager,
36+
private readonly AccountService $accountService,
37+
private readonly AliasesService $aliasesService,
38+
private IMailSearch $mailSearch,
39+
private IUserPreferences $preferences,
40+
) {
41+
parent::__construct($appName, $request);
42+
}
43+
44+
/**
45+
* List all mailboxes of an account of the user which is currently logged-in
46+
*
47+
* @param int $accountId the mail account id
48+
* @return DataResponse<Http::STATUS_OK, string, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>
49+
*
50+
* 200: Mailbox list
51+
* 404: User was not logged in
52+
*/
53+
#[ApiRoute(verb: 'GET', url: '/mailbox/list')]
54+
#[NoAdminRequired]
55+
#[NoCSRFRequired]
56+
public function list(int $accountId): DataResponse {
57+
$userId = $this->userId;
58+
if ($userId === null) {
59+
return new DataResponse([], Http::STATUS_NOT_FOUND);
60+
}
61+
62+
$account = $this->accountService->find($userId, $accountId);
63+
64+
$mailboxes = $this->mailManager->getMailboxes($account);
65+
return new DataResponse(json_encode($mailboxes), Http::STATUS_OK);
66+
}
67+
68+
69+
70+
/**
71+
* List the newest messages in a mailbox of the user which is currently logged-in
72+
*
73+
* @param int $mailboxId the mailbox id
74+
* @param int $cursor the query cursor
75+
* @param string $filter the query filter
76+
* @param int|null $limit the number of messages to be returned, can be left ampty to get all messages
77+
* @param string $view returns messages in requested view ('singleton' or 'threaded')
78+
* @param string|null $v Cache buster version to guarantee unique urls (will trigger HTTP caching if set)
79+
* @return DataResponse<Http::STATUS_OK, string, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>|DataResponse<Http::STATUS_FORBIDDEN, array{}, array{}>
80+
*
81+
* 200: Message list
82+
* 403: User cannot access this mailbox
83+
* 404: User was not logged in
84+
*/
85+
#[ApiRoute(verb: 'GET', url: '/mailbox/messages/list')]
86+
#[NoAdminRequired]
87+
#[NoCSRFRequired]
88+
public function listMessages(int $mailboxId,
89+
?int $cursor = null,
90+
?string $filter = null,
91+
?int $limit = null,
92+
?string $view = null): DataResponse {
93+
$userId = $this->userId;
94+
if ($userId === null) {
95+
return new DataResponse([], Http::STATUS_NOT_FOUND);
96+
}
97+
try {
98+
$mailbox = $this->mailManager->getMailbox($userId, $mailboxId);
99+
$account = $this->accountService->find($userId, $mailbox->getAccountId());
100+
} catch (DoesNotExistException $e) {
101+
return new DataResponse([], Http::STATUS_FORBIDDEN);
102+
}
103+
104+
$sort = IMailSearch::ORDER_NEWEST_FIRST;
105+
106+
$view = $view === 'singleton' ? IMailSearch::VIEW_SINGLETON : IMailSearch::VIEW_THREADED;
107+
108+
$messages = $this->mailSearch->findMessages(
109+
$account,
110+
$mailbox,
111+
$sort,
112+
$filter === '' ? null : $filter,
113+
$cursor,
114+
$limit,
115+
$userId,
116+
$view
117+
);
118+
return new DataResponse(json_encode($messages), Http::STATUS_OK);
119+
}
120+
}

0 commit comments

Comments
 (0)