@@ -16,58 +16,58 @@ |
||
| 16 | 16 | use OCP\IUserManager; |
| 17 | 17 | |
| 18 | 18 | class DashboardService { |
| 19 | - public function __construct( |
|
| 20 | - private IConfig $config, |
|
| 21 | - private IAppConfig $appConfig, |
|
| 22 | - private ?string $userId, |
|
| 23 | - private IUserManager $userManager, |
|
| 24 | - private IAccountManager $accountManager, |
|
| 25 | - ) { |
|
| 19 | + public function __construct( |
|
| 20 | + private IConfig $config, |
|
| 21 | + private IAppConfig $appConfig, |
|
| 22 | + private ?string $userId, |
|
| 23 | + private IUserManager $userManager, |
|
| 24 | + private IAccountManager $accountManager, |
|
| 25 | + ) { |
|
| 26 | 26 | |
| 27 | - } |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @return list<string> |
|
| 31 | - */ |
|
| 32 | - public function getLayout(): array { |
|
| 33 | - $systemDefault = $this->appConfig->getAppValueString('layout', 'recommendations,spreed,mail,calendar'); |
|
| 34 | - return array_values(array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== '')); |
|
| 35 | - } |
|
| 29 | + /** |
|
| 30 | + * @return list<string> |
|
| 31 | + */ |
|
| 32 | + public function getLayout(): array { |
|
| 33 | + $systemDefault = $this->appConfig->getAppValueString('layout', 'recommendations,spreed,mail,calendar'); |
|
| 34 | + return array_values(array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== '')); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @return list<string> |
|
| 39 | - */ |
|
| 40 | - public function getStatuses() { |
|
| 41 | - $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', ''); |
|
| 42 | - try { |
|
| 43 | - // Parse the old format |
|
| 44 | - /** @var array<string, bool> $statuses */ |
|
| 45 | - $statuses = json_decode($configStatuses, true, 512, JSON_THROW_ON_ERROR); |
|
| 46 | - // We avoid getting an empty array as it will not produce an object in UI's JS |
|
| 47 | - return array_keys(array_filter($statuses, static fn (bool $value) => $value)); |
|
| 48 | - } catch (JsonException $e) { |
|
| 49 | - return array_values(array_filter(explode(',', $configStatuses), fn (string $value) => $value !== '')); |
|
| 50 | - } |
|
| 51 | - } |
|
| 37 | + /** |
|
| 38 | + * @return list<string> |
|
| 39 | + */ |
|
| 40 | + public function getStatuses() { |
|
| 41 | + $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', ''); |
|
| 42 | + try { |
|
| 43 | + // Parse the old format |
|
| 44 | + /** @var array<string, bool> $statuses */ |
|
| 45 | + $statuses = json_decode($configStatuses, true, 512, JSON_THROW_ON_ERROR); |
|
| 46 | + // We avoid getting an empty array as it will not produce an object in UI's JS |
|
| 47 | + return array_keys(array_filter($statuses, static fn (bool $value) => $value)); |
|
| 48 | + } catch (JsonException $e) { |
|
| 49 | + return array_values(array_filter(explode(',', $configStatuses), fn (string $value) => $value !== '')); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - public function getBirthdate(): string { |
|
| 54 | - if ($this->userId === null) { |
|
| 55 | - return ''; |
|
| 56 | - } |
|
| 53 | + public function getBirthdate(): string { |
|
| 54 | + if ($this->userId === null) { |
|
| 55 | + return ''; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $user = $this->userManager->get($this->userId); |
|
| 59 | - if ($user === null) { |
|
| 60 | - return ''; |
|
| 61 | - } |
|
| 58 | + $user = $this->userManager->get($this->userId); |
|
| 59 | + if ($user === null) { |
|
| 60 | + return ''; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - $account = $this->accountManager->getAccount($user); |
|
| 63 | + $account = $this->accountManager->getAccount($user); |
|
| 64 | 64 | |
| 65 | - try { |
|
| 66 | - $birthdate = $account->getProperty(IAccountManager::PROPERTY_BIRTHDATE); |
|
| 67 | - } catch (PropertyDoesNotExistException) { |
|
| 68 | - return ''; |
|
| 69 | - } |
|
| 65 | + try { |
|
| 66 | + $birthdate = $account->getProperty(IAccountManager::PROPERTY_BIRTHDATE); |
|
| 67 | + } catch (PropertyDoesNotExistException) { |
|
| 68 | + return ''; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - return $birthdate->getValue(); |
|
| 72 | - } |
|
| 71 | + return $birthdate->getValue(); |
|
| 72 | + } |
|
| 73 | 73 | } |
@@ -40,197 +40,197 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | class DashboardApiController extends OCSController { |
| 42 | 42 | |
| 43 | - public function __construct( |
|
| 44 | - string $appName, |
|
| 45 | - IRequest $request, |
|
| 46 | - private IManager $dashboardManager, |
|
| 47 | - private IAppConfig $appConfig, |
|
| 48 | - private IConfig $config, |
|
| 49 | - private ?string $userId, |
|
| 50 | - private DashboardService $service, |
|
| 51 | - ) { |
|
| 52 | - parent::__construct($appName, $request); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param string[] $widgetIds Limit widgets to given ids |
|
| 57 | - * @return IWidget[] |
|
| 58 | - */ |
|
| 59 | - private function getShownWidgets(array $widgetIds): array { |
|
| 60 | - if (empty($widgetIds)) { |
|
| 61 | - $systemDefault = $this->appConfig->getAppValueString('layout', 'recommendations,spreed,mail,calendar'); |
|
| 62 | - $widgetIds = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - return array_filter( |
|
| 66 | - $this->dashboardManager->getWidgets(), |
|
| 67 | - static function (IWidget $widget) use ($widgetIds) { |
|
| 68 | - return in_array($widget->getId(), $widgetIds); |
|
| 69 | - }, |
|
| 70 | - ); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Get the items for the widgets |
|
| 75 | - * |
|
| 76 | - * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items |
|
| 77 | - * @param int $limit Limit number of result items per widget |
|
| 78 | - * @psalm-param int<1, 30> $limit |
|
| 79 | - * @param list<string> $widgets Limit results to specific widgets |
|
| 80 | - * @return DataResponse<Http::STATUS_OK, array<string, list<DashboardWidgetItem>>, array{}> |
|
| 81 | - * |
|
| 82 | - * 200: Widget items returned |
|
| 83 | - */ |
|
| 84 | - #[NoAdminRequired] |
|
| 85 | - #[NoCSRFRequired] |
|
| 86 | - #[ApiRoute(verb: 'GET', url: '/api/v1/widget-items')] |
|
| 87 | - public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse { |
|
| 88 | - $items = []; |
|
| 89 | - $widgets = $this->getShownWidgets($widgets); |
|
| 90 | - foreach ($widgets as $widget) { |
|
| 91 | - if ($widget instanceof IAPIWidget) { |
|
| 92 | - $items[$widget->getId()] = array_map(static function (WidgetItem $item) { |
|
| 93 | - return $item->jsonSerialize(); |
|
| 94 | - }, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)); |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - return new DataResponse($items); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the items for the widgets |
|
| 103 | - * |
|
| 104 | - * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items |
|
| 105 | - * @param int $limit Limit number of result items per widget, not more than 30 are allowed |
|
| 106 | - * @psalm-param int<1, 30> $limit |
|
| 107 | - * @param list<string> $widgets Limit results to specific widgets |
|
| 108 | - * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}> |
|
| 109 | - * |
|
| 110 | - * 200: Widget items returned |
|
| 111 | - */ |
|
| 112 | - #[NoAdminRequired] |
|
| 113 | - #[NoCSRFRequired] |
|
| 114 | - #[ApiRoute(verb: 'GET', url: '/api/v2/widget-items')] |
|
| 115 | - public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse { |
|
| 116 | - $items = []; |
|
| 117 | - $widgets = $this->getShownWidgets($widgets); |
|
| 118 | - foreach ($widgets as $widget) { |
|
| 119 | - if ($widget instanceof IAPIWidgetV2) { |
|
| 120 | - $items[$widget->getId()] = $widget |
|
| 121 | - ->getItemsV2($this->userId, $sinceIds[$widget->getId()] ?? null, $limit) |
|
| 122 | - ->jsonSerialize(); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return new DataResponse($items); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get the widgets |
|
| 131 | - * |
|
| 132 | - * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidget>, array{}> |
|
| 133 | - * |
|
| 134 | - * 200: Widgets returned |
|
| 135 | - */ |
|
| 136 | - #[NoAdminRequired] |
|
| 137 | - #[NoCSRFRequired] |
|
| 138 | - #[ApiRoute(verb: 'GET', url: '/api/v1/widgets')] |
|
| 139 | - public function getWidgets(): DataResponse { |
|
| 140 | - $widgets = $this->dashboardManager->getWidgets(); |
|
| 141 | - |
|
| 142 | - $items = array_map(function (IWidget $widget) { |
|
| 143 | - $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault(); |
|
| 144 | - $data = [ |
|
| 145 | - 'id' => $widget->getId(), |
|
| 146 | - 'title' => $widget->getTitle(), |
|
| 147 | - 'order' => $widget->getOrder(), |
|
| 148 | - 'icon_class' => $widget->getIconClass(), |
|
| 149 | - 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '', |
|
| 150 | - 'widget_url' => $widget->getUrl(), |
|
| 151 | - 'item_icons_round' => $options->withRoundItemIcons(), |
|
| 152 | - 'item_api_versions' => [], |
|
| 153 | - 'reload_interval' => 0, |
|
| 154 | - ]; |
|
| 155 | - if ($widget instanceof IButtonWidget) { |
|
| 156 | - $data += [ |
|
| 157 | - 'buttons' => array_map(function (WidgetButton $button) { |
|
| 158 | - return [ |
|
| 159 | - 'type' => $button->getType(), |
|
| 160 | - 'text' => $button->getText(), |
|
| 161 | - 'link' => $button->getLink(), |
|
| 162 | - ]; |
|
| 163 | - }, $widget->getWidgetButtons($this->userId)), |
|
| 164 | - ]; |
|
| 165 | - } |
|
| 166 | - if ($widget instanceof IReloadableWidget) { |
|
| 167 | - $data['reload_interval'] = $widget->getReloadInterval(); |
|
| 168 | - } |
|
| 169 | - if ($widget instanceof IAPIWidget) { |
|
| 170 | - $data['item_api_versions'][] = 1; |
|
| 171 | - } |
|
| 172 | - if ($widget instanceof IAPIWidgetV2) { |
|
| 173 | - $data['item_api_versions'][] = 2; |
|
| 174 | - } |
|
| 175 | - return $data; |
|
| 176 | - }, $widgets); |
|
| 177 | - |
|
| 178 | - return new DataResponse($items); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get the layout |
|
| 183 | - * |
|
| 184 | - * @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}> |
|
| 185 | - * |
|
| 186 | - * 200: Layout returned |
|
| 187 | - */ |
|
| 188 | - #[NoAdminRequired] |
|
| 189 | - #[ApiRoute(verb: 'GET', url: '/api/v3/layout')] |
|
| 190 | - public function getLayout(): DataResponse { |
|
| 191 | - return new DataResponse(['layout' => $this->service->getLayout()]); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Update the layout |
|
| 196 | - * |
|
| 197 | - * @param list<string> $layout The new layout |
|
| 198 | - * @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}> |
|
| 199 | - * |
|
| 200 | - * 200: Statuses updated successfully |
|
| 201 | - */ |
|
| 202 | - #[NoAdminRequired] |
|
| 203 | - #[ApiRoute(verb: 'POST', url: '/api/v3/layout')] |
|
| 204 | - public function updateLayout(array $layout): DataResponse { |
|
| 205 | - $this->config->setUserValue($this->userId, 'dashboard', 'layout', implode(',', $layout)); |
|
| 206 | - return new DataResponse(['layout' => $layout]); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get the statuses |
|
| 211 | - * |
|
| 212 | - * @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}> |
|
| 213 | - * |
|
| 214 | - * 200: Statuses returned |
|
| 215 | - */ |
|
| 216 | - #[NoAdminRequired] |
|
| 217 | - #[ApiRoute(verb: 'GET', url: '/api/v3/statuses')] |
|
| 218 | - public function getStatuses(): DataResponse { |
|
| 219 | - return new DataResponse(['statuses' => $this->service->getStatuses()]); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Update the statuses |
|
| 224 | - * |
|
| 225 | - * @param list<string> $statuses The new statuses |
|
| 226 | - * @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}> |
|
| 227 | - * |
|
| 228 | - * 200: Statuses updated successfully |
|
| 229 | - */ |
|
| 230 | - #[NoAdminRequired] |
|
| 231 | - #[ApiRoute(verb: 'POST', url: '/api/v3/statuses')] |
|
| 232 | - public function updateStatuses(array $statuses): DataResponse { |
|
| 233 | - $this->config->setUserValue($this->userId, 'dashboard', 'statuses', implode(',', $statuses)); |
|
| 234 | - return new DataResponse(['statuses' => $statuses]); |
|
| 235 | - } |
|
| 43 | + public function __construct( |
|
| 44 | + string $appName, |
|
| 45 | + IRequest $request, |
|
| 46 | + private IManager $dashboardManager, |
|
| 47 | + private IAppConfig $appConfig, |
|
| 48 | + private IConfig $config, |
|
| 49 | + private ?string $userId, |
|
| 50 | + private DashboardService $service, |
|
| 51 | + ) { |
|
| 52 | + parent::__construct($appName, $request); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param string[] $widgetIds Limit widgets to given ids |
|
| 57 | + * @return IWidget[] |
|
| 58 | + */ |
|
| 59 | + private function getShownWidgets(array $widgetIds): array { |
|
| 60 | + if (empty($widgetIds)) { |
|
| 61 | + $systemDefault = $this->appConfig->getAppValueString('layout', 'recommendations,spreed,mail,calendar'); |
|
| 62 | + $widgetIds = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + return array_filter( |
|
| 66 | + $this->dashboardManager->getWidgets(), |
|
| 67 | + static function (IWidget $widget) use ($widgetIds) { |
|
| 68 | + return in_array($widget->getId(), $widgetIds); |
|
| 69 | + }, |
|
| 70 | + ); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Get the items for the widgets |
|
| 75 | + * |
|
| 76 | + * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items |
|
| 77 | + * @param int $limit Limit number of result items per widget |
|
| 78 | + * @psalm-param int<1, 30> $limit |
|
| 79 | + * @param list<string> $widgets Limit results to specific widgets |
|
| 80 | + * @return DataResponse<Http::STATUS_OK, array<string, list<DashboardWidgetItem>>, array{}> |
|
| 81 | + * |
|
| 82 | + * 200: Widget items returned |
|
| 83 | + */ |
|
| 84 | + #[NoAdminRequired] |
|
| 85 | + #[NoCSRFRequired] |
|
| 86 | + #[ApiRoute(verb: 'GET', url: '/api/v1/widget-items')] |
|
| 87 | + public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse { |
|
| 88 | + $items = []; |
|
| 89 | + $widgets = $this->getShownWidgets($widgets); |
|
| 90 | + foreach ($widgets as $widget) { |
|
| 91 | + if ($widget instanceof IAPIWidget) { |
|
| 92 | + $items[$widget->getId()] = array_map(static function (WidgetItem $item) { |
|
| 93 | + return $item->jsonSerialize(); |
|
| 94 | + }, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)); |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + return new DataResponse($items); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the items for the widgets |
|
| 103 | + * |
|
| 104 | + * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items |
|
| 105 | + * @param int $limit Limit number of result items per widget, not more than 30 are allowed |
|
| 106 | + * @psalm-param int<1, 30> $limit |
|
| 107 | + * @param list<string> $widgets Limit results to specific widgets |
|
| 108 | + * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}> |
|
| 109 | + * |
|
| 110 | + * 200: Widget items returned |
|
| 111 | + */ |
|
| 112 | + #[NoAdminRequired] |
|
| 113 | + #[NoCSRFRequired] |
|
| 114 | + #[ApiRoute(verb: 'GET', url: '/api/v2/widget-items')] |
|
| 115 | + public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse { |
|
| 116 | + $items = []; |
|
| 117 | + $widgets = $this->getShownWidgets($widgets); |
|
| 118 | + foreach ($widgets as $widget) { |
|
| 119 | + if ($widget instanceof IAPIWidgetV2) { |
|
| 120 | + $items[$widget->getId()] = $widget |
|
| 121 | + ->getItemsV2($this->userId, $sinceIds[$widget->getId()] ?? null, $limit) |
|
| 122 | + ->jsonSerialize(); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return new DataResponse($items); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get the widgets |
|
| 131 | + * |
|
| 132 | + * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidget>, array{}> |
|
| 133 | + * |
|
| 134 | + * 200: Widgets returned |
|
| 135 | + */ |
|
| 136 | + #[NoAdminRequired] |
|
| 137 | + #[NoCSRFRequired] |
|
| 138 | + #[ApiRoute(verb: 'GET', url: '/api/v1/widgets')] |
|
| 139 | + public function getWidgets(): DataResponse { |
|
| 140 | + $widgets = $this->dashboardManager->getWidgets(); |
|
| 141 | + |
|
| 142 | + $items = array_map(function (IWidget $widget) { |
|
| 143 | + $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault(); |
|
| 144 | + $data = [ |
|
| 145 | + 'id' => $widget->getId(), |
|
| 146 | + 'title' => $widget->getTitle(), |
|
| 147 | + 'order' => $widget->getOrder(), |
|
| 148 | + 'icon_class' => $widget->getIconClass(), |
|
| 149 | + 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '', |
|
| 150 | + 'widget_url' => $widget->getUrl(), |
|
| 151 | + 'item_icons_round' => $options->withRoundItemIcons(), |
|
| 152 | + 'item_api_versions' => [], |
|
| 153 | + 'reload_interval' => 0, |
|
| 154 | + ]; |
|
| 155 | + if ($widget instanceof IButtonWidget) { |
|
| 156 | + $data += [ |
|
| 157 | + 'buttons' => array_map(function (WidgetButton $button) { |
|
| 158 | + return [ |
|
| 159 | + 'type' => $button->getType(), |
|
| 160 | + 'text' => $button->getText(), |
|
| 161 | + 'link' => $button->getLink(), |
|
| 162 | + ]; |
|
| 163 | + }, $widget->getWidgetButtons($this->userId)), |
|
| 164 | + ]; |
|
| 165 | + } |
|
| 166 | + if ($widget instanceof IReloadableWidget) { |
|
| 167 | + $data['reload_interval'] = $widget->getReloadInterval(); |
|
| 168 | + } |
|
| 169 | + if ($widget instanceof IAPIWidget) { |
|
| 170 | + $data['item_api_versions'][] = 1; |
|
| 171 | + } |
|
| 172 | + if ($widget instanceof IAPIWidgetV2) { |
|
| 173 | + $data['item_api_versions'][] = 2; |
|
| 174 | + } |
|
| 175 | + return $data; |
|
| 176 | + }, $widgets); |
|
| 177 | + |
|
| 178 | + return new DataResponse($items); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get the layout |
|
| 183 | + * |
|
| 184 | + * @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}> |
|
| 185 | + * |
|
| 186 | + * 200: Layout returned |
|
| 187 | + */ |
|
| 188 | + #[NoAdminRequired] |
|
| 189 | + #[ApiRoute(verb: 'GET', url: '/api/v3/layout')] |
|
| 190 | + public function getLayout(): DataResponse { |
|
| 191 | + return new DataResponse(['layout' => $this->service->getLayout()]); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Update the layout |
|
| 196 | + * |
|
| 197 | + * @param list<string> $layout The new layout |
|
| 198 | + * @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}> |
|
| 199 | + * |
|
| 200 | + * 200: Statuses updated successfully |
|
| 201 | + */ |
|
| 202 | + #[NoAdminRequired] |
|
| 203 | + #[ApiRoute(verb: 'POST', url: '/api/v3/layout')] |
|
| 204 | + public function updateLayout(array $layout): DataResponse { |
|
| 205 | + $this->config->setUserValue($this->userId, 'dashboard', 'layout', implode(',', $layout)); |
|
| 206 | + return new DataResponse(['layout' => $layout]); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get the statuses |
|
| 211 | + * |
|
| 212 | + * @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}> |
|
| 213 | + * |
|
| 214 | + * 200: Statuses returned |
|
| 215 | + */ |
|
| 216 | + #[NoAdminRequired] |
|
| 217 | + #[ApiRoute(verb: 'GET', url: '/api/v3/statuses')] |
|
| 218 | + public function getStatuses(): DataResponse { |
|
| 219 | + return new DataResponse(['statuses' => $this->service->getStatuses()]); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Update the statuses |
|
| 224 | + * |
|
| 225 | + * @param list<string> $statuses The new statuses |
|
| 226 | + * @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}> |
|
| 227 | + * |
|
| 228 | + * 200: Statuses updated successfully |
|
| 229 | + */ |
|
| 230 | + #[NoAdminRequired] |
|
| 231 | + #[ApiRoute(verb: 'POST', url: '/api/v3/statuses')] |
|
| 232 | + public function updateStatuses(array $statuses): DataResponse { |
|
| 233 | + $this->config->setUserValue($this->userId, 'dashboard', 'statuses', implode(',', $statuses)); |
|
| 234 | + return new DataResponse(['statuses' => $statuses]); |
|
| 235 | + } |
|
| 236 | 236 | } |
@@ -21,85 +21,85 @@ |
||
| 21 | 21 | |
| 22 | 22 | class DashboardServiceTest extends TestCase { |
| 23 | 23 | |
| 24 | - private IConfig&MockObject $config; |
|
| 25 | - private IAppConfig&MockObject $appConfig; |
|
| 26 | - private IUserManager&MockObject $userManager; |
|
| 27 | - private IAccountManager&MockObject $accountManager; |
|
| 28 | - private DashboardService $service; |
|
| 29 | - |
|
| 30 | - protected function setUp(): void { |
|
| 31 | - parent::setUp(); |
|
| 32 | - |
|
| 33 | - $this->config = $this->createMock(IConfig::class); |
|
| 34 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 35 | - $this->userManager = $this->createMock(IUserManager::class); |
|
| 36 | - $this->accountManager = $this->createMock(IAccountManager::class); |
|
| 37 | - |
|
| 38 | - $this->service = new DashboardService( |
|
| 39 | - $this->config, |
|
| 40 | - $this->appConfig, |
|
| 41 | - 'alice', |
|
| 42 | - $this->userManager, |
|
| 43 | - $this->accountManager, |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function testGetBirthdate(): void { |
|
| 48 | - $user = $this->createMock(IUser::class); |
|
| 49 | - $this->userManager->method('get') |
|
| 50 | - ->willReturn($user); |
|
| 51 | - |
|
| 52 | - $account = new Account($user); |
|
| 53 | - $account->setProperty( |
|
| 54 | - IAccountManager::PROPERTY_BIRTHDATE, |
|
| 55 | - '2024-12-10T00:00:00.000Z', |
|
| 56 | - IAccountManager::SCOPE_LOCAL, |
|
| 57 | - IAccountManager::VERIFIED, |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - $this->accountManager->method('getAccount') |
|
| 61 | - ->willReturn($account); |
|
| 62 | - |
|
| 63 | - $birthdate = $this->service->getBirthdate(); |
|
| 64 | - |
|
| 65 | - $this->assertEquals('2024-12-10T00:00:00.000Z', $birthdate); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function testGetBirthdatePropertyDoesNotExist(): void { |
|
| 69 | - $user = $this->createMock(IUser::class); |
|
| 70 | - $this->userManager->method('get') |
|
| 71 | - ->willReturn($user); |
|
| 72 | - |
|
| 73 | - $account = new Account($user); |
|
| 74 | - $this->accountManager->method('getAccount') |
|
| 75 | - ->willReturn($account); |
|
| 76 | - |
|
| 77 | - $birthdate = $this->service->getBirthdate(); |
|
| 78 | - |
|
| 79 | - $this->assertEquals('', $birthdate); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function testGetBirthdateUserNotFound(): void { |
|
| 83 | - $this->userManager->method('get') |
|
| 84 | - ->willReturn(null); |
|
| 85 | - |
|
| 86 | - $birthdate = $this->service->getBirthdate(); |
|
| 87 | - |
|
| 88 | - $this->assertEquals('', $birthdate); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - public function testGetBirthdateNoUserId(): void { |
|
| 92 | - $service = new DashboardService( |
|
| 93 | - $this->config, |
|
| 94 | - $this->appConfig, |
|
| 95 | - null, |
|
| 96 | - $this->userManager, |
|
| 97 | - $this->accountManager, |
|
| 98 | - ); |
|
| 99 | - |
|
| 100 | - $birthdate = $service->getBirthdate(); |
|
| 101 | - |
|
| 102 | - $this->assertEquals('', $birthdate); |
|
| 103 | - } |
|
| 24 | + private IConfig&MockObject $config; |
|
| 25 | + private IAppConfig&MockObject $appConfig; |
|
| 26 | + private IUserManager&MockObject $userManager; |
|
| 27 | + private IAccountManager&MockObject $accountManager; |
|
| 28 | + private DashboardService $service; |
|
| 29 | + |
|
| 30 | + protected function setUp(): void { |
|
| 31 | + parent::setUp(); |
|
| 32 | + |
|
| 33 | + $this->config = $this->createMock(IConfig::class); |
|
| 34 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 35 | + $this->userManager = $this->createMock(IUserManager::class); |
|
| 36 | + $this->accountManager = $this->createMock(IAccountManager::class); |
|
| 37 | + |
|
| 38 | + $this->service = new DashboardService( |
|
| 39 | + $this->config, |
|
| 40 | + $this->appConfig, |
|
| 41 | + 'alice', |
|
| 42 | + $this->userManager, |
|
| 43 | + $this->accountManager, |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function testGetBirthdate(): void { |
|
| 48 | + $user = $this->createMock(IUser::class); |
|
| 49 | + $this->userManager->method('get') |
|
| 50 | + ->willReturn($user); |
|
| 51 | + |
|
| 52 | + $account = new Account($user); |
|
| 53 | + $account->setProperty( |
|
| 54 | + IAccountManager::PROPERTY_BIRTHDATE, |
|
| 55 | + '2024-12-10T00:00:00.000Z', |
|
| 56 | + IAccountManager::SCOPE_LOCAL, |
|
| 57 | + IAccountManager::VERIFIED, |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + $this->accountManager->method('getAccount') |
|
| 61 | + ->willReturn($account); |
|
| 62 | + |
|
| 63 | + $birthdate = $this->service->getBirthdate(); |
|
| 64 | + |
|
| 65 | + $this->assertEquals('2024-12-10T00:00:00.000Z', $birthdate); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function testGetBirthdatePropertyDoesNotExist(): void { |
|
| 69 | + $user = $this->createMock(IUser::class); |
|
| 70 | + $this->userManager->method('get') |
|
| 71 | + ->willReturn($user); |
|
| 72 | + |
|
| 73 | + $account = new Account($user); |
|
| 74 | + $this->accountManager->method('getAccount') |
|
| 75 | + ->willReturn($account); |
|
| 76 | + |
|
| 77 | + $birthdate = $this->service->getBirthdate(); |
|
| 78 | + |
|
| 79 | + $this->assertEquals('', $birthdate); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function testGetBirthdateUserNotFound(): void { |
|
| 83 | + $this->userManager->method('get') |
|
| 84 | + ->willReturn(null); |
|
| 85 | + |
|
| 86 | + $birthdate = $this->service->getBirthdate(); |
|
| 87 | + |
|
| 88 | + $this->assertEquals('', $birthdate); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + public function testGetBirthdateNoUserId(): void { |
|
| 92 | + $service = new DashboardService( |
|
| 93 | + $this->config, |
|
| 94 | + $this->appConfig, |
|
| 95 | + null, |
|
| 96 | + $this->userManager, |
|
| 97 | + $this->accountManager, |
|
| 98 | + ); |
|
| 99 | + |
|
| 100 | + $birthdate = $service->getBirthdate(); |
|
| 101 | + |
|
| 102 | + $this->assertEquals('', $birthdate); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | } |