Skip to content

Commit a840e8c

Browse files
marcelklehrjulien-nc
authored andcommitted
AI admin settings: Add save mechanism
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 8ec1926 commit a840e8c

File tree

4 files changed

+39
-75
lines changed

4 files changed

+39
-75
lines changed

apps/settings/appinfo/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
['name' => 'ChangePassword#changeUserPassword', 'url' => '/settings/users/changepassword', 'verb' => 'POST' , 'root' => ''],
7676
['name' => 'TwoFactorSettings#index', 'url' => '/settings/api/admin/twofactorauth', 'verb' => 'GET' , 'root' => ''],
7777
['name' => 'TwoFactorSettings#update', 'url' => '/settings/api/admin/twofactorauth', 'verb' => 'PUT' , 'root' => ''],
78+
['name' => 'AISettings#update', 'url' => '/settings/api/admin/ai', 'verb' => 'PUT' , 'root' => ''],
7879

7980
['name' => 'Help#help', 'url' => '/settings/help/{mode}', 'verb' => 'GET', 'defaults' => ['mode' => ''] , 'root' => ''],
8081

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
4-
* @copyright Copyright (c) 2016, ownCloud, Inc.
5-
*
6-
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
7-
* @author Daniel Kesselberg <mail@danielkesselberg.de>
8-
* @author Joas Schilling <coding@schilljs.com>
9-
* @author Lukas Reschke <lukas@statuscode.ch>
10-
* @author Morris Jobke <hey@morrisjobke.de>
11-
* @author Roeland Jago Douma <roeland@famdouma.nl>
3+
* @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
124
*
135
* @license AGPL-3.0
146
*
@@ -36,70 +28,40 @@
3628
use OCP\IURLGenerator;
3729
use OCP\IUserSession;
3830
use OCP\Mail\IMailer;
31+
use function GuzzleHttp\Promise\queue;
3932

