mirror of
https://github.com/nextcloud/server.git
synced 2026-02-20 00:12:30 -05:00
Merge pull request #31192 from nextcloud/backport/31106/stable23
[stable23] Improve user status revert performance
This commit is contained in:
commit
28c526b29e
5 changed files with 195 additions and 78 deletions
|
|
@ -57,14 +57,8 @@ class UserStatusProvider implements IProvider, ISettableProvider {
|
|||
return $userStatuses;
|
||||
}
|
||||
|
||||
public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false): void {
|
||||
if ($createBackup) {
|
||||
if ($this->service->backupCurrentStatus($userId) === false) {
|
||||
return; // Already a status set automatically => abort.
|
||||
}
|
||||
}
|
||||
$this->service->setStatus($userId, $status, null, true);
|
||||
$this->service->setPredefinedMessage($userId, $messageId, null);
|
||||
public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup): void {
|
||||
$this->service->setUserStatus($userId, $status, $messageId, $createBackup);
|
||||
}
|
||||
|
||||
public function revertUserStatus(string $userId, string $messageId, string $status): void {
|
||||
|
|
|
|||
|
|
@ -109,8 +109,7 @@ class UserStatusMapper extends QBMapper {
|
|||
$qb
|
||||
->select('*')
|
||||
->from($this->tableName)
|
||||
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($isBackup ? '_' . $userId : $userId, IQueryBuilder::PARAM_STR)))
|
||||
->andWhere($qb->expr()->eq('is_backup', $qb->createNamedParameter($isBackup, IQueryBuilder::PARAM_BOOL)));
|
||||
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($isBackup ? '_' . $userId : $userId, IQueryBuilder::PARAM_STR)));
|
||||
|
||||
return $this->findEntity($qb);
|
||||
}
|
||||
|
|
@ -166,4 +165,40 @@ class UserStatusMapper extends QBMapper {
|
|||
|
||||
$qb->execute();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a user status so we can restore the backup
|
||||
*
|
||||
* @param string $userId
|
||||
* @param string $messageId
|
||||
* @param string $status
|
||||
* @return bool True if an entry was deleted
|
||||
*/
|
||||
public function deleteCurrentStatusToRestoreBackup(string $userId, string $messageId, string $status): bool {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->delete($this->tableName)
|
||||
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
|
||||
->andWhere($qb->expr()->eq('message_id', $qb->createNamedParameter($messageId)))
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter($status)))
|
||||
->andWhere($qb->expr()->eq('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)));
|
||||
return $qb->executeStatement() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userId
|
||||
* @return bool
|
||||
* @throws \OCP\DB\Exception
|
||||
*/
|
||||
public function createBackupStatus(string $userId): bool {
|
||||
// Prefix user account with an underscore because user_id is marked as unique
|
||||
// in the table. Starting a username with an underscore is not allowed so this
|
||||
// shouldn't create any trouble.
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->update($this->tableName)
|
||||
->set('is_backup', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
|
||||
->set('user_id', $qb->createNamedParameter('_' . $userId))
|
||||
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
|
||||
return $qb->executeStatement() > 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ use OCA\UserStatus\Exception\InvalidStatusTypeException;
|
|||
use OCA\UserStatus\Exception\StatusMessageTooLongException;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
use OCP\DB\Exception;
|
||||
use OCP\IConfig;
|
||||
use OCP\UserStatus\IUserStatus;
|
||||
|
||||
|
|
@ -229,6 +230,7 @@ class StatusService {
|
|||
$userStatus->setStatus(IUserStatus::OFFLINE);
|
||||
$userStatus->setStatusTimestamp(0);
|
||||
$userStatus->setIsUserDefined(false);
|
||||
$userStatus->setIsBackup(false);
|
||||
}
|
||||
|
||||
if (!$this->predefinedStatusService->isValidId($messageId)) {
|
||||
|
|
@ -252,6 +254,60 @@ class StatusService {
|
|||
return $this->mapper->update($userStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userId
|
||||
* @param string $status
|
||||
* @param string $messageId
|
||||
* @param bool $createBackup
|
||||
* @throws InvalidStatusTypeException
|
||||
* @throws InvalidMessageIdException
|
||||
*/
|
||||
public function setUserStatus(string $userId,
|
||||
string $status,
|
||||
string $messageId,
|
||||
bool $createBackup): void {
|
||||
// Check if status-type is valid
|
||||
if (!\in_array($status, self::PRIORITY_ORDERED_STATUSES, true)) {
|
||||
throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported');
|
||||
}
|
||||
|
||||
if (!$this->predefinedStatusService->isValidId($messageId)) {
|
||||
throw new InvalidMessageIdException('Message-Id "' . $messageId . '" is not supported');
|
||||
}
|
||||
|
||||
if ($createBackup) {
|
||||
if ($this->backupCurrentStatus($userId) === false) {
|
||||
return; // Already a status set automatically => abort.
|
||||
}
|
||||
|
||||
// If we just created the backup
|
||||
$userStatus = new UserStatus();
|
||||
$userStatus->setUserId($userId);
|
||||
} else {
|
||||
try {
|
||||
$userStatus = $this->mapper->findByUserId($userId);
|
||||
} catch (DoesNotExistException $ex) {
|
||||
$userStatus = new UserStatus();
|
||||
$userStatus->setUserId($userId);
|
||||
}
|
||||
}
|
||||
|
||||
$userStatus->setStatus($status);
|
||||
$userStatus->setStatusTimestamp($this->timeFactory->getTime());
|
||||
$userStatus->setIsUserDefined(false);
|
||||
$userStatus->setIsBackup(false);
|
||||
$userStatus->setMessageId($messageId);
|
||||
$userStatus->setCustomIcon(null);
|
||||
$userStatus->setCustomMessage(null);
|
||||
$userStatus->setClearAt(null);
|
||||
|
||||
if ($userStatus->getId() !== null) {
|
||||
$this->mapper->update($userStatus);
|
||||
return;
|
||||
}
|
||||
$this->mapper->insert($userStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userId
|
||||
* @param string|null $statusIcon
|
||||
|
|
@ -434,30 +490,18 @@ class StatusService {
|
|||
}
|
||||
|
||||
/**
|
||||
* @return bool false iff there is already a backup. In this case abort the procedure.
|
||||
* @return bool false if there is already a backup. In this case abort the procedure.
|
||||
*/
|
||||
public function backupCurrentStatus(string $userId): bool {
|
||||
try {
|
||||
$this->mapper->findByUserId($userId, true);
|
||||
return false;
|
||||
} catch (DoesNotExistException $ex) {
|
||||
// No backup already existing => Good
|
||||
}
|
||||
|
||||
try {
|
||||
$userStatus = $this->mapper->findByUserId($userId);
|
||||
} catch (DoesNotExistException $ex) {
|
||||
// if there is no status to backup, just return
|
||||
$this->mapper->createBackupStatus($userId);
|
||||
return true;
|
||||
} catch (Exception $ex) {
|
||||
if ($ex->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||
return false;
|
||||
}
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
$userStatus->setIsBackup(true);
|
||||
// Prefix user account with an underscore because user_id is marked as unique
|
||||
// in the table. Starting an username with an underscore is not allowed so this
|
||||
// shouldn't create any trouble.
|
||||
$userStatus->setUserId('_' . $userStatus->getUserId());
|
||||
$this->mapper->update($userStatus);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function revertUserStatus(string $userId, ?string $messageId, string $status): void {
|
||||
|
|
@ -468,16 +512,13 @@ class StatusService {
|
|||
// No user status to revert, do nothing
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$userStatus = $this->mapper->findByUserId($userId);
|
||||
if ($userStatus->getMessageId() !== $messageId || $userStatus->getStatus() !== $status) {
|
||||
// Another status is set automatically, do nothing
|
||||
return;
|
||||
}
|
||||
$this->removeUserStatus($userId);
|
||||
} catch (DoesNotExistException $ex) {
|
||||
// No current status => nothing to delete
|
||||
|
||||
$deleted = $this->mapper->deleteCurrentStatusToRestoreBackup($userId, $messageId ?? '', $status);
|
||||
if (!$deleted) {
|
||||
// Another status is set automatically or no status, do nothing
|
||||
return;
|
||||
}
|
||||
|
||||
$backupUserStatus->setIsBackup(false);
|
||||
// Remove the underscore prefix added when creating the backup
|
||||
$backupUserStatus->setUserId(substr($backupUserStatus->getUserId(), 1));
|
||||
|
|
|
|||
|
|
@ -251,4 +251,65 @@ class UserStatusMapperTest extends TestCase {
|
|||
$this->mapper->insert($userStatus2);
|
||||
$this->mapper->insert($userStatus3);
|
||||
}
|
||||
|
||||
public function dataCreateBackupStatus(): array {
|
||||
return [
|
||||
[false, false, false],
|
||||
[true, false, true],
|
||||
[false, true, false],
|
||||
[true, true, false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataCreateBackupStatus
|
||||
* @param bool $hasStatus
|
||||
* @param bool $hasBackup
|
||||
* @param bool $backupCreated
|
||||
*/
|
||||
public function testCreateBackupStatus(bool $hasStatus, bool $hasBackup, bool $backupCreated): void {
|
||||
if ($hasStatus) {
|
||||
$userStatus1 = new UserStatus();
|
||||
$userStatus1->setUserId('user1');
|
||||
$userStatus1->setStatus('online');
|
||||
$userStatus1->setStatusTimestamp(5000);
|
||||
$userStatus1->setIsUserDefined(true);
|
||||
$userStatus1->setIsBackup(false);
|
||||
$userStatus1->setCustomIcon('🚀');
|
||||
$userStatus1->setCustomMessage('Current');
|
||||
$userStatus1->setClearAt(50000);
|
||||
$this->mapper->insert($userStatus1);
|
||||
}
|
||||
|
||||
if ($hasBackup) {
|
||||
$userStatus1 = new UserStatus();
|
||||
$userStatus1->setUserId('_user1');
|
||||
$userStatus1->setStatus('online');
|
||||
$userStatus1->setStatusTimestamp(5000);
|
||||
$userStatus1->setIsUserDefined(true);
|
||||
$userStatus1->setIsBackup(true);
|
||||
$userStatus1->setCustomIcon('🚀');
|
||||
$userStatus1->setCustomMessage('Backup');
|
||||
$userStatus1->setClearAt(50000);
|
||||
$this->mapper->insert($userStatus1);
|
||||
}
|
||||
|
||||
if ($hasStatus && $hasBackup) {
|
||||
$this->expectException(Exception::class);
|
||||
}
|
||||
|
||||
self::assertSame($backupCreated, $this->mapper->createBackupStatus('user1'));
|
||||
|
||||
if ($backupCreated) {
|
||||
$user1Status = $this->mapper->findByUserId('user1', true);
|
||||
$this->assertEquals('_user1', $user1Status->getUserId());
|
||||
$this->assertEquals(true, $user1Status->getIsBackup());
|
||||
$this->assertEquals('Current', $user1Status->getCustomMessage());
|
||||
} else if ($hasBackup) {
|
||||
$user1Status = $this->mapper->findByUserId('user1', true);
|
||||
$this->assertEquals('_user1', $user1Status->getUserId());
|
||||
$this->assertEquals(true, $user1Status->getIsBackup());
|
||||
$this->assertEquals('Backup', $user1Status->getCustomMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ declare(strict_types=1);
|
|||
*/
|
||||
namespace OCA\UserStatus\Tests\Service;
|
||||
|
||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||
use OC\DB\Exceptions\DbalException;
|
||||
use OCA\UserStatus\Db\UserStatus;
|
||||
use OCA\UserStatus\Db\UserStatusMapper;
|
||||
use OCA\UserStatus\Exception\InvalidClearAtException;
|
||||
|
|
@ -38,6 +40,7 @@ use OCA\UserStatus\Service\PredefinedStatusService;
|
|||
use OCA\UserStatus\Service\StatusService;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
use OCP\DB\Exception;
|
||||
use OCP\IConfig;
|
||||
use OCP\UserStatus\IUserStatus;
|
||||
use Test\TestCase;
|
||||
|
|
@ -723,52 +726,35 @@ class StatusServiceTest extends TestCase {
|
|||
parent::invokePrivate($this->service, 'cleanStatus', [$status]);
|
||||
}
|
||||
|
||||
public function testBackupWorkingHasBackupAlready() {
|
||||
$status = new UserStatus();
|
||||
$status->setStatus(IUserStatus::ONLINE);
|
||||
$status->setStatusTimestamp(1337);
|
||||
$status->setIsUserDefined(true);
|
||||
$status->setMessageId('meeting');
|
||||
$status->setUserId('john');
|
||||
$status->setIsBackup(true);
|
||||
|
||||
public function testBackupWorkingHasBackupAlready(): void {
|
||||
$p = $this->createMock(UniqueConstraintViolationException::class);
|
||||
$e = DbalException::wrap($p);
|
||||
$this->mapper->expects($this->once())
|
||||
->method('findByUserId')
|
||||
->with('john', true)
|
||||
->willReturn($status);
|
||||
->method('createBackupStatus')
|
||||
->with('john')
|
||||
->willThrowException($e);
|
||||
|
||||
$this->assertFalse($this->service->backupCurrentStatus('john'));
|
||||
}
|
||||
|
||||
public function testBackupThrowsOther(): void {
|
||||
$e = new Exception('', Exception::REASON_CONNECTION_LOST);
|
||||
$this->mapper->expects($this->once())
|
||||
->method('createBackupStatus')
|
||||
->with('john')
|
||||
->willThrowException($e);
|
||||
|
||||
$this->expectException(Exception::class);
|
||||
$this->service->backupCurrentStatus('john');
|
||||
}
|
||||
|
||||
public function testBackup() {
|
||||
$currentStatus = new UserStatus();
|
||||
$currentStatus->setStatus(IUserStatus::ONLINE);
|
||||
$currentStatus->setStatusTimestamp(1337);
|
||||
$currentStatus->setIsUserDefined(true);
|
||||
$currentStatus->setMessageId('meeting');
|
||||
$currentStatus->setUserId('john');
|
||||
|
||||
$this->mapper->expects($this->at(0))
|
||||
->method('findByUserId')
|
||||
->with('john', true)
|
||||
->willThrowException(new DoesNotExistException(''));
|
||||
$this->mapper->expects($this->at(1))
|
||||
->method('findByUserId')
|
||||
->with('john', false)
|
||||
->willReturn($currentStatus);
|
||||
|
||||
$newBackupStatus = new UserStatus();
|
||||
$newBackupStatus->setStatus(IUserStatus::ONLINE);
|
||||
$newBackupStatus->setStatusTimestamp(1337);
|
||||
$newBackupStatus->setIsUserDefined(true);
|
||||
$newBackupStatus->setMessageId('meeting');
|
||||
$newBackupStatus->setUserId('_john');
|
||||
$newBackupStatus->setIsBackup(true);
|
||||
|
||||
public function testBackup(): void {
|
||||
$e = new Exception('', Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION);
|
||||
$this->mapper->expects($this->once())
|
||||
->method('update')
|
||||
->with($newBackupStatus);
|
||||
->method('createBackupStatus')
|
||||
->with('john')
|
||||
->willReturn(true);
|
||||
|
||||
$this->service->backupCurrentStatus('john');
|
||||
$this->assertTrue($this->service->backupCurrentStatus('john'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue