|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
"""
|
|
|
|
|
The Client class.
|
|
|
|
|
Connect to the matrix server and handle interactions with the server.
|
|
|
|
@ -9,6 +10,8 @@ import nio
|
|
|
|
|
from aiopath import AsyncPath
|
|
|
|
|
from typing import (
|
|
|
|
|
Any,
|
|
|
|
|
Awaitable,
|
|
|
|
|
Callable,
|
|
|
|
|
Optional,
|
|
|
|
|
NoReturn,
|
|
|
|
|
Union
|
|
|
|
@ -32,6 +35,8 @@ class Client(Aobject):
|
|
|
|
|
Connect to the matrix server and handle interactions with the
|
|
|
|
|
server.
|
|
|
|
|
|
|
|
|
|
user: the user id of the client (exp: @bot:matrix.org)
|
|
|
|
|
|
|
|
|
|
/!\ The client is initialized asyncronously: `client = await Client(...)`
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
@ -43,6 +48,7 @@ class Client(Aobject):
|
|
|
|
|
__sync_token_queue: asyncio.Queue[str]
|
|
|
|
|
__invite_queue: asyncio.Queue[tuple[RoomId, nio.responses.InviteInfo]]
|
|
|
|
|
__invite_policy: InvitePolicy
|
|
|
|
|
user: str
|
|
|
|
|
|
|
|
|
|
async def __init__(
|
|
|
|
|
self,
|
|
|
|
@ -81,6 +87,7 @@ class Client(Aobject):
|
|
|
|
|
if isinstance(resp, nio.responses.LoginError):
|
|
|
|
|
raise RuntimeError(f"Fail to connect: {resp.message}")
|
|
|
|
|
log.info("logged in")
|
|
|
|
|
self.user_id = self.__client.user_id
|
|
|
|
|
|
|
|
|
|
async def resolve_room(
|
|
|
|
|
self,
|
|
|
|
@ -283,6 +290,18 @@ class Client(Aobject):
|
|
|
|
|
for room_id in invites:
|
|
|
|
|
await self.__invite_queue.put((room_id, invites[room_id]))
|
|
|
|
|
|
|
|
|
|
def add_message_callback(
|
|
|
|
|
self,
|
|
|
|
|
callback: Callable[[Client, nio.rooms.Room, nio.events.room_events.RoomMessageText], Awaitable[None]]
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Add a callback called when a message is received.
|
|
|
|
|
The callback is an async function that take the client, the room and the event message in arg.
|
|
|
|
|
"""
|
|
|
|
|
async def new_callback(room: nio.rooms.Room, msg: nio.events.room_events.RoomMessageText):
|
|
|
|
|
await callback(self, room, msg)
|
|
|
|
|
self.__client.add_event_callback(new_callback, nio.RoomMessageText)
|
|
|
|
|
|
|
|
|
|
async def run(
|
|
|
|
|
self,
|
|
|
|
|
sync_timeout:int=30
|
|
|
|
|