2019-04-12 03:22:43 +10:00
|
|
|
# Copyright 2019 miruka
|
|
|
|
# This file is part of harmonyqml, licensed under GPLv3.
|
|
|
|
|
2019-04-19 17:11:56 +10:00
|
|
|
import time
|
2019-04-19 16:07:01 +10:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
from threading import Event
|
2019-04-19 17:11:56 +10:00
|
|
|
from typing import DefaultDict, Tuple
|
2019-04-12 03:22:43 +10:00
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, pyqtSlot
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
import nio
|
|
|
|
import nio.responses as nr
|
2019-04-22 01:15:03 +10:00
|
|
|
import nio.events as ne
|
2019-04-12 03:22:43 +10:00
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
from .network_manager import NetworkManager
|
2019-04-19 16:07:01 +10:00
|
|
|
from .pyqt_future import futurize
|
2019-04-18 11:08:32 +10:00
|
|
|
|
2019-04-12 03:22:43 +10:00
|
|
|
# One pool per hostname/remote server;
|
|
|
|
# multiple Client for different accounts on the same server can exist.
|
|
|
|
_POOLS: DefaultDict[str, ThreadPoolExecutor] = \
|
|
|
|
DefaultDict(lambda: ThreadPoolExecutor(max_workers=6))
|
|
|
|
|
|
|
|
|
|
|
|
class Client(QObject):
|
2019-04-22 05:20:20 +10:00
|
|
|
roomInvited = pyqtSignal([str, dict], [str])
|
|
|
|
roomJoined = pyqtSignal(str)
|
|
|
|
roomLeft = pyqtSignal([str, dict], [str])
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
roomSyncPrevBatchTokenReceived = pyqtSignal(str, str)
|
|
|
|
roomPastPrevBatchTokenReceived = pyqtSignal(str, str)
|
|
|
|
roomEventReceived = pyqtSignal(str, str, dict)
|
|
|
|
roomTypingUsersUpdated = pyqtSignal(str, list)
|
2019-04-22 05:20:20 +10:00
|
|
|
|
|
|
|
messageAboutToBeSent = pyqtSignal(str, dict)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
2019-04-18 13:28:25 +10:00
|
|
|
def __init__(self,
|
|
|
|
manager,
|
|
|
|
hostname: str,
|
|
|
|
username: str,
|
|
|
|
device_id: str = "") -> None:
|
|
|
|
super().__init__(manager)
|
|
|
|
self.manager = manager
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
host, *port = hostname.split(":")
|
|
|
|
self.host: str = host
|
|
|
|
self.port: int = int(port[0]) if port else 443
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
self.pool: ThreadPoolExecutor = _POOLS[self.host]
|
|
|
|
|
2019-04-12 03:22:43 +10:00
|
|
|
self.nio: nio.client.HttpClient = \
|
|
|
|
nio.client.HttpClient(self.host, username, device_id)
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
# Since nio clients can't handle more than one talk operation
|
|
|
|
# at a time, this one is used exclusively to poll the sync API
|
|
|
|
self.nio_sync: nio.client.HttpClient = \
|
|
|
|
nio.client.HttpClient(self.host, username, device_id)
|
2019-04-12 03:22:43 +10:00
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
self.net = NetworkManager(self.host, self.port, self.nio)
|
|
|
|
self.net_sync = NetworkManager(self.host, self.port, self.nio_sync)
|
2019-04-12 03:22:43 +10:00
|
|
|
|
2019-04-18 12:34:22 +10:00
|
|
|
self._loading: bool = False
|
|
|
|
|
2019-04-12 03:22:43 +10:00
|
|
|
self._stop_sync: Event = Event()
|
|
|
|
|
2019-04-19 17:11:56 +10:00
|
|
|
# {room_id: (was_typing, at_timestamp_secs)}
|
|
|
|
self._last_typing_set: DefaultDict[str, Tuple[bool, float]] = \
|
|
|
|
DefaultDict(lambda: (False, 0))
|
|
|
|
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return "%s(host=%r, port=%r, user_id=%r)" % \
|
2019-04-21 07:36:21 +10:00
|
|
|
(type(self).__name__, self.host, self.port, self.userId)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtProperty(str, constant=True)
|
2019-04-21 07:36:21 +10:00
|
|
|
def userId(self) -> str:
|
2019-04-13 03:18:46 +10:00
|
|
|
return self.nio.user_id
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str)
|
|
|
|
@pyqtSlot(str, str)
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize()
|
2019-04-12 03:22:43 +10:00
|
|
|
def login(self, password: str, device_name: str = "") -> None:
|
2019-04-18 11:08:32 +10:00
|
|
|
response = self.net.talk(self.nio.login, password, device_name)
|
|
|
|
self.nio_sync.receive_response(response)
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str, str, str)
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize()
|
2019-04-12 03:22:43 +10:00
|
|
|
def resumeSession(self, user_id: str, token: str, device_id: str
|
|
|
|
) -> None:
|
|
|
|
response = nr.LoginResponse(user_id, device_id, token)
|
|
|
|
self.nio.receive_response(response)
|
2019-04-18 11:08:32 +10:00
|
|
|
self.nio_sync.receive_response(response)
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot()
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize()
|
2019-04-12 03:22:43 +10:00
|
|
|
def logout(self) -> None:
|
|
|
|
self._stop_sync.set()
|
2019-04-20 06:15:21 +10:00
|
|
|
self.net.http_disconnect()
|
|
|
|
self.net_sync.http_disconnect()
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
@pyqtSlot()
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize()
|
2019-04-12 03:22:43 +10:00
|
|
|
def startSyncing(self) -> None:
|
|
|
|
while True:
|
2019-04-18 11:08:32 +10:00
|
|
|
self._on_sync(self.net_sync.talk(
|
2019-04-20 07:04:53 +10:00
|
|
|
self.nio_sync.sync, timeout=8000
|
2019-04-18 11:08:32 +10:00
|
|
|
))
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
if self._stop_sync.is_set():
|
|
|
|
self._stop_sync.clear()
|
|
|
|
break
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
def _on_sync(self, response: nr.SyncResponse) -> None:
|
2019-04-18 11:08:32 +10:00
|
|
|
self.nio.receive_response(response)
|
|
|
|
|
2019-04-21 20:56:59 +10:00
|
|
|
for room_id, room_info in response.rooms.invite.items():
|
2019-04-22 01:15:03 +10:00
|
|
|
for ev in room_info.invite_state:
|
2019-04-22 05:20:20 +10:00
|
|
|
member_ev = isinstance(ev, ne.InviteMemberEvent)
|
2019-04-22 01:15:03 +10:00
|
|
|
|
2019-04-22 05:20:20 +10:00
|
|
|
if member_ev and ev.content["membership"] == "join":
|
2019-04-22 01:15:03 +10:00
|
|
|
self.roomInvited.emit(room_id, ev.content)
|
|
|
|
break
|
|
|
|
else:
|
2019-04-22 05:20:20 +10:00
|
|
|
self.roomInvited[str].emit(room_id)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
for room_id, room_info in response.rooms.join.items():
|
2019-04-13 03:18:46 +10:00
|
|
|
self.roomJoined.emit(room_id)
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
self.roomSyncPrevBatchTokenReceived.emit(
|
|
|
|
room_id, room_info.timeline.prev_batch
|
|
|
|
)
|
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
for ev in room_info.timeline.events:
|
|
|
|
self.roomEventReceived.emit(
|
|
|
|
room_id, type(ev).__name__, ev.__dict__
|
|
|
|
)
|
|
|
|
|
2019-04-15 06:12:07 +10:00
|
|
|
for ev in room_info.ephemeral:
|
|
|
|
if isinstance(ev, nr.TypingNoticeEvent):
|
|
|
|
self.roomTypingUsersUpdated.emit(room_id, ev.users)
|
|
|
|
else:
|
|
|
|
print("ephemeral event: ", ev)
|
|
|
|
|
2019-04-22 05:20:20 +10:00
|
|
|
for room_id, room_info in response.rooms.leave.items():
|
|
|
|
for ev in room_info.timeline.events:
|
|
|
|
member_ev = isinstance(ev, ne.RoomMemberEvent)
|
|
|
|
|
|
|
|
if member_ev and ev.content["membership"] in ("leave", "ban"):
|
|
|
|
self.roomLeft.emit(room_id, ev.__dict__)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.roomLeft[str].emit(room_id)
|
2019-04-18 11:08:32 +10:00
|
|
|
|
|
|
|
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize()
|
2019-04-18 12:34:22 +10:00
|
|
|
def loadPastEvents(self, room_id: str, start_token: str, limit: int = 100
|
|
|
|
) -> None:
|
2019-04-18 11:08:32 +10:00
|
|
|
# From QML, use Backend.loastPastEvents instead
|
2019-04-18 12:34:22 +10:00
|
|
|
|
|
|
|
if self._loading:
|
|
|
|
return
|
|
|
|
self._loading = True
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
self._on_past_events(
|
|
|
|
room_id,
|
|
|
|
self.net.talk(
|
2019-04-18 12:34:22 +10:00
|
|
|
self.nio.room_messages, room_id, start=start_token, limit=limit
|
2019-04-18 11:08:32 +10:00
|
|
|
)
|
|
|
|
)
|
2019-04-18 12:34:22 +10:00
|
|
|
self._loading = False
|
2019-04-18 11:08:32 +10:00
|
|
|
|
|
|
|
|
|
|
|
def _on_past_events(self, room_id: str, response: nr.RoomMessagesResponse
|
|
|
|
) -> None:
|
|
|
|
self.roomPastPrevBatchTokenReceived.emit(room_id, response.end)
|
|
|
|
|
|
|
|
for ev in response.chunk:
|
|
|
|
self.roomEventReceived.emit(
|
|
|
|
room_id, type(ev).__name__, ev.__dict__
|
|
|
|
)
|
2019-04-18 13:01:26 +10:00
|
|
|
|
|
|
|
|
2019-04-19 17:11:56 +10:00
|
|
|
@pyqtSlot(str, bool)
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize(max_instances=1)
|
2019-04-19 17:11:56 +10:00
|
|
|
def setTypingState(self, room_id: str, typing: bool) -> None:
|
|
|
|
set_for_secs = 5
|
|
|
|
last_set, last_time = self._last_typing_set[room_id]
|
|
|
|
|
|
|
|
if not typing and last_set is False:
|
|
|
|
return
|
|
|
|
|
|
|
|
if typing and time.time() - last_time < set_for_secs - 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._last_typing_set[room_id] = (typing, time.time())
|
|
|
|
|
|
|
|
self.net.talk(
|
|
|
|
self.nio.room_typing,
|
2019-04-20 06:15:21 +10:00
|
|
|
room_id = room_id,
|
|
|
|
typing_state = typing,
|
|
|
|
timeout = set_for_secs * 1000,
|
2019-04-19 17:11:56 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-18 13:01:26 +10:00
|
|
|
@pyqtSlot(str, str)
|
2019-04-18 13:28:25 +10:00
|
|
|
def sendMarkdown(self, room_id: str, text: str) -> None:
|
|
|
|
html = self.manager.backend.htmlFilter.fromMarkdown(text)
|
2019-04-18 13:01:26 +10:00
|
|
|
content = {
|
|
|
|
"body": text,
|
2019-04-18 13:28:25 +10:00
|
|
|
"formatted_body": html,
|
2019-04-18 13:01:26 +10:00
|
|
|
"format": "org.matrix.custom.html",
|
|
|
|
"msgtype": "m.text",
|
|
|
|
}
|
2019-04-19 03:46:39 +10:00
|
|
|
self.messageAboutToBeSent.emit(room_id, content)
|
|
|
|
|
2019-04-20 08:29:25 +10:00
|
|
|
# If the thread pool workers are all occupied, and @futurize
|
|
|
|
# wrapped sendMarkdown, the messageAboutToBeSent signal neccessary
|
|
|
|
# for local echoes would not be sent until a thread is free.
|
|
|
|
@futurize()
|
|
|
|
def send(self):
|
|
|
|
return self.net.talk(
|
|
|
|
self.nio.room_send,
|
|
|
|
room_id = room_id,
|
|
|
|
message_type = "m.room.message",
|
|
|
|
content = content,
|
|
|
|
)
|
|
|
|
|
|
|
|
return send(self)
|