Run matrix script into an async loop

This commit is contained in:
Yohann D'ANELLO 2021-04-04 16:02:37 +02:00
parent 7dd85d7402
commit aa69e6eadb
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 349 additions and 361 deletions

View File

@ -1,9 +1,9 @@
# Copyright (C) 2020 by Animath # Copyright (C) 2020 by Animath
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import asyncio
import os import os
from asgiref.sync import async_to_sync
from django.core.management import BaseCommand from django.core.management import BaseCommand
from django.utils.http import urlencode from django.utils.http import urlencode
from django.utils.translation import activate from django.utils.translation import activate
@ -16,32 +16,33 @@ class Command(BaseCommand):
def handle(self, *args, **options): # noqa: C901 def handle(self, *args, **options): # noqa: C901
activate("fr") activate("fr")
Matrix.set_display_name("Bot du TFJM²") async def main():
await Matrix.set_display_name("Bot du TFJM²")
if not os.getenv("SYNAPSE_PASSWORD"): if not os.getenv("SYNAPSE_PASSWORD"):
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"):
avatar_uri = Matrix.get_avatar() avatar_uri = await Matrix.get_avatar()
if isinstance(avatar_uri, str): if isinstance(avatar_uri, str):
with open(".matrix_avatar", "w") as f: with open(".matrix_avatar", "w") as f:
f.write(avatar_uri) f.write(avatar_uri)
else: else:
stat_file = os.stat("tfjm/static/logo.png") stat_file = os.stat("tfjm/static/logo.png")
with open("tfjm/static/logo.png", "rb") as f: with open("tfjm/static/logo.png", "rb") as f:
resp = Matrix.upload(f, filename="logo.png", content_type="image/png", resp = await Matrix.upload(f, filename="logo.png", content_type="image/png",
filesize=stat_file.st_size)[0][0] filesize=stat_file.st_size)[0][0]
avatar_uri = resp.content_uri avatar_uri = resp.content_uri
with open(".matrix_avatar", "w") as f: with open(".matrix_avatar", "w") as f:
f.write(avatar_uri) f.write(avatar_uri)
Matrix.set_avatar(avatar_uri) await 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")
# Create basic channels # Create basic channels
if not async_to_sync(Matrix.resolve_room_alias)("#aide-jurys-orgas:tfjm.org"): if not await Matrix.resolve_room_alias("#aide-jurys-orgas:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="aide-jurys-orgas", alias="aide-jurys-orgas",
name="Aide jurys & orgas", name="Aide jurys & orgas",
@ -50,8 +51,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)("#annonces:tfjm.org"): if not Matrix.resolve_room_alias("#annonces:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="annonces", alias="annonces",
name="Annonces", name="Annonces",
@ -60,8 +61,8 @@ class Command(BaseCommand):
preset=RoomPreset.public_chat, preset=RoomPreset.public_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)("#bienvenue:tfjm.org"): if not Matrix.resolve_room_alias("#bienvenue:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="bienvenue", alias="bienvenue",
name="Bienvenue", name="Bienvenue",
@ -70,8 +71,8 @@ class Command(BaseCommand):
preset=RoomPreset.public_chat, preset=RoomPreset.public_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)("#bot:tfjm.org"): if not Matrix.resolve_room_alias("#bot:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="bot", alias="bot",
name="Bot", name="Bot",
@ -80,8 +81,8 @@ class Command(BaseCommand):
preset=RoomPreset.public_chat, preset=RoomPreset.public_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)("#cno:tfjm.org"): if not Matrix.resolve_room_alias("#cno:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="cno", alias="cno",
name="CNO", name="CNO",
@ -90,8 +91,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)("#dev-bot:tfjm.org"): if not Matrix.resolve_room_alias("#dev-bot:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="dev-bot", alias="dev-bot",
name="Bot - développement", name="Bot - développement",
@ -100,8 +101,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)("#faq:tfjm.org"): if not Matrix.resolve_room_alias("#faq:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="faq", alias="faq",
name="FAQ", name="FAQ",
@ -110,8 +111,8 @@ class Command(BaseCommand):
preset=RoomPreset.public_chat, preset=RoomPreset.public_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)("#flood:tfjm.org"): if not Matrix.resolve_room_alias("#flood:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="flood", alias="flood",
name="Flood", name="Flood",
@ -120,8 +121,8 @@ class Command(BaseCommand):
preset=RoomPreset.public_chat, preset=RoomPreset.public_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)("#je-cherche-une-equipe:tfjm.org"): if not Matrix.resolve_room_alias("#je-cherche-une-equipe:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias="je-cherche-une-equipe", alias="je-cherche-une-equipe",
name="Je cherche une équipe", name="Je cherche une équipe",
@ -131,52 +132,52 @@ class Command(BaseCommand):
) )
# Setup avatars # Setup avatars
Matrix.set_room_avatar("#aide-jurys-orgas:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#aide-jurys-orgas:tfjm.org", avatar_uri)
Matrix.set_room_avatar("#annonces:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#annonces:tfjm.org", avatar_uri)
Matrix.set_room_avatar("#bienvenue:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#bienvenue:tfjm.org", avatar_uri)
Matrix.set_room_avatar("#bot:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#bot:tfjm.org", avatar_uri)
Matrix.set_room_avatar("#cno:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#cno:tfjm.org", avatar_uri)
Matrix.set_room_avatar("#dev-bot:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#dev-bot:tfjm.org", avatar_uri)
Matrix.set_room_avatar("#faq:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#faq:tfjm.org", avatar_uri)
Matrix.set_room_avatar("#flood:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#flood:tfjm.org", avatar_uri)
Matrix.set_room_avatar("#je-cherche-une-equipe:tfjm.org", avatar_uri) await Matrix.set_room_avatar("#je-cherche-une-equipe:tfjm.org", avatar_uri)
# Read-only channels # Read-only channels
Matrix.set_room_power_level_event("#annonces:tfjm.org", "events_default", 50) await Matrix.set_room_power_level_event("#annonces:tfjm.org", "events_default", 50)
Matrix.set_room_power_level_event("#bienvenue:tfjm.org", "events_default", 50) await Matrix.set_room_power_level_event("#bienvenue:tfjm.org", "events_default", 50)
# Invite everyone to public channels # Invite everyone to public channels
for r in Registration.objects.all(): for r in Registration.objects.all():
Matrix.invite("#annonces:tfjm.org", f"@{r.matrix_username}:tfjm.org") await Matrix.invite("#annonces:tfjm.org", f"@{r.matrix_username}:tfjm.org")
Matrix.invite("#bienvenue:tfjm.org", f"@{r.matrix_username}:tfjm.org") await Matrix.invite("#bienvenue:tfjm.org", f"@{r.matrix_username}:tfjm.org")
Matrix.invite("#bot:tfjm.org", f"@{r.matrix_username}:tfjm.org") await Matrix.invite("#bot:tfjm.org", f"@{r.matrix_username}:tfjm.org")
Matrix.invite("#faq:tfjm.org", f"@{r.matrix_username}:tfjm.org") await Matrix.invite("#faq:tfjm.org", f"@{r.matrix_username}:tfjm.org")
Matrix.invite("#flood:tfjm.org", f"@{r.matrix_username}:tfjm.org") await Matrix.invite("#flood:tfjm.org", f"@{r.matrix_username}:tfjm.org")
Matrix.invite("#je-cherche-une-equipe:tfjm.org", await Matrix.invite("#je-cherche-une-equipe:tfjm.org",
f"@{r.matrix_username}:tfjm.org") f"@{r.matrix_username}:tfjm.org")
self.stdout.write(f"Invite {r} in most common channels...") self.stdout.write(f"Invite {r} in most common channels...")
# Volunteers have access to the help channel # Volunteers have access to the help channel
for volunteer in VolunteerRegistration.objects.all(): for volunteer in VolunteerRegistration.objects.all():
Matrix.invite("#aide-jurys-orgas:tfjm.org", f"@{volunteer.matrix_username}:tfjm.org") await Matrix.invite("#aide-jurys-orgas:tfjm.org", f"@{volunteer.matrix_username}:tfjm.org")
self.stdout.write(f"Invite {volunteer} in #aide-jury-orgas...") self.stdout.write(f"Invite {volunteer} in #aide-jury-orgas...")
# Admins are admins # Admins are admins
for admin in AdminRegistration.objects.all(): for admin in AdminRegistration.objects.all():
self.stdout.write(f"Invite {admin} in #cno and #dev-bot...") self.stdout.write(f"Invite {admin} in #cno and #dev-bot...")
Matrix.invite("#cno:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite("#cno:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
Matrix.invite("#dev-bot:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite("#dev-bot:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
self.stdout.write(f"Give admin permissions for {admin}...") self.stdout.write(f"Give admin permissions for {admin}...")
Matrix.set_room_power_level("#aide-jurys-orgas:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#aide-jurys-orgas:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level("#annonces:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#annonces:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level("#bienvenue:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#bienvenue:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level("#bot:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#bot:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level("#cno:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#cno:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level("#dev-bot:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#dev-bot:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level("#faq:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#faq:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level("#flood:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#flood:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level("#je-cherche-une-equipe:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level("#je-cherche-une-equipe:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
# Create tournament-specific channels # Create tournament-specific channels
for tournament in Tournament.objects.all(): for tournament in Tournament.objects.all():
@ -185,8 +186,8 @@ class Command(BaseCommand):
name = tournament.name name = tournament.name
slug = name.lower().replace(" ", "-") slug = name.lower().replace(" ", "-")
if not async_to_sync(Matrix.resolve_room_alias)(f"#annonces-{slug}:tfjm.org"): if not Matrix.resolve_room_alias(f"#annonces-{slug}:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"annonces-{slug}", alias=f"annonces-{slug}",
name=f"{name} - Annonces", name=f"{name} - Annonces",
@ -195,8 +196,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)(f"#general-{slug}:tfjm.org"): if not Matrix.resolve_room_alias(f"#general-{slug}:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"general-{slug}", alias=f"general-{slug}",
name=f"{name} - Général", name=f"{name} - Général",
@ -205,8 +206,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)(f"#flood-{slug}:tfjm.org"): if not Matrix.resolve_room_alias(f"#flood-{slug}:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"flood-{slug}", alias=f"flood-{slug}",
name=f"{name} - Flood", name=f"{name} - Flood",
@ -215,8 +216,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)(f"#jury-{slug}:tfjm.org"): if not Matrix.resolve_room_alias(f"#jury-{slug}:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"jury-{slug}", alias=f"jury-{slug}",
name=f"{name} - Jury", name=f"{name} - Jury",
@ -225,8 +226,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)(f"#orga-{slug}:tfjm.org"): if not Matrix.resolve_room_alias(f"#orga-{slug}:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"orga-{slug}", alias=f"orga-{slug}",
name=f"{name} - Organisateurs", name=f"{name} - Organisateurs",
@ -235,8 +236,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)(f"#tirage-au-sort-{slug}:tfjm.org"): if not Matrix.resolve_room_alias(f"#tirage-au-sort-{slug}:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"tirage-au-sort-{slug}", alias=f"tirage-au-sort-{slug}",
name=f"{name} - Tirage au sort", name=f"{name} - Tirage au sort",
@ -246,64 +247,64 @@ class Command(BaseCommand):
) )
# Setup avatars # Setup avatars
Matrix.set_room_avatar(f"#annonces-{slug}:tfjm.org", avatar_uri) await Matrix.set_room_avatar(f"#annonces-{slug}:tfjm.org", avatar_uri)
Matrix.set_room_avatar(f"#flood-{slug}:tfjm.org", avatar_uri) await Matrix.set_room_avatar(f"#flood-{slug}:tfjm.org", avatar_uri)
Matrix.set_room_avatar(f"#general-{slug}:tfjm.org", avatar_uri) await Matrix.set_room_avatar(f"#general-{slug}:tfjm.org", avatar_uri)
Matrix.set_room_avatar(f"#jury-{slug}:tfjm.org", avatar_uri) await Matrix.set_room_avatar(f"#jury-{slug}:tfjm.org", avatar_uri)
Matrix.set_room_avatar(f"#orga-{slug}:tfjm.org", avatar_uri) await Matrix.set_room_avatar(f"#orga-{slug}:tfjm.org", avatar_uri)
Matrix.set_room_avatar(f"#tirage-au-sort-{slug}:tfjm.org", avatar_uri) await Matrix.set_room_avatar(f"#tirage-au-sort-{slug}:tfjm.org", avatar_uri)
# Invite admins and give permissions # Invite admins and give permissions
for admin in AdminRegistration.objects.all(): for admin in AdminRegistration.objects.all():
self.stdout.write(f"Invite {admin} in all channels of the tournament {name}...") self.stdout.write(f"Invite {admin} in all channels of the tournament {name}...")
Matrix.invite(f"#annonces-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite(f"#annonces-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
Matrix.invite(f"#flood-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite(f"#flood-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
Matrix.invite(f"#general-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite(f"#general-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
Matrix.invite(f"#jury-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite(f"#jury-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
Matrix.invite(f"#orga-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite(f"#orga-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
Matrix.invite(f"#tirage-au-sort-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite(f"#tirage-au-sort-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
self.stdout.write(f"Give permissions to {admin} in all channels of the tournament {name}...") self.stdout.write(f"Give permissions to {admin} in all channels of the tournament {name}...")
Matrix.set_room_power_level(f"#annonces-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level(f"#annonces-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level(f"#flood-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level(f"#flood-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level(f"#general-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level(f"#general-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level(f"#jury-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level(f"#jury-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level(f"#orga-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level(f"#orga-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level(f"#tirage-au-sort-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95) await Matrix.set_room_power_level(f"#tirage-au-sort-{slug}:tfjm.org", f"@{admin.matrix_username}:tfjm.org", 95)
# Invite organizers and give permissions # Invite organizers and give permissions
for orga in tournament.organizers.all(): for orga in tournament.organizers.all():
self.stdout.write(f"Invite organizer {orga} in all channels of the tournament {name}...") self.stdout.write(f"Invite organizer {orga} in all channels of the tournament {name}...")
Matrix.invite(f"#annonces-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org") await Matrix.invite(f"#annonces-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org")
Matrix.invite(f"#flood-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org") await Matrix.invite(f"#flood-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org")
Matrix.invite(f"#general-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org") await Matrix.invite(f"#general-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org")
Matrix.invite(f"#jury-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org") await Matrix.invite(f"#jury-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org")
Matrix.invite(f"#orga-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org") await Matrix.invite(f"#orga-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org")
Matrix.invite(f"#tirage-au-sort-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org") await Matrix.invite(f"#tirage-au-sort-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org")
if not orga.is_admin: if not orga.is_admin:
Matrix.set_room_power_level(f"#annonces-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50) await Matrix.set_room_power_level(f"#annonces-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50)
Matrix.set_room_power_level(f"#flood-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50) await Matrix.set_room_power_level(f"#flood-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50)
Matrix.set_room_power_level(f"#general-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50) await Matrix.set_room_power_level(f"#general-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50)
Matrix.set_room_power_level(f"#jury-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50) await Matrix.set_room_power_level(f"#jury-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50)
Matrix.set_room_power_level(f"#orga-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50) await Matrix.set_room_power_level(f"#orga-{slug}:tfjm.org", f"@{orga.matrix_username}:tfjm.org", 50)
Matrix.set_room_power_level(f"#tirage-au-sort-{slug}:tfjm.org", await Matrix.set_room_power_level(f"#tirage-au-sort-{slug}:tfjm.org",
f"@{orga.matrix_username}:tfjm.org", 50) f"@{orga.matrix_username}:tfjm.org", 50)
# Invite participants # Invite participants
for participation in tournament.participations.filter(valid=True).all(): for participation in tournament.participations.filter(valid=True).all():
for participant in participation.team.participants.all(): for participant in participation.team.participants.all():
self.stdout.write(f"Invite {participant} in public channels of the tournament {name}...") self.stdout.write(f"Invite {participant} in public channels of the tournament {name}...")
Matrix.invite(f"#annonces-{slug}:tfjm.org", f"@{participant.matrix_username}:tfjm.org") await Matrix.invite(f"#annonces-{slug}:tfjm.org", f"@{participant.matrix_username}:tfjm.org")
Matrix.invite(f"#flood-{slug}:tfjm.org", f"@{participant.matrix_username}:tfjm.org") await Matrix.invite(f"#flood-{slug}:tfjm.org", f"@{participant.matrix_username}:tfjm.org")
Matrix.invite(f"#general-{slug}:tfjm.org", f"@{participant.matrix_username}:tfjm.org") await Matrix.invite(f"#general-{slug}:tfjm.org", f"@{participant.matrix_username}:tfjm.org")
Matrix.invite(f"#tirage-au-sort-{slug}:tfjm.org", f"@{participant.matrix_username}:tfjm.org") await Matrix.invite(f"#tirage-au-sort-{slug}:tfjm.org", f"@{participant.matrix_username}:tfjm.org")
# Create pool-specific channels # Create pool-specific channels
for pool in tournament.pools.all(): for pool in tournament.pools.all():
self.stdout.write(f"Managing {pool}...") self.stdout.write(f"Managing {pool}...")
if not async_to_sync(Matrix.resolve_room_alias)(f"#poule-{slug}-{pool.id}:tfjm.org"): if not Matrix.resolve_room_alias(f"#poule-{slug}-{pool.id}:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"poule-{slug}-{pool.id}", alias=f"poule-{slug}-{pool.id}",
name=f"{name} - Jour {pool.round} - Poule " name=f"{name} - Jour {pool.round} - Poule "
@ -312,8 +313,8 @@ class Command(BaseCommand):
federate=False, federate=False,
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
if not async_to_sync(Matrix.resolve_room_alias)(f"#poule-{slug}-{pool.id}-jurys:tfjm.org"): if not Matrix.resolve_room_alias(f"#poule-{slug}-{pool.id}-jurys:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"poule-{slug}-{pool.id}-jurys", alias=f"poule-{slug}-{pool.id}-jurys",
name=f"{name} - Jour {pool.round} - Jurys poule " name=f"{name} - Jour {pool.round} - Jurys poule "
@ -323,69 +324,69 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
Matrix.set_room_avatar(f"#poule-{slug}-{pool.id}:tfjm.org", avatar_uri) await Matrix.set_room_avatar(f"#poule-{slug}-{pool.id}:tfjm.org", avatar_uri)
Matrix.set_room_avatar(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", avatar_uri) await Matrix.set_room_avatar(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", avatar_uri)
url_params = urlencode(dict(url=pool.bbb_url, url_params = urlencode(dict(url=pool.bbb_url,
isAudioConf='false', displayName='$matrix_display_name', isAudioConf='false', displayName='$matrix_display_name',
avatarUrl='$matrix_avatar_url', userId='$matrix_user_id')) \ avatarUrl='$matrix_avatar_url', userId='$matrix_user_id')) \
.replace("%24", "$") .replace("%24", "$")
Matrix.add_integration(f"#poule-{slug}-{pool.id}:tfjm.org", await Matrix.add_integration(f"#poule-{slug}-{pool.id}:tfjm.org",
f"https://scalar.vector.im/api/widgets/bigbluebutton.html?{url_params}", f"https://scalar.vector.im/api/widgets/bigbluebutton.html?{url_params}",
f"bbb-{slug}-{pool.id}", "bigbluebutton", "BigBlueButton", str(pool)) f"bbb-{slug}-{pool.id}", "bigbluebutton", "BigBlueButton", str(pool))
Matrix.add_integration(f"#poule-{slug}-{pool.id}:tfjm.org", await Matrix.add_integration(f"#poule-{slug}-{pool.id}:tfjm.org",
f"https://board.tfjm.org/boards/{slug}-{pool.id}", f"board-{slug}-{pool.id}", f"https://board.tfjm.org/boards/{slug}-{pool.id}", f"board-{slug}-{pool.id}",
"customwidget", "Tableau", str(pool)) "customwidget", "Tableau", str(pool))
# Invite admins and give permissions # Invite admins and give permissions
for admin in AdminRegistration.objects.all(): for admin in AdminRegistration.objects.all():
Matrix.invite(f"#poule-{slug}-{pool.id}:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite(f"#poule-{slug}-{pool.id}:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
Matrix.invite(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", f"@{admin.matrix_username}:tfjm.org") await Matrix.invite(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", f"@{admin.matrix_username}:tfjm.org")
Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}:tfjm.org", await Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}:tfjm.org",
f"@{admin.matrix_username}:tfjm.org", 95) f"@{admin.matrix_username}:tfjm.org", 95)
Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", await Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}-jurys:tfjm.org",
f"@{admin.matrix_username}:tfjm.org", 95) f"@{admin.matrix_username}:tfjm.org", 95)
# Invite organizers and give permissions # Invite organizers and give permissions
for orga in VolunteerRegistration.objects.all(): for orga in VolunteerRegistration.objects.all():
Matrix.invite(f"#poule-{slug}-{pool.id}:tfjm.org", f"@{orga.matrix_username}:tfjm.org") await Matrix.invite(f"#poule-{slug}-{pool.id}:tfjm.org", f"@{orga.matrix_username}:tfjm.org")
Matrix.invite(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", f"@{orga.matrix_username}:tfjm.org") await Matrix.invite(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", f"@{orga.matrix_username}:tfjm.org")
if not orga.is_admin: if not orga.is_admin:
Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}:tfjm.org", await Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}:tfjm.org",
f"@{orga.matrix_username}:tfjm.org", 50) f"@{orga.matrix_username}:tfjm.org", 50)
Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", await Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}-jurys:tfjm.org",
f"@{orga.matrix_username}:tfjm.org", 50) f"@{orga.matrix_username}:tfjm.org", 50)
# Invite the jury, give good permissions # Invite the jury, give good permissions
for jury in pool.juries.all(): for jury in pool.juries.all():
Matrix.invite(f"#annonces-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org") await Matrix.invite(f"#annonces-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org")
Matrix.invite(f"#general-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org") await Matrix.invite(f"#general-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org")
Matrix.invite(f"#flood-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org") await Matrix.invite(f"#flood-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org")
Matrix.invite(f"#jury-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org") await Matrix.invite(f"#jury-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org")
Matrix.invite(f"#orga-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org") await Matrix.invite(f"#orga-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org")
Matrix.invite(f"#poule-{slug}-{pool.id}:tfjm.org", f"@{jury.matrix_username}:tfjm.org") await Matrix.invite(f"#poule-{slug}-{pool.id}:tfjm.org", f"@{jury.matrix_username}:tfjm.org")
Matrix.invite(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", f"@{jury.matrix_username}:tfjm.org") await Matrix.invite(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", f"@{jury.matrix_username}:tfjm.org")
Matrix.invite(f"#tirage-au-sort-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org") await Matrix.invite(f"#tirage-au-sort-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org")
if not jury.is_admin: if not jury.is_admin:
Matrix.set_room_power_level(f"#jury-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org", 50) await Matrix.set_room_power_level(f"#jury-{slug}:tfjm.org", f"@{jury.matrix_username}:tfjm.org", 50)
Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}:tfjm.org", await Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}:tfjm.org",
f"@{jury.matrix_username}:tfjm.org", 50) f"@{jury.matrix_username}:tfjm.org", 50)
Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}-jurys:tfjm.org", await Matrix.set_room_power_level(f"#poule-{slug}-{pool.id}-jurys:tfjm.org",
f"@{jury.matrix_username}:tfjm.org", 50) f"@{jury.matrix_username}:tfjm.org", 50)
# Invite participants to the right pool # Invite participants to the right pool
for participation in pool.participations.all(): for participation in pool.participations.all():
for participant in participation.team.participants.all(): for participant in participation.team.participants.all():
Matrix.invite(f"#poule-{slug}-{pool.id}:tfjm.org", f"@{participant.matrix_username}:tfjm.org") await Matrix.invite(f"#poule-{slug}-{pool.id}:tfjm.org", f"@{participant.matrix_username}:tfjm.org")
# Create private channels for teams # Create private channels for teams
for team in Team.objects.all(): for team in Team.objects.all():
self.stdout.write(f"Create private channel for {team}...") self.stdout.write(f"Create private channel for {team}...")
if not async_to_sync(Matrix.resolve_room_alias)(f"#equipe-{team.trigram.lower()}:tfjm.org"): if not Matrix.resolve_room_alias(f"#equipe-{team.trigram.lower()}:tfjm.org"):
Matrix.create_room( await Matrix.create_room(
visibility=RoomVisibility.public, visibility=RoomVisibility.public,
alias=f"equipe-{team.trigram.lower()}", alias=f"equipe-{team.trigram.lower()}",
name=f"Équipe {team.trigram}", name=f"Équipe {team.trigram}",
@ -394,6 +395,8 @@ class Command(BaseCommand):
preset=RoomPreset.private_chat, preset=RoomPreset.private_chat,
) )
for participant in team.participants.all(): for participant in team.participants.all():
Matrix.invite(f"#equipe-{team.trigram.lower}:tfjm.org", f"@{participant.matrix_username}:tfjm.org") await Matrix.invite(f"#equipe-{team.trigram.lower}:tfjm.org", f"@{participant.matrix_username}:tfjm.org")
Matrix.set_room_power_level(f"#equipe-{team.trigram.lower()}:tfjm.org", await Matrix.set_room_power_level(f"#equipe-{team.trigram.lower()}:tfjm.org",
f"@{participant.matrix_username}:tfjm.org", 50) f"@{participant.matrix_username}:tfjm.org", 50)
asyncio.get_event_loop().run_until_complete(main())

View File

@ -4,8 +4,6 @@
from enum import Enum from enum import Enum
import os import os
from asgiref.sync import async_to_sync
class Matrix: class Matrix:
""" """
@ -51,7 +49,6 @@ class Matrix:
return client return client
@classmethod @classmethod
@async_to_sync
async def set_display_name(cls, name: str): async def set_display_name(cls, name: str):
""" """
Set the display name of the bot account. Set the display name of the bot account.
@ -60,7 +57,6 @@ class Matrix:
return await client.set_displayname(name) return await client.set_displayname(name)
@classmethod @classmethod
@async_to_sync
async def set_avatar(cls, avatar_url: str): # pragma: no cover async def set_avatar(cls, avatar_url: str): # pragma: no cover
""" """
Set the display avatar of the bot account. Set the display avatar of the bot account.
@ -69,7 +65,6 @@ class Matrix:
return await client.set_avatar(avatar_url) return await client.set_avatar(avatar_url)
@classmethod @classmethod
@async_to_sync
async def get_avatar(cls): # pragma: no cover async def get_avatar(cls): # pragma: no cover
""" """
Set the display avatar of the bot account. Set the display avatar of the bot account.
@ -79,7 +74,6 @@ class Matrix:
return resp.avatar_url if hasattr(resp, "avatar_url") else resp return resp.avatar_url if hasattr(resp, "avatar_url") else resp
@classmethod @classmethod
@async_to_sync
async def upload( async def upload(
cls, cls,
data_provider, data_provider,
@ -146,7 +140,6 @@ class Matrix:
if not isinstance(client, FakeMatrixClient) else None, None if not isinstance(client, FakeMatrixClient) else None, None
@classmethod @classmethod
@async_to_sync
async def create_room( async def create_room(
cls, cls,
visibility=None, visibility=None,
@ -232,7 +225,6 @@ class Matrix:
return resp.room_id if resp and hasattr(resp, "room_id") else None return resp.room_id if resp and hasattr(resp, "room_id") else None
@classmethod @classmethod
@async_to_sync
async def invite(cls, room_id: str, user_id: str): async def invite(cls, room_id: str, user_id: str):
""" """
Invite a user to a room. Invite a user to a room.
@ -251,7 +243,6 @@ class Matrix:
return await client.room_invite(room_id, user_id) return await client.room_invite(room_id, user_id)
@classmethod @classmethod
@async_to_sync
async def send_message(cls, room_id: str, body: str, formatted_body: str = None, async def send_message(cls, room_id: str, body: str, formatted_body: str = None,
msgtype: str = "m.text", html: bool = True): msgtype: str = "m.text", html: bool = True):
""" """
@ -274,7 +265,6 @@ class Matrix:
) )
@classmethod @classmethod
@async_to_sync
async def add_integration(cls, room_id: str, widget_url: str, state_key: str, async def add_integration(cls, room_id: str, widget_url: str, state_key: str,
widget_type: str = "customwidget", widget_name: str = "Custom widget", widget_type: str = "customwidget", widget_name: str = "Custom widget",
widget_title: str = ""): widget_title: str = ""):
@ -301,7 +291,6 @@ class Matrix:
) )
@classmethod @classmethod
@async_to_sync
async def remove_integration(cls, room_id: str, state_key: str): async def remove_integration(cls, room_id: str, state_key: str):
client = await cls._get_client() client = await cls._get_client()
if room_id.startswith("#"): if room_id.startswith("#"):
@ -314,7 +303,6 @@ class Matrix:
) )
@classmethod @classmethod
@async_to_sync
async def kick(cls, room_id: str, user_id: str, reason: str = None): async def kick(cls, room_id: str, user_id: str, reason: str = None):
""" """
Kick a user from a room, or withdraw their invitation. Kick a user from a room, or withdraw their invitation.
@ -337,7 +325,6 @@ class Matrix:
return await client.room_kick(room_id, user_id, reason) return await client.room_kick(room_id, user_id, reason)
@classmethod @classmethod
@async_to_sync
async def set_room_power_level(cls, room_id: str, user_id: str, power_level: int): # pragma: no cover async def set_room_power_level(cls, room_id: str, user_id: str, power_level: int): # pragma: no cover
""" """
Put a given power level to a user in a certain room. Put a given power level to a user in a certain room.
@ -364,7 +351,6 @@ class Matrix:
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
@async_to_sync
async def set_room_power_level_event(cls, room_id: str, event: str, power_level: int): # pragma: no cover async def set_room_power_level_event(cls, room_id: str, event: str, power_level: int): # pragma: no cover
""" """
Define the minimal power level to have to send a certain event type Define the minimal power level to have to send a certain event type
@ -395,7 +381,6 @@ class Matrix:
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
@async_to_sync
async def set_room_avatar(cls, room_id: str, avatar_uri: str): async def set_room_avatar(cls, room_id: str, avatar_uri: str):
""" """
Define the avatar of a room. Define the avatar of a room.