]> source.dussan.org Git - nextcloud-server.git/commitdiff
Provide a PHP Api for UserStatus 22106/head
authorGeorg Ehrke <developer@georgehrke.com>
Tue, 4 Aug 2020 17:34:55 +0000 (19:34 +0200)
committerGeorg Ehrke <developer@georgehrke.com>
Wed, 5 Aug 2020 11:48:46 +0000 (13:48 +0200)
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
17 files changed:
apps/user_status/composer/composer/autoload_classmap.php
apps/user_status/composer/composer/autoload_static.php
apps/user_status/lib/AppInfo/Application.php
apps/user_status/lib/Connector/UserStatus.php [new file with mode: 0644]
apps/user_status/lib/Connector/UserStatusProvider.php [new file with mode: 0644]
apps/user_status/lib/Db/UserStatusMapper.php
apps/user_status/lib/Service/StatusService.php
apps/user_status/tests/Unit/Connector/UserStatusProviderTest.php [new file with mode: 0644]
apps/user_status/tests/Unit/Connector/UserStatusTest.php [new file with mode: 0644]
apps/user_status/tests/Unit/Db/UserStatusMapperTest.php
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/Server.php
lib/private/UserStatus/Manager.php [new file with mode: 0644]
lib/public/UserStatus/IManager.php [new file with mode: 0644]
lib/public/UserStatus/IProvider.php [new file with mode: 0644]
lib/public/UserStatus/IUserStatus.php [new file with mode: 0644]

index aebcc98bea055c9314e8eabc0fbe0946ce92f33b..ffbe128697e8d3a923b475b82ab4c044d08ae1ce 100644 (file)
@@ -9,6 +9,8 @@ return array(
     'OCA\\UserStatus\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
     'OCA\\UserStatus\\BackgroundJob\\ClearOldStatusesBackgroundJob' => $baseDir . '/../lib/BackgroundJob/ClearOldStatusesBackgroundJob.php',
     'OCA\\UserStatus\\Capabilities' => $baseDir . '/../lib/Capabilities.php',
+    'OCA\\UserStatus\\Connector\\UserStatus' => $baseDir . '/../lib/Connector/UserStatus.php',
+    'OCA\\UserStatus\\Connector\\UserStatusProvider' => $baseDir . '/../lib/Connector/UserStatusProvider.php',
     'OCA\\UserStatus\\Controller\\HeartbeatController' => $baseDir . '/../lib/Controller/HeartbeatController.php',
     'OCA\\UserStatus\\Controller\\PredefinedStatusController' => $baseDir . '/../lib/Controller/PredefinedStatusController.php',
     'OCA\\UserStatus\\Controller\\StatusesController' => $baseDir . '/../lib/Controller/StatusesController.php',
index c652caa5ad3ad6fb45014bc378e67a6e58bd2bad..9ab1c9e4df82ed53da9f287253b263e0554b7976 100644 (file)
@@ -24,6 +24,8 @@ class ComposerStaticInitUserStatus
         'OCA\\UserStatus\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
         'OCA\\UserStatus\\BackgroundJob\\ClearOldStatusesBackgroundJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/ClearOldStatusesBackgroundJob.php',
         'OCA\\UserStatus\\Capabilities' => __DIR__ . '/..' . '/../lib/Capabilities.php',
+        'OCA\\UserStatus\\Connector\\UserStatus' => __DIR__ . '/..' . '/../lib/Connector/UserStatus.php',
+        'OCA\\UserStatus\\Connector\\UserStatusProvider' => __DIR__ . '/..' . '/../lib/Connector/UserStatusProvider.php',
         'OCA\\UserStatus\\Controller\\HeartbeatController' => __DIR__ . '/..' . '/../lib/Controller/HeartbeatController.php',
         'OCA\\UserStatus\\Controller\\PredefinedStatusController' => __DIR__ . '/..' . '/../lib/Controller/PredefinedStatusController.php',
         'OCA\\UserStatus\\Controller\\StatusesController' => __DIR__ . '/..' . '/../lib/Controller/StatusesController.php',
index 6de72e018398e05bba92deff2fc1c3d816d3f737..3fda9c49eae4b625a8b9dfc244b8b7993d7a58bc 100644 (file)
@@ -26,6 +26,7 @@ declare(strict_types=1);
 namespace OCA\UserStatus\AppInfo;
 
 use OCA\UserStatus\Capabilities;
+use OCA\UserStatus\Connector\UserStatusProvider;
 use OCA\UserStatus\Listener\BeforeTemplateRenderedListener;
 use OCA\UserStatus\Listener\UserDeletedListener;
 use OCA\UserStatus\Listener\UserLiveStatusListener;
@@ -36,6 +37,7 @@ use OCP\AppFramework\Bootstrap\IRegistrationContext;
 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
 use OCP\User\Events\UserDeletedEvent;
 use OCP\User\Events\UserLiveStatusEvent;
+use OCP\UserStatus\IManager;
 
 /**
  * Class Application
@@ -70,5 +72,8 @@ class Application extends App implements IBootstrap {
        }
 
        public function boot(IBootContext $context): void {
+               /** @var IManager $userStatusManager */
+               $userStatusManager = $context->getServerContainer()->get(IManager::class);
+               $userStatusManager->registerProvider(UserStatusProvider::class);
        }
 }
