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

Commit 026ac44

Browse files
Update module API "update room membership" method to allow for remote joins (#13441)
Co-authored-by: MattC <[email protected]> Co-authored-by: Brendan Abolivier <[email protected]>
1 parent b6a6bb4 commit 026ac44

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

changelog.d/13441.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add remote join capability to the module API's `update_room_membership` method (in a backwards compatible manner).

synapse/module_api/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,12 @@ async def update_room_membership(
929929
room_id: str,
930930
new_membership: str,
931931
content: Optional[JsonDict] = None,
932+
remote_room_hosts: Optional[List[str]] = None,
932933
) -> EventBase:
933934
"""Updates the membership of a user to the given value.
934935
935936
Added in Synapse v1.46.0.
937+
Changed in Synapse v1.65.0: Added the 'remote_room_hosts' parameter.
936938
937939
Args:
938940
sender: The user performing the membership change. Must be a user local to
@@ -946,6 +948,7 @@ async def update_room_membership(
946948
https://spec.matrix.org/unstable/client-server-api/#mroommember for the
947949
list of allowed values.
948950
content: Additional values to include in the resulting event's content.
951+
remote_room_hosts: Remote servers to use for remote joins/knocks/etc.
949952
950953
Returns:
951954
The newly created membership event.
@@ -1005,15 +1008,12 @@ async def update_room_membership(
10051008
room_id=room_id,
10061009
action=new_membership,
10071010
content=content,
1011+
remote_room_hosts=remote_room_hosts,
10081012
)
10091013

10101014
# Try to retrieve the resulting event.
10111015
event = await self._hs.get_datastores().main.get_event(event_id)
10121016

1013-
# update_membership is supposed to always return after the event has been
1014-
# successfully persisted.
1015-
assert event is not None
1016-
10171017
return event
10181018

10191019
async def create_and_send_event_into_room(self, event_dict: JsonDict) -> EventBase:

tests/module_api/test_api.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from twisted.internet import defer
1717

1818
from synapse.api.constants import EduTypes, EventTypes
19+
from synapse.api.errors import NotFoundError
1920
from synapse.events import EventBase
2021
from synapse.federation.units import Transaction
2122
from synapse.handlers.presence import UserPresenceState
@@ -532,6 +533,34 @@ def test_update_membership(self):
532533
self.assertEqual(res["displayname"], "simone")
533534
self.assertIsNone(res["avatar_url"])
534535

536+
def test_update_room_membership_remote_join(self):
537+
"""Test that the module API can join a remote room."""
538+
# Necessary to fake a remote join.
539+
fake_stream_id = 1
540+
mocked_remote_join = simple_async_mock(
541+
return_value=("fake-event-id", fake_stream_id)
542+
)
543+
self.hs.get_room_member_handler()._remote_join = mocked_remote_join
544+
fake_remote_host = f"{self.module_api.server_name}-remote"
545+
546+
# Given that the join is to be faked, we expect the relevant join event not to
547+
# be persisted and the module API method to raise that.
548+
self.get_failure(
549+
defer.ensureDeferred(
550+
self.module_api.update_room_membership(
551+
sender=f"@user:{self.module_api.server_name}",
552+
target=f"@user:{self.module_api.server_name}",
553+
room_id=f"!nonexistent:{fake_remote_host}",
554+
new_membership="join",
555+
remote_room_hosts=[fake_remote_host],
556+
)
557+
),
558+
NotFoundError,
559+
)
560+
561+
# Check that a remote join was attempted.
562+
self.assertEqual(mocked_remote_join.call_count, 1)
563+
535564
def test_get_room_state(self):
536565
"""Tests that a module can retrieve the state of a room through the module API."""
537566
user_id = self.register_user("peter", "hackme")

0 commit comments

Comments
 (0)