Merge pull request #11533 from nextcloud/accessibility-cache-fixes

Added cache override to ensure an always up-to-date accessibility css
This commit is contained in:
Morris Jobke 2018-10-24 15:04:29 +02:00 committed by GitHub
commit 2ec5d2a6de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 20 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -54,8 +54,10 @@ class Application extends App {
$loggedUser = $this->userSession->getUser(); $loggedUser = $this->userSession->getUser();
if (!is_null($loggedUser)) { if (!is_null($loggedUser)) {
$userValues = $this->config->getUserKeys($loggedUser->getUID(), $this->appName); $userValues = $this->config->getUserKeys($loggedUser->getUID(), $this->appName);
// we want to check if any theme or font is enabled.
if (count($userValues) > 0) { if (count($userValues) > 0) {
$linkToCSS = $this->urlGenerator->linkToRoute($this->appName . '.accessibility.getCss', ['md5' => md5(implode('-', $userValues))]); $hash = $this->config->getUserValue($loggedUser->getUID(), $this->appName, 'icons-css', md5(implode('-', $userValues)));
$linkToCSS = $this->urlGenerator->linkToRoute($this->appName . '.accessibility.getCss', ['md5' => $hash]);
\OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]); \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]);
} }
} }

View File

@ -182,6 +182,9 @@ class AccessibilityController extends Controller {
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$response->addHeader('Pragma', 'cache'); $response->addHeader('Pragma', 'cache');
// store current cache hash
$this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css));
return $response; return $response;
} }

View File

@ -36,6 +36,9 @@ class ConfigController extends OCSController {
/** @var string */ /** @var string */
protected $appName; protected $appName;
/** @var string */
protected $userId;
/** @var string */ /** @var string */
protected $serverRoot; protected $serverRoot;
@ -67,6 +70,7 @@ class ConfigController extends OCSController {
$this->config = $config; $this->config = $config;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->accessibilityProvider = $accessibilityProvider; $this->accessibilityProvider = $accessibilityProvider;
$this->userId = $userSession->getUser()->getUID();
} }
/** /**
@ -79,8 +83,8 @@ class ConfigController extends OCSController {
*/ */
public function getConfig(): DataResponse { public function getConfig(): DataResponse {
return new DataResponse([ return new DataResponse([
'theme' => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false), 'theme' => $this->config->getUserValue($this->userId, $this->appName, 'theme', false),
'font' => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false) 'font' => $this->config->getUserValue($this->userId, $this->appName, 'font', false)
]); ]);
} }
@ -95,20 +99,28 @@ class ConfigController extends OCSController {
*/ */
public function setConfig(string $key, $value): DataResponse { public function setConfig(string $key, $value): DataResponse {
if ($key === 'theme' || $key === 'font') { if ($key === 'theme' || $key === 'font') {
$themes = $this->accessibilityProvider->getThemes();
$fonts = $this->accessibilityProvider->getFonts();
if ($value === false) { if ($value === false) {
$this->config->deleteUserValue($this->userSession->getUser()->getUID(), $this->appName, $key); $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(); return new DataResponse();
} }
$themes = $this->accessibilityProvider->getThemes();
$fonts = $this->accessibilityProvider->getFonts();
$availableOptions = array_map(function($option) { $availableOptions = array_map(function($option) {
return $option['id']; return $option['id'];
}, array_merge($themes, $fonts)); }, array_merge($themes, $fonts));
if (in_array($value, $availableOptions)) { if (in_array($value, $availableOptions)) {
$this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, $key, $value); $this->config->setUserValue($this->userId, $this->appName, $key, $value);
return new DataResponse(); return new DataResponse();
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "accessibility", "name": "accessibility",
"version": "1.0.1", "version": "1.0.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,7 +1,7 @@
{ {
"name": "accessibility", "name": "accessibility",
"description": "Provides multiple accessibilities options to ease your use of nextcloud", "description": "Provides multiple accessibilities options to ease your use of nextcloud",
"version": "1.0.1", "version": "1.0.2",
"author": "John Molakvoæ <skjnldsv@protonmail.com>", "author": "John Molakvoæ <skjnldsv@protonmail.com>",
"license": "agpl", "license": "agpl",
"private": true, "private": true,

View File

@ -24,7 +24,7 @@ import preview from './components/itemPreview';
import axios from 'axios'; import axios from 'axios';
export default { export default {
name: 'app', name: 'Accessibility',
components: { preview }, components: { preview },
beforeMount() { beforeMount() {
// importing server data into the app // importing server data into the app
@ -54,7 +54,7 @@ export default {
}; };
}, },
tokenHeaders() { tokenHeaders() {
return { headers: { requesttoken: OC.requestToken } } return { headers: { requesttoken: OC.requestToken } };
} }
}, },
methods: { methods: {
@ -73,8 +73,11 @@ export default {
* @param {string} id the data of the change * @param {string} id the data of the change
*/ */
selectItem(type, id) { selectItem(type, id) {
axios axios.post(
.post(OC.linkToOCS('apps/accessibility/api/v1/config', 2) + type, {value: id}, this.tokenHeaders) OC.linkToOCS('apps/accessibility/api/v1/config', 2) + type,
{ value: id },
this.tokenHeaders
)
.then(response => { .then(response => {
this.serverData[type] = id; this.serverData[type] = id;
@ -84,11 +87,23 @@ export default {
// insert new css // insert new css
let link = document.createElement('link'); let link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.href = OC.generateUrl('/apps/accessibility/css/user-style.css'); link.href = OC.generateUrl('/apps/accessibility/css/user-style.css') + '?v=' + new Date().getTime();
document.head.appendChild(link) document.head.appendChild(link);
} else { } else {
// force update // compare arrays
link.href = link.href.split('?')[0] + '?v=' + new Date().getTime(); if (
JSON.stringify(Object.values(this.selected)) ===
JSON.stringify([false, false])
) {
// if nothing is selected, blindly remove the css
link.remove();
} else {
// force update
link.href =
link.href.split('?')[0] +
'?v=' +
new Date().getTime();
}
} }
}) })
.catch(err => { .catch(err => {