From 836e7305d02d718673848c2ea35fe194fd81142c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?=
 <skjnldsv@protonmail.com>
Date: Mon, 18 Nov 2019 22:13:38 +0100
Subject: [PATCH] Fix accessibility
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
---
 apps/accessibility/appinfo/routes.php         |   7 +++-
 apps/accessibility/js/accessibility.js        | Bin 157503 -> 157508 bytes
 apps/accessibility/js/accessibility.js.map    | Bin 785806 -> 785853 bytes
 .../lib/Controller/ConfigController.php       |  38 +++++++++++++-----
 apps/accessibility/lib/Settings/Personal.php  |  31 +++++++++-----
 apps/accessibility/src/Accessibility.vue      |   3 +-
 .../templates/settings-personal.php           |   3 --
 7 files changed, 58 insertions(+), 24 deletions(-)

diff --git a/apps/accessibility/appinfo/routes.php b/apps/accessibility/appinfo/routes.php
index ce03b3486b7..0096b729164 100644
--- a/apps/accessibility/appinfo/routes.php
+++ b/apps/accessibility/appinfo/routes.php
@@ -35,7 +35,12 @@ return [
 		[
 			'name' => 'Config#setConfig',
 			'url'  => '/api/v1/config/{key}',
-			'verb' => 'POST',
+			'verb' => 'PUT',
+		],
+		[
+			'name' => 'Config#deleteConfig',
+			'url'  => '/api/v1/config/{key}',
+			'verb' => 'DELETE',
 		],
     ]
 ];
diff --git a/apps/accessibility/js/accessibility.js b/apps/accessibility/js/accessibility.js
index d3dced4a57d1108d835f7807862fc387b40eb6ae..e15f5691b43b0a93dd401719c11bcfd7e70f43e9 100644
GIT binary patch
delta 323
zcmYL^u}T9$6h+yG2mw0<V;82{iNmrXX?B)T1h)_(SR^(PlDEmk491->JBh*)u<#Eo
z2K){|I~(hOzo0*2Vj?z=3-@yGdGCwT$D;J`=)FMH1*Y8c9-bjBhT8aXO!GTs`y(HN
ziIFpbK_>4lE&7<tO3010Q<GITX2^^nL<o_wbxt&xJL$rnd+Wj_HOJ&MX(p|NaB|UZ
z5jH@JyqMv5DtR`aQyTx)Hk|0ezNcNg2hY1YF!EN<q))jHMn(>Wc7RX=C7wRN%I+ND
zz*FuTp{OcK|CVd@I5iS))|C<8AVy2yh63GA3gzvUX6ULZ9C_$e3eBL-R}4gE1M>e}
cZuP{cOG1`S$75zkDrcQe$fDQp6t1>@0NR>pVgLXD

delta 294
zcmX?dj&uJx&W0AoElig2)2-u~v?Vh&tJCs}G_0*Pi_0@hk~1_i^$Lno%WN~J*T*x7
z3PO1XI+=QTsTC!*#?#luGiAFgDcRcE=GiN`xcaz;xGGsG1^5StDCwjmmLytLmnG(u
zrds9I)@o{6CnpxCDi~XpWMmfWl@_NKIp^o4Wu`}`#@goTXcSG4N?@8LT%=c&nwwvi
zs-bCBq?b{Ynr2%xojsAskSX7G^2J9I(}NP3BpLIk=Oi*!YvpTd!kl7gWSvx$nwSl-
z!@>&cR12Vdi2>04f=NsgY#_Ey=5+leCOgK=>3K;^J|ad|AV(>s=A|fE6{VJx7Ud~q
RPCu6fw8LWiizKEP7660QV08ch

diff --git a/apps/accessibility/js/accessibility.js.map b/apps/accessibility/js/accessibility.js.map
index c7db886351da6aeebf05926a0c18536e031ca7c4..8baa4f042754d2f33d0f38898a2d105d40ec0182 100644
GIT binary patch
delta 225
zcmeDCt-trT{)V7>mb5ez!^x}b)SGMT+iU9?ftYD~Z9TKoVk37)Gbcx#U`KNxneFUo
z0i?VgogH-wT|s2I6NvN$5s{8LK+@l_c={7179UZ4ASd0$G1C>S4ah0=aLj3URb~NV
zRv=~rVs;?r0Afxc=GyM6%+0rt!!kM1*eJy?d3*Fj?i~VB$@zK3B?_6vE~z=GC8-Lw
t3YjSiwzjqk>gv-uKXF${z$EMy)LmSCTti&d6|5B0146gY{lxu|3jjNNM!o<5

delta 187
zcmdn{TfgtO{)V7>7Lz3Nq{*x6)SGMT+iU9?ftYD~Z9TKoVpUH^GbcwKZ%1cGokCX-
zY2gGSeL+N|V-}F~cPyO#QHjMz)Dp-^cX3R2b=1jr2FaItIA*m6DYF1ED-g2*F*^`*
z05K;Jb8Qb&=H}bSVQ67umSk#byglO~_YQ&SE}ysyq*4@YZEY3Q)fMa&)LmSCTti&d
S6|5B01N?)xpZUc7kqZE=hdPk}