diff --git a/apps/user_status/lib/Connector/UserStatus.php b/apps/user_status/lib/Connector/UserStatus.php
new file mode 100644 (file)
index 0000000..acbdac2
--- /dev/null
@@ -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;
+       }
+}
diff --git a/apps/user_status/lib/Connector/UserStatusProvider.php b/apps/user_status/lib/Connector/UserStatusProvider.php
new file mode 100644 (file)
index 0000000..1197d19
--- /dev/null
@@ -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;
+       }
+}
index 4e78ef11e057ce121d78a192392eecf29c1ec5bd..c9e18a383575f45c916a896d8619acc782ff5513 100644 (file)
@@ -84,6 +84,20 @@ class UserStatusMapper extends QBMapper {
                return $this->findEntity($qb);
        }
 
+       /**
+        * @param array $userIds
+        * @return array
+        */
+       public function findByUserIds(array $userIds):array {
+               $qb = $this->db->getQueryBuilder();
+               $qb
+                       ->select('*')
+                       ->from($this->tableName)
+                       ->where($qb->expr()->in('user_id', $qb->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
+
+               return $this->findEntities($qb);
+       }
+
        /**
         * Clear all statuses older than a given timestamp
         *
index 83fcd0a8f0266f76974cd4db91e0f6ebd0b686c8..2cfdd5109a5b8330351da1f619f59eeee5ad09c3 100644 (file)
@@ -104,6 +104,16 @@ class StatusService {
                return $this->processStatus($this->mapper->findByUserId($userId));
        }
 
+       /**
+        * @param array $userIds
+        * @return UserStatus[]
+        */
+       public function findByUserIds(array $userIds):array {
+               return array_map(function ($status) {
+                       return $this->processStatus($status);
+               }, $this->mapper->findByUserIds($userIds));
+       }
+
        /**
         * @param string $userId
         * @param string $status
diff --git a/apps/user_status/tests/Unit/Connector/UserStatusProviderTest.php b/apps/user_status/tests/Unit/Connector/UserStatusProviderTest.php
new file mode 100644 (file)
index 0000000..bc485a5
--- /dev/null
@@ -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'));
+       }
+}
diff --git a/apps/user_status/tests/Unit/Connector/UserStatusTest.php b/apps/user_status/tests/Unit/Connector/UserStatusTest.php
new file mode 100644 (file)
index 0000000..35f79f9
--- /dev/null
@@ -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());
+       }
+}
index 16699d2ae2774e30353352f0238901b466529e5c..42c6f1cfe04309c5b790062039f46ac48f03b7ad 100644 (file)
@@ -96,6 +96,31 @@ class UserStatusMapperTest extends TestCase {
                $this->assertEquals(60000, $user2Status->getClearAt());
        }
 
+       public function testFindByUserIds(): void {
+               $this->insertSampleStatuses();
+
+               $statuses = $this->mapper->findByUserIds(['admin', 'user2']);
+               $this->assertCount(2, $statuses);
+
+               $adminStatus = $statuses[0];
+               $this->assertEquals('admin', $adminStatus->getUserId());
+               $this->assertEquals('offline', $adminStatus->getStatus());
+               $this->assertEquals(0, $adminStatus->getStatusTimestamp());
+               $this->assertEquals(false, $adminStatus->getIsUserDefined());
+               $this->assertEquals(null, $adminStatus->getCustomIcon());
+               $this->assertEquals(null, $adminStatus->getCustomMessage());
+               $this->assertEquals(null, $adminStatus->getClearAt());
+
+               $user2Status = $statuses[1];
+               $this->assertEquals('user2', $user2Status->getUserId());
+               $this->assertEquals('away', $user2Status->getStatus());
+               $this->assertEquals(5000, $user2Status->getStatusTimestamp());
+               $this->assertEquals(false, $user2Status->getIsUserDefined());
+               $this->assertEquals('🏝', $user2Status->getCustomIcon());
+               $this->assertEquals('On vacation', $user2Status->getCustomMessage());
+               $this->assertEquals(60000, $user2Status->getClearAt());
+       }
+
        public function testUserIdUnique(): void {
                // Test that inserting a second status for a user is throwing an exception
 
index 854ef66f23a22f0f1cd96486b42f5c940a82f61f..c82fbc271cb7a17bdfdac53892bafc21f078fb06 100644 (file)
@@ -495,6 +495,9 @@ return array(
     'OCP\\Template' => $baseDir . '/lib/public/Template.php',
     'OCP\\User' => $baseDir . '/lib/public/User.php',
     'OCP\\UserInterface' => $baseDir . '/lib/public/UserInterface.php',
+    'OCP\\UserStatus\\IManager' => $baseDir . '/lib/public/UserStatus/IManager.php',
+    'OCP\\UserStatus\\IProvider' => $baseDir . '/lib/public/UserStatus/IProvider.php',
+    'OCP\\UserStatus\\IUserStatus' => $baseDir . '/lib/public/UserStatus/IUserStatus.php',
     'OCP\\User\\Backend\\ABackend' => $baseDir . '/lib/public/User/Backend/ABackend.php',
     'OCP\\User\\Backend\\ICheckPasswordBackend' => $baseDir . '/lib/public/User/Backend/ICheckPasswordBackend.php',
     'OCP\\User\\Backend\\ICountUsersBackend' => $baseDir . '/lib/public/User/Backend/ICountUsersBackend.php',
@@ -1344,6 +1347,7 @@ return array(
     'OC\\Updater\\ChangesMapper' => $baseDir . '/lib/private/Updater/ChangesMapper.php',
     'OC\\Updater\\ChangesResult' => $baseDir . '/lib/private/Updater/ChangesResult.php',
     'OC\\Updater\\VersionCheck' => $baseDir . '/lib/private/Updater/VersionCheck.php',
+    'OC\\UserStatus\\Manager' => $baseDir . '/lib/private/UserStatus/Manager.php',
     'OC\\User\\Backend' => $baseDir . '/lib/private/User/Backend.php',
     'OC\\User\\Database' => $baseDir . '/lib/private/User/Database.php',
     'OC\\User\\LoginException' => $baseDir . '/lib/private/User/LoginException.php',
index 4a857abae358eb3b4418e36acaf21eb1e6bc18ca..545444e7cc63b433449fb0c6e3bc45b951a75556 100644 (file)
@@ -524,6 +524,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OCP\\Template' => __DIR__ . '/../../..' . '/lib/public/Template.php',
         'OCP\\User' => __DIR__ . '/../../..' . '/lib/public/User.php',
         'OCP\\UserInterface' => __DIR__ . '/../../..' . '/lib/public/UserInterface.php',
+        'OCP\\UserStatus\\IManager' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IManager.php',
+        'OCP\\UserStatus\\IProvider' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IProvider.php',
+        'OCP\\UserStatus\\IUserStatus' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IUserStatus.php',
         'OCP\\User\\Backend\\ABackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ABackend.php',
         'OCP\\User\\Backend\\ICheckPasswordBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ICheckPasswordBackend.php',
         'OCP\\User\\Backend\\ICountUsersBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ICountUsersBackend.php',
@@ -1373,6 +1376,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OC\\Updater\\ChangesMapper' => __DIR__ . '/../../..' . '/lib/private/Updater/ChangesMapper.php',
         'OC\\Updater\\ChangesResult' => __DIR__ . '/../../..' . '/lib/private/Updater/ChangesResult.php',
         'OC\\Updater\\VersionCheck' => __DIR__ . '/../../..' . '/lib/private/Updater/VersionCheck.php',
+        'OC\\UserStatus\\Manager' => __DIR__ . '/../../..' . '/lib/private/UserStatus/Manager.php',
         'OC\\User\\Backend' => __DIR__ . '/../../..' . '/lib/private/User/Backend.php',
         'OC\\User\\Database' => __DIR__ . '/../../..' . '/lib/private/User/Database.php',
         'OC\\User\\LoginException' => __DIR__ . '/../../..' . '/lib/private/User/LoginException.php',
index 09bb7336785a77dd1cd9d1be70f504ee5effe5bd..e054d8230d9a389a97524ff7e3f907068570cefb 100644 (file)
@@ -1370,6 +1370,8 @@ class Server extends ServerContainer implements IServerContainer {
 
                $this->registerAlias(IInitialStateService::class, InitialStateService::class);
 
+               $this->registerAlias(\OCP\UserStatus\IManager::class, \OC\UserStatus\Manager::class);
+
                $this->connectDispatcher();
        }
 
diff --git a/lib/private/UserStatus/Manager.php b/lib/private/UserStatus/Manager.php
new file mode 100644 (file)
index 0000000..c7726a8
--- /dev/null
@@ -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;
+       }
+}
diff --git a/lib/public/UserStatus/IManager.php b/lib/public/UserStatus/IManager.php
new file mode 100644 (file)
index 0000000..7ef1fdf
--- /dev/null
@@ -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;
+}
diff --git a/lib/public/UserStatus/IProvider.php b/lib/public/UserStatus/IProvider.php
new file mode 100644 (file)
index 0000000..1026950
--- /dev/null
@@ -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;
+}
diff --git a/lib/public/UserStatus/IUserStatus.php b/lib/public/UserStatus/IUserStatus.php
new file mode 100644 (file)
index 0000000..e892203
--- /dev/null
@@ -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;
+}