2020-10-29 15:40:30 +01:00
|
|
|
import os
|
2020-10-28 19:03:25 +01:00
|
|
|
|
2020-10-29 15:07:03 +01:00
|
|
|
from asgiref.sync import async_to_sync
|
2020-10-29 17:22:07 +01:00
|
|
|
from nio import *
|
2020-10-28 19:03:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Matrix:
|
2020-10-29 15:40:30 +01:00
|
|
|
_token: str = None
|
|
|
|
_device_id: str = None
|
|
|
|
|
2020-10-28 19:03:25 +01:00
|
|
|
@classmethod
|
2020-10-29 15:07:03 +01:00
|
|
|
async def _get_client(cls) -> AsyncClient:
|
|
|
|
client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr")
|
2020-10-29 15:40:30 +01:00
|
|
|
|
|
|
|
if os.path.isfile(".matrix_token"):
|
|
|
|
with open(".matrix_device", "r") as f:
|
|
|
|
cls._device_id = f.read().rstrip(" \t\r\n")
|
|
|
|
client.device_id = cls._device_id
|
|
|
|
with open(".matrix_token", "r") as f:
|
|
|
|
cls._token = f.read().rstrip(" \t\r\n")
|
|
|
|
client.access_token = cls._token
|
|
|
|
return client
|
|
|
|
|
2020-10-29 17:22:07 +01:00
|
|
|
await client.login(password=os.getenv("SYNAPSE_PASSWORD"), device_name="Plateforme")
|
2020-10-29 15:40:30 +01:00
|
|
|
cls._token = client.access_token
|
|
|
|
cls._device_id = client.device_id
|
|
|
|
with open(".matrix_token", "w") as f:
|
|
|
|
f.write(cls._token)
|
2020-10-29 17:22:07 +01:00
|
|
|
with open(".matrix_device", "w") as f:
|
|
|
|
f.write(cls._device_id)
|
2020-10-29 15:07:03 +01:00
|
|
|
return client
|
2020-10-28 19:03:25 +01:00
|
|
|
|
2020-10-29 17:22:07 +01:00
|
|
|
@classmethod
|
|
|
|
@async_to_sync
|
|
|
|
async def set_display_name(cls, name: str) -> Union[ProfileSetDisplayNameResponse, ProfileSetDisplayNameError]:
|
|
|
|
client = await cls._get_client()
|
|
|
|
return await client.set_displayname(name)
|
|
|
|
|
2020-10-28 19:03:25 +01:00
|
|
|
@classmethod
|
2020-10-29 15:07:03 +01:00
|
|
|
@async_to_sync
|
|
|
|
async def create_room(
|
2020-10-28 19:03:25 +01:00
|
|
|
cls,
|
|
|
|
visibility: RoomVisibility = RoomVisibility.private,
|
|
|
|
alias: Optional[str] = None,
|
|
|
|
name: Optional[str] = None,
|
|
|
|
topic: Optional[str] = None,
|
|
|
|
room_version: Optional[str] = None,
|
|
|
|
federate: bool = True,
|
|
|
|
is_direct: bool = False,
|
|
|
|
preset: Optional[RoomPreset] = None,
|
|
|
|
invite=(),
|
|
|
|
initial_state=(),
|
|
|
|
power_level_override: Optional[Dict[str, Any]] = None,
|
|
|
|
) -> Union[RoomCreateResponse, RoomCreateError]:
|
|
|
|
resp: Union[RoomCreateResponse, RoomCreateError]
|
|
|
|
|
2020-10-29 15:07:03 +01:00
|
|
|
client = await cls._get_client()
|
|
|
|
return await client.room_create(
|
2020-10-28 19:03:25 +01:00
|
|
|
visibility, alias, name, topic, room_version, federate, is_direct, preset, invite, initial_state,
|
2020-10-29 15:07:03 +01:00
|
|
|
power_level_override)
|
|
|
|
|
|
|
|
@classmethod
|
2020-10-29 17:22:07 +01:00
|
|
|
async def resolve_room_alias(cls, room_alias: str) -> Optional[str]:
|
2020-10-29 15:07:03 +01:00
|
|
|
client = await cls._get_client()
|
|
|
|
resp: RoomResolveAliasResponse = await client.room_resolve_alias(room_alias)
|
2020-10-29 17:22:07 +01:00
|
|
|
if isinstance(resp, RoomResolveAliasError):
|
|
|
|
return None
|
2020-10-29 15:07:03 +01:00
|
|
|
return resp.room_id
|
2020-10-28 19:03:25 +01:00
|
|
|
|
|
|
|
@classmethod
|
2020-10-29 15:07:03 +01:00
|
|
|
@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)
|
2020-10-29 15:40:30 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@async_to_sync
|
|
|
|
async def kick(cls, room_id: str, user_id: str, reason: str = None) -> Union[RoomKickResponse, RoomInviteError]:
|
|
|
|
client = await cls._get_client()
|
|
|
|
if room_id.startswith("#"):
|
|
|
|
room_id = await cls.resolve_room_alias(room_id)
|
|
|
|
return await client.room_kick(room_id, user_id, reason)
|
2020-10-29 17:22:07 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@async_to_sync
|
|
|
|
async def set_room_power_level(cls, room_id: str, user_id: 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
|
|
|
|
content["users"][user_id] = power_level
|
|
|
|
print(content)
|
2020-10-29 19:41:07 +01:00
|
|
|
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)
|
2020-10-29 17:22:07 +01:00
|
|
|
return await client.room_put_state(room_id, "m.room.power_levels", content=content, state_key=resp.state_key)
|