Merge pull request #21442 from nextcloud/td/js/accessibilityjs_to_initialstate

Move accessibility js route to static js and initial state
This commit is contained in:
Roeland Jago Douma 2020-06-18 08:30:16 +02:00 committed by GitHub
commit 3ac0484ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 72 deletions

View File

@ -27,7 +27,6 @@
return [
'routes' => [
['name' => 'accessibility#getCss', 'url' => '/css/user-{md5}', 'verb' => 'GET'],
['name' => 'accessibility#getJavascript', 'url' => '/js/accessibility', 'verb' => 'GET'],
],
'ocs' => [
[

View File

@ -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',
);

View File

@ -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',
);

View File

@ -0,0 +1,4 @@
OCA.Accessibility = OCP.InitialState.loadState('accessibility', 'data')
if (OCA.Accessibility.theme !== false) {
document.body.classList.add(OCA.Accessibility.theme);
}

View File

@ -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;
});
}
}

View File

@ -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;
@ -204,44 +203,6 @@ class AccessibilityController extends Controller {
return $response;
}
/**
* @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
*

View File

@ -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,
];
}
}