Update module API "update room membership" method to allow for remote joins (#13441)

Co-authored-by: MattC <buffless-matt@users.noreply.github.com>
Co-authored-by: Brendan Abolivier <babolivier@matrix.org>
This commit is contained in:
Matt C 2022-08-05 19:37:58 +10:00 committed by GitHub
parent b6a6bb4027
commit 026ac4486c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 4 deletions

View File

@ -0,0 +1 @@
Add remote join capability to the module API's `update_room_membership` method (in a backwards compatible manner).

View File

@ -929,10 +929,12 @@ class ModuleApi:
room_id: str, room_id: str,
new_membership: str, new_membership: str,
content: Optional[JsonDict] = None, content: Optional[JsonDict] = None,
remote_room_hosts: Optional[List[str]] = None,
) -> EventBase: ) -> EventBase:
"""Updates the membership of a user to the given value. """Updates the membership of a user to the given value.
Added in Synapse v1.46.0. Added in Synapse v1.46.0.
Changed in Synapse v1.65.0: Added the 'remote_room_hosts' parameter.
Args: Args:
sender: The user performing the membership change. Must be a user local to sender: The user performing the membership change. Must be a user local to
@ -946,6 +948,7 @@ class ModuleApi:
https://spec.matrix.org/unstable/client-server-api/#mroommember for the https://spec.matrix.org/unstable/client-server-api/#mroommember for the
list of allowed values. list of allowed values.
content: Additional values to include in the resulting event's content. content: Additional values to include in the resulting event's content.
remote_room_hosts: Remote servers to use for remote joins/knocks/etc.
Returns: Returns:
The newly created membership event. The newly created membership event.
@ -1005,15 +1008,12 @@ class ModuleApi:
room_id=room_id, room_id=room_id,
action=new_membership, action=new_membership,
content=content, content=content,
remote_room_hosts=remote_room_hosts,
) )
# Try to retrieve the resulting event. # Try to retrieve the resulting event.
event = await self._hs.get_datastores().main.get_event(event_id) event = await self._hs.get_datastores().main.get_event(event_id)
# update_membership is supposed to always return after the event has been
# successfully persisted.
assert event is not None
return event return event
async def create_and_send_event_into_room(self, event_dict: JsonDict) -> EventBase: async def create_and_send_event_into_room(self, event_dict: JsonDict) -> EventBase:

View File

@ -16,6 +16,7 @@ from unittest.mock import Mock
from twisted.internet import defer from twisted.internet import defer
from synapse.api.constants import EduTypes, EventTypes from synapse.api.constants import EduTypes, EventTypes
from synapse.api.errors import NotFoundError
from synapse.events import EventBase from synapse.events import EventBase
from synapse.federation.units import Transaction from synapse.federation.units import Transaction
from synapse.handlers.presence import UserPresenceState from synapse.handlers.presence import UserPresenceState
@ -532,6 +533,34 @@ class ModuleApiTestCase(HomeserverTestCase):
self.assertEqual(res["displayname"], "simone") self.assertEqual(res["displayname"], "simone")
self.assertIsNone(res["avatar_url"]) self.assertIsNone(res["avatar_url"])
def test_update_room_membership_remote_join(self):
"""Test that the module API can join a remote room."""
# Necessary to fake a remote join.
fake_stream_id = 1
mocked_remote_join = simple_async_mock(
return_value=("fake-event-id", fake_stream_id)
)
self.hs.get_room_member_handler()._remote_join = mocked_remote_join
fake_remote_host = f"{self.module_api.server_name}-remote"
# Given that the join is to be faked, we expect the relevant join event not to
# be persisted and the module API method to raise that.
self.get_failure(
defer.ensureDeferred(
self.module_api.update_room_membership(
sender=f"@user:{self.module_api.server_name}",
target=f"@user:{self.module_api.server_name}",
room_id=f"!nonexistent:{fake_remote_host}",
new_membership="join",
remote_room_hosts=[fake_remote_host],
)
),
NotFoundError,
)
# Check that a remote join was attempted.
self.assertEqual(mocked_remote_join.call_count, 1)
def test_get_room_state(self): def test_get_room_state(self):
"""Tests that a module can retrieve the state of a room through the module API.""" """Tests that a module can retrieve the state of a room through the module API."""
user_id = self.register_user("peter", "hackme") user_id = self.register_user("peter", "hackme")