1616use OCA \TermsOfService \Db \Mapper \SignatoryMapper ;
1717use OCA \TermsOfService \Db \Mapper \TermsMapper ;
1818use OCA \TermsOfService \Exceptions \TermsNotFoundException ;
19+ use OCA \TermsOfService \ResponseDefinitions ;
20+ use OCP \AppFramework \Http \Attribute \PublicPage ;
1921use OCP \AppFramework \OCSController ;
2022use OCP \AppFramework \Http ;
2123use OCP \AppFramework \Http \DataResponse ;
2628use OCA \TermsOfService \Events \TermsCreatedEvent ;
2729use OCP \EventDispatcher \IEventDispatcher ;
2830
31+ /**
32+ * @psalm-import-type TermsOfServiceAdminFormData from ResponseDefinitions
33+ * @psalm-import-type TermsOfServiceTerms from ResponseDefinitions
34+ */
2935class TermsController extends OCSController {
3036 /** @var IFactory */
3137 private $ factory ;
@@ -73,9 +79,13 @@ public function __construct(string $appName,
7379 }
7480
7581 /**
76- * @PublicPage
77- * @return DataResponse
82+ * Get all available terms for the current country
83+ *
84+ * @return DataResponse<Http::STATUS_OK, array{terms: list<TermsOfServiceTerms>, languages: array<string, string>, hasSigned: bool}, array{}>
85+ *
86+ * 200: Get list successfully
7887 */
88+ #[PublicPage]
7989 public function index (): DataResponse {
8090 $ currentCountry = $ this ->countryDetector ->getCountry ();
8191 $ countryTerms = $ this ->termsMapper ->getTermsForCountryCode ($ currentCountry );
@@ -86,30 +96,46 @@ public function index(): DataResponse {
8696 }
8797
8898 $ response = [
89- 'terms ' => $ countryTerms ,
99+ 'terms ' => array_map ( static fn ( Terms $ terms ): array => $ terms -> jsonSerialize (), $ countryTerms) ,
90100 'languages ' => $ this ->languageMapper ->getLanguages (),
91101 'hasSigned ' => $ this ->checker ->currentUserHasSigned (),
92102 ];
93103 return new DataResponse ($ response );
94104 }
95105
96106 /**
97- * @return DataResponse
107+ * Get the form data for the admin interface
108+ *
109+ * @return DataResponse<Http::STATUS_OK, TermsOfServiceAdminFormData, array{}>
110+ *
111+ * 200: Get form data successfully
98112 */
99113 public function getAdminFormData (): DataResponse {
114+ $ forPublicShares = $ this ->config ->getAppValue (Application::APPNAME , 'tos_on_public_shares ' , '0 ' );
115+ if ($ forPublicShares !== '0 ' ) {
116+ $ forPublicShares = '1 ' ;
117+ }
118+ $ forUsers = $ this ->config ->getAppValue (Application::APPNAME , 'tos_for_users ' , '1 ' );
119+ if ($ forUsers !== '1 ' ) {
120+ $ forUsers = '0 ' ;
121+ }
100122 $ response = [
101- 'terms ' => $ this ->termsMapper ->getTerms (),
123+ 'terms ' => array_map ( static fn ( Terms $ terms ): array => $ terms -> jsonSerialize (), $ this ->termsMapper ->getTerms () ),
102124 'countries ' => $ this ->countryMapper ->getCountries (),
103125 'languages ' => $ this ->languageMapper ->getLanguages (),
104- 'tos_on_public_shares ' => $ this -> config -> getAppValue (Application:: APPNAME , ' tos_on_public_shares ' , ' 0 ' ) ,
105- 'tos_for_users ' => $ this -> config -> getAppValue (Application:: APPNAME , ' tos_for_users ' , ' 1 ' ) ,
126+ 'tos_on_public_shares ' => $ forPublicShares ,
127+ 'tos_for_users ' => $ forUsers ,
106128 ];
107129 return new DataResponse ($ response );
108130 }
109131
110132 /**
111- * @param int $id
112- * @return DataResponse
133+ * Delete a given Term by id
134+ *
135+ * @param positive-int $id The terms which should be deleted
136+ * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
137+ *
138+ * 200: Deleted successfully
113139 */
114140 public function destroy (int $ id ): DataResponse {
115141 $ terms = new Terms ();
@@ -120,15 +146,21 @@ public function destroy(int $id): DataResponse {
120146
121147 return new DataResponse ();
122148 }
149+
123150 protected function createTermsCreatedEvent (): TermsCreatedEvent {
124151 return new TermsCreatedEvent ();
125152 }
126153
127154 /**
128- * @param string $countryCode
129- * @param string $languageCode
130- * @param string $body
131- * @return DataResponse
155+ * Create new terms
156+ *
157+ * @param string $countryCode One of the 2-letter region codes or `--` for "global"
158+ * @param string $languageCode One of the 2-letter language codes
159+ * @param string $body The actual terms and conditions text (can be markdown, using headers, basic text formating, lists and links)
160+ * @return DataResponse<Http::STATUS_OK, TermsOfServiceTerms, array{}>|DataResponse<Http::STATUS_EXPECTATION_FAILED, array<empty>, array{}>
161+ *
162+ * 200: Created successfully
163+ * 417: Country or language code was not a valid option
132164 */
133165 public function create (string $ countryCode ,
134166 string $ languageCode ,
@@ -161,6 +193,6 @@ public function create(string $countryCode,
161193 $ event = $ this ->createTermsCreatedEvent ();
162194 $ this ->eventDispatcher ->dispatchTyped ($ event );
163195
164- return new DataResponse ($ terms );
196+ return new DataResponse ($ terms-> jsonSerialize () );
165197 }
166198}
0 commit comments