4033
class AISettingsController extends Controller {
4134

42-
/** @var IL10N */
43-
private $l10n;
44-
/** @var IConfig */
45-
private $config;
46-
/** @var IUserSession */
47-
private $userSession;
48-
/** @var IMailer */
49-
private $mailer;
50-
/** @var IURLGenerator */
51-
private $urlGenerator;
52-
5335
/**
5436
* @param string $appName
5537
* @param IRequest $request
56-
* @param IL10N $l10n
5738
* @param IConfig $config
58-
* @param IUserSession $userSession
59-
* @param IURLGenerator $urlGenerator,
60-
* @param IMailer $mailer
6139
*/
62-
public function __construct($appName,
63-
IRequest $request,
64-
IL10N $l10n,
65-
IConfig $config,
66-
IUserSession $userSession,
67-
IURLGenerator $urlGenerator,
68-
IMailer $mailer) {
40+
public function __construct(
41+
$appName,
42+
IRequest $request,
43+
private IConfig $config,
44+
) {
6945
parent::__construct($appName, $request);
70-
$this->l10n = $l10n;
71-
$this->config = $config;
72-
$this->userSession = $userSession;
73-
$this->urlGenerator = $urlGenerator;
74-
$this->mailer = $mailer;
7546
}
7647

7748
/**
7849
* Sets the email settings
7950
*
80-
* @PasswordConfirmationRequired
8151
* @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\ArtificialIntelligence)
8252
*
8353
* @param array $settings
8454
* @return DataResponse
8555
*/
86-
public function setAISettings($settings) {
87-
$params = get_defined_vars();
88-
$configs = [];
89-
foreach ($params as $key => $value) {
90-
$configs[$key] = empty($value) ? null : $value;
91-
}
92-
93-
// Delete passwords from config in case no auth is specified
94-
if ($params['mail_smtpauth'] !== 1) {
95-
$configs['mail_smtpname'] = null;
96-
$configs['mail_smtppassword'] = null;
56+
public function update($settings) {
57+
$keys = ['ai.stt_provider', 'ai.textprocessing_provider_preferences', 'ai.translation_provider_preferences'];
58+
foreach ($keys as $key) {
59+
if (!isset($settings[$key])) {
60+
continue;
61+
}
62+
$this->config->setAppValue('core', $key, json_encode($settings[$key]));
9763
}
9864

99-
$this->config->setSystemValues($configs);
100-
101-
$this->config->setAppValue('core', 'emailTestSuccessful', '0');
102-
10365
return new DataResponse();
10466
}
10567
}

apps/settings/lib/Settings/Admin/ArtificialIntelligence.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function getName(): ?string {
157157

158158
public function getAuthorizedAppConfig(): array {
159159
return [
160-
'core' => ['/ai_.*/'],
160+
'core' => ['/ai..*/'],
161161
];
162162
}
163163
}

apps/settings/src/components/AdminAI.vue

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
<div>
33
<NcSettingsSection :title="t('settings', 'Machine translation')"
44
:description="t('settings', 'Machine translation can be implemented by different apps. Here you can define the precedence of the machine translation apps you have installed at the moment.')">
5-
<draggable v-model="settings['ai.translation_provider_preferences']">
6-
<div v-for="(providerClass, i) in settings['ai.translation_provider_preferences']" :key="providerClass" class="draggable__item"><span class="draggable__number">{{i+1}}</span> {{ translationProviders.find(p => p.class === providerClass)?.name }}</div>
5+
<draggable v-model="settings['ai.translation_provider_preferences']" @change="saveChanges">
6+
<div v-for="(providerClass, i) in settings['ai.translation_provider_preferences']" :key="providerClass" class="draggable__item">
7+
<span class="draggable__number">{{ i+1 }}</span> {{ translationProviders.find(p => p.class === providerClass)?.name }}
8+
</div>
79
</draggable>
810
</NcSettingsSection>
911
<NcSettingsSection :title="t('settings', 'Speech-To-Text')"
@@ -13,7 +15,8 @@
1315
:checked.sync="settings['ai.stt_provider']"
1416
:value="provider.class"
1517
name="stt_provider"
16-
type="radio">
18+
type="radio"
19+
@update:checked="saveChanges">
1720
{{ provider.name }}
1821
</NcCheckboxRadioSwitch>
1922
</template>
@@ -29,9 +32,16 @@
2932
<h3>{{ t('settings', 'Task:') }} {{ getTaskType(type).name }}</h3>
3033
<p>{{ getTaskType(type).description }}</p>
3134
<p>&nbsp;</p>
32-
<NcSelect v-model="settings['ai.textprocessing_provider_preferences'][type]" :clearable="false" :options="textProcessingProviders.filter(p => p.taskType === type).map(p => p.class)">
33-
<template #option="{label}">{{ textProcessingProviders.find(p => p.class === label)?.name }}</template>
34-
<template #selected-option="{label}">{{ textProcessingProviders.find(p => p.class === label)?.name }}</template>
35+
<NcSelect v-model="settings['ai.textprocessing_provider_preferences'][type]"
36+
:clearable="false"
37+
:options="textProcessingProviders.filter(p => p.taskType === type).map(p => p.class)"
38+
@change="saveChanges">
39+
<template #option="{label}">
40+
{{ textProcessingProviders.find(p => p.class === label)?.name }}
41+
</template>
42+
<template #selected-option="{label}">
43+
{{ textProcessingProviders.find(p => p.class === label)?.name }}
44+
</template>
3545
</NcSelect>
3646
<p>&nbsp;</p>
3747
</template>
@@ -74,24 +84,15 @@ export default {
7484
}
7585
},
7686
methods: {
77-
saveChanges() {
87+
async saveChanges() {
7888
this.loading = true
79-
80-
const data = {
81-
enforced: this.enforced,
82-
enforcedGroups: this.enforcedGroups,
83-
excludedGroups: this.excludedGroups,
89+
const data = this.settings
90+
try {
91+
await axios.put(generateUrl('/settings/api/admin/ai'), data)
92+
} catch (err) {
93+
console.error('could not save changes', err)
8494
}
85-
axios.put(generateUrl('/settings/api/admin/twofactorauth'), data)
86-
.then(resp => resp.data)
87-
.then(state => {
88-
this.state = state
89-
this.dirty = false
90-
})
91-
.catch(err => {
92-
console.error('could not save changes', err)
93-
})
94-
.then(() => { this.loading = false })
95+
this.loading = false
9596
},
9697
getTaskType(type) {
9798
if (!Array.isArray(this.textProcessingTaskTypes)) {

0 commit comments

Comments
 (0)