From 3265c1fc1b322a27e5ff71d8017c41173c1b6da7 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 29 Oct 2020 19:41:07 +0100 Subject: [PATCH] Create #annonces channel --- .../commands/fix_matrix_channels.py | 20 ++++++++++++++++++- corres2math/matrix.py | 17 +++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/apps/participation/management/commands/fix_matrix_channels.py b/apps/participation/management/commands/fix_matrix_channels.py index ef48b7d..640c581 100644 --- a/apps/participation/management/commands/fix_matrix_channels.py +++ b/apps/participation/management/commands/fix_matrix_channels.py @@ -3,7 +3,7 @@ from nio import RoomPreset from corres2math.matrix import Matrix, RoomVisibility from django.core.management import BaseCommand -from registration.models import AdminRegistration +from registration.models import AdminRegistration, Registration class Command(BaseCommand): @@ -20,6 +20,24 @@ class Command(BaseCommand): preset=RoomPreset.public_chat, ) + if not async_to_sync(Matrix.resolve_room_alias)("#annonces:correspondances-maths.fr"): + Matrix.create_room( + visibility=RoomVisibility.public, + alias="annonces", + name="Annonces", + topic="Informations importantes des Correspondances", + federate=False, + preset=RoomPreset.public_chat, + ) + + Matrix.set_room_power_level_event("#annonces:correspondances-maths.fr", "events_default", 50) + + for r in Registration.objects.all(): + Matrix.invite("#annonces:correspondances-maths.fr", f"@{r.matrix_username}:correspondances-maths.fr") + Matrix.invite("#faq:correspondances-maths.fr", f"@{r.matrix_username}:correspondances-maths.fr") + for admin in AdminRegistration.objects.all(): + Matrix.set_room_power_level("#annonces:correspondances-maths.fr", + f"@{admin.matrix_username}:correspondances-maths.fr", 95) Matrix.set_room_power_level("#faq:correspondances-maths.fr", f"@{admin.matrix_username}:correspondances-maths.fr", 95) diff --git a/corres2math/matrix.py b/corres2math/matrix.py index ce51207..588e454 100644 --- a/corres2math/matrix.py +++ b/corres2math/matrix.py @@ -94,5 +94,20 @@ class Matrix: content = resp.content content["users"][user_id] = power_level print(content) - print(resp.state_key) + return await client.room_put_state(room_id, "m.room.power_levels", content=content, state_key=resp.state_key) + + @classmethod + @async_to_sync + async def set_room_power_level_event(cls, room_id: str, event: str, power_level: int)\ + -> Union[RoomPutStateResponse, RoomPutStateError]: + client = await cls._get_client() + if room_id.startswith("#"): + room_id = await cls.resolve_room_alias(room_id) + resp = await client.room_get_state_event(room_id, "m.room.power_levels") + content = resp.content + if event.startswith("m."): + 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)