1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2025-02-06 11:33:00 +00:00

Define Bot avatar

This commit is contained in:
Yohann D'ANELLO 2020-10-29 20:10:57 +01:00
parent 3265c1fc1b
commit 79836f0d94
2 changed files with 39 additions and 3 deletions

View File

@ -1,7 +1,9 @@
import os
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from nio import RoomPreset from nio import RoomPreset
from corres2math.matrix import Matrix, RoomVisibility from corres2math.matrix import Matrix, RoomVisibility, UploadError
from django.core.management import BaseCommand from django.core.management import BaseCommand
from registration.models import AdminRegistration, Registration from registration.models import AdminRegistration, Registration
@ -10,6 +12,20 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
Matrix.set_display_name("Bot des Correspondances") Matrix.set_display_name("Bot des Correspondances")
if not os.path.isfile(".matrix_avatar"):
stat_file = os.stat("corres2math/static/logo.png")
with open("corres2math/static/logo.png", "rb") as f:
resp, _ = Matrix.upload(f, filename="logo.png", content_type="image/png", filesize=stat_file.st_size)
if isinstance(resp, UploadError):
raise Exception(resp)
avatar_uri = resp.content_uri
with open(".matrix_avatar", "w") as f:
f.write(avatar_uri)
Matrix.set_avatar(avatar_uri)
else:
with open(".matrix_avatar", "r") as f:
avatar_uri = f.read().rstrip(" \t\r\n")
if not async_to_sync(Matrix.resolve_room_alias)("#faq:correspondances-maths.fr"): if not async_to_sync(Matrix.resolve_room_alias)("#faq:correspondances-maths.fr"):
Matrix.create_room( Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,

View File

@ -1,4 +1,5 @@
import os import os
from typing import Tuple
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from nio import * from nio import *
@ -11,6 +12,7 @@ class Matrix:
@classmethod @classmethod
async def _get_client(cls) -> AsyncClient: async def _get_client(cls) -> AsyncClient:
client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr") client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr")
client.user_id = "@corres2mathbot:correspondances-maths.fr"
if os.path.isfile(".matrix_token"): if os.path.isfile(".matrix_token"):
with open(".matrix_device", "r") as f: with open(".matrix_device", "r") as f:
@ -36,6 +38,26 @@ class Matrix:
client = await cls._get_client() client = await cls._get_client()
return await client.set_displayname(name) return await client.set_displayname(name)
@classmethod
@async_to_sync
async def set_avatar(cls, avatar_url: str) -> Union[ProfileSetAvatarResponse, ProfileSetAvatarError]:
client = await cls._get_client()
return await client.set_avatar(avatar_url)
@classmethod
@async_to_sync
async def upload(
cls,
data_provider: DataProvider,
content_type: str = "application/octet-stream",
filename: Optional[str] = None,
encrypt: bool = False,
monitor: Optional[TransferMonitor] = None,
filesize: Optional[int] = None,
) -> Tuple[Union[UploadResponse, UploadError], Optional[Dict[str, Any]]]:
client = await cls._get_client()
return await client.upload(data_provider, content_type, filename, encrypt, monitor, filesize)
@classmethod @classmethod
@async_to_sync @async_to_sync
async def create_room( async def create_room(
@ -93,7 +115,6 @@ class Matrix:
resp = await client.room_get_state_event(room_id, "m.room.power_levels") resp = await client.room_get_state_event(room_id, "m.room.power_levels")
content = resp.content content = resp.content
content["users"][user_id] = power_level content["users"][user_id] = power_level
print(content)
return await client.room_put_state(room_id, "m.room.power_levels", content=content, state_key=resp.state_key) return await client.room_put_state(room_id, "m.room.power_levels", content=content, state_key=resp.state_key)
@classmethod @classmethod
@ -109,5 +130,4 @@ class Matrix:
content["events"][event] = power_level content["events"][event] = power_level
else: else:
content[event] = power_level content[event] = power_level
print(content)
return await client.room_put_state(room_id, "m.room.power_levels", content=content, state_key=resp.state_key) return await client.room_put_state(room_id, "m.room.power_levels", content=content, state_key=resp.state_key)