Skip to content

Commit 2ffd2fa

Browse files
committed
feat(api): Move from index.php to ocs API
Signed-off-by: Joas Schilling <[email protected]>
1 parent 1c045a7 commit 2ffd2fa

File tree

7 files changed

+56
-54
lines changed

7 files changed

+56
-54
lines changed

appinfo/routes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66

77
return [
8-
'resources' => [
8+
'ocs-resources' => [
99
'terms' => [
1010
'url' => '/terms'
1111
],
1212
],
13-
'routes' => [
13+
'ocs' => [
1414
[
1515
'name' => 'Terms#getAdminFormData',
1616
'url' => '/terms/admin',

lib/Controller/SigningController.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use OCA\TermsOfService\AppInfo\Application;
1010
use OCA\TermsOfService\Db\Entities\Signatory;
1111
use OCA\TermsOfService\Db\Mapper\SignatoryMapper;
12-
use OCP\AppFramework\Controller;
13-
use OCP\AppFramework\Http\JSONResponse;
12+
use OCP\AppFramework\Http\DataResponse;
13+
use OCP\AppFramework\OCSController;
1414
use OCP\IConfig;
1515
use OCP\IRequest;
1616
use OCP\ISession;
@@ -20,7 +20,7 @@
2020
use OCA\TermsOfService\Events\SignaturesResetEvent;
2121
use OCP\EventDispatcher\IEventDispatcher;
2222

23-
class SigningController extends Controller {
23+
class SigningController extends OCSController {
2424
/** @var string */
2525
private $userId;
2626
/** @var SignatoryMapper */
@@ -68,9 +68,9 @@ protected function resetAllSignaturesEvent(): SignaturesResetEvent {
6868
*
6969
* @param int $termId
7070
*
71-
* @return JSONResponse
71+
* @return DataResponse
7272
*/
73-
public function signTerms(int $termId): JSONResponse {
73+
public function signTerms(int $termId): DataResponse {
7474
$signatory = new Signatory();
7575
$signatory->setUserId($this->userId);
7676
$signatory->setTermsId($termId);
@@ -87,7 +87,7 @@ public function signTerms(int $termId): JSONResponse {
8787
// Mark all notifications as processed …
8888
$this->notificationsManager->markProcessed($notification);
8989

90-
return new JSONResponse();
90+
return new DataResponse();
9191
}
9292

9393

@@ -96,20 +96,20 @@ public function signTerms(int $termId): JSONResponse {
9696
*
9797
* @param int $termId
9898
* @UseSession
99-
* @return JSONResponse
99+
* @return DataResponse
100100
*/
101-
public function signTermsPublic(int $termId): JSONResponse {
101+
public function signTermsPublic(int $termId): DataResponse {
102102
$uuid = $this->config->getAppValue(Application::APPNAME, 'term_uuid', '');
103103
$this->session->set('term_uuid', $uuid);
104104

105-
return new JSONResponse();
105+
return new DataResponse();
106106
}
107107

108108

109109
/**
110-
* @return JSONResponse
110+
* @return DataResponse
111111
*/
112-
public function resetAllSignatories(): JSONResponse {
112+
public function resetAllSignatories(): DataResponse {
113113
$this->signatoryMapper->deleteAllSignatories();
114114
$this->config->setAppValue(Application::APPNAME, 'term_uuid', uniqid());
115115

@@ -132,6 +132,6 @@ public function resetAllSignatories(): JSONResponse {
132132
$event = $this->resetAllSignaturesEvent();
133133
$this->eventDispatcher->dispatchTyped($event);
134134

135-
return new JSONResponse();
135+
return new DataResponse();
136136
}
137137
}

lib/Controller/TermsController.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
use OCA\TermsOfService\Db\Mapper\SignatoryMapper;
1616
use OCA\TermsOfService\Db\Mapper\TermsMapper;
1717
use OCA\TermsOfService\Exceptions\TermsNotFoundException;
18-
use OCP\AppFramework\Controller;
18+
use OCP\AppFramework\OCSController;
1919
use OCP\AppFramework\Http;
20-
use OCP\AppFramework\Http\JSONResponse;
20+
use OCP\AppFramework\Http\DataResponse;
2121
use OCP\IConfig;
2222
use OCP\IRequest;
2323
use OCP\L10N\IFactory;
2424
use OCA\TermsOfService\Events\TermsCreatedEvent;
2525
use OCP\EventDispatcher\IEventDispatcher;
2626

27-
class TermsController extends Controller {
27+
class TermsController extends OCSController {
2828
/** @var IFactory */
2929
private $factory;
3030
/** @var TermsMapper */
@@ -71,9 +71,9 @@ public function __construct(string $appName,
7171

7272
/**
7373
* @PublicPage
74-
* @return JSONResponse
74+
* @return DataResponse
7575
*/
76-
public function index(): JSONResponse {
76+
public function index(): DataResponse {
7777
$currentCountry = $this->countryDetector->getCountry();
7878
$countryTerms = $this->termsMapper->getTermsForCountryCode($currentCountry);
7979

@@ -87,35 +87,35 @@ public function index(): JSONResponse {
8787
'languages' => $this->languageMapper->getLanguages(),
8888
'hasSigned' => $this->checker->currentUserHasSigned(),
8989
];
90-
return new JSONResponse($response);
90+
return new DataResponse($response);
9191
}
9292

9393
/**
94-
* @return JSONResponse
94+
* @return DataResponse
9595
*/
96-
public function getAdminFormData(): JSONResponse {
96+
public function getAdminFormData(): DataResponse {
9797
$response = [
9898
'terms' => $this->termsMapper->getTerms(),
9999
'countries' => $this->countryMapper->getCountries(),
100100
'languages' => $this->languageMapper->getLanguages(),
101101
'tos_on_public_shares' => $this->config->getAppValue(Application::APPNAME, 'tos_on_public_shares', '0'),
102102
'tos_for_users' => $this->config->getAppValue(Application::APPNAME, 'tos_for_users', '1'),
103103
];
104-
return new JSONResponse($response);
104+
return new DataResponse($response);
105105
}
106106

107107
/**
108108
* @param int $id
109-
* @return JSONResponse
109+
* @return DataResponse
110110
*/
111-
public function destroy(int $id): JSONResponse {
111+
public function destroy(int $id): DataResponse {
112112
$terms = new Terms();
113113
$terms->setId($id);
114114

115115
$this->termsMapper->delete($terms);
116116
$this->signatoryMapper->deleteTerm($terms);
117117

118-
return new JSONResponse();
118+
return new DataResponse();
119119
}
120120
protected function createTermsCreatedEvent(): TermsCreatedEvent {
121121
return new TermsCreatedEvent();
@@ -125,11 +125,11 @@ protected function createTermsCreatedEvent(): TermsCreatedEvent {
125125
* @param string $countryCode
126126
* @param string $languageCode
127127
* @param string $body
128-
* @return JSONResponse
128+
* @return DataResponse
129129
*/
130130
public function create(string $countryCode,
131131
string $languageCode,
132-
string $body): JSONResponse {
132+
string $body): DataResponse {
133133
$update = false;
134134
try {
135135
// Update terms
@@ -141,7 +141,7 @@ public function create(string $countryCode,
141141
}
142142

143143
if (!$this->countryMapper->isValidCountry($countryCode) || !$this->languageMapper->isValidLanguage($languageCode)) {
144-
return new JSONResponse([], Http::STATUS_EXPECTATION_FAILED);
144+
return new DataResponse([], Http::STATUS_EXPECTATION_FAILED);
145145
}
146146

147147
$terms->setCountryCode($countryCode);
@@ -157,6 +157,6 @@ public function create(string $countryCode,
157157
$event = $this->createTermsCreatedEvent();
158158
$this->eventDispatcher->dispatchTyped($event);
159159

160-
return new JSONResponse($terms);
160+
return new DataResponse($terms);
161161
}
162162
}

src/App.vue

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadi
7272
import NcSelect from '@nextcloud/vue/dist/Components/NcSelect.js'
7373
import NcSettingsSection from '@nextcloud/vue/dist/Components/NcSettingsSection.js'
7474
import { showError, showSuccess } from '@nextcloud/dialogs'
75-
import { generateUrl } from '@nextcloud/router'
75+
import { generateOcsUrl } from '@nextcloud/router'
7676
7777
// Styles
7878
import '@nextcloud/dialogs/style.css'
@@ -139,15 +139,16 @@ export default {
139139
this.saveButtonText = t('terms_of_service', 'Loading …')
140140
this.resetButtonText = t('terms_of_service', 'Reset all signatories')
141141
axios
142-
.get(generateUrl('/apps/terms_of_service/terms/admin'))
142+
.get(generateOcsUrl('/apps/terms_of_service/terms/admin'))
143143
.then(response => {
144-
if (response.data.terms.length !== 0) {
145-
this.terms = response.data.terms
144+
const data = response.data.ocs.data
145+
if (data.terms.length !== 0) {
146+
this.terms = data.terms
146147
}
147-
this.countries = response.data.countries
148-
this.languages = response.data.languages
149-
this.showOnPublicShares = response.data.tos_on_public_shares === '1'
150-
this.showForLoggedInUser = response.data.tos_for_users === '1'
148+
this.countries = data.countries
149+
this.languages = data.languages
150+
this.showOnPublicShares = data.tos_on_public_shares === '1'
151+
this.showForLoggedInUser = data.tos_for_users === '1'
151152
Object.keys(this.countries).forEach((countryCode) => {
152153
this.countryOptions.push({
153154
value: countryCode,
@@ -178,14 +179,15 @@ export default {
178179
this.saveButtonDisabled = true
179180
180181
axios
181-
.post(generateUrl('/apps/terms_of_service/terms'),
182+
.post(generateOcsUrl('/apps/terms_of_service/terms'),
182183
{
183184
countryCode: this.country.value,
184185
languageCode: this.language.value,
185186
body: this.body,
186187
})
187188
.then(response => {
188-
this.$set(this.terms, response.data.id, response.data)
189+
const data = response.data.ocs.data
190+
this.$set(this.terms, data.id, data)
189191
190192
showSuccess(t('terms_of_service', 'Terms saved successfully!'))
191193
this.saveButtonDisabled = false
@@ -195,7 +197,7 @@ export default {
195197
this.resetButtonDisabled = true
196198
197199
axios
198-
.delete(generateUrl('/apps/terms_of_service/sign'))
200+
.delete(generateOcsUrl('/apps/terms_of_service/sign'))
199201
.then(() => {
200202
showSuccess(t('terms_of_service', 'All signatories reset!'))
201203
this.resetButtonDisabled = false

src/Registration.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
<script>
4949
import axios from '@nextcloud/axios'
50-
import { generateUrl } from '@nextcloud/router'
50+
import { generateOcsUrl } from '@nextcloud/router'
5151
import NcModal from '@nextcloud/vue/dist/Components/NcModal.js'
5252
import ModalContent from './components/ModalContent.vue'
5353
@@ -88,10 +88,10 @@ export default {
8888
methods: {
8989
async loadTerms() {
9090
try {
91-
const response = await axios.get(generateUrl('/apps/terms_of_service/terms'))
91+
const response = await axios.get(generateOcsUrl('/apps/terms_of_service/terms'))
9292
93-
// this.hasSigned = response.data.hasSigned
94-
this.terms = response.data.terms
93+
// this.hasSigned = response.data.ocs.data.hasSigned
94+
this.terms = response.data.ocs.data.terms
9595
9696
const language = OC.getLanguage().split('-')[0]
9797
@@ -112,7 +112,7 @@ export default {
112112
this.selectedLanguage = index
113113
}
114114
115-
this.languages.push(response.data.languages[this.terms[index].languageCode])
115+
this.languages.push(response.data.ocs.data.languages[this.terms[index].languageCode])
116116
})
117117
}
118118
// this.showTerms()

src/UserApp.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<script>
3232
import axios from '@nextcloud/axios'
33-
import { generateUrl } from '@nextcloud/router'
33+
import { generateOcsUrl } from '@nextcloud/router'
3434
import NcModal from '@nextcloud/vue/dist/Components/NcModal.js'
3535
import ModalContent from './components/ModalContent.vue'
3636
@@ -69,9 +69,9 @@ export default {
6969
methods: {
7070
async loadTerms() {
7171
try {
72-
const response = await axios.get(generateUrl('/apps/terms_of_service/terms'))
73-
this.hasSigned = response.data.hasSigned
74-
this.terms = response.data.terms
72+
const response = await axios.get(generateOcsUrl('/apps/terms_of_service/terms'))
73+
this.hasSigned = response.data.ocs.data.hasSigned
74+
this.terms = response.data.ocs.data.terms
7575
7676
const language = OC.getLanguage().split('-')[0]
7777
@@ -91,7 +91,7 @@ export default {
9191
this.selectedLanguage = index
9292
}
9393
94-
this.languages.push(response.data.languages[this.terms[index].languageCode])
94+
this.languages.push(response.data.ocs.data.languages[this.terms[index].languageCode])
9595
})
9696
}
9797
@@ -123,7 +123,7 @@ export default {
123123
}
124124
125125
axios.post(
126-
generateUrl(url),
126+
generateOcsUrl(url),
127127
{ termId: this.termsId },
128128
).then(() => {
129129
window.location.reload()

src/components/Term.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import axios from '@nextcloud/axios'
3232
import IconDelete from 'vue-material-design-icons/Delete.vue'
3333
import IconPencil from 'vue-material-design-icons/Pencil.vue'
3434
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
35-
import { generateUrl } from '@nextcloud/router'
35+
import { generateOcsUrl } from '@nextcloud/router'
3636
3737
export default {
3838
name: 'Term',
@@ -110,7 +110,7 @@ export default {
110110
onDelete() {
111111
this.deleteButtonDisabled = true
112112
axios
113-
.delete(generateUrl('/apps/terms_of_service/terms/' + this.id))
113+
.delete(generateOcsUrl('/apps/terms_of_service/terms/' + this.id))
114114
.then(() => {
115115
this.$delete(this.$parent.$parent.$parent.terms, this.id)
116116
})

0 commit comments

Comments
 (0)