Always use IUserStatus consts

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
This commit is contained in:
Georg Ehrke 2020-09-03 16:23:35 +02:00
parent 2146950a71
commit d7ccc616e4
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43
9 changed files with 38 additions and 34 deletions

View File

@ -25,7 +25,6 @@ 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;
@ -57,8 +56,8 @@ class UserStatus implements IUserStatus {
$this->message = $status->getCustomMessage(); $this->message = $status->getCustomMessage();
$this->icon = $status->getCustomIcon(); $this->icon = $status->getCustomIcon();
if ($status->getStatus() === StatusService::INVISIBLE) { if ($status->getStatus() === IUserStatus::INVISIBLE) {
$this->status = StatusService::OFFLINE; $this->status = IUserStatus::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,7 +25,6 @@ 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;
@ -34,6 +33,7 @@ use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\User\Events\UserLiveStatusEvent; use OCP\User\Events\UserLiveStatusEvent;
use OCP\UserStatus\IUserStatus;
class HeartbeatController extends Controller { class HeartbeatController extends Controller {
@ -73,7 +73,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, [StatusService::ONLINE, StatusService::AWAY], true)) { if (!\in_array($status, [IUserStatus::ONLINE, IUserStatus::AWAY], true)) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST); return new JSONResponse([], Http::STATUS_BAD_REQUEST);
} }

View File

@ -32,6 +32,7 @@ use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\IRequest; use OCP\IRequest;
use OCP\UserStatus\IUserStatus;
class StatusesController extends OCSController { class StatusesController extends OCSController {
@ -92,8 +93,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 === StatusService::INVISIBLE) { if ($visibleStatus === IUserStatus::INVISIBLE) {
$visibleStatus = StatusService::OFFLINE; $visibleStatus = IUserStatus::OFFLINE;
} }
return [ return [

View File

@ -32,6 +32,7 @@ use OCP\IInitialStateService;
use OCP\IL10N; use OCP\IL10N;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\UserStatus\IUserStatus;
/** /**
* Class UserStatusWidget * Class UserStatusWidget
@ -146,8 +147,8 @@ class UserStatusWidget implements IWidget {
return [ return [
'userId' => $status->getUserId(), 'userId' => $status->getUserId(),
'displayName' => $displayName, 'displayName' => $displayName,
'status' => $status->getStatus() === StatusService::INVISIBLE 'status' => $status->getStatus() === IUserStatus::INVISIBLE
? StatusService::OFFLINE ? IUserStatus::OFFLINE
: $status->getStatus(), : $status->getStatus(),
'icon' => $status->getCustomIcon(), 'icon' => $status->getCustomIcon(),
'message' => $status->getCustomMessage(), 'message' => $status->getCustomMessage(),

View File

@ -25,10 +25,10 @@ declare(strict_types=1);
namespace OCA\UserStatus\Db; namespace OCA\UserStatus\Db;
use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Db\QBMapper; use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\UserStatus\IUserStatus;
/** /**
* Class UserStatusMapper * Class UserStatusMapper
@ -82,7 +82,7 @@ class UserStatusMapper extends QBMapper {
->select('*') ->select('*')
->from($this->tableName) ->from($this->tableName)
->orderBy('status_timestamp', 'DESC') ->orderBy('status_timestamp', 'DESC')
->where($qb->expr()->notIn('status', $qb->createNamedParameter([StatusService::ONLINE, StatusService::AWAY, StatusService::OFFLINE], IQueryBuilder::PARAM_STR_ARRAY))) ->where($qb->expr()->notIn('status', $qb->createNamedParameter([IUserStatus::ONLINE, IUserStatus::AWAY, IUserStatus::OFFLINE], IQueryBuilder::PARAM_STR_ARRAY)))
->orWhere($qb->expr()->isNotNull('message_id')) ->orWhere($qb->expr()->isNotNull('message_id'))
->orWhere($qb->expr()->isNotNull('custom_icon')) ->orWhere($qb->expr()->isNotNull('custom_icon'))
->orWhere($qb->expr()->isNotNull('custom_message')); ->orWhere($qb->expr()->isNotNull('custom_message'));
@ -133,13 +133,13 @@ class UserStatusMapper extends QBMapper {
public function clearStatusesOlderThan(int $olderThan, int $now): void { public function clearStatusesOlderThan(int $olderThan, int $now): void {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb->update($this->tableName) $qb->update($this->tableName)
->set('status', $qb->createNamedParameter(StatusService::OFFLINE)) ->set('status', $qb->createNamedParameter(IUserStatus::OFFLINE))
->set('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) ->set('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
->set('status_timestamp', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT)) ->set('status_timestamp', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
->where($qb->expr()->lte('status_timestamp', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) ->where($qb->expr()->lte('status_timestamp', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->orX( ->andWhere($qb->expr()->orX(
$qb->expr()->eq('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL), $qb->expr()->eq('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL),
$qb->expr()->eq('status', $qb->createNamedParameter(StatusService::ONLINE)) $qb->expr()->eq('status', $qb->createNamedParameter(IUserStatus::ONLINE))
)); ));
$qb->execute(); $qb->execute();

View File

@ -33,6 +33,7 @@ use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventListener; use OCP\EventDispatcher\IEventListener;
use OCP\EventDispatcher\Event; use OCP\EventDispatcher\Event;
use OCP\User\Events\UserLiveStatusEvent; use OCP\User\Events\UserLiveStatusEvent;
use OCP\UserStatus\IUserStatus;
/** /**
* Class UserDeletedListener * Class UserDeletedListener
@ -74,7 +75,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(StatusService::OFFLINE); $userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0); $userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false); $userStatus->setIsUserDefined(false);
} }

View File

@ -27,6 +27,7 @@ namespace OCA\UserStatus\Service;
use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\UserStatus\IUserStatus;
class JSDataService implements \JsonSerializable { class JSDataService implements \JsonSerializable {
@ -65,7 +66,7 @@ class JSDataService implements \JsonSerializable {
'messageIsPredefined' => false, 'messageIsPredefined' => false,
'icon' => null, 'icon' => null,
'clearAt' => null, 'clearAt' => null,
'status' => StatusService::OFFLINE, 'status' => IUserStatus::OFFLINE,
'statusIsUserDefined' => false, 'statusIsUserDefined' => false,
]; ];
} }

View File

@ -34,6 +34,7 @@ use OCA\UserStatus\Exception\InvalidStatusTypeException;
use OCA\UserStatus\Exception\StatusMessageTooLongException; use OCA\UserStatus\Exception\StatusMessageTooLongException;
use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\UserStatus\IUserStatus;
/** /**
* Class StatusService * Class StatusService
@ -54,21 +55,15 @@ class StatusService {
/** @var EmojiService */ /** @var EmojiService */
private $emojiService; private $emojiService;
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 * List of priorities ordered by their priority
*/ */
public const PRIORITY_ORDERED_STATUSES = [ public const PRIORITY_ORDERED_STATUSES = [
self::ONLINE, IUserStatus::ONLINE,
self::AWAY, IUserStatus::AWAY,
self::DND, IUserStatus::DND,
self::INVISIBLE, IUserStatus::INVISIBLE,
self::OFFLINE IUserStatus::OFFLINE
]; ];
/** /**
@ -76,9 +71,9 @@ class StatusService {
* or UserLiveStatusEvents * or UserLiveStatusEvents
*/ */
public const PERSISTENT_STATUSES = [ public const PERSISTENT_STATUSES = [
self::AWAY, IUserStatus::AWAY,
self::DND, IUserStatus::DND,
self::INVISIBLE, IUserStatus::INVISIBLE,
]; ];
/** @var int */ /** @var int */
@ -200,7 +195,7 @@ class StatusService {
} catch (DoesNotExistException $ex) { } catch (DoesNotExistException $ex) {
$userStatus = new UserStatus(); $userStatus = new UserStatus();
$userStatus->setUserId($userId); $userStatus->setUserId($userId);
$userStatus->setStatus(self::OFFLINE); $userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0); $userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false); $userStatus->setIsUserDefined(false);
} }
@ -245,7 +240,7 @@ class StatusService {
} catch (DoesNotExistException $ex) { } catch (DoesNotExistException $ex) {
$userStatus = new UserStatus(); $userStatus = new UserStatus();
$userStatus->setUserId($userId); $userStatus->setUserId($userId);
$userStatus->setStatus(self::OFFLINE); $userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0); $userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false); $userStatus->setIsUserDefined(false);
} }
@ -287,7 +282,7 @@ class StatusService {
return false; return false;
} }
$userStatus->setStatus(self::OFFLINE); $userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0); $userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false); $userStatus->setIsUserDefined(false);
@ -343,7 +338,7 @@ class StatusService {
$clearAt = $status->getClearAt(); $clearAt = $status->getClearAt();
if ($status->getStatusTimestamp() < $this->timeFactory->getTime() - self::INVALIDATE_STATUS_THRESHOLD if ($status->getStatusTimestamp() < $this->timeFactory->getTime() - self::INVALIDATE_STATUS_THRESHOLD
&& (!$status->getIsUserDefined() || $status->getStatus() === self::ONLINE)) { && (!$status->getIsUserDefined() || $status->getStatus() === IUserStatus::ONLINE)) {
$this->cleanStatus($status); $this->cleanStatus($status);
} }
if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) { if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
@ -360,7 +355,7 @@ class StatusService {
* @param UserStatus $status * @param UserStatus $status
*/ */
private function cleanStatus(UserStatus $status): void { private function cleanStatus(UserStatus $status): void {
$status->setStatus(self::OFFLINE); $status->setStatus(IUserStatus::OFFLINE);
$status->setStatusTimestamp($this->timeFactory->getTime()); $status->setStatusTimestamp($this->timeFactory->getTime());
$status->setIsUserDefined(false); $status->setIsUserDefined(false);

View File

@ -59,6 +59,12 @@ interface IUserStatus {
*/ */
public const OFFLINE = 'offline'; public const OFFLINE = 'offline';
/**
* @var string
* @since 20.0.0
*/
public const INVISIBLE = 'invisible';
/** /**
* Get the user this status is connected to * Get the user this status is connected to
* *