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

Add a ModuleApi method to update a user's membership in a room #11147

Merged
merged 16 commits into from
Oct 28, 2021
Merged
17 changes: 12 additions & 5 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,21 +601,28 @@ async def update_room_membership(
"Tried to send an event as a user that isn't local to this homeserver",
)

requester = create_requester(sender)
target_user_id = UserID.from_string(target)

if content is None:
content = {}

# Set the profile if not already done by the module.
if "avatar_url" not in content:
content["avatar_url"] = self._hs.get_profile_handler().get_avatar_url(target)
content["avatar_url"] = await self._hs.get_profile_handler().get_avatar_url(
requester.user,
)
babolivier marked this conversation as resolved.
Show resolved Hide resolved

if "displayname" not in content:
content["displayname"] = self._hs.get_profile_handler().get_displayname(
target,
content[
"displayname"
] = await self._hs.get_profile_handler().get_displayname(
target_user_id,
babolivier marked this conversation as resolved.
Show resolved Hide resolved
)

event_id, _ = await self._hs.get_room_member_handler().update_membership(
requester=create_requester(sender),
target=UserID.from_string(target),
requester=requester,
target=target_user_id,
room_id=room_id,
action=new_membership,
content=content,
Expand Down
27 changes: 26 additions & 1 deletion tests/module_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from synapse.federation.units import Transaction
from synapse.handlers.presence import UserPresenceState
from synapse.rest import admin
from synapse.rest.client import login, presence, room
from synapse.rest.client import login, presence, profile, room
from synapse.types import create_requester

from tests.events.test_presence_router import send_presence_update, sync_presence
Expand All @@ -37,6 +37,7 @@ class ModuleApiTestCase(HomeserverTestCase):
login.register_servlets,
room.register_servlets,
presence.register_servlets,
profile.register_servlets,
]

def prepare(self, reactor, clock, homeserver):
Expand Down Expand Up @@ -390,12 +391,32 @@ def test_update_membership(self):
peter = self.register_user("peter", "hackme")
lesley = self.register_user("lesley", "hackme")
tok = self.login("peter", "hackme")
lesley_tok = self.login("lesley", "hackme")

# Make peter create a public room.
room_id = self.helper.create_room_as(
room_creator=peter, is_public=True, tok=tok
)

# Set a profile for lesley.
channel = self.make_request(
method="PUT",
path="/_matrix/client/r0/profile/%s/displayname" % lesley,
content={"displayname": "Lesley May"},
access_token=lesley_tok,
)

self.assertEqual(channel.code, 200, channel.result)

channel = self.make_request(
method="PUT",
path="/_matrix/client/r0/profile/%s/avatar_url" % lesley,
content={"avatar_url": "some_url"},
babolivier marked this conversation as resolved.
Show resolved Hide resolved
access_token=lesley_tok,
)

self.assertEqual(channel.code, 200, channel.result)

# Make lesley join it.
self.get_success(
defer.ensureDeferred(
Expand All @@ -413,6 +434,10 @@ def test_update_membership(self):

self.assertEqual(res["membership"], "join")

# Also check that the profile was correctly filled out.
self.assertEqual(res["displayname"], "Lesley May")
self.assertEqual(res["avatar_url"], "some_url")

babolivier marked this conversation as resolved.
Show resolved Hide resolved
# Make peter kick lesley from the room.
self.get_success(
defer.ensureDeferred(
Expand Down