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 \Http ;
19+ use OCP \AppFramework \Http \Attribute \ApiRoute ;
20+ use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
21+ use OCP \AppFramework \Http \Attribute \NoCSRFRequired ;
22+ use OCP \AppFramework \Http \DataResponse ;
23+ use OCP \AppFramework \OCSController ;
24+ use OCP \IRequest ;
25+
26+ /**
27+ * @psalm-import-type MailAccountListResponse from ResponseDefinitions
28+ */
29+ class MailboxesApiController extends OCSController {
30+ public function __construct (
31+ string $ appName ,
32+ IRequest $ request ,
33+ private readonly ?string $ userId ,
34+ private IMailManager $ mailManager ,
35+ private readonly AccountService $ accountService ,
36+ private readonly AliasesService $ aliasesService ,
37+ private IMailSearch $ mailSearch ,
38+ private IUserPreferences $ preferences ,
39+ ) {
40+ parent ::__construct ($ appName , $ request );
41+ }
42+
43+ /**
44+ * List all mailboxes of an account of the user which is currently logged-in
45+ *
46+ * @return DataResponse<Http::STATUS_OK, string, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>
47+ *
48+ * 200: Mailbox list
49+ * 404: User was not logged in
50+ */
51+ #[ApiRoute(verb: 'GET ' , url: '/mailbox/list ' )]
52+ #[NoAdminRequired]
53+ #[NoCSRFRequired]
54+ public function list (int $ accountId ): DataResponse {
55+ $ userId = $ this ->userId ;
56+ if ($ userId === null ) {
57+ return new DataResponse ([], Http::STATUS_NOT_FOUND );
58+ }
59+
60+ $ account = $ this ->accountService ->find ($ userId , $ accountId );
61+
62+ $ mailboxes = $ this ->mailManager ->getMailboxes ($ account );
63+ return new DataResponse (json_encode ($ mailboxes ), Http::STATUS_OK );
64+ }
65+
66+
67+
68+ /**
69+ * List the messages in a mailbox of the user which is currently logged-in
70+ *
71+ * @return DataResponse<Http::STATUS_OK, string, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>|DataResponse<Http::STATUS_FORBIDDEN, array{}, array{}>
72+ *
73+ * 200: Message list
74+ * 403: User cannot access this mailbox
75+ * 404: User was not logged in
76+ */
77+ #[ApiRoute(verb: 'GET ' , url: '/mailbox/messages/list ' )]
78+ #[NoAdminRequired]
79+ #[NoCSRFRequired]
80+ public function listMessages (int $ mailboxId ,
81+ ?int $ cursor = null ,
82+ ?string $ filter = null ,
83+ ?int $ limit = null ,
84+ ?string $ view = null ): DataResponse {
85+ $ userId = $ this ->userId ;
86+ if ($ userId === null ) {
87+ return new DataResponse ([], Http::STATUS_NOT_FOUND );
88+ }
89+ try {
90+ $ mailbox = $ this ->mailManager ->getMailbox ($ userId , $ mailboxId );
91+ $ account = $ this ->accountService ->find ($ userId , $ mailbox ->getAccountId ());
92+ } catch (DoesNotExistException $ e ) {
93+ return new DataResponse ([], Http::STATUS_FORBIDDEN );
94+ }
95+
96+ $ sort = $ this ->preferences ->getPreference ($ userId , 'sort-order ' , 'newest ' ) === 'newest ' ? IMailSearch::ORDER_NEWEST_FIRST : IMailSearch::ORDER_OLDEST_FIRST ;
97+
98+ $ view = $ view === 'singleton ' ? IMailSearch::VIEW_SINGLETON : IMailSearch::VIEW_THREADED ;
99+
100+ $ messages = $ this ->mailSearch ->findMessages (
101+ $ account ,
102+ $ mailbox ,
103+ $ sort ,
104+ $ filter === '' ? null : $ filter ,
105+ $ cursor ,
106+ $ limit ,
107+ $ userId ,
108+ $ view
109+ );
110+ return new DataResponse (json_encode ($ messages ), Http::STATUS_OK );
111+ }
112+ }
0 commit comments