2019-04-12 18:33:09 +10:00
|
|
|
# Copyright 2019 miruka
|
|
|
|
# This file is part of harmonyqml, licensed under GPLv3.
|
|
|
|
|
|
|
|
import hashlib
|
2019-04-19 16:07:01 +10:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2019-04-21 07:36:21 +10:00
|
|
|
from typing import Dict, Sequence, Set
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSlot
|
|
|
|
|
2019-04-18 12:34:22 +10:00
|
|
|
from .html_filter import HtmlFilter
|
2019-04-12 18:33:09 +10:00
|
|
|
from .model.qml_models import QMLModels
|
2019-04-19 16:07:01 +10:00
|
|
|
from .pyqt_future import futurize
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
|
|
|
class Backend(QObject):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__()
|
2019-04-19 16:07:01 +10:00
|
|
|
self.pool: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=6)
|
|
|
|
|
|
|
|
self._queried_displaynames: Dict[str, str] = {}
|
|
|
|
|
2019-04-18 12:34:22 +10:00
|
|
|
self.past_tokens: Dict[str, str] = {}
|
|
|
|
self.fully_loaded_rooms: Set[str] = set()
|
2019-04-18 11:08:32 +10:00
|
|
|
|
2019-04-18 13:28:25 +10:00
|
|
|
from .client_manager import ClientManager
|
|
|
|
self._client_manager: ClientManager = ClientManager(self)
|
2019-04-12 18:33:09 +10:00
|
|
|
self._models: QMLModels = QMLModels()
|
2019-04-18 00:49:54 +10:00
|
|
|
self._html_filter: HtmlFilter = HtmlFilter()
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
from .signal_manager import SignalManager
|
|
|
|
self._signal_manager: SignalManager = SignalManager(self)
|
|
|
|
|
|
|
|
self.clientManager.configLoad()
|
|
|
|
|
|
|
|
|
|
|
|
@pyqtProperty("QVariant", constant=True)
|
|
|
|
def clientManager(self):
|
|
|
|
return self._client_manager
|
|
|
|
|
|
|
|
@pyqtProperty("QVariant", constant=True)
|
|
|
|
def models(self):
|
|
|
|
return self._models
|
|
|
|
|
2019-04-18 00:49:54 +10:00
|
|
|
@pyqtProperty("QVariant", constant=True)
|
|
|
|
def htmlFilter(self):
|
|
|
|
return self._html_filter
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-04-19 16:07:01 +10:00
|
|
|
@pyqtSlot(str, result="QVariant")
|
|
|
|
@pyqtSlot(str, bool, result="QVariant")
|
2019-04-20 08:21:19 +10:00
|
|
|
@futurize()
|
2019-04-19 16:07:01 +10:00
|
|
|
def getUserDisplayName(self, user_id: str, can_block: bool = True) -> str:
|
|
|
|
if user_id in self._queried_displaynames:
|
|
|
|
return self._queried_displaynames[user_id]
|
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
for client in self.clientManager.clients.values():
|
|
|
|
for room in client.nio.rooms.values():
|
2019-04-19 16:07:01 +10:00
|
|
|
displayname = room.user_name(user_id)
|
|
|
|
|
|
|
|
if displayname:
|
|
|
|
return displayname
|
|
|
|
|
|
|
|
return self._query_user_displayname(user_id) if can_block else user_id
|
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
|
2019-04-19 16:07:01 +10:00
|
|
|
def _query_user_displayname(self, user_id: str) -> str:
|
|
|
|
client = next(iter(self.clientManager.clients.values()))
|
|
|
|
response = client.net.talk(client.nio.get_displayname, user_id)
|
|
|
|
displayname = getattr(response, "displayname", "") or user_id
|
2019-04-15 02:56:30 +10:00
|
|
|
|
2019-04-19 16:07:01 +10:00
|
|
|
self._queried_displaynames[user_id] = displayname
|
|
|
|
return displayname
|
2019-04-15 02:56:30 +10:00
|
|
|
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
@pyqtSlot(str, result=float)
|
|
|
|
def hueFromString(self, string: str) -> float:
|
|
|
|
# pylint:disable=no-self-use
|
|
|
|
md5 = hashlib.md5(bytes(string, "utf-8")).hexdigest()
|
|
|
|
return float("0.%s" % int(md5[-10:], 16))
|
2019-04-18 11:08:32 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str)
|
2019-04-18 12:34:22 +10:00
|
|
|
@pyqtSlot(str, int)
|
|
|
|
def loadPastEvents(self, room_id: str, limit: int = 100) -> None:
|
2019-04-18 11:08:32 +10:00
|
|
|
if not room_id in self.past_tokens:
|
|
|
|
return # Initial sync not done yet
|
|
|
|
|
2019-04-18 12:34:22 +10:00
|
|
|
if room_id in self.fully_loaded_rooms:
|
|
|
|
return
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
for client in self.clientManager.clients.values():
|
|
|
|
if room_id in client.nio.rooms:
|
2019-04-18 12:34:22 +10:00
|
|
|
client.loadPastEvents(
|
|
|
|
room_id, self.past_tokens[room_id], limit
|
|
|
|
)
|
2019-04-18 11:08:32 +10:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Room not found in any client: {room_id}")
|
2019-04-21 07:36:21 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
@pyqtSlot(list)
|
|
|
|
def pdb(self, additional_data: Sequence = ()) -> None:
|
|
|
|
# pylint: disable=all
|
|
|
|
ad = additional_data
|
2019-04-22 00:44:04 +10:00
|
|
|
cm = self.clientManager
|
|
|
|
cl = self.clientManager.clients
|
|
|
|
m = self.models
|
|
|
|
|
|
|
|
tcl = lambda user: cl[f"@test_{user}:matrix.org"]
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
import pdb
|
|
|
|
from PyQt5.QtCore import pyqtRemoveInputHook
|
|
|
|
pyqtRemoveInputHook()
|
|
|
|
pdb.set_trace()
|