4848 redact_event ,
4949 serialize_events ,
5050)
51+ from synapse .synapse_rust .types import Requester
5152from synapse .types import JsonDict
5253
5354from . import EventBase , StrippedStateEvent
@@ -135,15 +136,6 @@ def admin_override(cls, event: "EventBase") -> "FilteredEvent":
135136 return cls (event = event , membership = None )
136137
137138
138- _DEFAULT_SERIALIZE_EVENT_CONFIG = SerializeEventConfig ()
139-
140-
141- def make_config_for_admin (existing : SerializeEventConfig ) -> SerializeEventConfig :
142- # Thin wrapper kept for callers; see SerializeEventConfig.for_admin for the
143- # admin-only options it sets.
144- return existing .for_admin ()
145-
146-
147139class EventClientSerializer :
148140 """Serializes events that are to be sent to clients.
149141
@@ -160,12 +152,65 @@ def __init__(self, hs: "HomeServer") -> None:
160152 ADD_EXTRA_FIELDS_TO_UNSIGNED_CLIENT_EVENT_CALLBACK
161153 ] = []
162154
155+ async def create_config (
156+ self ,
157+ * ,
158+ as_client_event : bool = True ,
159+ event_format : EventFormat = EventFormat .ClientV1 ,
160+ requester : Requester | None = None ,
161+ event_field_allowlist : list [str ] | None = None ,
162+ include_stripped_room_state : bool = False ,
163+ include_admin_metadata : bool | None = None ,
164+ ) -> SerializeEventConfig :
165+ """
166+ Create a new SerializeEventConfig for the given parameters.
167+
168+ Helper method that sets the `include_admin_metadata` field based on
169+ whether the requester is a server admin if it is not explicitly
170+ provided. Also sets the `msc4354_enabled` field based on the homeserver
171+ config.
172+
173+ Args:
174+ as_client_event: Whether to serialize the events as client events.
175+ event_format: The format to serialize events in. requester: The user
176+ requesting the events, if any. Used to determine
177+ whether to include admin-only metadata in the serialized events.
178+ event_field_allowlist: A list of event fields to include in the
179+ serialized events.
180+ include_stripped_room_state: Whether to include stripped room state
181+ in the serialized events.
182+ include_admin_metadata: Whether to include admin-only metadata in
183+ the serialized events. If None, this will be determined based on
184+ whether the requester is a server admin.
185+ Returns:
186+ A SerializeEventConfig instance.
187+ """
188+
189+ # If include_admin_metadata is None, determine whether to include
190+ # admin-only metadata based on the requester.
191+ if include_admin_metadata is None :
192+ # Check if the requester is a server admin.
193+ if requester is not None and await self ._auth .is_server_admin (requester ):
194+ include_admin_metadata = True
195+ else :
196+ include_admin_metadata = False
197+
198+ return SerializeEventConfig (
199+ as_client_event = as_client_event ,
200+ event_format = event_format ,
201+ requester = requester ,
202+ event_field_allowlist = event_field_allowlist ,
203+ include_stripped_room_state = include_stripped_room_state ,
204+ include_admin_metadata = include_admin_metadata ,
205+ msc4354_enabled = self ._config .experimental .msc4354_enabled ,
206+ )
207+
163208 async def serialize_event (
164209 self ,
165210 event : JsonDict | FilteredEvent ,
166211 time_now : int ,
167212 * ,
168- config : SerializeEventConfig = _DEFAULT_SERIALIZE_EVENT_CONFIG ,
213+ config : SerializeEventConfig | None = None ,
169214 bundle_aggregations : dict [str , "BundledAggregations" ] | None = None ,
170215 redaction_map : Mapping [str , "EventBase" ] | None = None ,
171216 ) -> JsonDict :
@@ -187,7 +232,9 @@ async def serialize_event(
187232 if not isinstance (event , FilteredEvent ):
188233 return event
189234
190- config = await self ._update_config (config )
235+ if config is None :
236+ # Generate default config if none was provided.
237+ config = await self .create_config ()
191238
192239 # Perform all the async DB/IO work up front, then run the synchronous
193240 # serialization core.
@@ -204,28 +251,6 @@ async def serialize_event(
204251 unsigned_additions = unsigned_additions ,
205252 )[0 ]
206253
207- async def _update_config (
208- self , config : SerializeEventConfig
209- ) -> SerializeEventConfig :
210- """Update the config based on the requester and server config."""
211-
212- # Force-enable server admin metadata because the only time an event with
213- # relevant metadata will be when the admin requested it via their admin
214- # client config account data. Also, it's "just" some `unsigned` fields, so
215- # shouldn't cause much in terms of problems to downstream consumers.
216- #
217- # The requester is constant across the whole (recursive) serialization,
218- # so we only need to resolve this once.
219- if config .requester is not None and await self ._auth .is_server_admin (
220- config .requester
221- ):
222- config = make_config_for_admin (config )
223-
224- if self ._config .experimental .msc4354_enabled :
225- config = config .with_msc4354 (True )
226-
227- return config
228-
229254 async def _prepare_serialization (
230255 self ,
231256 events : Collection [FilteredEvent ],
@@ -313,7 +338,7 @@ async def serialize_events(
313338 events : Collection [JsonDict | FilteredEvent ],
314339 time_now : int ,
315340 * ,
316- config : SerializeEventConfig = _DEFAULT_SERIALIZE_EVENT_CONFIG ,
341+ config : SerializeEventConfig | None = None ,
317342 bundle_aggregations : dict [str , "BundledAggregations" ] | None = None ,
318343 ) -> list [JsonDict ]:
319344 """Serializes multiple events.
@@ -334,7 +359,9 @@ async def serialize_events(
334359 str (len (events )),
335360 )
336361
337- config = await self ._update_config (config )
362+ if config is None :
363+ # Generate default config if none was provided.
364+ config = await self .create_config ()
338365
339366 filtered_events = [e for e in events if isinstance (e , FilteredEvent )]
340367
0 commit comments