Skip to content

Commit

Permalink
Always use IUserStatus consts
Browse files Browse the repository at this point in the history
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
  • Loading branch information
georgehrke committed Sep 7, 2020
1 parent 2146950 commit d7ccc61
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 34 deletions.
5 changes: 2 additions & 3 deletions apps/user_status/lib/Connector/UserStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
namespace OCA\UserStatus\Connector;

use DateTimeImmutable;
use OCA\UserStatus\Service\StatusService;
use OCP\UserStatus\IUserStatus;
use OCA\UserStatus\Db;

Expand Down Expand Up @@ -57,8 +56,8 @@ public function __construct(Db\UserStatus $status) {
$this->message = $status->getCustomMessage();
$this->icon = $status->getCustomIcon();

if ($status->getStatus() === StatusService::INVISIBLE) {
$this->status = StatusService::OFFLINE;
if ($status->getStatus() === IUserStatus::INVISIBLE) {
$this->status = IUserStatus::OFFLINE;
}
if ($status->getClearAt() !== null) {
$this->clearAt = DateTimeImmutable::createFromFormat('U', (string)$status->getClearAt());
Expand Down
4 changes: 2 additions & 2 deletions apps/user_status/lib/Controller/HeartbeatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

namespace OCA\UserStatus\Controller;

use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
Expand All @@ -34,6 +33,7 @@
use OCP\IRequest;
use OCP\IUserSession;
use OCP\User\Events\UserLiveStatusEvent;
use OCP\UserStatus\IUserStatus;

class HeartbeatController extends Controller {

Expand Down Expand Up @@ -73,7 +73,7 @@ public function __construct(string $appName,
* @return 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);
}

Expand Down
5 changes: 3 additions & 2 deletions apps/user_status/lib/Controller/StatusesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\UserStatus\IUserStatus;

class StatusesController extends OCSController {

Expand Down Expand Up @@ -92,8 +93,8 @@ public function find(string $userId): DataResponse {
*/
private function formatStatus(UserStatus $status): array {
$visibleStatus = $status->getStatus();
if ($visibleStatus === StatusService::INVISIBLE) {
$visibleStatus = StatusService::OFFLINE;
if ($visibleStatus === IUserStatus::INVISIBLE) {
$visibleStatus = IUserStatus::OFFLINE;
}

return [
Expand Down
5 changes: 3 additions & 2 deletions apps/user_status/lib/Dashboard/UserStatusWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\IL10N;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\UserStatus\IUserStatus;

/**
* Class UserStatusWidget
Expand Down Expand Up @@ -146,8 +147,8 @@ static function (UserStatus $status) use ($currentUserId): bool {
return [
'userId' => $status->getUserId(),
'displayName' => $displayName,
'status' => $status->getStatus() === StatusService::INVISIBLE
? StatusService::OFFLINE
'status' => $status->getStatus() === IUserStatus::INVISIBLE
? IUserStatus::OFFLINE
: $status->getStatus(),
'icon' => $status->getCustomIcon(),
'message' => $status->getCustomMessage(),
Expand Down
8 changes: 4 additions & 4 deletions apps/user_status/lib/Db/UserStatusMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

namespace OCA\UserStatus\Db;

use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\UserStatus\IUserStatus;

/**
* Class UserStatusMapper
Expand Down Expand Up @@ -82,7 +82,7 @@ public function findAllRecent(?int $limit = null, ?int $offset = null): array {
->select('*')
->from($this->tableName)
->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('custom_icon'))
->orWhere($qb->expr()->isNotNull('custom_message'));
Expand Down Expand Up @@ -133,13 +133,13 @@ public function findByUserIds(array $userIds):array {
public function clearStatusesOlderThan(int $olderThan, int $now): void {
$qb = $this->db->getQueryBuilder();
$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('status_timestamp', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
->where($qb->expr()->lte('status_timestamp', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->orX(
$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();
Expand Down
3 changes: 2 additions & 1 deletion apps/user_status/lib/Listener/UserLiveStatusListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\EventDispatcher\IEventListener;
use OCP\EventDispatcher\Event;
use OCP\User\Events\UserLiveStatusEvent;
use OCP\UserStatus\IUserStatus;

/**
* Class UserDeletedListener
Expand Down Expand Up @@ -74,7 +75,7 @@ public function handle(Event $event): void {
} catch (DoesNotExistException $ex) {
$userStatus = new UserStatus();
$userStatus->setUserId($user->getUID());
$userStatus->setStatus(StatusService::OFFLINE);
$userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);
}
Expand Down
3 changes: 2 additions & 1 deletion apps/user_status/lib/Service/JSDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IUserSession;
use OCP\UserStatus\IUserStatus;

class JSDataService implements \JsonSerializable {

Expand Down Expand Up @@ -65,7 +66,7 @@ public function jsonSerialize() {
'messageIsPredefined' => false,
'icon' => null,
'clearAt' => null,
'status' => StatusService::OFFLINE,
'status' => IUserStatus::OFFLINE,
'statusIsUserDefined' => false,
];
}
Expand Down
33 changes: 14 additions & 19 deletions apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\UserStatus\Exception\StatusMessageTooLongException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\UserStatus\IUserStatus;

/**
* Class StatusService
Expand All @@ -54,31 +55,25 @@ class StatusService {
/** @var 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
*/
public const PRIORITY_ORDERED_STATUSES = [
self::ONLINE,
self::AWAY,
self::DND,
self::INVISIBLE,
self::OFFLINE
IUserStatus::ONLINE,
IUserStatus::AWAY,
IUserStatus::DND,
IUserStatus::INVISIBLE,
IUserStatus::OFFLINE
];

/**
* List of statuses that persist the clear-up
* or UserLiveStatusEvents
*/
public const PERSISTENT_STATUSES = [
self::AWAY,
self::DND,
self::INVISIBLE,
IUserStatus::AWAY,
IUserStatus::DND,
IUserStatus::INVISIBLE,
];

/** @var int */
Expand Down Expand Up @@ -200,7 +195,7 @@ public function setPredefinedMessage(string $userId,
} catch (DoesNotExistException $ex) {
$userStatus = new UserStatus();
$userStatus->setUserId($userId);
$userStatus->setStatus(self::OFFLINE);
$userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);
}
Expand Down Expand Up @@ -245,7 +240,7 @@ public function setCustomMessage(string $userId,
} catch (DoesNotExistException $ex) {
$userStatus = new UserStatus();
$userStatus->setUserId($userId);
$userStatus->setStatus(self::OFFLINE);
$userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);
}
Expand Down Expand Up @@ -287,7 +282,7 @@ public function clearStatus(string $userId): bool {
return false;
}

$userStatus->setStatus(self::OFFLINE);
$userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);

Expand Down Expand Up @@ -343,7 +338,7 @@ private function processStatus(UserStatus $status): UserStatus {
$clearAt = $status->getClearAt();

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);
}
if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
Expand All @@ -360,7 +355,7 @@ private function processStatus(UserStatus $status): UserStatus {
* @param UserStatus $status
*/
private function cleanStatus(UserStatus $status): void {
$status->setStatus(self::OFFLINE);
$status->setStatus(IUserStatus::OFFLINE);
$status->setStatusTimestamp($this->timeFactory->getTime());
$status->setIsUserDefined(false);

Expand Down
6 changes: 6 additions & 0 deletions lib/public/UserStatus/IUserStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ interface IUserStatus {
*/
public const OFFLINE = 'offline';

/**
* @var string
* @since 20.0.0
*/
public const INVISIBLE = 'invisible';

/**
* Get the user this status is connected to
*
Expand Down

0 comments on commit d7ccc61

Please sign in to comment.