]> source.dussan.org Git - nextcloud-server.git/commitdiff
AI admin settings: Add save mechanism
authorMarcel Klehr <mklehr@gmx.net>
Mon, 24 Jul 2023 12:47:15 +0000 (14:47 +0200)
committerJulien Veyssier <julien-nc@posteo.net>
Wed, 2 Aug 2023 10:37:35 +0000 (12:37 +0200)
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
apps/settings/appinfo/routes.php
apps/settings/lib/Controller/AISettingsController.php
apps/settings/lib/Settings/Admin/ArtificialIntelligence.php
apps/settings/src/components/AdminAI.vue

index 938842dd576c4bab4bb735674d712a82f5392b0a..e238510b1a75f42b474b0af5203de329f71184ee 100644 (file)
@@ -75,6 +75,7 @@ return [
                ['name' => 'ChangePassword#changeUserPassword', 'url' => '/settings/users/changepassword', 'verb' => 'POST' , 'root' => ''],
                ['name' => 'TwoFactorSettings#index', 'url' => '/settings/api/admin/twofactorauth', 'verb' => 'GET' , 'root' => ''],
                ['name' => 'TwoFactorSettings#update', 'url' => '/settings/api/admin/twofactorauth', 'verb' => 'PUT' , 'root' => ''],
+               ['name' => 'AISettings#update', 'url' => '/settings/api/admin/ai', 'verb' => 'PUT' , 'root' => ''],
 
                ['name' => 'Help#help', 'url' => '/settings/help/{mode}', 'verb' => 'GET', 'defaults' => ['mode' => ''] , 'root' => ''],
 
index 53aca66c94633911a18ad910a240c53099981dc6..2c443c1a771fc84f31ee6e9bde5156275c4e7d3a 100644 (file)
@@ -1,14 +1,6 @@
 <?php
 /**
- * @copyright Copyright (c) 2017  Joas Schilling <coding@schilljs.com>
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Daniel Kesselberg <mail@danielkesselberg.de>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
+ * @copyright Copyright (c) 2023  Marcel Klehr <mklehr@gmx.net>
  *
  * @license AGPL-3.0
  *
@@ -36,70 +28,40 @@ use OCP\IRequest;
 use OCP\IURLGenerator;
 use OCP\IUserSession;
 use OCP\Mail\IMailer;
+use function GuzzleHttp\Promise\queue;
 
 class AISettingsController extends Controller {
 
-       /** @var IL10N */
-       private $l10n;
-       /** @var IConfig */
-       private $config;
-       /** @var IUserSession */
-       private $userSession;
-       /** @var IMailer */
-       private $mailer;
-       /** @var IURLGenerator */
-       private $urlGenerator;
-
        /**
         * @param string $appName
         * @param IRequest $request
-        * @param IL10N $l10n
         * @param IConfig $config
-        * @param IUserSession $userSession
-        * @param IURLGenerator $urlGenerator,
-        * @param IMailer $mailer
         */
-       public function __construct($appName,
-                                                               IRequest $request,
-                                                               IL10N $l10n,
-                                                               IConfig $config,
-                                                               IUserSession $userSession,
-                                                               IURLGenerator $urlGenerator,
-                                                               IMailer $mailer) {
+       public function __construct(
+               $appName,
+               IRequest $request,
+               private IConfig $config,
+       ) {
                parent::__construct($appName, $request);
-               $this->l10n = $l10n;
-               $this->config = $config;
-               $this->userSession = $userSession;
-               $this->urlGenerator = $urlGenerator;
-               $this->mailer = $mailer;
        }
 
        /**
         * Sets the email settings
         *
-        * @PasswordConfirmationRequired
         * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\ArtificialIntelligence)
         *
         * @param array $settings
         * @return DataResponse
         */
-       public function setAISettings($settings) {
-               $params = get_defined_vars();
-               $configs = [];
-               foreach ($params as $key => $value) {
-                       $configs[$key] = empty($value) ? null : $value;
-               }
-
-               // Delete passwords from config in case no auth is specified
-               if ($params['mail_smtpauth'] !== 1) {
-                       $configs['mail_smtpname'] = null;
-                       $configs['mail_smtppassword'] = null;
+       public function update($settings) {
+               $keys = ['ai.stt_provider', 'ai.textprocessing_provider_preferences', 'ai.translation_provider_preferences'];
+               foreach ($keys as $key) {
+                       if (!isset($settings[$key])) {
+                               continue;
+                       }
+                       $this->config->setAppValue('core', $key, json_encode($settings[$key]));
                }
 
-               $this->config->setSystemValues($configs);
-
-               $this->config->setAppValue('core', 'emailTestSuccessful', '0');
-
                return new DataResponse();
        }
 }
index d33802d6c4521eb449818724d89937d28db8ee13..ad5425d8ded9b62e725636b09ae2e6d6166bf96d 100644 (file)
@@ -157,7 +157,7 @@ class ArtificialIntelligence implements IDelegatedSettings {
 
        public function getAuthorizedAppConfig(): array {
                return [
-                       'core' => ['/ai_.*/'],
+                       'core' => ['/ai..*/'],
                ];
        }
 }
index f11cb0c3da04614288261d982b25471f2daf03bb..649c484c70a24a88e6d87d7dd48e39014722637b 100644 (file)
@@ -2,8 +2,10 @@
        <div>
                <NcSettingsSection :title="t('settings', 'Machine translation')"
                        :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.')">
-                       <draggable v-model="settings['ai.translation_provider_preferences']">
-                               <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>
+                       <draggable v-model="settings['ai.translation_provider_preferences']" @change="saveChanges">
+                               <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>
                        </draggable>
                </NcSettingsSection>
                <NcSettingsSection :title="t('settings', 'Speech-To-Text')"
@@ -13,7 +15,8 @@
                                        :checked.sync="settings['ai.stt_provider']"
                                        :value="provider.class"
                                        name="stt_provider"
-                                       type="radio">
+                                       type="radio"
+                                       @update:checked="saveChanges">
                                        {{ provider.name }}
                                </NcCheckboxRadioSwitch>
                        </template>
                                <h3>{{ t('settings', 'Task:') }} {{ getTaskType(type).name }}</h3>
                                <p>{{ getTaskType(type).description }}</p>
                                <p>&nbsp;</p>
-                               <NcSelect v-model="settings['ai.textprocessing_provider_preferences'][type]"  :clearable="false" :options="textProcessingProviders.filter(p => p.taskType === type).map(p => p.class)">
-                                       <template #option="{label}">{{ textProcessingProviders.find(p => p.class === label)?.name }}</template>
-                                       <template #selected-option="{label}">{{ textProcessingProviders.find(p => p.class === label)?.name }}</template>
+                               <NcSelect v-model="settings['ai.textprocessing_provider_preferences'][type]"
+                                       :clearable="false"
+                                       :options="textProcessingProviders.filter(p => p.taskType === type).map(p => p.class)"
+                                       @change="saveChanges">
+                                       <template #option="{label}">
+                                               {{ textProcessingProviders.find(p => p.class === label)?.name }}
+                                       </template>
+                                       <template #selected-option="{label}">
+                                               {{ textProcessingProviders.find(p => p.class === label)?.name }}
+                                       </template>
                                </NcSelect>
                                <p>&nbsp;</p>
                        </template>
@@ -74,24 +84,15 @@ export default {
                }
        },
        methods: {
-               saveChanges() {
+               async saveChanges() {
                        this.loading = true
-
-                       const data = {
-                               enforced: this.enforced,
-                               enforcedGroups: this.enforcedGroups,
-                               excludedGroups: this.excludedGroups,
+                       const data = this.settings
+                       try {
+                               await axios.put(generateUrl('/settings/api/admin/ai'), data)
+                       } catch (err) {
+                               console.error('could not save changes', err)
                        }
-                       axios.put(generateUrl('/settings/api/admin/twofactorauth'), data)
-                               .then(resp => resp.data)
-                               .then(state => {
-                                       this.state = state
-                                       this.dirty = false
-                               })
-                               .catch(err => {
-                                       console.error('could not save changes', err)
-                               })
-                               .then(() => { this.loading = false })
+                       this.loading = false
                },
                getTaskType(type) {
                  if (!Array.isArray(this.textProcessingTaskTypes)) {