Introduce consts for statuses

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
This commit is contained in:
Georg Ehrke 2020-09-02 11:49:14 +02:00
parent 16e1d1cb12
commit a352a7c7f3
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43
11 changed files with 57 additions and 50 deletions

View File

@ -58,6 +58,6 @@ class ClearOldStatusesBackgroundJob extends TimedJob {
* @inheritDoc * @inheritDoc
*/ */
protected function run($argument) { protected function run($argument) {
$this->mapper->clearOlderThan($this->time->getTime()); $this->mapper->clearMessagesOlderThan($this->time->getTime());
} }
} }

View File

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace OCA\UserStatus\Connector; namespace OCA\UserStatus\Connector;
use DateTimeImmutable; use DateTimeImmutable;
use OCA\UserStatus\Service\StatusService;
use OCP\UserStatus\IUserStatus; use OCP\UserStatus\IUserStatus;
use OCA\UserStatus\Db; use OCA\UserStatus\Db;
@ -56,8 +57,8 @@ class UserStatus implements IUserStatus {
$this->message = $status->getCustomMessage(); $this->message = $status->getCustomMessage();
$this->icon = $status->getCustomIcon(); $this->icon = $status->getCustomIcon();
if ($status->getStatus() === 'invisible') { if ($status->getStatus() === StatusService::INVISIBLE) {
$this->status = 'offline'; $this->status = StatusService::OFFLINE;
} }
if ($status->getClearAt() !== null) { if ($status->getClearAt() !== null) {
$this->clearAt = DateTimeImmutable::createFromFormat('U', (string)$status->getClearAt()); $this->clearAt = DateTimeImmutable::createFromFormat('U', (string)$status->getClearAt());

View File

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace OCA\UserStatus\Controller; namespace OCA\UserStatus\Controller;
use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
@ -70,7 +71,7 @@ class HeartbeatController extends Controller {
* @return JSONResponse * @return JSONResponse
*/ */
public function heartbeat(string $status): 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); return new JSONResponse([], Http::STATUS_BAD_REQUEST);
} }

View File

@ -92,8 +92,8 @@ class StatusesController extends OCSController {
*/ */
private function formatStatus(UserStatus $status): array { private function formatStatus(UserStatus $status): array {
$visibleStatus = $status->getStatus(); $visibleStatus = $status->getStatus();
if ($visibleStatus === 'invisible') { if ($visibleStatus === StatusService::INVISIBLE) {
$visibleStatus = 'offline'; $visibleStatus = StatusService::OFFLINE;
} }
return [ return [

View File

@ -146,7 +146,9 @@ class UserStatusWidget implements IWidget {
return [ return [
'userId' => $status->getUserId(), 'userId' => $status->getUserId(),
'displayName' => $displayName, 'displayName' => $displayName,
'status' => $status->getStatus() === 'invisible' ? 'offline' : $status->getStatus(), 'status' => $status->getStatus() === StatusService::INVISIBLE
? StatusService::OFFLINE
: $status->getStatus(),
'icon' => $status->getCustomIcon(), 'icon' => $status->getCustomIcon(),
'message' => $status->getCustomMessage(), 'message' => $status->getCustomMessage(),
'timestamp' => $status->getStatusTimestamp(), 'timestamp' => $status->getStatusTimestamp(),

View File

@ -130,7 +130,7 @@ class UserStatusMapper extends QBMapper {
* *
* @param int $timestamp * @param int $timestamp
*/ */
public function clearOlderThan(int $timestamp): void { public function clearMessagesOlderThan(int $timestamp): void {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb->update($this->tableName) $qb->update($this->tableName)
->set('message_id', $qb->createNamedParameter(null)) ->set('message_id', $qb->createNamedParameter(null))

View File

@ -27,6 +27,7 @@ namespace OCA\UserStatus\Listener;
use OCA\UserStatus\Db\UserStatus; use OCA\UserStatus\Db\UserStatus;
use OCA\UserStatus\Db\UserStatusMapper; use OCA\UserStatus\Db\UserStatusMapper;
use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventListener; use OCP\EventDispatcher\IEventListener;
@ -46,25 +47,6 @@ class UserLiveStatusListener implements IEventListener {
/** @var ITimeFactory */ /** @var ITimeFactory */
private $timeFactory; 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. * UserLiveStatusListener constructor.
* *
@ -92,7 +74,7 @@ class UserLiveStatusListener implements IEventListener {
} catch (DoesNotExistException $ex) { } catch (DoesNotExistException $ex) {
$userStatus = new UserStatus(); $userStatus = new UserStatus();
$userStatus->setUserId($user->getUID()); $userStatus->setUserId($user->getUID());
$userStatus->setStatus('offline'); $userStatus->setStatus(StatusService::OFFLINE);
$userStatus->setStatusTimestamp(0); $userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false); $userStatus->setIsUserDefined(false);
} }
@ -100,7 +82,7 @@ class UserLiveStatusListener implements IEventListener {
// If the status is user-defined and one of the persistent statuses, we // If the status is user-defined and one of the persistent statuses, we
// will not override it. // will not override it.
if ($userStatus->getIsUserDefined() && if ($userStatus->getIsUserDefined() &&
\in_array($userStatus->getStatus(), $this->persistentUserStatuses, true)) { \in_array($userStatus->getStatus(), StatusService::PERSISTENT_STATUSES, true)) {
return; return;
} }
@ -108,13 +90,13 @@ class UserLiveStatusListener implements IEventListener {
// If the current status is older than 5 minutes, // If the current status is older than 5 minutes,
// treat it as outdated and update // 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; $needsUpdate = true;
} }
// If the emitted status is more important than the current status // If the emitted status is more important than the current status
// treat it as outdated and update // 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; $needsUpdate = true;
} }

View File

@ -65,7 +65,7 @@ class JSDataService implements \JsonSerializable {
'messageIsPredefined' => false, 'messageIsPredefined' => false,
'icon' => null, 'icon' => null,
'clearAt' => null, 'clearAt' => null,
'status' => 'offline', 'status' => StatusService::OFFLINE,
'statusIsUserDefined' => false, 'statusIsUserDefined' => false,
]; ];
} }

View File

@ -54,17 +54,38 @@ class StatusService {
/** @var EmojiService */ /** @var EmojiService */
private $emojiService; private $emojiService;
/** @var string[] */ public const ONLINE = 'online';
private $allowedStatusTypes = [ public const AWAY = 'away';
'online', public const DND = 'dnd';
'away', public const INVISIBLE = 'invisible';
'dnd', public const OFFLINE = 'offline';
'invisible',
'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 */ /** @var int */
private $maximumMessageLength = 80; public const INVALIDATE_STATUS_THRESHOLD = 5 /* minutes */ * 60 /* seconds */;
/** @var int */
public const MAXIMUM_MESSAGE_LENGTH = 80;
/** /**
* StatusService constructor. * StatusService constructor.
@ -145,7 +166,7 @@ class StatusService {
} }
// Check if status-type is valid // 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'); throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported');
} }
if ($statusTimestamp === null) { if ($statusTimestamp === null) {
@ -179,7 +200,7 @@ class StatusService {
} catch (DoesNotExistException $ex) { } catch (DoesNotExistException $ex) {
$userStatus = new UserStatus(); $userStatus = new UserStatus();
$userStatus->setUserId($userId); $userStatus->setUserId($userId);
$userStatus->setStatus('offline'); $userStatus->setStatus(self::OFFLINE);
$userStatus->setStatusTimestamp(0); $userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false); $userStatus->setIsUserDefined(false);
} }
@ -224,7 +245,7 @@ class StatusService {
} catch (DoesNotExistException $ex) { } catch (DoesNotExistException $ex) {
$userStatus = new UserStatus(); $userStatus = new UserStatus();
$userStatus->setUserId($userId); $userStatus->setUserId($userId);
$userStatus->setStatus('offline'); $userStatus->setStatus(self::OFFLINE);
$userStatus->setStatusTimestamp(0); $userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false); $userStatus->setIsUserDefined(false);
} }
@ -234,8 +255,8 @@ class StatusService {
throw new InvalidStatusIconException('Status-Icon is longer than one character'); throw new InvalidStatusIconException('Status-Icon is longer than one character');
} }
// Check for maximum length of custom message // Check for maximum length of custom message
if (\mb_strlen($message) > $this->maximumMessageLength) { if (\mb_strlen($message) > self::MAXIMUM_MESSAGE_LENGTH) {
throw new StatusMessageTooLongException('Message is longer than supported length of ' . $this->maximumMessageLength . ' characters'); throw new StatusMessageTooLongException('Message is longer than supported length of ' . self::MAXIMUM_MESSAGE_LENGTH . ' characters');
} }
// Check that clearAt is in the future // Check that clearAt is in the future
if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) { if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
@ -266,7 +287,7 @@ class StatusService {
return false; return false;
} }
$userStatus->setStatus('offline'); $userStatus->setStatus(self::OFFLINE);
$userStatus->setStatusTimestamp(0); $userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false); $userStatus->setIsUserDefined(false);
@ -321,7 +342,7 @@ class StatusService {
private function processStatus(UserStatus $status): UserStatus { private function processStatus(UserStatus $status): UserStatus {
$clearAt = $status->getClearAt(); $clearAt = $status->getClearAt();
if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) { if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
$this->cleanStatus($status); $this->cleanStatusMessage($status);
} }
if ($status->getMessageId() !== null) { if ($status->getMessageId() !== null) {
$this->addDefaultMessage($status); $this->addDefaultMessage($status);
@ -333,7 +354,7 @@ class StatusService {
/** /**
* @param UserStatus $status * @param UserStatus $status
*/ */
private function cleanStatus(UserStatus $status): void { private function cleanStatusMessage(UserStatus $status): void {
$status->setMessageId(null); $status->setMessageId(null);
$status->setCustomIcon(null); $status->setCustomIcon(null);
$status->setCustomMessage(null); $status->setCustomMessage(null);

View File

@ -52,7 +52,7 @@ class ClearOldStatusesBackgroundJobTest extends TestCase {
public function testRun() { public function testRun() {
$this->mapper->expects($this->once()) $this->mapper->expects($this->once())
->method('clearOlderThan') ->method('clearMessagesOlderThan')
->with(1337); ->with(1337);
$this->time->method('getTime') $this->time->method('getTime')

View File

@ -155,7 +155,7 @@ class UserStatusMapperTest extends TestCase {
public function testClearOlderThan(): void { public function testClearOlderThan(): void {
$this->insertSampleStatuses(); $this->insertSampleStatuses();
$this->mapper->clearOlderThan(55000); $this->mapper->clearMessagesOlderThan(55000);
$allStatuses = $this->mapper->findAll(); $allStatuses = $this->mapper->findAll();
$this->assertCount(3, $allStatuses); $this->assertCount(3, $allStatuses);