From c4464346380c3314ad9093aafc1327efd25489b9 Mon Sep 17 00:00:00 2001 From: Joe Groocock Date: Tue, 30 Apr 2024 12:48:32 +0000 Subject: [PATCH] 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 --- mautrix/client/api/user_data.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mautrix/client/api/user_data.py b/mautrix/client/api/user_data.py index 4c3d437a..597e79cb 100644 --- a/mautrix/client/api/user_data.py +++ b/mautrix/client/api/user_data.py @@ -71,7 +71,7 @@ async def search_users(self, search_query: str, limit: int | None = 10) -> UserS # region 10.2 Profiles # API reference: https://matrix.org/docs/spec/client_server/r0.4.0.html#profiles - async def set_displayname(self, displayname: str, check_current: bool = True) -> None: + async def set_displayname(self, displayname: str | None, check_current: bool = True) -> None: """ Set the display name of the current user. @@ -81,7 +81,7 @@ async def set_displayname(self, displayname: str, check_current: bool = True) -> displayname: The new display name for the user. check_current: Whether or not to check if the displayname is already set. """ - if check_current and await self.get_displayname(self.mxid) == displayname: + if check_current and await self.get_displayname(self.mxid) == (displayname or None): return await self.api.request( Method.PUT, @@ -112,7 +112,9 @@ async def get_displayname(self, user_id: UserID) -> str | None: except KeyError: return None - async def set_avatar_url(self, avatar_url: ContentURI, check_current: bool = True) -> None: + async def set_avatar_url( + self, avatar_url: ContentURI | None, check_current: bool = True + ) -> None: """ Set the avatar of the current user. @@ -122,7 +124,7 @@ async def set_avatar_url(self, avatar_url: ContentURI, check_current: bool = Tru avatar_url: The ``mxc://`` URI to the new avatar. check_current: Whether or not to check if the avatar is already set. """ - if check_current and await self.get_avatar_url(self.mxid) == avatar_url: + if check_current and await self.get_avatar_url(self.mxid) == (avatar_url or None): return await self.api.request( Method.PUT,