summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
Diffstat (limited to 'synapse')
-rw-r--r--synapse/handlers/device.py46
-rw-r--r--synapse/rest/client/v2_alpha/devices.py25
2 files changed, 62 insertions, 9 deletions
diff --git a/synapse/handlers/device.py b/synapse/handlers/device.py
index 6bbbf59e52..110f5fbb5c 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:
+            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"),
+    })
diff --git a/synapse/rest/client/v2_alpha/devices.py b/synapse/rest/client/v2_alpha/devices.py
index 5cf8bd1afa..8b9ab4f674 100644
--- a/synapse/rest/client/v2_alpha/devices.py
+++ b/synapse/rest/client/v2_alpha/devices.py
@@ -47,5 +47,30 @@ class DevicesRestServlet(RestServlet):
         defer.returnValue((200, {"devices": devices}))
 
 
+class DeviceRestServlet(RestServlet):
+    PATTERNS = client_v2_patterns("/devices/(?P<device_id>[^/]*)$",
+                                  releases=[], v2_alpha=False)
+
+    def __init__(self, hs):
+        """
+        Args:
+            hs (synapse.server.HomeServer): server
+        """
+        super(DeviceRestServlet, self).__init__()
+        self.hs = hs
+        self.auth = hs.get_auth()
+        self.device_handler = hs.get_device_handler()
+
+    @defer.inlineCallbacks
+    def on_GET(self, request, device_id):
+        requester = yield self.auth.get_user_by_req(request)
+        device = yield self.device_handler.get_device(
+            requester.user.to_string(),
+            device_id,
+        )
+        defer.returnValue((200, device))
+
+
 def register_servlets(hs, http_server):
     DevicesRestServlet(hs).register(http_server)
+    DeviceRestServlet(hs).register(http_server)