Skip to content

Commit 4ef121c

Browse files
committed
Avoid setting avatar_url to ""
Fixes an edge-case bug where calling `set_avatar_url("")` when the user doesn't have an avatar(`get_avatar_url()` returns `None`) caused a redundant PUT request to be made to nullify the already-empty avatar_url field in the user profile. This also fixes the errant `$user made no change` state events appearing in every room they are in when `set_avatar_url("")` is called. Signed-off-by: Joe Groocock <[email protected]>
1 parent 8eca64e commit 4ef121c

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

mautrix/client/api/user_data.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def search_users(self, search_query: str, limit: int | None = 10) -> UserS
7171
# region 10.2 Profiles
7272
# API reference: https://matrix.org/docs/spec/client_server/r0.4.0.html#profiles
7373

74-
async def set_displayname(self, displayname: str, check_current: bool = True) -> None:
74+
async def set_displayname(self, displayname: str | None, check_current: bool = True) -> None:
7575
"""
7676
Set the display name of the current user.
7777
@@ -81,7 +81,9 @@ async def set_displayname(self, displayname: str, check_current: bool = True) ->
8181
displayname: The new display name for the user.
8282
check_current: Whether or not to check if the displayname is already set.
8383
"""
84-
if check_current and await self.get_displayname(self.mxid) == displayname:
84+
if check_current and str_or_none(await self.get_displayname(self.mxid)) == str_or_none(
85+
displayname
86+
):
8587
return
8688
await self.api.request(
8789
Method.PUT,
@@ -112,7 +114,9 @@ async def get_displayname(self, user_id: UserID) -> str | None:
112114
except KeyError:
113115
return None
114116

115-
async def set_avatar_url(self, avatar_url: ContentURI, check_current: bool = True) -> None:
117+
async def set_avatar_url(
118+
self, avatar_url: ContentURI | None, check_current: bool = True
119+
) -> None:
116120
"""
117121
Set the avatar of the current user.
118122
@@ -122,7 +126,9 @@ async def set_avatar_url(self, avatar_url: ContentURI, check_current: bool = Tru
122126
avatar_url: The ``mxc://`` URI to the new avatar.
123127
check_current: Whether or not to check if the avatar is already set.
124128
"""
125-
if check_current and await self.get_avatar_url(self.mxid) == avatar_url:
129+
if check_current and str_or_none(await self.get_avatar_url(self.mxid)) == str_or_none(
130+
avatar_url
131+
):
126132
return
127133
await self.api.request(
128134
Method.PUT,
@@ -185,3 +191,10 @@ async def beeper_update_profile(self, custom_fields: dict[str, Any]) -> None:
185191
await self.api.request(Method.PATCH, Path.v3.profile[self.mxid], custom_fields)
186192

187193
# endregion
194+
195+
196+
def str_or_none(v: str | None) -> str | None:
197+
"""
198+
str_or_none empty string values to None
199+
"""
200+
return None if v == "" else v

0 commit comments

Comments
 (0)