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.
This commit is contained in:
Andrew Morgan 2022-11-14 19:26:30 +00:00 committed by Andrew Morgan
parent f112fd6c12
commit 5c7d2e05e8

View file

@ -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
)