1
0

ViewConfig.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files\Service;
  7. use OCA\Files\AppInfo\Application;
  8. use OCP\IConfig;
  9. use OCP\IUser;
  10. use OCP\IUserSession;
  11. class ViewConfig {
  12. public const CONFIG_KEY = 'files_views_configs';
  13. public const ALLOWED_CONFIGS = [
  14. [
  15. // The default sorting key for the files list view
  16. 'key' => 'sorting_mode',
  17. // null by default as views can provide default sorting key
  18. // and will fallback to it if user hasn't change it
  19. 'default' => null,
  20. ],
  21. [
  22. // The default sorting direction for the files list view
  23. 'key' => 'sorting_direction',
  24. 'default' => 'asc',
  25. 'allowed' => ['asc', 'desc'],
  26. ],
  27. [
  28. // If the navigation entry for this view is expanded or not
  29. 'key' => 'expanded',
  30. 'default' => true,
  31. 'allowed' => [true, false],
  32. ],
  33. ];
  34. protected IConfig $config;
  35. protected ?IUser $user = null;
  36. public function __construct(IConfig $config, IUserSession $userSession) {
  37. $this->config = $config;
  38. $this->user = $userSession->getUser();
  39. }
  40. /**
  41. * Get the list of all allowed user config keys
  42. * @return string[]
  43. */
  44. public function getAllowedConfigKeys(): array {
  45. return array_map(function ($config) {
  46. return $config['key'];
  47. }, self::ALLOWED_CONFIGS);
  48. }
  49. /**
  50. * Get the list of allowed config values for a given key
  51. *
  52. * @param string $key a valid config key
  53. * @return array
  54. */
  55. private function getAllowedConfigValues(string $key): array {
  56. foreach (self::ALLOWED_CONFIGS as $config) {
  57. if ($config['key'] === $key) {
  58. return $config['allowed'] ?? [];
  59. }
  60. }
  61. return [];
  62. }
  63. /**
  64. * Get the default config value for a given key
  65. *
  66. * @param string $key a valid config key
  67. * @return string|bool|null
  68. */
  69. private function getDefaultConfigValue(string $key) {
  70. foreach (self::ALLOWED_CONFIGS as $config) {
  71. if ($config['key'] === $key) {
  72. return $config['default'];
  73. }
  74. }
  75. return '';
  76. }
  77. /**
  78. * Set a user config
  79. *
  80. * @param string $view
  81. * @param string $key
  82. * @param string|bool $value
  83. * @throws \Exception
  84. * @throws \InvalidArgumentException
  85. */
  86. public function setConfig(string $view, string $key, $value): void {
  87. if ($this->user === null) {
  88. throw new \Exception('No user logged in');
  89. }
  90. if (!$view) {
  91. throw new \Exception('Unknown view');
  92. }
  93. if (!in_array($key, $this->getAllowedConfigKeys())) {
  94. throw new \InvalidArgumentException('Unknown config key');
  95. }
  96. if (!in_array($value, $this->getAllowedConfigValues($key))
  97. && !empty($this->getAllowedConfigValues($key))) {
  98. throw new \InvalidArgumentException('Invalid config value');
  99. }
  100. // Cast boolean values
  101. if (is_bool($this->getDefaultConfigValue($key))) {
  102. $value = $value === '1';
  103. }
  104. $config = $this->getConfigs();
  105. $config[$view][$key] = $value;
  106. $this->config->setUserValue($this->user->getUID(), Application::APP_ID, self::CONFIG_KEY, json_encode($config));
  107. }
  108. /**
  109. * Get the current user configs array for a given view
  110. *
  111. * @return array
  112. */
  113. public function getConfig(string $view): array {
  114. if ($this->user === null) {
  115. throw new \Exception('No user logged in');
  116. }
  117. $userId = $this->user->getUID();
  118. $configs = json_decode($this->config->getUserValue($userId, Application::APP_ID, self::CONFIG_KEY, '[]'), true);
  119. if (!isset($configs[$view])) {
  120. $configs[$view] = [];
  121. }
  122. // Extend undefined values with defaults
  123. return array_reduce(self::ALLOWED_CONFIGS, function ($carry, $config) use ($view, $configs) {
  124. $key = $config['key'];
  125. $carry[$key] = $configs[$view][$key] ?? $this->getDefaultConfigValue($key);
  126. return $carry;
  127. }, []);
  128. }
  129. /**
  130. * Get the current user configs array
  131. *
  132. * @return array
  133. */
  134. public function getConfigs(): array {
  135. if ($this->user === null) {
  136. throw new \Exception('No user logged in');
  137. }
  138. $userId = $this->user->getUID();
  139. $configs = json_decode($this->config->getUserValue($userId, Application::APP_ID, self::CONFIG_KEY, '[]'), true);
  140. $views = array_keys($configs);
  141. return array_reduce($views, function ($carry, $view) use ($configs) {
  142. $carry[$view] = $this->getConfig($view);
  143. return $carry;
  144. }, []);
  145. }
  146. }