diff options
author | Joas Schilling <coding@schilljs.com> | 2022-02-10 17:28:05 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-02-15 16:06:33 +0100 |
commit | deec4f31dbe02008207f9b8c21f0302af919c652 (patch) | |
tree | feb86c775616aafb690bfd2d2b77d0d299f33c54 /apps/user_status/lib | |
parent | 194338cca3823ec4e1f1f473e278bdb26fb229fa (diff) | |
download | nextcloud-server-deec4f31dbe02008207f9b8c21f0302af919c652.tar.gz nextcloud-server-deec4f31dbe02008207f9b8c21f0302af919c652.zip |
Allow to revert the user status of multiple users in 3 queries instead of 3*n
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/user_status/lib')
-rw-r--r-- | apps/user_status/lib/Connector/UserStatusProvider.php | 4 | ||||
-rw-r--r-- | apps/user_status/lib/Db/UserStatusMapper.php | 19 | ||||
-rw-r--r-- | apps/user_status/lib/Service/StatusService.php | 38 |
3 files changed, 58 insertions, 3 deletions
diff --git a/apps/user_status/lib/Connector/UserStatusProvider.php b/apps/user_status/lib/Connector/UserStatusProvider.php index f3f4c5f710e..cec9406d28b 100644 --- a/apps/user_status/lib/Connector/UserStatusProvider.php +++ b/apps/user_status/lib/Connector/UserStatusProvider.php @@ -70,4 +70,8 @@ class UserStatusProvider implements IProvider, ISettableProvider { public function revertUserStatus(string $userId, string $messageId, string $status): void { $this->service->revertUserStatus($userId, $messageId, $status); } + + public function revertMultipleUserStatus(array $userIds, string $messageId, string $status): void { + $this->service->revertMultipleUserStatus($userIds, $messageId, $status); + } } diff --git a/apps/user_status/lib/Db/UserStatusMapper.php b/apps/user_status/lib/Db/UserStatusMapper.php index 1bb4ad11be8..496cb5080f0 100644 --- a/apps/user_status/lib/Db/UserStatusMapper.php +++ b/apps/user_status/lib/Db/UserStatusMapper.php @@ -111,7 +111,7 @@ class UserStatusMapper extends QBMapper { * @param array $userIds * @return array */ - public function findByUserIds(array $userIds):array { + public function findByUserIds(array $userIds): array { $qb = $this->db->getQueryBuilder(); $qb ->select('*') @@ -177,4 +177,21 @@ class UserStatusMapper extends QBMapper { ->andWhere($qb->expr()->eq('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))); return $qb->executeStatement() > 0; } + + public function deleteByIds(array $ids): void { + $qb = $this->db->getQueryBuilder(); + $qb->delete($this->tableName) + ->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))); + $qb->executeStatement(); + } + + public function restoreBackupStatuses(array $ids): void { + $qb = $this->db->getQueryBuilder(); + $qb->update($this->tableName) + ->set('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) + ->set('user_id', $qb->func()->substring('user_id', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT))) + ->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))); + + $qb->executeStatement(); + } } diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php index e31b1772c4d..0c281314546 100644 --- a/apps/user_status/lib/Service/StatusService.php +++ b/apps/user_status/lib/Service/StatusService.php @@ -36,6 +36,7 @@ use OCA\UserStatus\Exception\StatusMessageTooLongException; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; +use OCP\IUser; use OCP\UserStatus\IUserStatus; /** @@ -460,7 +461,7 @@ class StatusService { return true; } - public function revertUserStatus(string $userId, ?string $messageId, string $status): void { + public function revertUserStatus(string $userId, string $messageId, string $status): void { try { /** @var UserStatus $userStatus */ $backupUserStatus = $this->mapper->findByUserId($userId, true); @@ -469,7 +470,7 @@ class StatusService { return; } - $deleted = $this->mapper->deleteCurrentStatusToRestoreBackup($userId, $messageId ?? '', $status); + $deleted = $this->mapper->deleteCurrentStatusToRestoreBackup($userId, $messageId, $status); if (!$deleted) { // Another status is set automatically or no status, do nothing return; @@ -480,4 +481,37 @@ class StatusService { $backupUserStatus->setUserId(substr($backupUserStatus->getUserId(), 1)); $this->mapper->update($backupUserStatus); } + + public function revertMultipleUserStatus(array $userIds, string $messageId, string $status): void { + // Get all user statuses and the backups + $findById = $userIds; + foreach ($userIds as $userId) { + $findById[] = '_' . $userId; + } + $userStatuses = $this->mapper->findByUserIds($findById); + + $backups = $restoreIds = $statuesToDelete = []; + foreach ($userStatuses as $userStatus) { + if (!$userStatus->getIsBackup() + && $userStatus->getMessageId() === $messageId + && $userStatus->getStatus() === $status) { + $statuesToDelete[$userStatus->getUserId()] = $userStatus->getId(); + } else if ($userStatus->getIsBackup()) { + $backups[$userStatus->getUserId()] = $userStatus->getId(); + } + } + + // For users with both (normal and backup) delete the status when matching + foreach ($statuesToDelete as $userId => $statusId) { + $backupUserId = '_' . $userId; + if (isset($backups[$backupUserId])) { + $restoreIds[] = $backups[$backupUserId]; + } + } + + $this->mapper->deleteByIds(array_values($statuesToDelete)); + + // For users that matched restore the previous status + $this->mapper->restoreBackupStatuses($restoreIds); + } } |