Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit c027bc0

Browse files
authored
Add FrozenEvent.get_state_key and use it in a couple of places (#11793)
This is more efficient, since we only have to look up `state_key` in the event dict once, rather than three (!) times.
1 parent 4c20965 commit c027bc0

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

changelog.d/11793.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `FrozenEvent.get_state_key` and use it in a couple of places.

synapse/events/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,11 @@ def __init__(
315315
redacts: DefaultDictProperty[Optional[str]] = DefaultDictProperty("redacts", None)
316316
room_id: DictProperty[str] = DictProperty("room_id")
317317
sender: DictProperty[str] = DictProperty("sender")
318-
# TODO state_key should be Optional[str], this is generally asserted in Synapse
319-
# by calling is_state() first (which ensures this), but it is hard (not possible?)
318+
# TODO state_key should be Optional[str]. This is generally asserted in Synapse
319+
# by calling is_state() first (which ensures it is not None), but it is hard (not possible?)
320320
# to properly annotate that calling is_state() asserts that state_key exists
321-
# and is non-None.
321+
# and is non-None. It would be better to replace such direct references with
322+
# get_state_key() (and a check for None).
322323
state_key: DictProperty[str] = DictProperty("state_key")
323324
type: DictProperty[str] = DictProperty("type")
324325
user_id: DictProperty[str] = DictProperty("sender")
@@ -332,7 +333,11 @@ def membership(self) -> str:
332333
return self.content["membership"]
333334

334335
def is_state(self) -> bool:
335-
return hasattr(self, "state_key") and self.state_key is not None
336+
return self.get_state_key() is not None
337+
338+
def get_state_key(self) -> Optional[str]:
339+
"""Get the state key of this event, or None if it's not a state event"""
340+
return self._dict.get("state_key")
336341

337342
def get_dict(self) -> JsonDict:
338343
d = dict(self._dict)

synapse/events/snapshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ async def serialize(self, event: EventBase, store: "DataStore") -> JsonDict:
163163
return {
164164
"prev_state_id": prev_state_id,
165165
"event_type": event.type,
166-
"event_state_key": event.state_key if event.is_state() else None,
166+
"event_state_key": event.get_state_key(),
167167
"state_group": self._state_group,
168168
"state_group_before_event": self.state_group_before_event,
169169
"rejected": self.rejected,

0 commit comments

Comments
 (0)