]> source.dussan.org Git - nextcloud-server.git/commitdiff
Delete the user status without loading it first
authorJoas Schilling <coding@schilljs.com>
Thu, 10 Feb 2022 15:51:30 +0000 (16:51 +0100)
committerJoas Schilling <coding@schilljs.com>
Tue, 15 Feb 2022 15:13:42 +0000 (16:13 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
apps/user_status/lib/Db/UserStatusMapper.php
apps/user_status/lib/Service/StatusService.php

index 4670c28f7f7bcd72216a08d752d93d1cc8791724..02f9a607bd86939fe4e3fe1c531f59b66c0e519a 100644 (file)
@@ -166,4 +166,23 @@ 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;
+       }
 }
index 1f72fd8a21ba26a1d04921a0ee59614fe81a91cb..e31b1772c4d3918b64336c6d85fa12fd83d67325 100644 (file)
@@ -468,16 +468,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->mapper->delete($userStatus);
-               } 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));