* @inheritDoc
*/
protected function run($argument) {
- $this->mapper->clearOlderThan($this->time->getTime());
+ $this->mapper->clearMessagesOlderThan($this->time->getTime());
}
}
namespace OCA\UserStatus\Connector;
use DateTimeImmutable;
+use OCA\UserStatus\Service\StatusService;
use OCP\UserStatus\IUserStatus;
use OCA\UserStatus\Db;
$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());
namespace OCA\UserStatus\Controller;
+use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
* @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);
}
*/
private function formatStatus(UserStatus $status): array {
$visibleStatus = $status->getStatus();
- if ($visibleStatus === 'invisible') {
- $visibleStatus = 'offline';
+ if ($visibleStatus === StatusService::INVISIBLE) {
+ $visibleStatus = StatusService::OFFLINE;
}
return [
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(),
*
* @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))
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;
/** @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.
*
} catch (DoesNotExistException $ex) {
$userStatus = new UserStatus();
$userStatus->setUserId($user->getUID());
- $userStatus->setStatus('offline');
+ $userStatus->setStatus(StatusService::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);
}
// 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;
}
// 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;
}
'messageIsPredefined' => false,
'icon' => null,
'clearAt' => null,
- 'status' => 'offline',
+ 'status' => StatusService::OFFLINE,
'statusIsUserDefined' => false,
];
}
/** @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.
}
// 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) {
} catch (DoesNotExistException $ex) {
$userStatus = new UserStatus();
$userStatus->setUserId($userId);
- $userStatus->setStatus('offline');
+ $userStatus->setStatus(self::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);
}
} catch (DoesNotExistException $ex) {
$userStatus = new UserStatus();
$userStatus->setUserId($userId);
- $userStatus->setStatus('offline');
+ $userStatus->setStatus(self::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);
}
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()) {
return false;
}
- $userStatus->setStatus('offline');
+ $userStatus->setStatus(self::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);
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);
/**
* @param UserStatus $status
*/
- private function cleanStatus(UserStatus $status): void {
+ private function cleanStatusMessage(UserStatus $status): void {
$status->setMessageId(null);
$status->setCustomIcon(null);
$status->setCustomMessage(null);
public function testRun() {
$this->mapper->expects($this->once())
- ->method('clearOlderThan')
+ ->method('clearMessagesOlderThan')
->with(1337);
$this->time->method('getTime')
public function testClearOlderThan(): void {
$this->insertSampleStatuses();
- $this->mapper->clearOlderThan(55000);
+ $this->mapper->clearMessagesOlderThan(55000);
$allStatuses = $this->mapper->findAll();
$this->assertCount(3, $allStatuses);