@@ -46,166 +46,166 @@ |
||
| 46 | 46 | |
| 47 | 47 | class DashboardController extends Controller { |
| 48 | 48 | |
| 49 | - /** @var IInitialState */ |
|
| 50 | - private $inititalState; |
|
| 51 | - /** @var IEventDispatcher */ |
|
| 52 | - private $eventDispatcher; |
|
| 53 | - /** @var IAppManager */ |
|
| 54 | - private $appManager; |
|
| 55 | - /** @var IManager */ |
|
| 56 | - private $dashboardManager; |
|
| 57 | - /** @var IConfig */ |
|
| 58 | - private $config; |
|
| 59 | - /** @var string */ |
|
| 60 | - private $userId; |
|
| 61 | - /** |
|
| 62 | - * @var BackgroundService |
|
| 63 | - */ |
|
| 64 | - private $backgroundService; |
|
| 65 | - |
|
| 66 | - public function __construct( |
|
| 67 | - string $appName, |
|
| 68 | - IRequest $request, |
|
| 69 | - IInitialState $initialState, |
|
| 70 | - IEventDispatcher $eventDispatcher, |
|
| 71 | - IAppManager $appManager, |
|
| 72 | - IManager $dashboardManager, |
|
| 73 | - IConfig $config, |
|
| 74 | - BackgroundService $backgroundService, |
|
| 75 | - $userId |
|
| 76 | - ) { |
|
| 77 | - parent::__construct($appName, $request); |
|
| 78 | - |
|
| 79 | - $this->inititalState = $initialState; |
|
| 80 | - $this->eventDispatcher = $eventDispatcher; |
|
| 81 | - $this->appManager = $appManager; |
|
| 82 | - $this->dashboardManager = $dashboardManager; |
|
| 83 | - $this->config = $config; |
|
| 84 | - $this->backgroundService = $backgroundService; |
|
| 85 | - $this->userId = $userId; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @NoCSRFRequired |
|
| 90 | - * @NoAdminRequired |
|
| 91 | - * @return TemplateResponse |
|
| 92 | - */ |
|
| 93 | - public function index(): TemplateResponse { |
|
| 94 | - \OCP\Util::addStyle('dashboard', 'dashboard'); |
|
| 95 | - $this->eventDispatcher->dispatchTyped(new LoadSidebar()); |
|
| 96 | - if (class_exists(LoadViewer::class)) { |
|
| 97 | - $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $this->eventDispatcher->dispatchTyped(new RegisterWidgetEvent($this->dashboardManager)); |
|
| 101 | - |
|
| 102 | - $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar'); |
|
| 103 | - $userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)); |
|
| 104 | - $widgets = array_map(function (IWidget $widget) { |
|
| 105 | - return [ |
|
| 106 | - 'id' => $widget->getId(), |
|
| 107 | - 'title' => $widget->getTitle(), |
|
| 108 | - 'iconClass' => $widget->getIconClass(), |
|
| 109 | - 'url' => $widget->getUrl() |
|
| 110 | - ]; |
|
| 111 | - }, $this->dashboardManager->getWidgets()); |
|
| 112 | - $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', ''); |
|
| 113 | - $statuses = json_decode($configStatuses, true); |
|
| 114 | - // We avoid getting an empty array as it will not produce an object in UI's JS |
|
| 115 | - // It does not matter if some statuses are missing from the array, missing ones are considered enabled |
|
| 116 | - $statuses = ($statuses && count($statuses) > 0) ? $statuses : ['weather' => true]; |
|
| 117 | - |
|
| 118 | - // if theming app is enabled and wants to override default, we pass it |
|
| 119 | - $themingDefaultBackground = $this->appManager->isEnabledForUser('theming') |
|
| 120 | - ? $this->config->getAppValue('theming', 'backgroundMime', '') |
|
| 121 | - : ''; |
|
| 122 | - $this->inititalState->provideInitialState('themingDefaultBackground', $themingDefaultBackground); |
|
| 123 | - $this->inititalState->provideInitialState('panels', $widgets); |
|
| 124 | - $this->inititalState->provideInitialState('statuses', $statuses); |
|
| 125 | - $this->inititalState->provideInitialState('layout', $userLayout); |
|
| 126 | - $this->inititalState->provideInitialState('firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1'); |
|
| 127 | - $this->inititalState->provideInitialState('shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS); |
|
| 128 | - $this->inititalState->provideInitialState('background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default')); |
|
| 129 | - $this->inititalState->provideInitialState('version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0)); |
|
| 130 | - $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0'); |
|
| 131 | - |
|
| 132 | - $response = new TemplateResponse('dashboard', 'index'); |
|
| 133 | - |
|
| 134 | - // For the weather widget we should allow the geolocation |
|
| 135 | - $featurePolicy = new Http\FeaturePolicy(); |
|
| 136 | - $featurePolicy->addAllowedGeoLocationDomain('\'self\''); |
|
| 137 | - $response->setFeaturePolicy($featurePolicy); |
|
| 138 | - |
|
| 139 | - return $response; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @NoAdminRequired |
|
| 144 | - * @param string $layout |
|
| 145 | - * @return JSONResponse |
|
| 146 | - */ |
|
| 147 | - public function updateLayout(string $layout): JSONResponse { |
|
| 148 | - $this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout); |
|
| 149 | - return new JSONResponse(['layout' => $layout]); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @NoAdminRequired |
|
| 154 | - * @param string $statuses |
|
| 155 | - * @return JSONResponse |
|
| 156 | - */ |
|
| 157 | - public function updateStatuses(string $statuses): JSONResponse { |
|
| 158 | - $this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses); |
|
| 159 | - return new JSONResponse(['statuses' => $statuses]); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @NoAdminRequired |
|
| 164 | - */ |
|
| 165 | - public function setBackground(string $type = 'default', string $value = ''): JSONResponse { |
|
| 166 | - $currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', '0'); |
|
| 167 | - try { |
|
| 168 | - switch ($type) { |
|
| 169 | - case 'shipped': |
|
| 170 | - $this->backgroundService->setShippedBackground($value); |
|
| 171 | - break; |
|
| 172 | - case 'custom': |
|
| 173 | - $this->backgroundService->setFileBackground($value); |
|
| 174 | - break; |
|
| 175 | - case 'color': |
|
| 176 | - $this->backgroundService->setColorBackground($value); |
|
| 177 | - break; |
|
| 178 | - case 'default': |
|
| 179 | - $this->backgroundService->setDefaultBackground(); |
|
| 180 | - break; |
|
| 181 | - default: |
|
| 182 | - return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST); |
|
| 183 | - } |
|
| 184 | - } catch (\InvalidArgumentException $e) { |
|
| 185 | - return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
| 186 | - } catch (\Throwable $e) { |
|
| 187 | - return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 188 | - } |
|
| 189 | - $currentVersion++; |
|
| 190 | - $this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', (string)$currentVersion); |
|
| 191 | - return new JSONResponse([ |
|
| 192 | - 'type' => $type, |
|
| 193 | - 'value' => $value, |
|
| 194 | - 'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion) |
|
| 195 | - ]); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @NoAdminRequired |
|
| 200 | - * @NoCSRFRequired |
|
| 201 | - */ |
|
| 202 | - public function getBackground(): Http\Response { |
|
| 203 | - $file = $this->backgroundService->getBackground(); |
|
| 204 | - if ($file !== null) { |
|
| 205 | - $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]); |
|
| 206 | - $response->cacheFor(24 * 60 * 60); |
|
| 207 | - return $response; |
|
| 208 | - } |
|
| 209 | - return new NotFoundResponse(); |
|
| 210 | - } |
|
| 49 | + /** @var IInitialState */ |
|
| 50 | + private $inititalState; |
|
| 51 | + /** @var IEventDispatcher */ |
|
| 52 | + private $eventDispatcher; |
|
| 53 | + /** @var IAppManager */ |
|
| 54 | + private $appManager; |
|
| 55 | + /** @var IManager */ |
|
| 56 | + private $dashboardManager; |
|
| 57 | + /** @var IConfig */ |
|
| 58 | + private $config; |
|
| 59 | + /** @var string */ |
|
| 60 | + private $userId; |
|
| 61 | + /** |
|
| 62 | + * @var BackgroundService |
|
| 63 | + */ |
|
| 64 | + private $backgroundService; |
|
| 65 | + |
|
| 66 | + public function __construct( |
|
| 67 | + string $appName, |
|
| 68 | + IRequest $request, |
|
| 69 | + IInitialState $initialState, |
|
| 70 | + IEventDispatcher $eventDispatcher, |
|
| 71 | + IAppManager $appManager, |
|
| 72 | + IManager $dashboardManager, |
|
| 73 | + IConfig $config, |
|
| 74 | + BackgroundService $backgroundService, |
|
| 75 | + $userId |
|
| 76 | + ) { |
|
| 77 | + parent::__construct($appName, $request); |
|
| 78 | + |
|
| 79 | + $this->inititalState = $initialState; |
|
| 80 | + $this->eventDispatcher = $eventDispatcher; |
|
| 81 | + $this->appManager = $appManager; |
|
| 82 | + $this->dashboardManager = $dashboardManager; |
|
| 83 | + $this->config = $config; |
|
| 84 | + $this->backgroundService = $backgroundService; |
|
| 85 | + $this->userId = $userId; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @NoCSRFRequired |
|
| 90 | + * @NoAdminRequired |
|
| 91 | + * @return TemplateResponse |
|
| 92 | + */ |
|
| 93 | + public function index(): TemplateResponse { |
|
| 94 | + \OCP\Util::addStyle('dashboard', 'dashboard'); |
|
| 95 | + $this->eventDispatcher->dispatchTyped(new LoadSidebar()); |
|
| 96 | + if (class_exists(LoadViewer::class)) { |
|
| 97 | + $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $this->eventDispatcher->dispatchTyped(new RegisterWidgetEvent($this->dashboardManager)); |
|
| 101 | + |
|
| 102 | + $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar'); |
|
| 103 | + $userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)); |
|
| 104 | + $widgets = array_map(function (IWidget $widget) { |
|
| 105 | + return [ |
|
| 106 | + 'id' => $widget->getId(), |
|
| 107 | + 'title' => $widget->getTitle(), |
|
| 108 | + 'iconClass' => $widget->getIconClass(), |
|
| 109 | + 'url' => $widget->getUrl() |
|
| 110 | + ]; |
|
| 111 | + }, $this->dashboardManager->getWidgets()); |
|
| 112 | + $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', ''); |
|
| 113 | + $statuses = json_decode($configStatuses, true); |
|
| 114 | + // We avoid getting an empty array as it will not produce an object in UI's JS |
|
| 115 | + // It does not matter if some statuses are missing from the array, missing ones are considered enabled |
|
| 116 | + $statuses = ($statuses && count($statuses) > 0) ? $statuses : ['weather' => true]; |
|
| 117 | + |
|
| 118 | + // if theming app is enabled and wants to override default, we pass it |
|
| 119 | + $themingDefaultBackground = $this->appManager->isEnabledForUser('theming') |
|
| 120 | + ? $this->config->getAppValue('theming', 'backgroundMime', '') |
|
| 121 | + : ''; |
|
| 122 | + $this->inititalState->provideInitialState('themingDefaultBackground', $themingDefaultBackground); |
|
| 123 | + $this->inititalState->provideInitialState('panels', $widgets); |
|
| 124 | + $this->inititalState->provideInitialState('statuses', $statuses); |
|
| 125 | + $this->inititalState->provideInitialState('layout', $userLayout); |
|
| 126 | + $this->inititalState->provideInitialState('firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1'); |
|
| 127 | + $this->inititalState->provideInitialState('shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS); |
|
| 128 | + $this->inititalState->provideInitialState('background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default')); |
|
| 129 | + $this->inititalState->provideInitialState('version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0)); |
|
| 130 | + $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0'); |
|
| 131 | + |
|
| 132 | + $response = new TemplateResponse('dashboard', 'index'); |
|
| 133 | + |
|
| 134 | + // For the weather widget we should allow the geolocation |
|
| 135 | + $featurePolicy = new Http\FeaturePolicy(); |
|
| 136 | + $featurePolicy->addAllowedGeoLocationDomain('\'self\''); |
|
| 137 | + $response->setFeaturePolicy($featurePolicy); |
|
| 138 | + |
|
| 139 | + return $response; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @NoAdminRequired |
|
| 144 | + * @param string $layout |
|
| 145 | + * @return JSONResponse |
|
| 146 | + */ |
|
| 147 | + public function updateLayout(string $layout): JSONResponse { |
|
| 148 | + $this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout); |
|
| 149 | + return new JSONResponse(['layout' => $layout]); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @NoAdminRequired |
|
| 154 | + * @param string $statuses |
|
| 155 | + * @return JSONResponse |
|
| 156 | + */ |
|
| 157 | + public function updateStatuses(string $statuses): JSONResponse { |
|
| 158 | + $this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses); |
|
| 159 | + return new JSONResponse(['statuses' => $statuses]); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @NoAdminRequired |
|
| 164 | + */ |
|
| 165 | + public function setBackground(string $type = 'default', string $value = ''): JSONResponse { |
|
| 166 | + $currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', '0'); |
|
| 167 | + try { |
|
| 168 | + switch ($type) { |
|
| 169 | + case 'shipped': |
|
| 170 | + $this->backgroundService->setShippedBackground($value); |
|
| 171 | + break; |
|
| 172 | + case 'custom': |
|
| 173 | + $this->backgroundService->setFileBackground($value); |
|
| 174 | + break; |
|
| 175 | + case 'color': |
|
| 176 | + $this->backgroundService->setColorBackground($value); |
|
| 177 | + break; |
|
| 178 | + case 'default': |
|
| 179 | + $this->backgroundService->setDefaultBackground(); |
|
| 180 | + break; |
|
| 181 | + default: |
|
| 182 | + return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST); |
|
| 183 | + } |
|
| 184 | + } catch (\InvalidArgumentException $e) { |
|
| 185 | + return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
| 186 | + } catch (\Throwable $e) { |
|
| 187 | + return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 188 | + } |
|
| 189 | + $currentVersion++; |
|
| 190 | + $this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', (string)$currentVersion); |
|
| 191 | + return new JSONResponse([ |
|
| 192 | + 'type' => $type, |
|
| 193 | + 'value' => $value, |
|
| 194 | + 'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion) |
|
| 195 | + ]); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @NoAdminRequired |
|
| 200 | + * @NoCSRFRequired |
|
| 201 | + */ |
|
| 202 | + public function getBackground(): Http\Response { |
|
| 203 | + $file = $this->backgroundService->getBackground(); |
|
| 204 | + if ($file !== null) { |
|
| 205 | + $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]); |
|
| 206 | + $response->cacheFor(24 * 60 * 60); |
|
| 207 | + return $response; |
|
| 208 | + } |
|
| 209 | + return new NotFoundResponse(); |
|
| 210 | + } |
|
| 211 | 211 | } |