Create #annonces channel

This commit is contained in:
Yohann D'ANELLO 2020-10-29 19:41:07 +01:00
parent 09a2947095
commit 3265c1fc1b
2 changed files with 35 additions and 2 deletions

View File

@ -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)

View File

@ -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)