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

Commit dd9e71d

Browse files
authored
Add set_displayname to the module API (#14629)
1 parent 071f8b0 commit dd9e71d

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

changelog.d/14629.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adds a `set_displayname()` method to the module API for setting a user's display name.

synapse/module_api/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,33 @@ async def create_room(
15851585

15861586
return room_id_and_alias["room_id"], room_id_and_alias.get("room_alias", None)
15871587

1588+
async def set_displayname(
1589+
self,
1590+
user_id: UserID,
1591+
new_displayname: str,
1592+
deactivation: bool = False,
1593+
) -> None:
1594+
"""Sets a user's display name.
1595+
1596+
Added in Synapse v1.76.0.
1597+
1598+
Args:
1599+
user_id:
1600+
The user whose display name is to be changed.
1601+
new_displayname:
1602+
The new display name to give the user.
1603+
deactivation:
1604+
Whether this change was made while deactivating the user.
1605+
"""
1606+
requester = create_requester(user_id)
1607+
await self._hs.get_profile_handler().set_displayname(
1608+
target_user=user_id,
1609+
requester=requester,
1610+
new_displayname=new_displayname,
1611+
by_admin=True,
1612+
deactivation=deactivation,
1613+
)
1614+
15881615

15891616
class PublicRoomListManager:
15901617
"""Contains methods for adding to, removing from and querying whether a room

tests/module_api/test_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ def test_can_set_admin(self):
110110
self.assertEqual(found_user.user_id.to_string(), user_id)
111111
self.assertIdentical(found_user.is_admin, True)
112112

113+
def test_can_set_displayname(self):
114+
localpart = "alice_wants_a_new_displayname"
115+
user_id = self.register_user(
116+
localpart, "1234", displayname="Alice", admin=False
117+
)
118+
found_userinfo = self.get_success(self.module_api.get_userinfo_by_id(user_id))
119+
120+
self.get_success(
121+
self.module_api.set_displayname(
122+
found_userinfo.user_id, "Bob", deactivation=False
123+
)
124+
)
125+
found_profile = self.get_success(
126+
self.module_api.get_profile_for_user(localpart)
127+
)
128+
129+
self.assertEqual(found_profile.display_name, "Bob")
130+
113131
def test_get_userinfo_by_id(self):
114132
user_id = self.register_user("alice", "1234")
115133
found_user = self.get_success(self.module_api.get_userinfo_by_id(user_id))

0 commit comments

Comments
 (0)