summaryrefslogtreecommitdiffstats
path: root/apps/user_status/lib
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2022-02-15 14:55:40 +0100
committerJoas Schilling <coding@schilljs.com>2022-02-15 16:16:18 +0100
commit2e328fc8981e4da721a2fcdd76e3b6ae63bc248e (patch)
treee770373f934b56cba87c2544a2809f76c205904a /apps/user_status/lib
parent7309e979071565da592819991c9d9deb3134a70d (diff)
downloadnextcloud-server-2e328fc8981e4da721a2fcdd76e3b6ae63bc248e.tar.gz
nextcloud-server-2e328fc8981e4da721a2fcdd76e3b6ae63bc248e.zip
Create the backup user status in 1 query instead of 3
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/user_status/lib')
-rw-r--r--apps/user_status/lib/Db/UserStatusMapper.php17
-rw-r--r--apps/user_status/lib/Service/StatusService.php27
2 files changed, 25 insertions, 19 deletions
diff --git a/apps/user_status/lib/Db/UserStatusMapper.php b/apps/user_status/lib/Db/UserStatusMapper.php
index 0f5dd4ef1cc..1ef327a21f4 100644
--- a/apps/user_status/lib/Db/UserStatusMapper.php
+++ b/apps/user_status/lib/Db/UserStatusMapper.php
@@ -184,4 +184,21 @@ class UserStatusMapper extends QBMapper {
->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;
+ }
}
diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php
index e31b1772c4d..dcabbe8e11f 100644
--- a/apps/user_status/lib/Service/StatusService.php
+++ b/apps/user_status/lib/Service/StatusService.php
@@ -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;
@@ -434,30 +435,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 {