summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrew Morgan <andrewm@element.io>2022-11-14 19:26:30 +0000
committerAndrew Morgan <andrew@amorgan.xyz>2022-12-19 16:44:24 +0000
commit5c7d2e05e86b78bd251007fab85c6b7f71587892 (patch)
tree334aafdfcafe7fb3c7c8e5251617e218b110869e
parentAdd servlets, handler, storage functions for deleting user/room account data (diff)
downloadsynapse-5c7d2e05e86b78bd251007fab85c6b7f71587892.tar.xz
Allow deleting account data by PUT'ing with empty content
MSC3391 specifies that for backwards compatibility purposes, setting an account data type's content to {}
should be equivalent to deleting that account data. That call should succeed regardless of whether the
account data existed previously or not.
-rw-r--r--synapse/rest/client/account_data.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/synapse/rest/client/account_data.py b/synapse/rest/client/account_data.py
index 6c1470f464..6d13be1a7c 100644
--- a/synapse/rest/client/account_data.py
+++ b/synapse/rest/client/account_data.py
@@ -41,6 +41,7 @@ class AccountDataServlet(RestServlet):
 
     def __init__(self, hs: "HomeServer"):
         super().__init__()
+        self._hs = hs
         self.auth = hs.get_auth()
         self.store = hs.get_datastores().main
         self.handler = hs.get_account_data_handler()
@@ -54,6 +55,16 @@ class AccountDataServlet(RestServlet):
 
         body = parse_json_object_from_request(request)
 
+        # If experimental support for MSC3391 is enabled, then providing an empty dict
+        # as the value for an account data type should be functionally equivalent to
+        # calling the DELETE method on the same type.
+        if self._hs.config.experimental.msc3391_enabled:
+            if body == {}:
+                await self.handler.remove_account_data_for_user(
+                    user_id, account_data_type
+                )
+                return 200, {}
+
         await self.handler.add_account_data_for_user(user_id, account_data_type, body)
 
         return 200, {}
@@ -124,6 +135,7 @@ class RoomAccountDataServlet(RestServlet):
 
     def __init__(self, hs: "HomeServer"):
         super().__init__()
+        self._hs = hs
         self.auth = hs.get_auth()
         self.store = hs.get_datastores().main
         self.handler = hs.get_account_data_handler()
@@ -156,6 +168,16 @@ class RoomAccountDataServlet(RestServlet):
                 Codes.BAD_JSON,
             )
 
+        # If experimental support for MSC3391 is enabled, then providing an empty dict
+        # as the value for an account data type should be functionally equivalent to
+        # calling the DELETE method on the same type.
+        if self._hs.config.experimental.msc3391_enabled:
+            if body == {}:
+                await self.handler.remove_account_data_for_room(
+                    user_id, room_id, account_data_type
+                )
+                return 200, {}
+
         await self.handler.add_account_data_to_room(
             user_id, room_id, account_data_type, body
         )