summary refs log tree commit diff
diff options
context:
space:
mode:
authorOlivier Wilkinson (reivilibre) <oliverw@matrix.org>2021-12-08 15:11:31 +0000
committerOlivier Wilkinson (reivilibre) <oliverw@matrix.org>2021-12-08 15:11:31 +0000
commit86ef692d5a36ed58a0f82a72b81f85853074ce90 (patch)
tree7f1676843bcd5d34815490a8d6fab471d0fa2c4d
parentRemove early return because we need more logic here (diff)
downloadsynapse-86ef692d5a36ed58a0f82a72b81f85853074ce90.tar.xz
Add get_device_opt which returns None instead of raising if it doesn't exist
-rw-r--r--synapse/storage/databases/main/devices.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/synapse/storage/databases/main/devices.py b/synapse/storage/databases/main/devices.py
index 838a2a6a3d..afc516a978 100644
--- a/synapse/storage/databases/main/devices.py
+++ b/synapse/storage/databases/main/devices.py
@@ -112,6 +112,8 @@ class DeviceWorkerStore(SQLBaseStore):
             A dict containing the device information
         Raises:
             StoreError: if the device is not found
+        See also:
+            `get_device_opt` which returns None instead if the device is not found
         """
         return await self.db_pool.simple_select_one(
             table="devices",
@@ -120,6 +122,26 @@ class DeviceWorkerStore(SQLBaseStore):
             desc="get_device",
         )
 
+    async def get_device_opt(
+        self, user_id: str, device_id: str
+    ) -> Optional[Dict[str, Any]]:
+        """Retrieve a device. Only returns devices that are not marked as
+        hidden.
+
+        Args:
+            user_id: The ID of the user which owns the device
+            device_id: The ID of the device to retrieve
+        Returns:
+            A dict containing the device information, or None if the device does not exist.
+        """
+        return await self.db_pool.simple_select_one(
+            table="devices",
+            keyvalues={"user_id": user_id, "device_id": device_id, "hidden": False},
+            retcols=("user_id", "device_id", "display_name"),
+            desc="get_device",
+            allow_none=True,
+        )
+
     async def get_devices_by_user(self, user_id: str) -> Dict[str, Dict[str, str]]:
         """Retrieve all of a user's registered devices. Only returns devices
         that are not marked as hidden.