diff options
Diffstat (limited to 'apps/files/lib')
-rw-r--r-- | apps/files/lib/AppInfo/Application.php | 4 | ||||
-rw-r--r-- | apps/files/lib/Controller/ApiController.php | 67 | ||||
-rw-r--r-- | apps/files/lib/Controller/ViewController.php | 52 | ||||
-rw-r--r-- | apps/files/lib/Service/UserConfig.php | 142 |
4 files changed, 212 insertions, 53 deletions
diff --git a/apps/files/lib/AppInfo/Application.php b/apps/files/lib/AppInfo/Application.php index f2104be4df8..01fe46bb877 100644 --- a/apps/files/lib/AppInfo/Application.php +++ b/apps/files/lib/AppInfo/Application.php @@ -47,6 +47,7 @@ use OCA\Files\Listener\LoadSidebarListener; use OCA\Files\Notification\Notifier; use OCA\Files\Search\FilesSearchProvider; use OCA\Files\Service\TagService; +use OCA\Files\Service\UserConfig; use OCP\Activity\IManager as IActivityManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; @@ -88,7 +89,8 @@ class Application extends App implements IBootstrap { $c->get(IPreview::class), $c->get(IShareManager::class), $c->get(IConfig::class), - $server->getUserFolder() + $server->getUserFolder(), + $c->get(UserConfig::class), ); }); diff --git a/apps/files/lib/Controller/ApiController.php b/apps/files/lib/Controller/ApiController.php index a87b4f9490a..e9d15018d03 100644 --- a/apps/files/lib/Controller/ApiController.php +++ b/apps/files/lib/Controller/ApiController.php @@ -39,6 +39,7 @@ namespace OCA\Files\Controller; use OC\Files\Node\Node; use OCA\Files\Service\TagService; +use OCA\Files\Service\UserConfig; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -61,18 +62,13 @@ use OCP\Share\IShare; * @package OCA\Files\Controller */ class ApiController extends Controller { - /** @var TagService */ - private $tagService; - /** @var IManager * */ - private $shareManager; - /** @var IPreview */ - private $previewManager; - /** @var IUserSession */ - private $userSession; - /** @var IConfig */ - private $config; - /** @var Folder */ - private $userFolder; + private TagService $tagService; + private IManager $shareManager; + private IPreview $previewManager; + private IUserSession $userSession; + private IConfig $config; + private Folder $userFolder; + private UserConfig $userConfig; /** * @param string $appName @@ -91,7 +87,8 @@ class ApiController extends Controller { IPreview $previewManager, IManager $shareManager, IConfig $config, - Folder $userFolder) { + Folder $userFolder, + UserConfig $userConfig) { parent::__construct($appName, $request); $this->userSession = $userSession; $this->tagService = $tagService; @@ -99,6 +96,7 @@ class ApiController extends Controller { $this->shareManager = $shareManager; $this->config = $config; $this->userFolder = $userFolder; + $this->userConfig = $userConfig; } /** @@ -283,16 +281,47 @@ class ApiController extends Controller { } /** + * Toggle default files user config + * + * @NoAdminRequired + * + * @param bool $key + * @param string|bool $value + * @return JSONResponse + */ + public function setConfig(string $key, string|bool $value): JSONResponse { + try { + $this->userConfig->setConfig($key, $value); + } catch (\InvalidArgumentException $e) { + return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); + } + + return new JSONResponse(['message' => 'ok', 'data' => ['key' => $key, 'value' => $value]]); + } + + + /** + * Get the user config + * + * @NoAdminRequired + * + * @return JSONResponse + */ + public function getConfigs(): JSONResponse { + return new JSONResponse(['message' => 'ok', 'data' => $this->userConfig->getConfigs()]); + } + + /** * Toggle default for showing/hiding hidden files * * @NoAdminRequired * - * @param bool $show + * @param bool $value * @return Response * @throws \OCP\PreConditionNotMetException */ - public function showHiddenFiles(bool $show): Response { - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', $show ? '1' : '0'); + public function showHiddenFiles(bool $value): Response { + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', $value ? '1' : '0'); return new Response(); } @@ -301,12 +330,12 @@ class ApiController extends Controller { * * @NoAdminRequired * - * @param bool $crop + * @param bool $value * @return Response * @throws \OCP\PreConditionNotMetException */ - public function cropImagePreviews(bool $crop): Response { - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', $crop ? '1' : '0'); + public function cropImagePreviews(bool $value): Response { + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', $value ? '1' : '0'); return new Response(); } diff --git a/apps/files/lib/Controller/ViewController.php b/apps/files/lib/Controller/ViewController.php index 63863b3e367..4e81b630bab 100644 --- a/apps/files/lib/Controller/ViewController.php +++ b/apps/files/lib/Controller/ViewController.php @@ -36,8 +36,10 @@ namespace OCA\Files\Controller; use OCA\Files\Activity\Helper; +use OCA\Files\AppInfo\Application; use OCA\Files\Event\LoadAdditionalScriptsEvent; use OCA\Files\Event\LoadSidebar; +use OCA\Files\Service\UserConfig; use OCA\Viewer\Event\LoadViewer; use OCP\App\IAppManager; use OCP\AppFramework\Controller; @@ -65,32 +67,18 @@ use OCP\Share\IManager; * @package OCA\Files\Controller */ class ViewController extends Controller { - /** @var string */ - protected $appName; - /** @var IRequest */ - protected $request; - /** @var IURLGenerator */ - protected $urlGenerator; - /** @var IL10N */ - protected $l10n; - /** @var IConfig */ - protected $config; - /** @var IEventDispatcher */ - protected $eventDispatcher; - /** @var IUserSession */ - protected $userSession; - /** @var IAppManager */ - protected $appManager; - /** @var IRootFolder */ - protected $rootFolder; - /** @var Helper */ - protected $activityHelper; - /** @var IInitialState */ - private $initialState; - /** @var ITemplateManager */ - private $templateManager; - /** @var IManager */ - private $shareManager; + private IURLGenerator $urlGenerator; + private IL10N $l10n; + private IConfig $config; + private IEventDispatcher $eventDispatcher; + private IUserSession $userSession; + private IAppManager $appManager; + private IRootFolder $rootFolder; + private Helper $activityHelper; + private IInitialState $initialState; + private ITemplateManager $templateManager; + private IManager $shareManager; + private UserConfig $userConfig; public function __construct(string $appName, IRequest $request, @@ -104,11 +92,10 @@ class ViewController extends Controller { Helper $activityHelper, IInitialState $initialState, ITemplateManager $templateManager, - IManager $shareManager + IManager $shareManager, + UserConfig $userConfig ) { parent::__construct($appName, $request); - $this->appName = $appName; - $this->request = $request; $this->urlGenerator = $urlGenerator; $this->l10n = $l10n; $this->config = $config; @@ -120,6 +107,7 @@ class ViewController extends Controller { $this->initialState = $initialState; $this->templateManager = $templateManager; $this->shareManager = $shareManager; + $this->userConfig = $userConfig; } /** @@ -236,7 +224,6 @@ class ViewController extends Controller { 'folderPosition' => $sortingValue, 'name' => basename($favElement), 'icon' => 'folder', - 'quickaccesselement' => 'true' ]; array_push($favoritesSublistArray, $element); @@ -266,11 +253,10 @@ class ViewController extends Controller { $nav->assign('quota', $storageInfo['quota']); $nav->assign('usage_relative', $storageInfo['relative']); - $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . rawurlencode($userId))); - $contentItems = []; $this->initialState->provideInitialState('navigation', $navItems); + $this->initialState->provideInitialState('config', $this->userConfig->getConfigs()); // render the container content for every navigation item foreach ($navItems as $item) { @@ -328,7 +314,7 @@ class ViewController extends Controller { $params['hiddenFields'] = $event->getHiddenFields(); $response = new TemplateResponse( - $this->appName, + Application::APP_ID, 'index', $params ); diff --git a/apps/files/lib/Service/UserConfig.php b/apps/files/lib/Service/UserConfig.php new file mode 100644 index 00000000000..e3b863c7333 --- /dev/null +++ b/apps/files/lib/Service/UserConfig.php @@ -0,0 +1,142 @@ +<?php +/** + * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @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\Files\Service; + +use OCA\Files\AppInfo\Application; +use OCP\IConfig; +use OCP\IUser; +use OCP\IUserSession; + +class UserConfig { + const ALLOWED_CONFIGS = [ + [ + 'key' => 'crop_image_previews', + 'default' => true, + 'allowed' => [true, false], + ], + [ + 'key' => 'show_hidden', + 'default' => false, + 'allowed' => [true, false], + ], + ]; + + private IConfig $config; + private IUser|null $user; + + public function __construct(IConfig $config, IUserSession $userSession) { + $this->config = $config; + $this->user = $userSession->getUser(); + } + + /** + * Get the list of all allowed user config keys + * @return string[] + */ + public function getAllowedConfigKeys(): array { + return array_map(function($config) { + return $config['key']; + }, self::ALLOWED_CONFIGS); + } + + /** + * Get the list of allowed config values for a given key + * + * @param string $key a valid config key + * @return array + */ + private function getAllowedConfigValues(string $key): array { + foreach (self::ALLOWED_CONFIGS as $config) { + if ($config['key'] === $key) { + return $config['allowed']; + } + } + return []; + } + + /** + * Get the default config value for a given key + * + * @param string $key a valid config key + * @return string|bool + */ + private function getDefaultConfigValue(string $key): string|bool { + foreach (self::ALLOWED_CONFIGS as $config) { + if ($config['key'] === $key) { + return $config['default']; + } + } + return ''; + } + + /** + * Set a user config + * + * @param string $key + * @param string $value + * @throws \Exception + * @throws \InvalidArgumentException + */ + public function setConfig($key, $value) { + if (!$this->user) { + throw new \Exception('No user logged in'); + } + + if (!in_array($key, $this->getAllowedConfigKeys())) { + throw new \InvalidArgumentException('Unknown config key'); + } + + if (!in_array($value, $this->getAllowedConfigValues($key))) { + throw new \InvalidArgumentException('Invalid config value'); + } + + if (is_bool($value)) { + $value = $value ? '1' : '0'; + } + + $this->config->setUserValue($this->user->getUID(), Application::APP_ID, $key, $value); + } + + /** + * Get the current user configs array + * + * @return array + */ + public function getConfigs(): array { + if (!$this->user) { + throw new \Exception('No user logged in'); + } + + $userId = $this->user->getUID(); + $userConfigs = array_map(function(string $key) use ($userId): string|bool { + $value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key)); + // If the default is expected to be a boolean, we need to cast the value + if (is_bool($this->getDefaultConfigValue($key))) { + return $value === '1'; + } + return $value; + }, $this->getAllowedConfigKeys()); + + return array_combine($this->getAllowedConfigKeys(), $userConfigs); + } +} |