Skip to content

Commit

Permalink
Merge pull request #40564 from nextcloud/fix/userstatus/message-times…
Browse files Browse the repository at this point in the history
…tamp

fix(userstatus): Track message timestamp too
  • Loading branch information
ChristophWurst authored Sep 29, 2023
2 parents 3c0c1cb + fbdf733 commit 94430ed
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/user_status/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>User status</name>
<summary>User status</summary>
<description><![CDATA[User status]]></description>
<version>1.8.0</version>
<version>1.8.1</version>
<licence>agpl</licence>
<author mail="[email protected]" >Georg Ehrke</author>
<namespace>UserStatus</namespace>
Expand Down
1 change: 1 addition & 0 deletions apps/user_status/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'OCA\\UserStatus\\Migration\\Version0002Date20200902144824' => $baseDir . '/../lib/Migration/Version0002Date20200902144824.php',
'OCA\\UserStatus\\Migration\\Version1000Date20201111130204' => $baseDir . '/../lib/Migration/Version1000Date20201111130204.php',
'OCA\\UserStatus\\Migration\\Version1003Date20210809144824' => $baseDir . '/../lib/Migration/Version1003Date20210809144824.php',
'OCA\\UserStatus\\Migration\\Version1008Date20230921144701' => $baseDir . '/../lib/Migration/Version1008Date20230921144701.php',
'OCA\\UserStatus\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
'OCA\\UserStatus\\Service\\JSDataService' => $baseDir . '/../lib/Service/JSDataService.php',
'OCA\\UserStatus\\Service\\PredefinedStatusService' => $baseDir . '/../lib/Service/PredefinedStatusService.php',
Expand Down
1 change: 1 addition & 0 deletions apps/user_status/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ComposerStaticInitUserStatus
'OCA\\UserStatus\\Migration\\Version0002Date20200902144824' => __DIR__ . '/..' . '/../lib/Migration/Version0002Date20200902144824.php',
'OCA\\UserStatus\\Migration\\Version1000Date20201111130204' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20201111130204.php',
'OCA\\UserStatus\\Migration\\Version1003Date20210809144824' => __DIR__ . '/..' . '/../lib/Migration/Version1003Date20210809144824.php',
'OCA\\UserStatus\\Migration\\Version1008Date20230921144701' => __DIR__ . '/..' . '/../lib/Migration/Version1008Date20230921144701.php',
'OCA\\UserStatus\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php',
'OCA\\UserStatus\\Service\\JSDataService' => __DIR__ . '/..' . '/../lib/Service/JSDataService.php',
'OCA\\UserStatus\\Service\\PredefinedStatusService' => __DIR__ . '/..' . '/../lib/Service/PredefinedStatusService.php',
Expand Down
2 changes: 1 addition & 1 deletion apps/user_status/lib/Dashboard/UserStatusWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static function (UserStatus $status) use ($userId, $since): bool {
: $status->getStatus(),
'icon' => $status->getCustomIcon(),
'message' => $status->getCustomMessage(),
'timestamp' => $status->getStatusTimestamp(),
'timestamp' => $status->getStatusMessageTimestamp(),
];
}, $recentStatusUpdates);
}
Expand Down
6 changes: 6 additions & 0 deletions apps/user_status/lib/Db/UserStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
* @method void setClearAt(int|null $clearAt)
* @method setIsBackup(bool $true): void
* @method getIsBackup(): bool
* @method int getStatusMessageTimestamp()
* @method void setStatusMessageTimestamp(int $statusTimestamp)
*/
class UserStatus extends Entity {

Expand Down Expand Up @@ -82,6 +84,9 @@ class UserStatus extends Entity {
/** @var bool $isBackup */
public $isBackup;

/** @var int */
protected $statusMessageTimestamp = 0;

public function __construct() {
$this->addType('userId', 'string');
$this->addType('status', 'string');
Expand All @@ -92,5 +97,6 @@ public function __construct() {
$this->addType('customMessage', 'string');
$this->addType('clearAt', 'int');
$this->addType('isBackup', 'boolean');
$this->addType('statusMessageTimestamp', 'int');
}
}
3 changes: 2 additions & 1 deletion apps/user_status/lib/Db/UserStatusMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public function findAllRecent(?int $limit = null, ?int $offset = null): array {
$qb
->select('*')
->from($this->tableName)
->orderBy('status_timestamp', 'DESC')
->orderBy('status_message_timestamp', 'DESC')
->where($qb->expr()->andX(
$qb->expr()->neq('status_message_timestamp', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
$qb->expr()->orX(
$qb->expr()->notIn('status', $qb->createNamedParameter([IUserStatus::ONLINE, IUserStatus::AWAY, IUserStatus::OFFLINE], IQueryBuilder::PARAM_STR_ARRAY)),
$qb->expr()->isNotNull('message_id'),
Expand Down
67 changes: 67 additions & 0 deletions apps/user_status/lib/Migration/Version1008Date20230921144701.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/**
* @author Christoph Wurst <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\UserStatus\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version1008Date20230921144701 extends SimpleMigrationStep {

public function __construct(private IDBConnection $connection) {
}

public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$statusTable = $schema->getTable('user_status');
if (!($statusTable->hasColumn('status_message_timestamp'))) {
$statusTable->addColumn('status_message_timestamp', Types::INTEGER, [
'notnull' => true,
'length' => 11,
'unsigned' => true,
'default' => 0,
]);
}
if (!$statusTable->hasIndex('user_status_mtstmp_ix')) {
$statusTable->addIndex(['status_message_timestamp'], 'user_status_mtstmp_ix');
}

return $schema;
}

public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
$qb = $this->connection->getQueryBuilder();

$update = $qb->update('user_status')
->set('status_message_timestamp', 'status_timestamp');

$update->executeStatement();
}
}
6 changes: 6 additions & 0 deletions apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public function setPredefinedMessage(string $userId,
$userStatus->setCustomIcon(null);
$userStatus->setCustomMessage(null);
$userStatus->setClearAt($clearAt);
$userStatus->setStatusMessageTimestamp($this->timeFactory->now()->getTimestamp());

if ($userStatus->getId() === null) {
return $this->mapper->insert($userStatus);
Expand Down Expand Up @@ -291,6 +292,7 @@ public function setUserStatus(string $userId,
$userStatus->setCustomIcon(null);
$userStatus->setCustomMessage(null);
$userStatus->setClearAt(null);
$userStatus->setStatusMessageTimestamp($this->timeFactory->now()->getTimestamp());

if ($userStatus->getId() !== null) {
$this->mapper->update($userStatus);
Expand Down Expand Up @@ -340,6 +342,7 @@ public function setCustomMessage(string $userId,
$userStatus->setCustomIcon($statusIcon);
$userStatus->setCustomMessage($message);
$userStatus->setClearAt($clearAt);
$userStatus->setStatusMessageTimestamp($this->timeFactory->now()->getTimestamp());

if ($userStatus->getId() === null) {
return $this->mapper->insert($userStatus);
Expand Down Expand Up @@ -384,6 +387,7 @@ public function clearMessage(string $userId): bool {
$userStatus->setCustomMessage(null);
$userStatus->setCustomIcon(null);
$userStatus->setClearAt(null);
$userStatus->setStatusMessageTimestamp(0);

$this->mapper->update($userStatus);
return true;
Expand Down Expand Up @@ -465,6 +469,7 @@ private function cleanStatusMessage(UserStatus $status): void {
$status->setCustomIcon(null);
$status->setCustomMessage(null);
$status->setClearAt(null);
$status->setStatusMessageTimestamp(0);

$this->mapper->update($status);
}
Expand All @@ -478,6 +483,7 @@ private function addDefaultMessage(UserStatus $status): void {
if ($predefinedMessage !== null) {
$status->setCustomMessage($predefinedMessage['message']);
$status->setCustomIcon($predefinedMessage['icon']);
$status->setStatusMessageTimestamp($this->timeFactory->now()->getTimestamp());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

/*
* @copyright 2023 Christoph Wurst <[email protected]>
*
* @author 2023 Christoph Wurst <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\UserStatus\Tests\Integration\BackgroundJob;

use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IDBConnection;
use OCP\Server;
use OCP\UserStatus\IUserStatus;
use Test\TestCase;
use function sleep;
use function time;

/**
* @group DB
*/
class StatusServiceIntegrationTest extends TestCase {

private StatusService $service;

protected function setUp(): void {
parent::setUp();

$this->service = Server::get(StatusService::class);

$db = Server::get(IDBConnection::class);
$qb = $db->getQueryBuilder();
$qb->delete('user_status')->executeStatement();
}

public function testNoStatusYet(): void {
$this->expectException(DoesNotExistException::class);

$this->service->findByUserId('test123');
}

public function testCustomStatusMessageTimestamp(): void {
$this->service->setCustomMessage(
'test123',
'🍕',
'Lunch',
null,
);

$status = $this->service->findByUserId('test123');

self::assertSame('Lunch', $status->getCustomMessage());
self::assertGreaterThanOrEqual(time(), $status->getStatusMessageTimestamp());
}

public function testOnlineStatusKeepsMessageTimestamp(): void {
$this->service->setStatus(
'test123',
IUserStatus::OFFLINE,
time() + 1000,
false,
);
$this->service->setCustomMessage(
'test123',
'🍕',
'Lunch',
null,
);
$timeAfterInsert = time();
sleep(1);
$this->service->setStatus(
'test123',
IUserStatus::ONLINE,
time() + 2000,
false,
);
$status = $this->service->findByUserId('test123');

self::assertSame('Lunch', $status->getCustomMessage());
self::assertLessThanOrEqual($timeAfterInsert, $status->getStatusMessageTimestamp());
}

public function testCreateRestoreBackupAutomatically(): void {
$this->service->setStatus(
'test123',
IUserStatus::ONLINE,
null,
false,
);
$this->service->setUserStatus(
'test123',
IUserStatus::DND,
'meeting',
true,
);
self::assertSame(
'meeting',
$this->service->findByUserId('test123')->getMessageId(),
);

$this->service->revertUserStatus(
'test123',
'meeting',
);
self::assertSame(
IUserStatus::ONLINE,
$this->service->findByUserId('test123')->getStatus(),
);
}

public function testCi(): void {
// TODO: remove if CI turns red
self::assertTrue(false);
}

}
2 changes: 2 additions & 0 deletions apps/user_status/tests/Unit/Db/UserStatusMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ private function insertSampleStatuses(): void {
$userStatus2->setUserId('user1');
$userStatus2->setStatus('dnd');
$userStatus2->setStatusTimestamp(5000);
$userStatus2->setStatusMessageTimestamp(5000);
$userStatus2->setIsUserDefined(true);
$userStatus2->setCustomIcon('💩');
$userStatus2->setCustomMessage('Do not disturb');
Expand All @@ -237,6 +238,7 @@ private function insertSampleStatuses(): void {
$userStatus3->setUserId('user2');
$userStatus3->setStatus('away');
$userStatus3->setStatusTimestamp(6000);
$userStatus3->setStatusMessageTimestamp(6000);
$userStatus3->setIsUserDefined(false);
$userStatus3->setCustomIcon('🏝');
$userStatus3->setCustomMessage('On vacation');
Expand Down

0 comments on commit 94430ed

Please sign in to comment.