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).
This commit is contained in:
Erik Johnston
2026-04-22 11:43:59 +01:00
committed by GitHub
parent 3cdae2e278
commit c8ce96f504
3 changed files with 28 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
Small simplifications to the events class.
+9
View File
@@ -34,6 +34,7 @@ from typing import (
)
import attr
from typing_extensions import deprecated
from unpaddedbase64 import encode_base64
from synapse.api.constants import (
@@ -219,6 +220,9 @@ class EventBase(metaclass=abc.ABCMeta):
state_key: DictProperty[str] = DictProperty("state_key")
type: DictProperty[str] = DictProperty("type")
# This is a deprecated property, use `sender` instead. Only used by modules.
user_id: DictProperty[str] = DictProperty("sender")
@property
def event_id(self) -> str:
raise NotImplementedError()
@@ -360,6 +364,11 @@ class EventBase(metaclass=abc.ABCMeta):
">"
)
# Using `__getitem__` is deprecated. Only used by modules.
@deprecated("Use attribute access instead")
def __getitem__(self, field: str) -> Any | None:
return self._dict[field]
class FrozenEvent(EventBase):
format_version = EventFormatVersions.ROOM_V1_V2 # All events of this type are V1
+18
View File
@@ -828,6 +828,24 @@ class ModuleApiTestCase(BaseModuleApiTestCase):
# Ensure the pushers were deleted after the callback.
self.assertEqual(len(self.hs.get_pusherpool().pushers[user_id].values()), 0)
def test_event_deprecated_methods(self) -> None:
"""Test that deprecated methods on events are still functional."""
user_id = self.register_user("user", "password")
tok = self.login("user", "password")
room_id = self.helper.create_room_as(tok=tok)
state = self.get_success(
self.hs.get_storage_controllers().state.get_current_state(room_id)
)
create_event = state[(EventTypes.Create, "")]
# `.user_id` is a deprecated alias for `.sender`.
self.assertEqual(create_event.user_id, user_id)
# The event supports looking up keys via `__getitem__` although deprecated
self.assertEqual(create_event["room_id"], room_id)
class ModuleApiWorkerTestCase(BaseModuleApiTestCase, BaseMultiWorkerStreamTestCase):
"""For testing ModuleApi functionality in a multi-worker setup"""