Don't re-upload a new avatar every time

This commit is contained in:
Yohann D'ANELLO 2021-01-22 08:57:01 +01:00
parent 384de5758b
commit 74c0260593
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 20 additions and 8 deletions

View File

@ -22,14 +22,16 @@ class Command(BaseCommand):
avatar_uri = "plop" avatar_uri = "plop"
else: # pragma: no cover else: # pragma: no cover
if not os.path.isfile(".matrix_avatar"): if not os.path.isfile(".matrix_avatar"):
stat_file = os.stat("tfjm/static/logo.png") avatar_uri = Matrix.get_avatar()
with open("tfjm/static/logo.png", "rb") as f: if not isinstance(avatar_uri, str):
resp = Matrix.upload(f, filename="logo.png", content_type="image/png", stat_file = os.stat("tfjm/static/logo.png")
filesize=stat_file.st_size)[0][0] with open("tfjm/static/logo.png", "rb") as f:
avatar_uri = resp.content_uri resp = Matrix.upload(f, filename="logo.png", content_type="image/png",
with open(".matrix_avatar", "w") as f: filesize=stat_file.st_size)[0][0]
f.write(avatar_uri) avatar_uri = resp.content_uri
Matrix.set_avatar(avatar_uri) with open(".matrix_avatar", "w") as f:
f.write(avatar_uri)
Matrix.set_avatar(avatar_uri)
with open(".matrix_avatar", "r") as f: with open(".matrix_avatar", "r") as f:
avatar_uri = f.read().rstrip(" \t\r\n") avatar_uri = f.read().rstrip(" \t\r\n")

View File

@ -68,6 +68,16 @@ class Matrix:
client = await cls._get_client() client = await cls._get_client()
return await client.set_avatar(avatar_url) 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 @classmethod
@async_to_sync @async_to_sync
async def upload( async def upload(