summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2020-06-16 21:54:20 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2020-06-17 19:37:37 +0200
commitc30bab34e6c0716069d59631597f4c36f7a45d8f (patch)
tree166ee7d803791505d01a4743a391b6c8f770990f /apps
parent38cbd17e0a4387d9349a4926dfcc7c670bab0dfb (diff)
downloadnextcloud-server-c30bab34e6c0716069d59631597f4c36f7a45d8f.tar.gz
nextcloud-server-c30bab34e6c0716069d59631597f4c36f7a45d8f.zip
Move accessibility js route to static js and initial state
Yet again another js file that doesn't have to be served via nextcloud. But can just be done via the webbrowser. Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps')
-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,
+ ];
+ }
+}