aboutsummaryrefslogtreecommitdiffstats
path: root/apps/accessibility
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2020-06-18 08:30:16 +0200
committerGitHub <noreply@github.com>2020-06-18 08:30:16 +0200
commit3ac0484dedb0ea494c98bcd5d3ded41118b508c9 (patch)
tree26a95dfe7fe7bdaa4451fd4be3f9bf26424a1a71 /apps/accessibility
parente324c90a8d85b894e6ca04b4e2c4fdc9b74cce40 (diff)
parentc30bab34e6c0716069d59631597f4c36f7a45d8f (diff)
downloadnextcloud-server-3ac0484dedb0ea494c98bcd5d3ded41118b508c9.tar.gz
nextcloud-server-3ac0484dedb0ea494c98bcd5d3ded41118b508c9.zip
Merge pull request #21442 from nextcloud/td/js/accessibilityjs_to_initialstate
Move accessibility js route to static js and initial state
Diffstat (limited to 'apps/accessibility')
-rw-r--r--apps/accessibility/appinfo/routes.php1
-rw-r--r--apps/accessibility/composer/composer/autoload_classmap.php1
-rw-r--r--apps/accessibility/composer/composer/autoload_static.php1
-rw-r--r--apps/accessibility/js/accessibilityoca.js4
-rw-r--r--apps/accessibility/lib/AppInfo/Application.php45
-rw-r--r--apps/accessibility/lib/Controller/AccessibilityController.php39
-rw-r--r--apps/accessibility/lib/Service/JSDataService.php62
7 files changed, 81 insertions, 72 deletions
diff --git a/apps/accessibility/appinfo/routes.php b/apps/accessibility/appinfo/routes.php
index 0278457b6a3..da0bfdbd18f 100644
--- a/apps/accessibility/appinfo/routes.php
+++ b/apps/accessibility/appinfo/routes.php
@@ -27,7 +27,6 @@
return [
'routes' => [
['name' => 'accessibility#getCss', 'url' => '/css/user-{md5}', 'verb' => 'GET'],
- ['name' => 'accessibility#getJavascript', 'url' => '/js/accessibility', 'verb' => 'GET'],
],
'ocs' => [
[
diff --git a/apps/accessibility/composer/composer/autoload_classmap.php b/apps/accessibility/composer/composer/autoload_classmap.php
index d0d7a9a2807..dd926bcda3e 100644
--- a/apps/accessibility/composer/composer/autoload_classmap.php
+++ b/apps/accessibility/composer/composer/autoload_classmap.php
@@ -11,6 +11,7 @@ return array(
'OCA\\Accessibility\\Controller\\AccessibilityController' => $baseDir . '/../lib/Controller/AccessibilityController.php',
'OCA\\Accessibility\\Controller\\ConfigController' => $baseDir . '/../lib/Controller/ConfigController.php',
'OCA\\Accessibility\\Migration\\RepairUserConfig' => $baseDir . '/../lib/Migration/RepairUserConfig.php',
+ 'OCA\\Accessibility\\Service\\JSDataService' => $baseDir . '/../lib/Service/JSDataService.php',
'OCA\\Accessibility\\Settings\\Personal' => $baseDir . '/../lib/Settings/Personal.php',
'OCA\\Accessibility\\Settings\\PersonalSection' => $baseDir . '/../lib/Settings/PersonalSection.php',
);
diff --git a/apps/accessibility/composer/composer/autoload_static.php b/apps/accessibility/composer/composer/autoload_static.php
index e8870488db2..7d736964766 100644
--- a/apps/accessibility/composer/composer/autoload_static.php
+++ b/apps/accessibility/composer/composer/autoload_static.php
@@ -26,6 +26,7 @@ class ComposerStaticInitAccessibility
'OCA\\Accessibility\\Controller\\AccessibilityController' => __DIR__ . '/..' . '/../lib/Controller/AccessibilityController.php',
'OCA\\Accessibility\\Controller\\ConfigController' => __DIR__ . '/..' . '/../lib/Controller/ConfigController.php',
'OCA\\Accessibility\\Migration\\RepairUserConfig' => __DIR__ . '/..' . '/../lib/Migration/RepairUserConfig.php',
+ 'OCA\\Accessibility\\Service\\JSDataService' => __DIR__ . '/..' . '/../lib/Service/JSDataService.php',
'OCA\\Accessibility\\Settings\\Personal' => __DIR__ . '/..' . '/../lib/Settings/Personal.php',
'OCA\\Accessibility\\Settings\\PersonalSection' => __DIR__ . '/..' . '/../lib/Settings/PersonalSection.php',
);
diff --git a/apps/accessibility/js/accessibilityoca.js b/apps/accessibility/js/accessibilityoca.js
new file mode 100644
index 00000000000..756d21d0347
--- /dev/null
+++ b/apps/accessibility/js/accessibilityoca.js
@@ -0,0 +1,4 @@
+OCA.Accessibility = OCP.InitialState.loadState('accessibility', 'data')
+if (OCA.Accessibility.theme !== false) {
+ document.body.classList.add(OCA.Accessibility.theme);
+}
diff --git a/apps/accessibility/lib/AppInfo/Application.php b/apps/accessibility/lib/AppInfo/Application.php
index e691b8353d9..c5c495c1829 100644
--- a/apps/accessibility/lib/AppInfo/Application.php
+++ b/apps/accessibility/lib/AppInfo/Application.php
@@ -29,12 +29,14 @@ declare(strict_types=1);
namespace OCA\Accessibility\AppInfo;
+use OCA\Accessibility\Service\JSDataService;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
+use OCP\AppFramework\IAppContainer;
use OCP\IConfig;
-use OCP\IServerContainer;
+use OCP\IInitialStateService;
use OCP\IURLGenerator;
use OCP\IUserSession;
use function count;
@@ -46,15 +48,6 @@ class Application extends App implements IBootstrap {
/** @var string */
public const APP_ID = 'accessibility';
- /** @var IConfig */
- private $config;
-
- /** @var IUserSession */
- private $userSession;
-
- /** @var IURLGenerator */
- private $urlGenerator;
-
public function __construct() {
parent::__construct(self::APP_ID);
}
@@ -68,11 +61,8 @@ class Application extends App implements IBootstrap {
$context->getAppContainer()->query(IConfig::class),
$context->getAppContainer()->query(IURLGenerator::class)
);
- $this->injectJavascript(
- $context->getAppContainer()->query(IURLGenerator::class),
- $context->getAppContainer()->query(IConfig::class),
- $context->getServerContainer()
- );
+
+ $this->registerInitialState($context->getAppContainer());
}
private function injectCss(IUserSession $userSession,
@@ -91,23 +81,14 @@ class Application extends App implements IBootstrap {
}
}
- private function injectJavascript(IURLGenerator $urlGenerator,
- IConfig $config,
- IServerContainer $serverContainer) {
- $linkToJs = $urlGenerator->linkToRoute(
- self::APP_ID . '.accessibility.getJavascript',
- [
- 'v' => $config->getAppValue(self::APP_ID, 'cachebuster', '0'),
- ]
- );
+ private function registerInitialState(IAppContainer $container) {
+ /** @var IInitialStateService $initialState */
+ $initialState = $container->query(IInitialStateService::class);
- \OCP\Util::addHeader(
- 'script',
- [
- 'src' => $linkToJs,
- 'nonce' => $serverContainer->getContentSecurityPolicyNonceManager()->getNonce()
- ],
- ''
- );
+ $initialState->provideLazyInitialState(self::APP_ID, 'data', function () use ($container) {
+ /** @var JSDataService $data */
+ $data = $container->query(JSDataService::class);
+ return $data;
+ });
}
}
diff --git a/apps/accessibility/lib/Controller/AccessibilityController.php b/apps/accessibility/lib/Controller/AccessibilityController.php
index 25e117c7155..bafe8eb3e4b 100644
--- a/apps/accessibility/lib/Controller/AccessibilityController.php
+++ b/apps/accessibility/lib/Controller/AccessibilityController.php
@@ -39,7 +39,6 @@ use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDisplayResponse;
-use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\ILogger;
@@ -205,44 +204,6 @@ class AccessibilityController extends Controller {
}
/**
- * @NoCSRFRequired
- * @PublicPage
- * @NoSameSiteCookieRequired
- *
- * @return DataDownloadResponse
- */
- public function getJavascript(): DataDownloadResponse {
- $user = $this->userSession->getUser();
-
- if ($user === null) {
- $theme = false;
- $highcontrast = false;
- } else {
- $theme = $this->config->getUserValue($user->getUID(), $this->appName, 'theme', false);
- $highcontrast = $this->config->getUserValue($user->getUID(), $this->appName, 'highcontrast', false) !== false;
- }
- if ($theme !== false) {
- $responseJS = '(function() {
- OCA.Accessibility = {
- highcontrast: ' . json_encode($highcontrast) . ',
- theme: ' . json_encode($theme) . ',
- };
- document.body.classList.add(' . json_encode($theme) . ');
-})();';
- } else {
- $responseJS = '(function() {
- OCA.Accessibility = {
- highcontrast: ' . json_encode($highcontrast) . ',
- theme: ' . json_encode($theme) . ',
- };
-})();';
- }
- $response = new DataDownloadResponse($responseJS, 'javascript', 'text/javascript');
- $response->cacheFor(3600);
- return $response;
- }
-
- /**
* Return an array with the user theme & font settings
*
* @return array
diff --git a/apps/accessibility/lib/Service/JSDataService.php b/apps/accessibility/lib/Service/JSDataService.php
new file mode 100644
index 00000000000..61bcd334721
--- /dev/null
+++ b/apps/accessibility/lib/Service/JSDataService.php
@@ -0,0 +1,62 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Accessibility\Service;
+
+use OCA\Accessibility\AppInfo\Application;
+use OCP\IConfig;
+use OCP\IUserSession;
+
+class JSDataService implements \JsonSerializable {
+ /** @var IUserSession */
+ private $userSession;
+ /** @var IConfig */
+ private $config;
+
+ public function __construct(
+ IUserSession $userSession,
+ IConfig $config
+ ) {
+ $this->userSession = $userSession;
+ $this->config = $config;
+ }
+
+ public function jsonSerialize() {
+ $user = $this->userSession->getUser();
+
+ if ($user === null) {
+ $theme = false;
+ $highcontrast = false;
+ } else {
+ $theme = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'theme', false);
+ $highcontrast = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'highcontrast', false) !== false;
+ }
+
+ return [
+ 'theme' => $theme,
+ 'highcontrast' => $highcontrast,
+ ];
+ }
+}