Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid setting avatar_url to "" #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions mautrix/client/api/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -81,7 +81,9 @@ 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 str_or_none(await self.get_displayname(self.mxid)) == str_or_none(
displayname
):
return
await self.api.request(
Method.PUT,
Expand Down Expand Up @@ -112,7 +114,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.

Expand All @@ -122,7 +126,9 @@ 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 str_or_none(await self.get_avatar_url(self.mxid)) == str_or_none(
avatar_url
):
return
await self.api.request(
Method.PUT,
Expand Down Expand Up @@ -185,3 +191,10 @@ async def beeper_update_profile(self, custom_fields: dict[str, Any]) -> None:
await self.api.request(Method.PATCH, Path.v3.profile[self.mxid], custom_fields)

# endregion


def str_or_none(v: str | None) -> str | None:
"""
str_or_none empty string values to None
"""
return None if v == "" else v