diff --git a/apps/participation/management/commands/fix_matrix_channels.py b/apps/participation/management/commands/fix_matrix_channels.py index ee033d0..5b67eb1 100644 --- a/apps/participation/management/commands/fix_matrix_channels.py +++ b/apps/participation/management/commands/fix_matrix_channels.py @@ -22,14 +22,16 @@ class Command(BaseCommand): avatar_uri = "plop" else: # pragma: no cover if not os.path.isfile(".matrix_avatar"): - stat_file = os.stat("tfjm/static/logo.png") - with open("tfjm/static/logo.png", "rb") as f: - resp = Matrix.upload(f, filename="logo.png", content_type="image/png", - filesize=stat_file.st_size)[0][0] - avatar_uri = resp.content_uri - with open(".matrix_avatar", "w") as f: - f.write(avatar_uri) - Matrix.set_avatar(avatar_uri) + avatar_uri = Matrix.get_avatar() + if not isinstance(avatar_uri, str): + stat_file = os.stat("tfjm/static/logo.png") + with open("tfjm/static/logo.png", "rb") as f: + resp = Matrix.upload(f, filename="logo.png", content_type="image/png", + filesize=stat_file.st_size)[0][0] + avatar_uri = resp.content_uri + with open(".matrix_avatar", "w") as f: + f.write(avatar_uri) + Matrix.set_avatar(avatar_uri) with open(".matrix_avatar", "r") as f: avatar_uri = f.read().rstrip(" \t\r\n") diff --git a/tfjm/matrix.py b/tfjm/matrix.py index 1a65311..20732d6 100644 --- a/tfjm/matrix.py +++ b/tfjm/matrix.py @@ -68,6 +68,16 @@ class Matrix: client = await cls._get_client() return await client.set_avatar(avatar_url) + @classmethod + @async_to_sync + async def get_avatar(cls): # pragma: no cover + """ + Set the display avatar of the bot account. + """ + client = await cls._get_client() + resp = await client.get_avatar() + return resp.avatar_url if resp.status_code == 200 else resp + @classmethod @async_to_sync async def upload(