diff --git a/apps/accessibility/lib/Controller/ConfigController.php b/apps/accessibility/lib/Controller/ConfigController.php
index d98ba231fa8..589861163f1 100644
--- a/apps/accessibility/lib/Controller/ConfigController.php
+++ b/apps/accessibility/lib/Controller/ConfigController.php
@@ -102,16 +102,8 @@ class ConfigController extends OCSController {
 	public function setConfig(string $key, $value): DataResponse {
 		if ($key === 'theme' || $key === 'font' || $key === 'highcontrast') {
 
-			if ($value === false) {
-				$this->config->deleteUserValue($this->userId, $this->appName, $key);
-				$userValues = $this->config->getUserKeys($this->userId, $this->appName);
-
-				// remove hash if no settings selected
-				if (count($userValues) === 1 && $userValues[0] === 'icons-css') {
-					$this->config->deleteUserValue($this->userId, $this->appName, 'icons-css');
-				}
-
-				return new DataResponse();
+			if ($value === false || $value === '') {
+				throw new OCSBadRequestException('Invalid value: ' . $value);
 			}
 
 			$themes = $this->accessibilityProvider->getThemes();
@@ -133,4 +125,30 @@ class ConfigController extends OCSController {
 		throw new OCSBadRequestException('Invalid key: ' . $key);
 	}
 
+	/**
+	 * @NoAdminRequired
+	 *
+	 * Unset theme or font config
+	 *
+	 * @param string $key theme or font
+	 * @return DataResponse
+	 * @throws Exception
+	 */
+	public function deleteConfig(string $key): DataResponse {
+		if ($key === 'theme' || $key === 'font' || $key === 'highcontrast') {
+
+			$this->config->deleteUserValue($this->userId, $this->appName, $key);
+			$userValues = $this->config->getUserKeys($this->userId, $this->appName);
+
+			// remove hash if no settings selected
+			if (count($userValues) === 1 && $userValues[0] === 'icons-css') {
+				$this->config->deleteUserValue($this->userId, $this->appName, 'icons-css');
+			}
+
+			return new DataResponse();
+		}
+
+		throw new OCSBadRequestException('Invalid key: ' . $key);
+	}
+
 }
diff --git a/apps/accessibility/lib/Settings/Personal.php b/apps/accessibility/lib/Settings/Personal.php
index ef9fc407147..484f33cd064 100644
--- a/apps/accessibility/lib/Settings/Personal.php
+++ b/apps/accessibility/lib/Settings/Personal.php
@@ -27,10 +27,12 @@ namespace OCA\Accessibility\Settings;
 use OCA\Accessibility\AccessibilityProvider;
 use OCP\AppFramework\Http\TemplateResponse;
 use OCP\IConfig;
+use OCP\IInitialStateService;
 use OCP\IL10N;
 use OCP\IURLGenerator;
 use OCP\IUserSession;
 use OCP\Settings\ISettings;
+use OCP\Util;
 
 class Personal implements ISettings {
 
@@ -52,6 +54,9 @@ class Personal implements ISettings {
 	/** @var AccessibilityProvider */
 	private $accessibilityProvider;
 
+	/** @var IInitialStateService */
+	private $initialStateService;
+
 	/**
 	 * Settings constructor.
 	 *
@@ -67,13 +72,15 @@ class Personal implements ISettings {
 								IUserSession $userSession,
 								IL10N $l,
 								IURLGenerator $urlGenerator,
-								AccessibilityProvider $accessibilityProvider) {
+								AccessibilityProvider $accessibilityProvider,
+								IInitialStateService $initialStateService) {
 		$this->appName               = $appName;
 		$this->config                = $config;
 		$this->userSession           = $userSession;
 		$this->l                     = $l;
 		$this->urlGenerator          = $urlGenerator;
 		$this->accessibilityProvider = $accessibilityProvider;
+		$this->initialStateService   = $initialStateService;
 	}
 
 	/**
@@ -81,19 +88,25 @@ class Personal implements ISettings {
 	 * @since 9.1
 	 */
 	public function getForm() {
+		Util::addScript('accessibility', 'accessibility');
+		Util::addStyle('accessibility', 'style');
 
-		$serverData = [
+		$availableConfig = [
 			'themes' => $this->accessibilityProvider->getThemes(),
 			'fonts'  => $this->accessibilityProvider->getFonts(),
-			'highcontrast' => $this->accessibilityProvider->getHighContrast(),
-			'selected' => [
-				'highcontrast' => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false),
-				'theme'  => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false),
-				'font'   => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false)
-			]
+			'highcontrast' => $this->accessibilityProvider->getHighContrast()
+		];
+
+		$userConfig = [
+			'highcontrast' => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false),
+			'theme'  => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false),
+			'font'   => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false)
 		];
 
-		return new TemplateResponse($this->appName, 'settings-personal', ['serverData' => $serverData]);
+		$this->initialStateService->provideInitialState($this->appName, 'available-config', $availableConfig);
+		$this->initialStateService->provideInitialState($this->appName, 'user-config', $userConfig);
+
+		return new TemplateResponse($this->appName, 'settings-personal');
 	}
 
 	/**
diff --git a/apps/accessibility/src/Accessibility.vue b/apps/accessibility/src/Accessibility.vue
index 3b90e8d9fe6..6ba680dc009 100644
--- a/apps/accessibility/src/Accessibility.vue
+++ b/apps/accessibility/src/Accessibility.vue
@@ -118,9 +118,10 @@ export default {
 		 */
 		async selectItem(type, id) {
 			try {
+				const isDelete = id === ''
 				await axios({
 					url: generateOcsUrl('apps/accessibility/api/v1/config', 2) + type,
-					method: id === '' ? 'DELETE' : 'POST',
+					method: isDelete ? 'DELETE' : 'PUT',
 					data: {
 						value: id
 					}
diff --git a/apps/accessibility/templates/settings-personal.php b/apps/accessibility/templates/settings-personal.php
index 3149241c5ed..8761ec14bb5 100644
--- a/apps/accessibility/templates/settings-personal.php
+++ b/apps/accessibility/templates/settings-personal.php
@@ -21,9 +21,6 @@
  *
  */
 
-script('accessibility', 'accessibility');
-style('accessibility', 'style');
 ?>
 
-<span id="serverData" data-server="<?php p(json_encode($_['serverData']));?>"></span>
 <span id="accessibility"></span>
\ No newline at end of file
-- 
GitLab