]> source.dussan.org Git - nextcloud-server.git/commitdiff
Introduce consts for statuses
authorGeorg Ehrke <developer@georgehrke.com>
Wed, 2 Sep 2020 09:49:14 +0000 (11:49 +0200)
committerGeorg Ehrke <developer@georgehrke.com>
Mon, 7 Sep 2020 07:22:34 +0000 (09:22 +0200)
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
apps/user_status/lib/BackgroundJob/ClearOldStatusesBackgroundJob.php
apps/user_status/lib/Connector/UserStatus.php
apps/user_status/lib/Controller/HeartbeatController.php
apps/user_status/lib/Controller/StatusesController.php
apps/user_status/lib/Dashboard/UserStatusWidget.php
apps/user_status/lib/Db/UserStatusMapper.php
apps/user_status/lib/Listener/UserLiveStatusListener.php
apps/user_status/lib/Service/JSDataService.php
apps/user_status/lib/Service/StatusService.php
apps/user_status/tests/Unit/BackgroundJob/ClearOldStatusesBackgroundJobTest.php
apps/user_status/tests/Unit/Db/UserStatusMapperTest.php

index 406390488434ec9136a75b037ee8f360e76ce067..aa6202de43a2ac04c504daf03864e78b75f57989 100644 (file)
@@ -58,6 +58,6 @@ class ClearOldStatusesBackgroundJob extends TimedJob {
         * @inheritDoc
         */
        protected function run($argument) {
-               $this->mapper->clearOlderThan($this->time->getTime());
+               $this->mapper->clearMessagesOlderThan($this->time->getTime());
        }
 }
index acbdac2007b396cccb7e51051189ed56032f2a35..e8ab31bb10365c3287442d98f9f372c1eca98482 100644 (file)
@@ -25,6 +25,7 @@ declare(strict_types=1);
 namespace OCA\UserStatus\Connector;
 
 use DateTimeImmutable;
+use OCA\UserStatus\Service\StatusService;
 use OCP\UserStatus\IUserStatus;
 use OCA\UserStatus\Db;
 
