1111use OCA \Federation \TrustedServers ;
1212use OCP \AppFramework \Http ;
1313use OCP \AppFramework \Http \Attribute \AuthorizedAdminSetting ;
14- use OCP \AppFramework \Http \DataResponse ;
1514use OCP \AppFramework \Http \JSONResponse ;
1615use OCP \AppFramework \OCSController ;
17- use OCP \HintException ;
1816use OCP \IL10N ;
1917use OCP \IRequest ;
2018
@@ -30,26 +28,26 @@ public function __construct(
3028
3129
3230 /**
33- * Add server to the list of trusted Nextclouds.
31+ * Add server to the list of trusted Nextcloud servers
32+ *
33+ * @param string $url The URL of the server to add
34+ * @return JSONResponse<Http::STATUS_OK, array{data: array{id: int, message: string, url: string}, status: 'ok'}, array{}>|JSONResponse<Http::STATUS_NOT_FOUND|Http::STATUS_CONFLICT, array{data: array{hint: string, message: string}, status: 'error'}, array{}>
35+ *
36+ * 200: Server added successfully
37+ * 404: Server not found at the given URL
38+ * 409: Server is already in the list of trusted servers
3439 */
3540 #[AuthorizedAdminSetting(settings: Admin::class)]
3641 public function addServer (string $ url ): JSONResponse {
37- try {
38- $ this ->checkServer (trim ($ url ));
39- } catch (HintException $ e ) {
40- return new JSONResponse ([
41- 'message ' => 'error ' ,
42- 'data ' => [
43- 'message ' => $ e ->getMessage (),
44- 'hint ' => $ e ->getHint (),
45- ],
46- ], $ e ->getCode ());
42+ $ check = $ this ->checkServer (trim ($ url ));
43+ if ($ check instanceof JSONResponse) {
44+ return $ check ;
4745 }
4846
4947 // Add the server to the list of trusted servers, all is well
5048 $ id = $ this ->trustedServers ->addServer (trim ($ url ));
5149 return new JSONResponse ([
52- 'message ' => 'ok ' ,
50+ 'status ' => 'ok ' ,
5351 'data ' => [
5452 'url ' => $ url ,
5553 'id ' => $ id ,
@@ -59,36 +57,94 @@ public function addServer(string $url): JSONResponse {
5957 }
6058
6159 /**
62- * Add server to the list of trusted Nextclouds.
60+ * Add server to the list of trusted Nextcloud servers
61+ *
62+ * @param int $id The ID of the trusted server to remove
63+ * @return JSONResponse<Http::STATUS_OK, array{data: array{id: int}, status: 'ok'}, array{}>|JSONResponse<Http::STATUS_NOT_FOUND, array{data: array{message: string}, status: 'error'}, array{}>
64+ *
65+ * 200: Server removed successfully
66+ * 404: Server not found at the given ID
6367 */
6468 #[AuthorizedAdminSetting(settings: Admin::class)]
6569 public function removeServer (int $ id ): JSONResponse {
66- $ this ->trustedServers ->removeServer ($ id );
70+ try {
71+ $ this ->trustedServers ->removeServer ($ id );
72+ return new JSONResponse ([
73+ 'status ' => 'ok ' ,
74+ 'data ' => ['id ' => $ id ],
75+ ]);
76+ } catch (\Exception $ e ) {
77+ return new JSONResponse ([
78+ 'status ' => 'error ' ,
79+ 'data ' => [
80+ 'message ' => $ e ->getMessage (),
81+ ],
82+ ], Http::STATUS_NOT_FOUND );
83+ }
84+ }
85+
86+ /**
87+ * List all trusted servers
88+ *
89+ * @return JSONResponse<Http::STATUS_OK, array{data: list<array{id: int, status: int, url: string}>, status: 'ok'}, array{}>
90+ *
91+ * 200: List of trusted servers
92+ */
93+ #[AuthorizedAdminSetting(settings: Admin::class)]
94+ public function getServers (): JSONResponse {
95+ $ servers = $ this ->trustedServers ->getServers ();
96+
97+ // obfuscate the shared secret
98+ $ servers = array_map (function ($ server ) {
99+ return [
100+ 'url ' => $ server ['url ' ],
101+ 'id ' => $ server ['id ' ],
102+ 'status ' => $ server ['status ' ],
103+ ];
104+ }, $ servers );
105+
106+ // return the list of trusted servers
67107 return new JSONResponse ([
68- 'message ' => 'ok ' ,
69- 'data ' => [ ' id ' => $ id ] ,
108+ 'status ' => 'ok ' ,
109+ 'data ' => $ servers ,
70110 ]);
71111 }
72112
113+
73114 /**
74115 * Check if the server should be added to the list of trusted servers or not.
75116 *
76- * @throws HintException
117+ * @return JSONResponse<Http::STATUS_NOT_FOUND|Http::STATUS_CONFLICT, array{data: array{hint: string, message: string}, status: 'error'}, array{}>|null
118+ *
119+ * 404: Server not found at the given URL
120+ * 409: Server is already in the list of trusted servers
77121 */
78122 #[AuthorizedAdminSetting(settings: Admin::class)]
79- protected function checkServer (string $ url ): bool {
123+ protected function checkServer (string $ url ): ? JSONResponse {
80124 if ($ this ->trustedServers ->isTrustedServer ($ url ) === true ) {
81125 $ message = 'Server is already in the list of trusted servers. ' ;
82126 $ hint = $ this ->l ->t ('Server is already in the list of trusted servers. ' );
83- throw new HintException ($ message , $ hint , Http::STATUS_CONFLICT );
127+ return new JSONResponse ([
128+ 'status ' => 'error ' ,
129+ 'data ' => [
130+ 'message ' => $ message ,
131+ 'hint ' => $ hint ,
132+ ],
133+ ], Http::STATUS_CONFLICT );
84134 }
85135
86136 if ($ this ->trustedServers ->isNextcloudServer ($ url ) === false ) {
87137 $ message = 'No server to federate with found ' ;
88138 $ hint = $ this ->l ->t ('No server to federate with found ' );
89- throw new HintException ($ message , $ hint , Http::STATUS_NOT_FOUND );
139+ return new JSONResponse ([
140+ 'status ' => 'error ' ,
141+ 'data ' => [
142+ 'message ' => $ message ,
143+ 'hint ' => $ hint ,
144+ ],
145+ ], Http::STATUS_NOT_FOUND );
90146 }
91147
92- return true ;
148+ return null ;
93149 }
94150}
0 commit comments