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

Commit

Permalink
Add FrozenEvent.get_state_key and use it in a couple of places (#11793
Browse files Browse the repository at this point in the history
)

This is more efficient, since we only have to look up `state_key` in the event
dict once, rather than three (!) times.
  • Loading branch information
richvdh authored Jan 21, 2022
1 parent 4c20965 commit c027bc0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.d/11793.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `FrozenEvent.get_state_key` and use it in a couple of places.
13 changes: 9 additions & 4 deletions synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,11 @@ def __init__(
redacts: DefaultDictProperty[Optional[str]] = DefaultDictProperty("redacts", None)
room_id: DictProperty[str] = DictProperty("room_id")
sender: DictProperty[str] = DictProperty("sender")
# TODO state_key should be Optional[str], this is generally asserted in Synapse
# by calling is_state() first (which ensures this), but it is hard (not possible?)
# TODO state_key should be Optional[str]. This is generally asserted in Synapse
# by calling is_state() first (which ensures it is not None), but it is hard (not possible?)
# to properly annotate that calling is_state() asserts that state_key exists
# and is non-None.
# and is non-None. It would be better to replace such direct references with
# get_state_key() (and a check for None).
state_key: DictProperty[str] = DictProperty("state_key")
type: DictProperty[str] = DictProperty("type")
user_id: DictProperty[str] = DictProperty("sender")
Expand All @@ -332,7 +333,11 @@ def membership(self) -> str:
return self.content["membership"]

def is_state(self) -> bool:
return hasattr(self, "state_key") and self.state_key is not None
return self.get_state_key() is not None

def get_state_key(self) -> Optional[str]:
"""Get the state key of this event, or None if it's not a state event"""
return self._dict.get("state_key")

def get_dict(self) -> JsonDict:
d = dict(self._dict)
Expand Down
2 changes: 1 addition & 1 deletion synapse/events/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async def serialize(self, event: EventBase, store: "DataStore") -> JsonDict:
return {
"prev_state_id": prev_state_id,
"event_type": event.type,
"event_state_key": event.state_key if event.is_state() else None,
"event_state_key": event.get_state_key(),
"state_group": self._state_group,
"state_group_before_event": self.state_group_before_event,
"rejected": self.rejected,
Expand Down

0 comments on commit c027bc0

Please sign in to comment.