File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ Small simplifications to the events class.
Original file line number Diff line number Diff line change 3434)
3535
3636import attr
37+ from typing_extensions import deprecated
3738from unpaddedbase64 import encode_base64
3839
3940from 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
364373class FrozenEvent (EventBase ):
365374 format_version = EventFormatVersions .ROOM_V1_V2 # All events of this type are V1
Original file line number Diff line number Diff 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
832850class ModuleApiWorkerTestCase (BaseModuleApiTestCase , BaseMultiWorkerStreamTestCase ):
833851 """For testing ModuleApi functionality in a multi-worker setup"""
You can’t perform that action at this time.
0 commit comments