Skip to content

Commit c8ce96f

Browse files
authored
Reinstate removed EventBase methods (#19712)
Both `__getitem__` and `.user_id` were removed in #19680 to simplify the event class. However, `EventBase` is exposed to modules who might also make use of those methods, so let's reinstate them (but otherwise not reinstate the usage of them in the code).
1 parent 3cdae2e commit c8ce96f

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

changelog.d/19712.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Small simplifications to the events class.

synapse/events/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
)
3535

3636
import attr
37+
from typing_extensions import deprecated
3738
from unpaddedbase64 import encode_base64
3839

3940
from synapse.api.constants import (
@@ -219,6 +220,9 @@ def __init__(
219220
state_key: DictProperty[str] = DictProperty("state_key")
220221
type: DictProperty[str] = DictProperty("type")
221222

223+
# This is a deprecated property, use `sender` instead. Only used by modules.
224+
user_id: DictProperty[str] = DictProperty("sender")
225+
222226
@property
223227
def event_id(self) -> str:
224228
raise NotImplementedError()
@@ -360,6 +364,11 @@ def __repr__(self) -> str:
360364
">"
361365
)
362366

367+
# Using `__getitem__` is deprecated. Only used by modules.
368+
@deprecated("Use attribute access instead")
369+
def __getitem__(self, field: str) -> Any | None:
370+
return self._dict[field]
371+
363372

364373
class FrozenEvent(EventBase):
365374
format_version = EventFormatVersions.ROOM_V1_V2 # All events of this type are V1

tests/module_api/test_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,24 @@ async def _on_logged_out_mock(
828828
# Ensure the pushers were deleted after the callback.
829829
self.assertEqual(len(self.hs.get_pusherpool().pushers[user_id].values()), 0)
830830

831+
def test_event_deprecated_methods(self) -> None:
832+
"""Test that deprecated methods on events are still functional."""
833+
user_id = self.register_user("user", "password")
834+
tok = self.login("user", "password")
835+
836+
room_id = self.helper.create_room_as(tok=tok)
837+
838+
state = self.get_success(
839+
self.hs.get_storage_controllers().state.get_current_state(room_id)
840+
)
841+
create_event = state[(EventTypes.Create, "")]
842+
843+
# `.user_id` is a deprecated alias for `.sender`.
844+
self.assertEqual(create_event.user_id, user_id)
845+
846+
# The event supports looking up keys via `__getitem__` although deprecated
847+
self.assertEqual(create_event["room_id"], room_id)
848+
831849

832850
class ModuleApiWorkerTestCase(BaseModuleApiTestCase, BaseMultiWorkerStreamTestCase):
833851
"""For testing ModuleApi functionality in a multi-worker setup"""

0 commit comments

Comments
 (0)