2019-04-12 03:22:43 +10:00
|
|
|
# Copyright 2019 miruka
|
|
|
|
# This file is part of harmonyqml, licensed under GPLv3.
|
|
|
|
|
2019-05-09 02:12:43 +10:00
|
|
|
import logging as log
|
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-29 04:20:30 +10:00
|
|
|
from typing import DefaultDict, Tuple
|
2019-04-12 03:22:43 +10:00
|
|
|
|
2019-05-07 12:42:48 +10:00
|
|
|
from PyQt5.QtCore import (
|
|
|
|
QObject, QStandardPaths, pyqtProperty, pyqtSignal, pyqtSlot
|
|
|
|
)
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
import nio
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
from .network_manager import NetworkManager
|
2019-05-09 00:37:46 +10:00
|
|
|
from .pyqt_future import PyQtFuture, futurize
|
2019-04-18 11:08:32 +10:00
|
|
|
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
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-27 06:02:20 +10:00
|
|
|
roomAboutToBeForgotten = pyqtSignal(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,
|
2019-05-01 15:23:38 +10:00
|
|
|
hostname: str,
|
|
|
|
username: str,
|
|
|
|
device_id: str = "") -> None:
|
2019-04-18 13:28:25 +10:00
|
|
|
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-05-01 15:23:38 +10:00
|
|
|
self.pool: ThreadPoolExecutor = ThreadPoolExecutor(6)
|
2019-04-18 11:08:32 +10:00
|
|
|
|
2019-05-07 12:42:48 +10:00
|
|
|
store_path = self.manager.backend.getDir(
|
|
|
|
QStandardPaths.AppDataLocation
|
|
|
|
)
|
|
|
|
|
|
|
|
self.nio: nio.client.HttpClient = nio.client.HttpClient(
|
|
|
|
self.host, username, device_id, store_path=store_path
|
|
|
|
)
|
2019-04-12 03:22:43 +10:00
|
|
|
|
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
|
2019-05-07 12:42:48 +10:00
|
|
|
self.nio_sync: nio.client.HttpClient = nio.client.HttpClient(
|
|
|
|
self.host, username, device_id, store_path=store_path
|
|
|
|
)
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-05-09 00:22:52 +10:00
|
|
|
@futurize(max_running=1, discard_if_max_running=True, pyqt=False)
|
2019-05-07 13:27:31 +10:00
|
|
|
def _keys_upload(self) -> None:
|
|
|
|
self.net.talk(self.nio.keys_upload)
|
|
|
|
|
|
|
|
|
|
|
|
def _keys_query(self) -> None:
|
|
|
|
self.net.talk(self.nio.keys_query)
|
|
|
|
|
|
|
|
|
2019-05-09 02:12:43 +10:00
|
|
|
def _keys_claim(self, room_id: str) -> None:
|
|
|
|
self.net.talk(self.nio.keys_claim, room_id)
|
|
|
|
|
|
|
|
|
|
|
|
def _share_group_session(self,
|
|
|
|
room_id: str,
|
|
|
|
ignore_missing_sessions: bool = False) -> None:
|
|
|
|
self.net.talk(
|
|
|
|
self.nio.share_group_session,
|
|
|
|
room_id = room_id,
|
|
|
|
ignore_missing_sessions = ignore_missing_sessions,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-28 11:07:20 +10:00
|
|
|
@pyqtSlot(str, result="QVariant")
|
|
|
|
@pyqtSlot(str, str, result="QVariant")
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize()
|
2019-04-28 11:07:20 +10:00
|
|
|
def login(self, password: str, device_name: str = "") -> "Client":
|
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-05-07 13:08:36 +10:00
|
|
|
|
|
|
|
if not self.nio.olm_account_shared:
|
|
|
|
self._keys_upload()
|
|
|
|
|
2019-04-28 11:07:20 +10:00
|
|
|
return self
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
2019-04-28 11:07:20 +10:00
|
|
|
@pyqtSlot(str, str, str, result="QVariant")
|
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
|
2019-04-28 11:07:20 +10:00
|
|
|
) -> "Client":
|
2019-04-28 15:00:59 +10:00
|
|
|
response = nio.LoginResponse(user_id, device_id, token)
|
2019-04-12 03:22:43 +10:00
|
|
|
self.nio.receive_response(response)
|
2019-04-18 11:08:32 +10:00
|
|
|
self.nio_sync.receive_response(response)
|
2019-05-07 13:08:36 +10:00
|
|
|
|
|
|
|
if not self.nio.olm_account_shared:
|
|
|
|
self._keys_upload()
|
|
|
|
|
2019-04-28 11:07:20 +10:00
|
|
|
return self
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
2019-04-28 11:07:20 +10:00
|
|
|
@pyqtSlot(result="QVariant")
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize()
|
2019-04-28 11:07:20 +10:00
|
|
|
def logout(self) -> "Client":
|
2019-04-12 03:22:43 +10:00
|
|
|
self._stop_sync.set()
|
2019-04-20 06:15:21 +10:00
|
|
|
self.net.http_disconnect()
|
|
|
|
self.net_sync.http_disconnect()
|
2019-04-28 11:07:20 +10:00
|
|
|
return self
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
2019-04-27 08:47:25 +10:00
|
|
|
@futurize(pyqt=False)
|
2019-04-12 03:22:43 +10:00
|
|
|
def startSyncing(self) -> None:
|
|
|
|
while True:
|
2019-05-01 15:23:38 +10:00
|
|
|
try:
|
|
|
|
response = self.net_sync.talk(self.nio_sync.sync, timeout=8000)
|
|
|
|
except nio.LocalProtocolError: # logout occured
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self._on_sync(response)
|
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-28 15:00:59 +10:00
|
|
|
def _on_sync(self, response: nio.SyncResponse) -> None:
|
2019-04-18 11:08:32 +10:00
|
|
|
self.nio.receive_response(response)
|
|
|
|
|
2019-05-07 13:08:36 +10:00
|
|
|
if self.nio.should_upload_keys:
|
|
|
|
self._keys_upload()
|
|
|
|
|
2019-05-07 13:27:31 +10:00
|
|
|
if self.nio.should_query_keys:
|
|
|
|
self._keys_query()
|
|
|
|
|
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-28 15:00:59 +10:00
|
|
|
member_ev = isinstance(ev, nio.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:
|
2019-04-28 15:00:59 +10:00
|
|
|
if isinstance(ev, nio.TypingNoticeEvent):
|
2019-04-15 06:12:07 +10:00
|
|
|
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:
|
2019-04-28 15:00:59 +10:00
|
|
|
member_ev = isinstance(ev, nio.RoomMemberEvent)
|
2019-04-22 05:20:20 +10:00
|
|
|
|
|
|
|
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-05-09 00:22:52 +10:00
|
|
|
@futurize(max_running=1, discard_if_max_running=True)
|
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
|
|
|
|
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-28 15:00:59 +10:00
|
|
|
def _on_past_events(self, room_id: str, response: nio.RoomMessagesResponse
|
2019-04-18 11:08:32 +10:00
|
|
|
) -> 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-05-09 00:22:52 +10:00
|
|
|
@futurize(max_running=1, discard_if_max_running=True)
|
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-05-09 00:37:46 +10:00
|
|
|
def sendMarkdown(self, room_id: str, text: str) -> PyQtFuture:
|
2019-04-18 13:28:25 +10:00
|
|
|
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.
|
2019-05-09 00:37:46 +10:00
|
|
|
#
|
|
|
|
# send() only takes the room_id argument explicitely because
|
|
|
|
# of consider_args=True: This means the max number of messages being
|
|
|
|
# sent at a time is one per room at a time.
|
|
|
|
@futurize(max_running=1, consider_args=True)
|
|
|
|
def send(self, room_id: str) -> PyQtFuture:
|
2019-05-09 02:12:43 +10:00
|
|
|
talk = lambda: self.net.talk(
|
2019-04-20 08:29:25 +10:00
|
|
|
self.nio.room_send,
|
|
|
|
room_id = room_id,
|
|
|
|
message_type = "m.room.message",
|
|
|
|
content = content,
|
|
|
|
)
|
|
|
|
|
2019-05-09 02:12:43 +10:00
|
|
|
try:
|
|
|
|
log.debug("Try sending message %r to %r", content, room_id)
|
|
|
|
return talk()
|
|
|
|
except nio.GroupEncryptionError as err:
|
|
|
|
log.warning(err)
|
|
|
|
try:
|
|
|
|
self._share_group_session(room_id)
|
|
|
|
except nio.EncryptionError as err:
|
|
|
|
log.warning(err)
|
|
|
|
self._keys_claim(room_id)
|
|
|
|
self._share_group_session(room_id,
|
|
|
|
ignore_missing_sessions=True)
|
|
|
|
|
|
|
|
log.debug("Final try to send %r to %r", content, room_id)
|
|
|
|
return talk()
|
|
|
|
|
2019-05-09 00:37:46 +10:00
|
|
|
return send(self, room_id)
|
2019-04-22 06:12:53 +10:00
|
|
|
|
|
|
|
|
2019-04-29 04:20:30 +10:00
|
|
|
@pyqtSlot(str, result="QVariant")
|
2019-04-22 06:12:53 +10:00
|
|
|
@futurize()
|
|
|
|
def joinRoom(self, room_id: str) -> None:
|
|
|
|
return self.net.talk(self.nio.join, room_id=room_id)
|
|
|
|
|
|
|
|
|
2019-04-29 04:20:30 +10:00
|
|
|
@pyqtSlot(str, result="QVariant")
|
2019-04-22 06:12:53 +10:00
|
|
|
@futurize()
|
|
|
|
def leaveRoom(self, room_id: str) -> None:
|
|
|
|
return self.net.talk(self.nio.room_leave, room_id=room_id)
|
|
|
|
|
|
|
|
|
2019-04-29 04:20:30 +10:00
|
|
|
@pyqtSlot(str, result="QVariant")
|
2019-04-22 06:12:53 +10:00
|
|
|
@futurize()
|
|
|
|
def forgetRoom(self, room_id: str) -> None:
|
2019-04-27 06:02:20 +10:00
|
|
|
self.roomAboutToBeForgotten.emit(room_id)
|
2019-05-07 13:37:49 +10:00
|
|
|
response = self.net.talk(self.nio.room_forget, room_id=room_id)
|
|
|
|
self.nio.invalidate_outbound_session(room_id)
|
|
|
|
return response
|
2019-05-09 03:45:25 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str, result=bool)
|
|
|
|
def roomHasUnknownDevices(self, room_id: str) -> bool:
|
|
|
|
return self.nio.room_contains_unverified(room_id)
|