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 ?IUser $user = null;
  35. public function __construct(
  36. protected IConfig $config,
  37. IUserSession $userSession,
  38. ) {
  39. $this->user = $userSession->getUser();
  40. }
  41. /**
  42. * Get the list of all allowed user config keys
  43. * @return string[]
  44. */
  45. public function getAllowedConfigKeys(): array {
  46. return array_map(function ($config) {
  47. return $config['key'];
  48. }, self::ALLOWED_CONFIGS);
  49. }
  50. /**
  51. * Get the list of allowed config values for a given key
  52. *
  53. * @param string $key a valid config key
  54. * @return array
  55. */
  56. private function getAllowedConfigValues(string $key): array {
  57. foreach (self::ALLOWED_CONFIGS as $config) {
  58. if ($config['key'] === $key) {
  59. return $config['allowed'] ?? [];
  60. }
  61. }
  62. return [];
  63. }
  64. /**
  65. * Get the default config value for a given key
  66. *
  67. * @param string $key a valid config key
  68. * @return string|bool|null
  69. */
  70. private function getDefaultConfigValue(string $key) {
  71. foreach (self::ALLOWED_CONFIGS as $config) {
  72. if ($config['key'] === $key) {
  73. return $config['default'];
  74. }
  75. }
  76. return '';
  77. }
  78. /**
  79. * Set a user config
  80. *
  81. * @param string $view
  82. * @param string $key
  83. * @param string|bool $value
  84. * @throws \Exception
  85. * @throws \InvalidArgumentException
  86. */
  87. public function setConfig(string $view, string $key, $value): void {
  88. if ($this->user === null) {
  89. throw new \Exception('No user logged in');
  90. }
  91. if (!$view) {
  92. throw new \Exception('Unknown view');
  93. }
  94. if (!in_array($key, $this->getAllowedConfigKeys())) {
  95. throw new \InvalidArgumentException('Unknown config key');
  96. }
  97. if (!in_array($value, $this->getAllowedConfigValues($key))
  98. && !empty($this->getAllowedConfigValues($key))) {
  99. throw new \InvalidArgumentException('Invalid config value');
  100. }
  101. // Cast boolean values
  102. if (is_bool($this->getDefaultConfigValue($key))) {
  103. $value = $value === '1';
  104. }
  105. $config = $this->getConfigs();
  106. $config[$view][$key] = $value;
  107. $this->config->setUserValue($this->user->getUID(), Application::APP_ID, self::CONFIG_KEY, json_encode($config));
  108. }
  109. /**
  110. * Get the current user configs array for a given view
  111. *
  112. * @return array
  113. */
  114. public function getConfig(string $view): array {
  115. if ($this->user === null) {
  116. throw new \Exception('No user logged in');
  117. }
  118. $userId = $this->user->getUID();
  119. $configs = json_decode($this->config->getUserValue($userId, Application::APP_ID, self::CONFIG_KEY, '[]'), true);
  120. if (!isset($configs[$view])) {
  121. $configs[$view] = [];
  122. }
  123. // Extend undefined values with defaults
  124. return array_reduce(self::ALLOWED_CONFIGS, function ($carry, $config) use ($view, $configs) {
  125. $key = $config['key'];
  126. $carry[$key] = $configs[$view][$key] ?? $this->getDefaultConfigValue($key);
  127. return $carry;
  128. }, []);
  129. }
  130. /**
  131. * Get the current user configs array
  132. *
  133. * @return array
  134. */
  135. public function getConfigs(): array {
  136. if ($this->user === null) {
  137. throw new \Exception('No user logged in');
  138. }
  139. $userId = $this->user->getUID();
  140. $configs = json_decode($this->config->getUserValue($userId, Application::APP_ID, self::CONFIG_KEY, '[]'), true);
  141. $views = array_keys($configs);
  142. return array_reduce($views, function ($carry, $view) use ($configs) {
  143. $carry[$view] = $this->getConfig($view);
  144. return $carry;
  145. }, []);
  146. }
  147. }