summary refs log tree commit diff
path: root/synapse/handlers
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2016-07-20 17:58:44 +0100
committerRichard van der Hoff <richard@matrix.org>2016-07-21 12:00:29 +0100
commit406f7aa0f6ca7433e52433485824e80b79930498 (patch)
treeaee4eb5c403d7f22b077e43c7c5cb5bc86453937 /synapse/handlers
parentMerge branch 'rav/get_devices_api' into develop (diff)
downloadsynapse-406f7aa0f6ca7433e52433485824e80b79930498.tar.xz
Implement GET /device/{deviceId}
Diffstat (limited to 'synapse/handlers')
-rw-r--r--synapse/handlers/device.py46
1 files changed, 37 insertions, 9 deletions
diff --git a/synapse/handlers/device.py b/synapse/handlers/device.py
index 6bbbf59e52..3c88be0679 100644
--- a/synapse/handlers/device.py
+++ b/synapse/handlers/device.py
@@ -12,7 +12,8 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-from synapse.api.errors import StoreError
+
+from synapse.api import errors
 from synapse.util import stringutils
 from twisted.internet import defer
 from ._base import BaseHandler
@@ -65,10 +66,10 @@ class DeviceHandler(BaseHandler):
                     ignore_if_known=False,
                 )
                 defer.returnValue(device_id)
-            except StoreError:
+            except errors.StoreError:
                 attempts += 1
 
-        raise StoreError(500, "Couldn't generate a device ID.")
+        raise errors.StoreError(500, "Couldn't generate a device ID.")
 
     @defer.inlineCallbacks
     def get_devices_by_user(self, user_id):
@@ -88,11 +89,38 @@ class DeviceHandler(BaseHandler):
             devices=((user_id, device_id) for device_id in devices.keys())
         )
 
-        for device_id in devices.keys():
-            ip = ips.get((user_id, device_id), {})
-            devices[device_id].update({
-                "last_seen_ts": ip.get("last_seen"),
-                "last_seen_ip": ip.get("ip"),
-            })
+        for device in devices.values():
+            _update_device_from_client_ips(device, ips)
 
         defer.returnValue(devices)
+
+    @defer.inlineCallbacks
+    def get_device(self, user_id, device_id):
+        """ Retrieve the given device
+
+        Args:
+            user_id (str):
+            device_id (str)
+
+        Returns:
+            defer.Deferred: dict[str, X]: info on the device
+        Raises:
+            errors.NotFoundError: if the device was not found
+        """
+        try:
+            device = yield self.store.get_device(user_id, device_id)
+        except errors.StoreError, e:
+            raise errors.NotFoundError
+        ips = yield self.store.get_last_client_ip_by_device(
+            devices=((user_id, device_id),)
+        )
+        _update_device_from_client_ips(device, ips)
+        defer.returnValue(device)
+
+
+def _update_device_from_client_ips(device, client_ips):
+    ip = client_ips.get((device["user_id"], device["device_id"]), {})
+    device.update({
+        "last_seen_ts": ip.get("last_seen"),
+        "last_seen_ip": ip.get("ip"),
+    })