1717from typing import TYPE_CHECKING , Awaitable , Callable , Collection , List , Optional , Tuple
1818
1919from synapse .replication .http .account_data import (
20+ ReplicationAddRoomAccountDataRestServlet ,
2021 ReplicationAddTagRestServlet ,
22+ ReplicationAddUserAccountDataRestServlet ,
23+ ReplicationRemoveRoomAccountDataRestServlet ,
2124 ReplicationRemoveTagRestServlet ,
22- ReplicationRoomAccountDataRestServlet ,
23- ReplicationUserAccountDataRestServlet ,
25+ ReplicationRemoveUserAccountDataRestServlet ,
2426)
2527from synapse .streams import EventSource
2628from synapse .types import JsonDict , StreamKeyType , UserID
@@ -41,8 +43,18 @@ def __init__(self, hs: "HomeServer"):
4143 self ._instance_name = hs .get_instance_name ()
4244 self ._notifier = hs .get_notifier ()
4345
44- self ._user_data_client = ReplicationUserAccountDataRestServlet .make_client (hs )
45- self ._room_data_client = ReplicationRoomAccountDataRestServlet .make_client (hs )
46+ self ._add_user_data_client = (
47+ ReplicationAddUserAccountDataRestServlet .make_client (hs )
48+ )
49+ self ._remove_user_data_client = (
50+ ReplicationRemoveUserAccountDataRestServlet .make_client (hs )
51+ )
52+ self ._add_room_data_client = (
53+ ReplicationAddRoomAccountDataRestServlet .make_client (hs )
54+ )
55+ self ._remove_room_data_client = (
56+ ReplicationRemoveRoomAccountDataRestServlet .make_client (hs )
57+ )
4658 self ._add_tag_client = ReplicationAddTagRestServlet .make_client (hs )
4759 self ._remove_tag_client = ReplicationRemoveTagRestServlet .make_client (hs )
4860 self ._account_data_writers = hs .config .worker .writers .account_data
@@ -112,7 +124,7 @@ async def add_account_data_to_room(
112124
113125 return max_stream_id
114126 else :
115- response = await self ._room_data_client (
127+ response = await self ._add_room_data_client (
116128 instance_name = random .choice (self ._account_data_writers ),
117129 user_id = user_id ,
118130 room_id = room_id ,
@@ -121,15 +133,59 @@ async def add_account_data_to_room(
121133 )
122134 return response ["max_stream_id" ]
123135
136+ async def remove_account_data_for_room (
137+ self , user_id : str , room_id : str , account_data_type : str
138+ ) -> Optional [int ]:
139+ """
140+ Deletes the room account data for the given user and account data type.
141+
142+ "Deleting" account data merely means setting the content of the account data
143+ to an empty JSON object: {}.
144+
145+ Args:
146+ user_id: The user ID to remove room account data for.
147+ room_id: The room ID to target.
148+ account_data_type: The account data type to remove.
149+
150+ Returns:
151+ The maximum stream ID, or None if the room account data item did not exist.
152+ """
153+ if self ._instance_name in self ._account_data_writers :
154+ max_stream_id = await self ._store .remove_account_data_for_room (
155+ user_id , room_id , account_data_type
156+ )
157+ if max_stream_id is None :
158+ # The referenced account data did not exist, so no delete occurred.
159+ return None
160+
161+ self ._notifier .on_new_event (
162+ StreamKeyType .ACCOUNT_DATA , max_stream_id , users = [user_id ]
163+ )
164+
165+ # Notify Synapse modules that the content of the type has changed to an
166+ # empty dictionary.
167+ await self ._notify_modules (user_id , room_id , account_data_type , {})
168+
169+ return max_stream_id
170+ else :
171+ response = await self ._remove_room_data_client (
172+ instance_name = random .choice (self ._account_data_writers ),
173+ user_id = user_id ,
174+ room_id = room_id ,
175+ account_data_type = account_data_type ,
176+ content = {},
177+ )
178+ return response ["max_stream_id" ]
179+
124180 async def add_account_data_for_user (
125181 self , user_id : str , account_data_type : str , content : JsonDict
126182 ) -> int :
127183 """Add some global account_data for a user.
128184
129185 Args:
130- user_id: The user to add a tag for.
186+ user_id: The user to add some account data for.
131187 account_data_type: The type of account_data to add.
132- content: A json object to associate with the tag .
188+ content: The content json dictionary .
133189
134190 Returns:
135191 The maximum stream ID.
@@ -148,14 +204,53 @@ async def add_account_data_for_user(
148204
149205 return max_stream_id
150206 else :
151- response = await self ._user_data_client (
207+ response = await self ._add_user_data_client (
152208 instance_name = random .choice (self ._account_data_writers ),
153209 user_id = user_id ,
154210 account_data_type = account_data_type ,
155211 content = content ,
156212 )
157213 return response ["max_stream_id" ]
158214
215+ async def remove_account_data_for_user (
216+ self , user_id : str , account_data_type : str
217+ ) -> Optional [int ]:
218+ """Removes a piece of global account_data for a user.
219+
220+ Args:
221+ user_id: The user to remove account data for.
222+ account_data_type: The type of account_data to remove.
223+
224+ Returns:
225+ The maximum stream ID, or None if the room account data item did not exist.
226+ """
227+
228+ if self ._instance_name in self ._account_data_writers :
229+ max_stream_id = await self ._store .remove_account_data_for_user (
230+ user_id , account_data_type
231+ )
232+ if max_stream_id is None :
233+ # The referenced account data did not exist, so no delete occurred.
234+ return None
235+
236+ self ._notifier .on_new_event (
237+ StreamKeyType .ACCOUNT_DATA , max_stream_id , users = [user_id ]
238+ )
239+
240+ # Notify Synapse modules that the content of the type has changed to an
241+ # empty dictionary.
242+ await self ._notify_modules (user_id , None , account_data_type , {})
243+
244+ return max_stream_id
245+ else :
246+ response = await self ._remove_user_data_client (
247+ instance_name = random .choice (self ._account_data_writers ),
248+ user_id = user_id ,
249+ account_data_type = account_data_type ,
250+ content = {},
251+ )
252+ return response ["max_stream_id" ]
253+
159254 async def add_tag_to_room (
160255 self , user_id : str , room_id : str , tag : str , content : JsonDict
161256 ) -> int :
0 commit comments