Skip to content

Commit

Permalink
Provide a PHP Api for UserStatus
Browse files Browse the repository at this point in the history
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
  • Loading branch information
georgehrke committed Aug 5, 2020
1 parent 0581356 commit 0e0e0d1
Show file tree
Hide file tree
Showing 17 changed files with 687 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/user_status/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
'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',
Expand Down
2 changes: 2 additions & 0 deletions apps/user_status/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions apps/user_status/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -36,6 +37,7 @@
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\User\Events\UserDeletedEvent;
use OCP\User\Events\UserLiveStatusEvent;
use OCP\UserStatus\IManager;

/**
* Class Application
Expand Down Expand Up @@ -70,5 +72,8 @@ public function register(IRegistrationContext $context): void {
}

public function boot(IBootContext $context): void {
/** @var IManager $userStatusManager */
$userStatusManager = $context->getServerContainer()->get(IManager::class);
$userStatusManager->registerProvider(UserStatusProvider::class);
}
}
101 changes: 101 additions & 0 deletions apps/user_status/lib/Connector/UserStatus.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
57 changes: 57 additions & 0 deletions apps/user_status/lib/Connector/UserStatusProvider.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 14 additions & 0 deletions apps/user_status/lib/Db/UserStatusMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ public function findByUserId(string $userId):UserStatus {
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
*
Expand Down
10 changes: 10 additions & 0 deletions apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public function findByUserId(string $userId):UserStatus {
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
Expand Down
93 changes: 93 additions & 0 deletions apps/user_status/tests/Unit/Connector/UserStatusProviderTest.php
Original file line number Diff line number Diff line change
@@ -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'));
}
}
Loading

0 comments on commit 0e0e0d1

Please sign in to comment.