@@ -56,8 +57,8 @@ class UserStatus implements IUserStatus {
                $this->message = $status->getCustomMessage();
                $this->icon = $status->getCustomIcon();
 
-               if ($status->getStatus() === 'invisible') {
-                       $this->status = 'offline';
+               if ($status->getStatus() === StatusService::INVISIBLE) {
+                       $this->status = StatusService::OFFLINE;
                }
                if ($status->getClearAt() !== null) {
                        $this->clearAt = DateTimeImmutable::createFromFormat('U', (string)$status->getClearAt());
index fb8259a2ad71ace488252efc1dc475cd1aaadb96..e05a12fae8ff88cb773f0a61ece407fcfa131c3a 100644 (file)
@@ -25,6 +25,7 @@ declare(strict_types=1);
 
 namespace OCA\UserStatus\Controller;
 
+use OCA\UserStatus\Service\StatusService;
 use OCP\AppFramework\Controller;
 use OCP\AppFramework\Http;
 use OCP\AppFramework\Http\JSONResponse;
@@ -70,7 +71,7 @@ class HeartbeatController extends Controller {
         * @return JSONResponse
         */
        public function heartbeat(string $status): JSONResponse {
-               if (!\in_array($status, ['online', 'away'])) {
+               if (!\in_array($status, [StatusService::ONLINE, StatusService::AWAY], true)) {
                        return new JSONResponse([], Http::STATUS_BAD_REQUEST);
                }
 
index b707708f46a98c70d7f0a8d86b86e5f66606e225..0391ea1492845726da04dc13f832dd13621c8ba6 100644 (file)
@@ -92,8 +92,8 @@ class StatusesController extends OCSController {
         */
        private function formatStatus(UserStatus $status): array {
                $visibleStatus = $status->getStatus();
-               if ($visibleStatus === 'invisible') {
-                       $visibleStatus = 'offline';
+               if ($visibleStatus === StatusService::INVISIBLE) {
+                       $visibleStatus = StatusService::OFFLINE;
                }
 
                return [
index 809aa0000050fc13d1d1d4b028da8ce84a29a417..9cd29e3ddba87996465a1f72a0dee8e07dd6979f 100644 (file)
@@ -146,7 +146,9 @@ class UserStatusWidget implements IWidget {
                        return [
                                'userId' => $status->getUserId(),
                                'displayName' => $displayName,
-                               'status' => $status->getStatus() === 'invisible' ? 'offline' : $status->getStatus(),
+                               'status' => $status->getStatus() === StatusService::INVISIBLE
+                                       ? StatusService::OFFLINE
+                                       : $status->getStatus(),
                                'icon' => $status->getCustomIcon(),
                                'message' => $status->getCustomMessage(),
                                'timestamp' => $status->getStatusTimestamp(),
index 2bd6a5024d0673884c64681cae394fd761d51a4f..0f3693a4d214ae77a6914de0455924a2f8208047 100644 (file)
@@ -130,7 +130,7 @@ class UserStatusMapper extends QBMapper {
         *
         * @param int $timestamp
         */
-       public function clearOlderThan(int $timestamp): void {
+       public function clearMessagesOlderThan(int $timestamp): void {
                $qb = $this->db->getQueryBuilder();
                $qb->update($this->tableName)
                        ->set('message_id', $qb->createNamedParameter(null))
index ce97841d9adcf1e6c7a37fc648eea60f72620dee..52e73017d4b8cb89e3568284c63e56ce72d3f450 100644 (file)
@@ -27,6 +27,7 @@ namespace OCA\UserStatus\Listener;
 
 use OCA\UserStatus\Db\UserStatus;
 use OCA\UserStatus\Db\UserStatusMapper;
+use OCA\UserStatus\Service\StatusService;
 use OCP\AppFramework\Db\DoesNotExistException;
 use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\EventDispatcher\IEventListener;
@@ -46,25 +47,6 @@ class UserLiveStatusListener implements IEventListener {
        /** @var ITimeFactory */
        private $timeFactory;
 
-       /** @var string[] */
-       private $priorityOrderedStatuses = [
-               'online',
-               'away',
-               'dnd',
-               'invisible',
-               'offline'
-       ];
-
-       /** @var string[] */
-       private $persistentUserStatuses = [
-               'away',
-               'dnd',
-               'invisible',
-       ];
-
-       /** @var int */
-       private $offlineThreshold = 300;
-
        /**
         * UserLiveStatusListener constructor.
         *
@@ -92,7 +74,7 @@ class UserLiveStatusListener implements IEventListener {
                } catch (DoesNotExistException $ex) {
                        $userStatus = new UserStatus();
                        $userStatus->setUserId($user->getUID());
-                       $userStatus->setStatus('offline');
+                       $userStatus->setStatus(StatusService::OFFLINE);
                        $userStatus->setStatusTimestamp(0);
                        $userStatus->setIsUserDefined(false);
                }
@@ -100,7 +82,7 @@ class UserLiveStatusListener implements IEventListener {
                // If the status is user-defined and one of the persistent statuses, we
                // will not override it.
                if ($userStatus->getIsUserDefined() &&
-                       \in_array($userStatus->getStatus(), $this->persistentUserStatuses, true)) {
+                       \in_array($userStatus->getStatus(), StatusService::PERSISTENT_STATUSES, true)) {
                        return;
                }
 
@@ -108,13 +90,13 @@ class UserLiveStatusListener implements IEventListener {
 
                // If the current status is older than 5 minutes,
                // treat it as outdated and update
-               if ($userStatus->getStatusTimestamp() < ($this->timeFactory->getTime() - $this->offlineThreshold)) {
+               if ($userStatus->getStatusTimestamp() < ($this->timeFactory->getTime() - StatusService::INVALIDATE_STATUS_THRESHOLD)) {
                        $needsUpdate = true;
                }
 
                // If the emitted status is more important than the current status
                // treat it as outdated and update
-               if (array_search($event->getStatus(), $this->priorityOrderedStatuses) < array_search($userStatus->getStatus(), $this->priorityOrderedStatuses)) {
+               if (array_search($event->getStatus(), StatusService::PRIORITY_ORDERED_STATUSES) < array_search($userStatus->getStatus(), StatusService::PRIORITY_ORDERED_STATUSES)) {
                        $needsUpdate = true;
                }
 
index ebe801cd57ac0d6ef94ed2f0eb6d785f8355ccc1..334ae248d4c6c0d172206f61e66cc8e15e54e830 100644 (file)
@@ -65,7 +65,7 @@ class JSDataService implements \JsonSerializable {
                                'messageIsPredefined' => false,
                                'icon' => null,
                                'clearAt' => null,
-                               'status' => 'offline',
+                               'status' => StatusService::OFFLINE,
                                'statusIsUserDefined' => false,
                        ];
                }
index e36d605cee431d64c315d687f7563627b671b8a2..06441f600591cb1338d19cf6aa9d9474286fb3c1 100644 (file)
@@ -54,17 +54,38 @@ class StatusService {
        /** @var EmojiService */
        private $emojiService;
 
-       /** @var string[] */
-       private $allowedStatusTypes = [
-               'online',
-               'away',
-               'dnd',
-               'invisible',
-               'offline'
+       public const ONLINE = 'online';
+       public const AWAY = 'away';
+       public const DND = 'dnd';
+       public const INVISIBLE = 'invisible';
+       public const OFFLINE = 'offline';
+
+       /**
+        * List of priorities ordered by their priority
+        */
+       public const PRIORITY_ORDERED_STATUSES = [
+               self::ONLINE,
+               self::AWAY,
+               self::DND,
+               self::INVISIBLE,
+               self::OFFLINE
        ];
 
+       /**
+        * List of statuses that persist the clear-up
+        * or UserLiveStatusEvents
+        */
+       public const PERSISTENT_STATUSES = [
+               self::AWAY,
+               self::DND,
+               self::INVISIBLE,
+       ];
+
+       /** @var int */
+       public const INVALIDATE_STATUS_THRESHOLD = 5 /* minutes */ * 60 /* seconds */;
+
        /** @var int */
-       private $maximumMessageLength = 80;
+       public const MAXIMUM_MESSAGE_LENGTH = 80;
 
        /**
         * StatusService constructor.
@@ -145,7 +166,7 @@ class StatusService {
                }
 
                // Check if status-type is valid
-               if (!\in_array($status, $this->allowedStatusTypes, true)) {
+               if (!\in_array($status, self::PRIORITY_ORDERED_STATUSES, true)) {
                        throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported');
                }
                if ($statusTimestamp === null) {
@@ -179,7 +200,7 @@ class StatusService {
                } catch (DoesNotExistException $ex) {
                        $userStatus = new UserStatus();
                        $userStatus->setUserId($userId);
-                       $userStatus->setStatus('offline');
+                       $userStatus->setStatus(self::OFFLINE);
                        $userStatus->setStatusTimestamp(0);
                        $userStatus->setIsUserDefined(false);
                }
@@ -224,7 +245,7 @@ class StatusService {
                } catch (DoesNotExistException $ex) {
                        $userStatus = new UserStatus();
                        $userStatus->setUserId($userId);
-                       $userStatus->setStatus('offline');
+                       $userStatus->setStatus(self::OFFLINE);
                        $userStatus->setStatusTimestamp(0);
                        $userStatus->setIsUserDefined(false);
                }
@@ -234,8 +255,8 @@ class StatusService {
                        throw new InvalidStatusIconException('Status-Icon is longer than one character');
                }
                // Check for maximum length of custom message
-               if (\mb_strlen($message) > $this->maximumMessageLength) {
-                       throw new StatusMessageTooLongException('Message is longer than supported length of ' . $this->maximumMessageLength . ' characters');
+               if (\mb_strlen($message) > self::MAXIMUM_MESSAGE_LENGTH) {
+                       throw new StatusMessageTooLongException('Message is longer than supported length of ' . self::MAXIMUM_MESSAGE_LENGTH . ' characters');
                }
                // Check that clearAt is in the future
                if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
@@ -266,7 +287,7 @@ class StatusService {
                        return false;
                }
 
-               $userStatus->setStatus('offline');
+               $userStatus->setStatus(self::OFFLINE);
                $userStatus->setStatusTimestamp(0);
                $userStatus->setIsUserDefined(false);
 
@@ -321,7 +342,7 @@ class StatusService {
        private function processStatus(UserStatus $status): UserStatus {
                $clearAt = $status->getClearAt();
                if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
-                       $this->cleanStatus($status);
+                       $this->cleanStatusMessage($status);
                }
                if ($status->getMessageId() !== null) {
                        $this->addDefaultMessage($status);
@@ -333,7 +354,7 @@ class StatusService {
        /**
         * @param UserStatus $status
         */
-       private function cleanStatus(UserStatus $status): void {
+       private function cleanStatusMessage(UserStatus $status): void {
                $status->setMessageId(null);
                $status->setCustomIcon(null);
                $status->setCustomMessage(null);
index 6c5f15d47e9195d8caf05f05b66e360fe5943a0e..a89d0e270fae087c5bd0423db1a0d57201a8c229 100644 (file)
@@ -52,7 +52,7 @@ class ClearOldStatusesBackgroundJobTest extends TestCase {
 
        public function testRun() {
                $this->mapper->expects($this->once())
-                       ->method('clearOlderThan')
+                       ->method('clearMessagesOlderThan')
                        ->with(1337);
 
                $this->time->method('getTime')
index bf4c911c812f9d0f7a168c09e7dc12486a5c5df9..de05d62c2271a7e26339cfdb9e32b1ef2b1e0776 100644 (file)
@@ -155,7 +155,7 @@ class UserStatusMapperTest extends TestCase {
        public function testClearOlderThan(): void {
                $this->insertSampleStatuses();
 
-               $this->mapper->clearOlderThan(55000);
+               $this->mapper->clearMessagesOlderThan(55000);
 
                $allStatuses = $this->mapper->findAll();
                $this->assertCount(3, $allStatuses);