diff --git a/apps/participation/management/commands/fix_matrix_channels.py b/apps/participation/management/commands/fix_matrix_channels.py index 640c581..4bd1ef6 100644 --- a/apps/participation/management/commands/fix_matrix_channels.py +++ b/apps/participation/management/commands/fix_matrix_channels.py @@ -1,7 +1,9 @@ +import os + from asgiref.sync import async_to_sync from nio import RoomPreset -from corres2math.matrix import Matrix, RoomVisibility +from corres2math.matrix import Matrix, RoomVisibility, UploadError from django.core.management import BaseCommand from registration.models import AdminRegistration, Registration @@ -10,6 +12,20 @@ class Command(BaseCommand): def handle(self, *args, **options): 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"): Matrix.create_room( visibility=RoomVisibility.public, diff --git a/corres2math/matrix.py b/corres2math/matrix.py index 588e454..1dae861 100644 --- a/corres2math/matrix.py +++ b/corres2math/matrix.py @@ -1,4 +1,5 @@ import os +from typing import Tuple from asgiref.sync import async_to_sync from nio import * @@ -11,6 +12,7 @@ class Matrix: @classmethod async def _get_client(cls) -> AsyncClient: client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr") + client.user_id = "@corres2mathbot:correspondances-maths.fr" if os.path.isfile(".matrix_token"): with open(".matrix_device", "r") as f: @@ -36,6 +38,26 @@ class Matrix: client = await cls._get_client() 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 @async_to_sync async def create_room( @@ -93,7 +115,6 @@ class Matrix: resp = await client.room_get_state_event(room_id, "m.room.power_levels") content = resp.content 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) @classmethod @@ -109,5 +130,4 @@ class Matrix: content["events"][event] = power_level else: 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)