Compare commits

...

5 Commits

Author SHA1 Message Date
H. Shay
85b30abfde update comment 2022-04-28 11:55:00 -07:00
H. Shay
d809a4c8fb update newsfragment number 2022-04-27 15:38:02 -07:00
H. Shay
df621cbaa5 lints 2022-04-27 15:34:11 -07:00
H. Shay
301b9cdfcc newsfragement 2022-04-27 15:17:45 -07:00
H. Shay
21002db229 add function to normalize username to module api + test 2022-04-27 15:17:33 -07:00
3 changed files with 30 additions and 0 deletions

View File

@@ -0,0 +1 @@
Add the Synapse function `types.map_username_to_mxid_localpart` to the Module API.

View File

@@ -117,6 +117,7 @@ from synapse.types import (
UserInfo,
UserProfile,
create_requester,
map_username_to_mxid_localpart,
)
from synapse.util import Clock
from synapse.util.async_helpers import maybe_awaitable
@@ -573,6 +574,26 @@ class ModuleApi:
return username
return UserID(username, self._hs.hostname).to_string()
def normalize_username(
self, username: Union[str, bytes], case_sensitive: bool = False
) -> str:
"""Map a username onto a string suitable for a MXID
This follows the algorithm laid out at
https://matrix.org/docs/spec/appendices.html#mapping-from-other-character-sets.
Added in Synapse v1.58.0
Args:
username: username to be mapped
case_sensitive: true if TEST and test should be mapped
onto different mxids
Returns:
string suitable for a mxid localpart
"""
return map_username_to_mxid_localpart(username, case_sensitive)
async def get_profile_for_user(self, localpart: str) -> ProfileInfo:
"""Look up the profile info for the user with the given localpart.

View File

@@ -635,6 +635,14 @@ class ModuleApiTestCase(HomeserverTestCase):
[{"set_tweak": "sound", "value": "default"}]
)
def test_normalize_username(self) -> None:
username = "Haxxor"
username2 = "_leet"
username3 = "aNoThErTeSt"
self.assertEqual(self.module_api.normalize_username(username), "haxxor")
self.assertEqual(self.module_api.normalize_username(username2), "=5fleet")
self.assertEqual(self.module_api.normalize_username(username3), "anothertest")
class ModuleApiWorkerTestCase(BaseMultiWorkerStreamTestCase):
"""For testing ModuleApi functionality in a multi-worker setup"""