Exemple #1
0
async def _create_client(user_id: Optional[UserID], data: dict) -> web.Response:
    homeserver = data.get("homeserver", None)
    access_token = data.get("access_token", None)
    new_client = MatrixClient(mxid="@not:a.mxid", base_url=homeserver, token=access_token,
                              loop=Client.loop, client_session=Client.http_client)
    try:
        mxid = await new_client.whoami()
    except MatrixInvalidToken:
        return resp.bad_client_access_token
    except MatrixRequestError:
        return resp.bad_client_access_details
    except MatrixConnectionError:
        return resp.bad_client_connection_details
    if user_id is None:
        existing_client = Client.get(mxid, None)
        if existing_client is not None:
            return resp.user_exists
    elif mxid != user_id:
        return resp.mxid_mismatch(mxid)
    db_instance = DBClient(id=mxid, homeserver=homeserver, access_token=access_token,
                           enabled=data.get("enabled", True), next_batch=SyncToken(""),
                           filter_id=FilterID(""), sync=data.get("sync", True),
                           autojoin=data.get("autojoin", True),
                           displayname=data.get("displayname", ""),
                           avatar_url=data.get("avatar_url", ""))
    client = Client(db_instance)
    client.db_instance.insert()
    await client.start()
    return resp.created(client.to_dict())
Exemple #2
0
    def __init__(self, mconf: Optional[MirkConfig] = None):
        self._mconf = mconf
        if not self._mconf:
            self._mconf = MirkConfig()
            self._mconf.load()

        self._joined_rooms: list[str] = []
        self._client = MatrixClient(mxid=self._mconf.username,
                                    base_url=self._mconf.host)
        self._client.add_event_handler(EventType.ROOM_MEMBER,
                                       self._handle_invite)
        self._client.add_event_handler(EventType.ROOM_MESSAGE,
                                       self._handle_message)
Exemple #3
0
async def _create_client(user_id: UserID | None, data: dict) -> web.Response:
    homeserver = data.get("homeserver", None)
    access_token = data.get("access_token", None)
    device_id = data.get("device_id", None)
    new_client = MatrixClient(
        mxid="@not:a.mxid",
        base_url=homeserver,
        token=access_token,
        client_session=Client.http_client,
    )
    try:
        whoami = await new_client.whoami()
    except MatrixInvalidToken:
        return resp.bad_client_access_token
    except MatrixRequestError:
        return resp.bad_client_access_details
    except MatrixConnectionError:
        return resp.bad_client_connection_details
    if user_id is None:
        existing_client = await Client.get(whoami.user_id)
        if existing_client is not None:
            return resp.user_exists
    elif whoami.user_id != user_id:
        return resp.mxid_mismatch(whoami.user_id)
    elif whoami.device_id and device_id and whoami.device_id != device_id:
        return resp.device_id_mismatch(whoami.device_id)
    client = await Client.get(
        whoami.user_id, homeserver=homeserver, access_token=access_token, device_id=device_id
    )
    client.enabled = data.get("enabled", True)
    client.sync = data.get("sync", True)
    client.autojoin = data.get("autojoin", True)
    client.online = data.get("online", True)
    client.displayname = data.get("displayname", "disable")
    client.avatar_url = data.get("avatar_url", "disable")
    await client.update()
    await client.start()
    return resp.created(client.to_dict())