mirror of https://github.com/nextcloud/server.git
Provide a PHP Api for UserStatus
Signed-off-by: Georg Ehrke <developer@georgehrke.com>pull/22106/head
parent
0581356169
commit
0e0e0d19e8
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2020, Georg Ehrke
|
||||
*
|
||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
namespace OCA\UserStatus\Connector;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use OCP\UserStatus\IUserStatus;
|
||||
use OCA\UserStatus\Db;
|
||||
|
||||
class UserStatus implements IUserStatus {
|
||||
|
||||
/** @var string */
|
||||
private $userId;
|
||||
|
||||
/** @var string */
|
||||
private $status;
|
||||
|
||||
/** @var string|null */
|
||||
private $message;
|
||||
|
||||
/** @var string|null */
|
||||
private $icon;
|
||||
|
||||
/** @var DateTimeImmutable|null */
|
||||
private $clearAt;
|
||||
|
||||
/**
|
||||
* UserStatus constructor.
|
||||
*
|
||||
* @param Db\UserStatus $status
|
||||
*/
|
||||
public function __construct(Db\UserStatus $status) {
|
||||
$this->userId = $status->getUserId();
|
||||
$this->status = $status->getStatus();
|
||||
$this->message = $status->getCustomMessage();
|
||||
$this->icon = $status->getCustomIcon();
|
||||
|
||||
if ($status->getStatus() === 'invisible') {
|
||||
$this->status = 'offline';
|
||||
}
|
||||
if ($status->getClearAt() !== null) {
|
||||
$this->clearAt = DateTimeImmutable::createFromFormat('U', (string)$status->getClearAt());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUserId(): string {
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getStatus(): string {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getMessage(): ?string {
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getIcon(): ?string {
|
||||
return $this->icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getClearAt(): ?DateTimeImmutable {
|
||||
return $this->clearAt;
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2020, Georg Ehrke
|
||||
*
|
||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
namespace OCA\UserStatus\Connector;
|
||||
|
||||
use OCA\UserStatus\Service\StatusService;
|
||||
use OCP\UserStatus\IProvider;
|
||||
|
||||
class UserStatusProvider implements IProvider {
|
||||
|
||||
/** @var StatusService */
|
||||
private $service;
|
||||
|
||||
/**
|
||||
* UserStatusProvider constructor.
|
||||
*
|
||||
* @param StatusService $service
|
||||
*/
|
||||
public function __construct(StatusService $service) {
|
||||
$this->service = $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUserStatuses(array $userIds): array {
|
||||
$statuses = $this->service->findByUserIds($userIds);
|
||||
|
||||
$userStatuses = [];
|
||||
foreach ($statuses as $status) {
|
||||
$userStatuses[$status->getUserId()] = new UserStatus($status);
|
||||
}
|
||||
|
||||
return $userStatuses;
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2020, Georg Ehrke
|
||||
*
|
||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\UserStatus\Tests\Connector;
|
||||
|
||||
use OCA\UserStatus\Connector\UserStatusProvider;
|
||||
use OCA\UserStatus\Db\UserStatus;
|
||||
use OCA\UserStatus\Service\StatusService;
|
||||
use Test\TestCase;
|
||||
|
||||
class UserStatusProviderTest extends TestCase {
|
||||
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
||||
private $service;
|
||||
|
||||
/** @var UserStatusProvider */
|
||||
private $provider;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->service = $this->createMock(StatusService::class);
|
||||
$this->provider = new UserStatusProvider($this->service);
|
||||
}
|
||||
|
||||
public function testGetUserStatuses(): void {
|
||||
$userStatus2 = new UserStatus();
|
||||
$userStatus2->setUserId('userId2');
|
||||
$userStatus2->setStatus('dnd');
|
||||
$userStatus2->setStatusTimestamp(5000);
|
||||
$userStatus2->setIsUserDefined(true);
|
||||
$userStatus2->setCustomIcon('💩');
|
||||
$userStatus2->setCustomMessage('Do not disturb');
|
||||
$userStatus2->setClearAt(50000);
|
||||
|
||||
$userStatus3 = new UserStatus();
|
||||
$userStatus3->setUserId('userId3');
|
||||
$userStatus3->setStatus('away');
|
||||
$userStatus3->setStatusTimestamp(5000);
|
||||
$userStatus3->setIsUserDefined(false);
|
||||
$userStatus3->setCustomIcon('🏝');
|
||||
$userStatus3->setCustomMessage('On vacation');
|
||||
$userStatus3->setClearAt(60000);
|
||||
|
||||
$this->service->expects($this->once())
|
||||
->method('findByUserIds')
|
||||
->with(['userId1', 'userId2', 'userId3'])
|
||||
->willReturn([$userStatus2, $userStatus3]);
|
||||
|
||||
$actual = $this->provider->getUserStatuses(['userId1', 'userId2', 'userId3']);
|
||||
|
||||
$this->assertCount(2, $actual);
|
||||
$status2 = $actual['userId2'];
|
||||
$this->assertEquals('userId2', $status2->getUserId());
|
||||
$this->assertEquals('dnd', $status2->getStatus());
|
||||
$this->assertEquals('Do not disturb', $status2->getMessage());
|
||||
$this->assertEquals('💩', $status2->getIcon());
|
||||
$dateTime2 = $status2->getClearAt();
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime2);
|
||||
$this->assertEquals('50000', $dateTime2->format('U'));
|
||||
|
||||
$status3 = $actual['userId3'];
|
||||
$this->assertEquals('userId3', $status3->getUserId());
|
||||
$this->assertEquals('away', $status3->getStatus());
|
||||
$this->assertEquals('On vacation', $status3->getMessage());
|
||||
$this->assertEquals('🏝', $status3->getIcon());
|
||||
$dateTime3 = $status3->getClearAt();
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime3);
|
||||
$this->assertEquals('60000', $dateTime3->format('U'));
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2020, Georg Ehrke
|
||||
*
|
||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\UserStatus\Tests\Connector;
|
||||
|
||||
use OCA\UserStatus\Connector\UserStatus;
|
||||
use Test\TestCase;
|
||||
use OCA\UserStatus\Db;
|
||||
|
||||
class UserStatusTest extends TestCase {
|
||||
public function testUserStatus() {
|
||||
$status = new Db\UserStatus();
|
||||
$status->setUserId('user2');
|
||||
$status->setStatus('away');
|
||||
$status->setStatusTimestamp(5000);
|
||||
$status->setIsUserDefined(false);
|
||||
$status->setCustomIcon('🏝');
|
||||
$status->setCustomMessage('On vacation');
|
||||
$status->setClearAt(60000);
|
||||
|
||||
$userStatus = new UserStatus($status);
|
||||
$this->assertEquals('user2', $userStatus->getUserId());
|
||||
$this->assertEquals('away', $userStatus->getStatus());
|
||||
$this->assertEquals('On vacation', $userStatus->getMessage());
|
||||
$this->assertEquals('🏝', $userStatus->getIcon());
|
||||
|
||||
$dateTime = $userStatus->getClearAt();
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
|
||||
$this->assertEquals('60000', $dateTime->format('U'));
|
||||
}
|
||||
|
||||
public function testUserStatusInvisible() {
|
||||
$status = new Db\UserStatus();
|
||||
$status->setUserId('user2');
|
||||
$status->setStatus('invisible');
|
||||
$status->setStatusTimestamp(5000);
|
||||
$status->setIsUserDefined(false);
|
||||
$status->setCustomIcon('🏝');
|
||||
$status->setCustomMessage('On vacation');
|
||||
$status->setClearAt(60000);
|
||||
|
||||
$userStatus = new UserStatus($status);
|
||||
$this->assertEquals('user2', $userStatus->getUserId());
|
||||
$this->assertEquals('offline', $userStatus->getStatus());
|
||||
$this->assertEquals('On vacation', $userStatus->getMessage());
|
||||
$this->assertEquals('🏝', $userStatus->getIcon());
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2020, Georg Ehrke
|
||||
*
|
||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\UserStatus;
|
||||
|
||||
use OCP\ILogger;
|
||||
use OCP\IServerContainer;
|
||||
use OCP\UserStatus\IManager;
|
||||
use OCP\UserStatus\IProvider;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
|
||||
class Manager implements IManager {
|
||||
|
||||
/** @var IServerContainer */
|
||||
private $container;
|
||||
|
||||
/** @var ILogger */
|
||||
private $logger;
|
||||
|
||||
/** @var null */
|
||||
private $providerClass;
|
||||
|
||||
/** @var IProvider */
|
||||
private $provider;
|
||||
|
||||
/**
|
||||
* Manager constructor.
|
||||
*
|
||||
* @param IServerContainer $container
|
||||
* @param ILogger $logger
|
||||
*/
|
||||
public function __construct(IServerContainer $container,
|
||||
ILogger $logger) {
|
||||
$this->container = $container;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUserStatuses(array $userIds): array {
|
||||
$this->setupProvider();
|
||||
if (!$this->provider) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->provider->getUserStatuses($userIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @since 20.0.0
|
||||
* @internal
|
||||
*/
|
||||
public function registerProvider(string $class): void {
|
||||
$this->providerClass = $class;
|
||||
$this->provider = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazily set up provider
|
||||
*/
|
||||
private function setupProvider(): void {
|
||||
if ($this->provider !== null) {
|
||||
return;
|
||||
}
|
||||
if ($this->providerClass === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$provider = $this->container->get($this->providerClass);
|
||||
} catch (ContainerExceptionInterface $e) {
|
||||
$this->logger->logException($e, [
|
||||
'message' => 'Could not load user-status provider dynamically: ' . $e->getMessage(),
|
||||
'level' => ILogger::ERROR,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->provider = $provider;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2020, Georg Ehrke
|
||||
*
|
||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCP\UserStatus;
|
||||
|
||||
/**
|
||||
* Interface IManager
|
||||
*
|
||||
* @package OCP\UserStatus
|
||||
* @since 20.0.0
|
||||
*/
|
||||
interface IManager {
|
||||
|
||||
/**
|
||||
* Gets the statuses for all users in $users
|
||||
*
|
||||
* @param string[] $userIds
|
||||
* @return IUserStatus[]
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public function getUserStatuses(array $userIds):array;
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2020, Georg Ehrke
|
||||
*
|
||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCP\UserStatus;
|
||||
|
||||
/**
|
||||
* Interface IManager
|
||||
*
|
||||
* @package OCP\UserStatus
|
||||
* @since 20.0.0
|
||||
*/
|
||||
interface IProvider {
|
||||
|
||||
/**
|
||||
* Gets the statuses for all users in $users
|
||||
*
|
||||
* @param string[] $userIds
|
||||
* @return IUserStatus[]
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public function getUserStatuses(array $userIds):array;
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2020, Georg Ehrke
|
||||
*
|
||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCP\UserStatus;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* Interface IUserStatus
|
||||
*
|
||||
* @package OCP\UserStatus
|
||||
* @since 20.0.0
|
||||
*/
|
||||
interface IUserStatus {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public const ONLINE = 'online';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public const AWAY = 'away';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public const DND = 'dnd';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public const OFFLINE = 'offline';
|
||||
|
||||
/**
|
||||
* Get the user this status is connected to
|
||||
*
|
||||
* @return string
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public function getUserId():string;
|
||||
|
||||
/**
|
||||
* Get the status
|
||||
*
|
||||
* It will return one of the constants defined above.
|
||||
* It will never return invisible. In case a user marked
|
||||
* themselves as invisible, it will return offline.
|
||||
*
|
||||
* @return string See IUserStatus constants
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public function getStatus():string;
|
||||
|
||||
/**
|
||||
* Get a custom message provided by the user
|
||||
*
|
||||
* @return string|null
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public function getMessage():?string;
|
||||
|
||||
/**
|
||||
* Get a custom icon provided by the user
|
||||
*
|
||||
* @return string|null
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public function getIcon():?string;
|
||||
|
||||
/**
|
||||
* Gets the time that the custom status will be cleared at
|
||||
*
|
||||
* @return DateTimeImmutable|null
|
||||
* @since 20.0.0
|
||||
*/
|
||||
public function getClearAt():?DateTimeImmutable;
|
||||
}
|
Loading…
Reference in New Issue