1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2025-06-21 18:38:24 +02:00

Create a Matrix channel when a team got created

This commit is contained in:
Yohann D'ANELLO
2020-10-29 15:07:03 +01:00
parent b33723efb3
commit 6647a284f4
5 changed files with 41 additions and 18 deletions

View File

@ -1,24 +1,22 @@
import asyncio
from typing import Any, Dict, Optional, Union
from asgiref.sync import async_to_sync
from nio import AsyncClient, RoomCreateError, RoomCreateResponse, RoomInviteError, RoomInviteResponse, RoomPreset, \
RoomVisibility
RoomVisibility, RoomResolveAliasResponse
class Matrix:
@classmethod
def _get_client(cls) -> AsyncClient:
if hasattr(cls, "_client"):
return cls._client
async def login():
cls._client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr")
await cls._client.login("toto1234")
return cls._client
return asyncio.get_event_loop().run_until_complete(login())
async def _get_client(cls) -> AsyncClient:
# TODO Store
client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr")
await client.login("toto1234")
return client
@classmethod
def create_room(
@async_to_sync
async def create_room(
cls,
visibility: RoomVisibility = RoomVisibility.private,
alias: Optional[str] = None,
@ -34,10 +32,21 @@ class Matrix:
) -> Union[RoomCreateResponse, RoomCreateError]:
resp: Union[RoomCreateResponse, RoomCreateError]
return asyncio.get_event_loop().run_until_complete(cls._get_client().room_create(
client = await cls._get_client()
return await client.room_create(
visibility, alias, name, topic, room_version, federate, is_direct, preset, invite, initial_state,
power_level_override))
power_level_override)
@classmethod
def invite(cls, room_id: str, user_id: str) -> Union[RoomInviteResponse, RoomInviteError]:
return asyncio.get_event_loop().run_until_complete(cls._get_client().room_invite(room_id, user_id))
async def resolve_room_alias(cls, room_alias: str) -> str:
client = await cls._get_client()
resp: RoomResolveAliasResponse = await client.room_resolve_alias(room_alias)
return resp.room_id
@classmethod
@async_to_sync
async def invite(cls, room_id: str, user_id: str) -> Union[RoomInviteResponse, RoomInviteError]:
client = await cls._get_client()
if room_id.startswith("#"):
room_id = await cls.resolve_room_alias(room_id)
return await client.room_invite(room_id, user_id)