summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuke Barnard <lukeb@openmarket.com>2017-03-13 16:33:51 +0000
committerLuke Barnard <lukeb@openmarket.com>2017-03-13 16:33:51 +0000
commitc81f33f73d37fdf5027a356b50cc6ab0f93da3d9 (patch)
tree080f124d1c6ab5eb3d142bdaa11a216436263238
parentMerge pull request #1978 from matrix-org/rav/refactor_received_pdu (diff)
downloadsynapse-c81f33f73d37fdf5027a356b50cc6ab0f93da3d9.tar.xz
Implement delete_devices API
This implements the proposal here https://docs.google.com/document/d/1C-25Gqz3TXy2jIAoeOKxpNtmme0jI4g3yFGqv5GlAAk for deleting multiple devices at once in a single request.
-rw-r--r--synapse/rest/client/v2_alpha/devices.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/synapse/rest/client/v2_alpha/devices.py b/synapse/rest/client/v2_alpha/devices.py
index a1feaf3d54..2560da141e 100644
--- a/synapse/rest/client/v2_alpha/devices.py
+++ b/synapse/rest/client/v2_alpha/devices.py
@@ -45,6 +45,52 @@ class DevicesRestServlet(servlet.RestServlet):
         )
         defer.returnValue((200, {"devices": devices}))
 
+class DeleteDevicesRestServlet(servlet.RestServlet):
+    PATTERNS = client_v2_patterns("/delete_devices", releases=[], v2_alpha=False)
+
+    def __init__(self, hs):
+        """
+        Args:
+            hs (synapse.server.HomeServer): server
+        """
+        super(DeleteDevicesRestServlet, self).__init__()
+        self.hs = hs
+        self.auth = hs.get_auth()
+        self.device_handler = hs.get_device_handler()
+        self.auth_handler = hs.get_auth_handler()
+
+    @defer.inlineCallbacks
+    def on_POST(self, request):
+        try:
+            body = servlet.parse_json_object_from_request(request)
+
+        except errors.SynapseError as e:
+            if e.errcode == errors.Codes.NOT_JSON:
+                # deal with older clients which didn't pass a J*DELETESON dict
+                # the same as those that pass an empty dict
+                body = {}
+            else:
+                raise
+
+        if 'devices' not in body:
+            raise errors.SynapseError(
+                400, "No devices supplied", errcode=errors.Codes.MISSING_PARAM
+            )
+
+        authed, result, params, _ = yield self.auth_handler.check_auth([
+            [constants.LoginType.PASSWORD],
+        ], body, self.hs.get_ip_from_request(request))
+
+        if not authed:
+            defer.returnValue((401, result))
+
+        requester = yield self.auth.get_user_by_req(request)
+        for d_id in body['devices']:
+            yield self.device_handler.delete_device(
+                requester.user.to_string(),
+                d_id,
+            )
+        defer.returnValue((200, {}))
 
 class DeviceRestServlet(servlet.RestServlet):
     PATTERNS = client_v2_patterns("/devices/(?P<device_id>[^/]*)$",
@@ -111,5 +157,6 @@ class DeviceRestServlet(servlet.RestServlet):
 
 
 def register_servlets(hs, http_server):
+    DeleteDevicesRestServlet(hs).register(http_server)
     DevicesRestServlet(hs).register(http_server)
     DeviceRestServlet(hs).register(http_server)