From 527154baf09e1a6e88151da680e5c1d33315a9b4 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 19 Oct 2021 14:22:11 +0200 Subject: [PATCH 1/7] Make it possible to revert the automatic status --- apps/user_status/appinfo/routes.php | 2 + .../lib/Connector/UserStatusProvider.php | 10 +- .../lib/Controller/StatusesController.php | 4 +- .../lib/Controller/UserStatusController.php | 18 +++ .../user_status/lib/Service/StatusService.php | 5 +- apps/user_status/src/UserStatus.vue | 1 - .../src/components/ResetStatus.vue | 118 ++++++++++++++++++ .../src/components/SetStatusModal.vue | 3 + .../user_status/src/services/statusService.js | 13 ++ .../user_status/src/store/backupUserStatus.js | 91 ++++++++++++++ apps/user_status/src/store/index.js | 2 + 11 files changed, 260 insertions(+), 7 deletions(-) create mode 100644 apps/user_status/src/components/ResetStatus.vue create mode 100644 apps/user_status/src/store/backupUserStatus.js diff --git a/apps/user_status/appinfo/routes.php b/apps/user_status/appinfo/routes.php index d360dc1ebd5b5..12206464c82e1 100644 --- a/apps/user_status/appinfo/routes.php +++ b/apps/user_status/appinfo/routes.php @@ -34,6 +34,8 @@ ['name' => 'UserStatus#setPredefinedMessage', 'url' => '/api/v1/user_status/message/predefined', 'verb' => 'PUT'], ['name' => 'UserStatus#setCustomMessage', 'url' => '/api/v1/user_status/message/custom', 'verb' => 'PUT'], ['name' => 'UserStatus#clearMessage', 'url' => '/api/v1/user_status/message', 'verb' => 'DELETE'], + // Routes for getting your backup status (if it exists) + ['name' => 'UserStatus#getBackupStatus', 'url' => '/api/v1/user_status_backup', 'verb' => 'GET'], // Routes for listing default routes ['name' => 'PredefinedStatus#findAll', 'url' => '/api/v1/predefined_statuses/', 'verb' => 'GET'] ], diff --git a/apps/user_status/lib/Connector/UserStatusProvider.php b/apps/user_status/lib/Connector/UserStatusProvider.php index f3f4c5f710e01..fbb05012c1c2a 100644 --- a/apps/user_status/lib/Connector/UserStatusProvider.php +++ b/apps/user_status/lib/Connector/UserStatusProvider.php @@ -28,19 +28,24 @@ use OCA\UserStatus\Service\StatusService; use OCP\UserStatus\IProvider; use OC\UserStatus\ISettableProvider; +use OCP\IConfig; class UserStatusProvider implements IProvider, ISettableProvider { /** @var StatusService */ private $service; + /** @var IConfig */ + private $config; + /** * UserStatusProvider constructor. * * @param StatusService $service */ - public function __construct(StatusService $service) { + public function __construct(StatusService $service, IConfig $config) { $this->service = $service; + $this->config = $config; } /** @@ -58,6 +63,9 @@ public function getUserStatuses(array $userIds): array { } public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false): void { + if (!(bool)$this->config->getUserValue($userId, 'user_status', 'automatic_status_enabled', (string)true)) { + return; + } if ($createBackup) { if ($this->service->backupCurrentStatus($userId) === false) { return; // Already a status set automatically => abort. diff --git a/apps/user_status/lib/Controller/StatusesController.php b/apps/user_status/lib/Controller/StatusesController.php index 56dbeb4f7a58e..d30389e1716c6 100644 --- a/apps/user_status/lib/Controller/StatusesController.php +++ b/apps/user_status/lib/Controller/StatusesController.php @@ -87,10 +87,8 @@ public function find(string $userId): DataResponse { } /** - * @NoAdminRequired - * * @param UserStatus $status - * @return array + * @return array{userId: string, message: string, icon: string, clearAt: int, status: string} */ private function formatStatus(UserStatus $status): array { $visibleStatus = $status->getStatus(); diff --git a/apps/user_status/lib/Controller/UserStatusController.php b/apps/user_status/lib/Controller/UserStatusController.php index a231103e91e2b..e73dd48271691 100644 --- a/apps/user_status/lib/Controller/UserStatusController.php +++ b/apps/user_status/lib/Controller/UserStatusController.php @@ -89,6 +89,21 @@ public function getStatus(): DataResponse { return new DataResponse($this->formatStatus($userStatus)); } + /** + * @NoAdminRequired + * + * @return DataResponse + */ + public function getBackupStatus(): DataResponse { + try { + $userStatus = $this->service->findByUserId($this->userId, true); + } catch (DoesNotExistException $ex) { + return new DataResponse(['hasBackup' => false]); + } + + return new DataResponse($this->formatStatus($userStatus)); + } + /** * @NoAdminRequired * @@ -99,6 +114,7 @@ public function getStatus(): DataResponse { public function setStatus(string $statusType): DataResponse { try { $status = $this->service->setStatus($this->userId, $statusType, null, true); + $this->service->removeUserStatus($this->userId, true); return new DataResponse($this->formatStatus($status)); } catch (InvalidStatusTypeException $ex) { $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid status type "' . $statusType . '"'); @@ -118,6 +134,7 @@ public function setPredefinedMessage(string $messageId, ?int $clearAt): DataResponse { try { $status = $this->service->setPredefinedMessage($this->userId, $messageId, $clearAt); + $this->service->removeUserStatus($this->userId, true); return new DataResponse($this->formatStatus($status)); } catch (InvalidClearAtException $ex) { $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"'); @@ -147,6 +164,7 @@ public function setCustomMessage(?string $statusIcon, $this->service->clearMessage($this->userId); $status = $this->service->findByUserId($this->userId); } + $this->service->removeUserStatus($this->userId, true); return new DataResponse($this->formatStatus($status)); } catch (InvalidClearAtException $ex) { $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"'); diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php index 655c37da9cc32..d9cc1d6110216 100644 --- a/apps/user_status/lib/Service/StatusService.php +++ b/apps/user_status/lib/Service/StatusService.php @@ -154,11 +154,12 @@ public function findAllRecentStatusChanges(?int $limit = null, ?int $offset = nu /** * @param string $userId + * @param bool $isBackup * @return UserStatus * @throws DoesNotExistException */ - public function findByUserId(string $userId):UserStatus { - return $this->processStatus($this->mapper->findByUserId($userId)); + public function findByUserId(string $userId, bool $isBackup = false):UserStatus { + return $this->processStatus($this->mapper->findByUserId($userId, $isBackup)); } /** diff --git a/apps/user_status/src/UserStatus.vue b/apps/user_status/src/UserStatus.vue index 608966b9d9a02..cc0aaca596de8 100644 --- a/apps/user_status/src/UserStatus.vue +++ b/apps/user_status/src/UserStatus.vue @@ -64,7 +64,6 @@ import { subscribe, unsubscribe } from '@nextcloud/event-bus' import debounce from 'debounce' import { sendHeartbeat } from './services/heartbeatService' -import OnlineStatusMixin from './mixins/OnlineStatusMixin' const { profileEnabled } = loadState('user_status', 'profileEnabled', false) diff --git a/apps/user_status/src/components/ResetStatus.vue b/apps/user_status/src/components/ResetStatus.vue new file mode 100644 index 0000000000000..3c9ea11194086 --- /dev/null +++ b/apps/user_status/src/components/ResetStatus.vue @@ -0,0 +1,118 @@ + + + + + + + diff --git a/apps/user_status/src/components/SetStatusModal.vue b/apps/user_status/src/components/SetStatusModal.vue index 50fa79b4cc52b..f590fed02e2e0 100644 --- a/apps/user_status/src/components/SetStatusModal.vue +++ b/apps/user_status/src/components/SetStatusModal.vue @@ -59,6 +59,7 @@ +