diff options
author | Joas Schilling <coding@schilljs.com> | 2022-02-15 15:28:55 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-02-15 16:06:33 +0100 |
commit | 058d1de26012ab829fad915f35b1bb761808ce7b (patch) | |
tree | e336ec7e9956fe07cb8db59642d5f86088068ef6 /apps/user_status | |
parent | 5fcbb1ca62d7cfbffb0e4089f7315cdfe29668b0 (diff) | |
download | nextcloud-server-058d1de26012ab829fad915f35b1bb761808ce7b.tar.gz nextcloud-server-058d1de26012ab829fad915f35b1bb761808ce7b.zip |
Do status and predefined message setting in one go
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/user_status')
-rw-r--r-- | apps/user_status/lib/Connector/UserStatusProvider.php | 10 | ||||
-rw-r--r-- | apps/user_status/lib/Service/StatusService.php | 55 | ||||
-rw-r--r-- | apps/user_status/tests/Unit/Db/UserStatusMapperTest.php | 61 |
3 files changed, 118 insertions, 8 deletions
diff --git a/apps/user_status/lib/Connector/UserStatusProvider.php b/apps/user_status/lib/Connector/UserStatusProvider.php index cec9406d28b..46b89739f7c 100644 --- a/apps/user_status/lib/Connector/UserStatusProvider.php +++ b/apps/user_status/lib/Connector/UserStatusProvider.php @@ -57,14 +57,8 @@ class UserStatusProvider implements IProvider, ISettableProvider { return $userStatuses; } - public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false): void { - if ($createBackup) { - if ($this->service->backupCurrentStatus($userId) === false) { - return; // Already a status set automatically => abort. - } - } - $this->service->setStatus($userId, $status, null, true); - $this->service->setPredefinedMessage($userId, $messageId, null); + public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup): void { + $this->service->setUserStatus($userId, $status, $messageId, $createBackup); } public function revertUserStatus(string $userId, string $messageId, string $status): void { diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php index 5cd5bb83c3a..c7ad7afe322 100644 --- a/apps/user_status/lib/Service/StatusService.php +++ b/apps/user_status/lib/Service/StatusService.php @@ -231,6 +231,7 @@ class StatusService { $userStatus->setStatus(IUserStatus::OFFLINE); $userStatus->setStatusTimestamp(0); $userStatus->setIsUserDefined(false); + $userStatus->setIsBackup(false); } if (!$this->predefinedStatusService->isValidId($messageId)) { @@ -256,6 +257,60 @@ class StatusService { /** * @param string $userId + * @param string $status + * @param string $messageId + * @param bool $createBackup + * @throws InvalidStatusTypeException + * @throws InvalidMessageIdException + */ + public function setUserStatus(string $userId, + string $status, + string $messageId, + bool $createBackup): void { + // Check if status-type is valid + if (!\in_array($status, self::PRIORITY_ORDERED_STATUSES, true)) { + throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported'); + } + + if (!$this->predefinedStatusService->isValidId($messageId)) { + throw new InvalidMessageIdException('Message-Id "' . $messageId . '" is not supported'); + } + + if ($createBackup) { + if ($this->backupCurrentStatus($userId) === false) { + return; // Already a status set automatically => abort. + } + + // If we just created the backup + $userStatus = new UserStatus(); + $userStatus->setUserId($userId); + } else { + try { + $userStatus = $this->mapper->findByUserId($userId); + } catch (DoesNotExistException $ex) { + $userStatus = new UserStatus(); + $userStatus->setUserId($userId); + } + } + + $userStatus->setStatus($status); + $userStatus->setStatusTimestamp($this->timeFactory->getTime()); + $userStatus->setIsUserDefined(false); + $userStatus->setIsBackup(false); + $userStatus->setMessageId($messageId); + $userStatus->setCustomIcon(null); + $userStatus->setCustomMessage(null); + $userStatus->setClearAt(null); + + if ($userStatus->getId() !== null) { + $this->mapper->update($userStatus); + return; + } + $this->mapper->insert($userStatus); + } + + /** + * @param string $userId * @param string|null $statusIcon * @param string $message * @param int|null $clearAt diff --git a/apps/user_status/tests/Unit/Db/UserStatusMapperTest.php b/apps/user_status/tests/Unit/Db/UserStatusMapperTest.php index c48805b57af..0d9f1c1f718 100644 --- a/apps/user_status/tests/Unit/Db/UserStatusMapperTest.php +++ b/apps/user_status/tests/Unit/Db/UserStatusMapperTest.php @@ -252,6 +252,67 @@ class UserStatusMapperTest extends TestCase { $this->mapper->insert($userStatus3); } + public function dataCreateBackupStatus(): array { + return [ + [false, false, false], + [true, false, true], + [false, true, false], + [true, true, false], + ]; + } + + /** + * @dataProvider dataCreateBackupStatus + * @param bool $hasStatus + * @param bool $hasBackup + * @param bool $backupCreated + */ + public function testCreateBackupStatus(bool $hasStatus, bool $hasBackup, bool $backupCreated): void { + if ($hasStatus) { + $userStatus1 = new UserStatus(); + $userStatus1->setUserId('user1'); + $userStatus1->setStatus('online'); + $userStatus1->setStatusTimestamp(5000); + $userStatus1->setIsUserDefined(true); + $userStatus1->setIsBackup(false); + $userStatus1->setCustomIcon('🚀'); + $userStatus1->setCustomMessage('Current'); + $userStatus1->setClearAt(50000); + $this->mapper->insert($userStatus1); + } + + if ($hasBackup) { + $userStatus1 = new UserStatus(); + $userStatus1->setUserId('_user1'); + $userStatus1->setStatus('online'); + $userStatus1->setStatusTimestamp(5000); + $userStatus1->setIsUserDefined(true); + $userStatus1->setIsBackup(true); + $userStatus1->setCustomIcon('🚀'); + $userStatus1->setCustomMessage('Backup'); + $userStatus1->setClearAt(50000); + $this->mapper->insert($userStatus1); + } + + if ($hasStatus && $hasBackup) { + $this->expectException(Exception::class); + } + + self::assertSame($backupCreated, $this->mapper->createBackupStatus('user1')); + + if ($backupCreated) { + $user1Status = $this->mapper->findByUserId('user1', true); + $this->assertEquals('_user1', $user1Status->getUserId()); + $this->assertEquals(true, $user1Status->getIsBackup()); + $this->assertEquals('Current', $user1Status->getCustomMessage()); + } else if ($hasBackup) { + $user1Status = $this->mapper->findByUserId('user1', true); + $this->assertEquals('_user1', $user1Status->getUserId()); + $this->assertEquals(true, $user1Status->getIsBackup()); + $this->assertEquals('Backup', $user1Status->getCustomMessage()); + } + } + public function testRestoreBackupStatuses(): void { $userStatus1 = new UserStatus(); $userStatus1->setUserId('_user1'); |