2019-04-12 18:33:09 +10:00
|
|
|
# Copyright 2019 miruka
|
|
|
|
# This file is part of harmonyqml, licensed under GPLv3.
|
|
|
|
|
2019-05-07 11:28:54 +10:00
|
|
|
import os
|
2019-05-12 05:52:56 +10:00
|
|
|
import random
|
2019-04-19 16:07:01 +10:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2019-05-12 05:52:56 +10:00
|
|
|
from typing import Deque, Dict, Optional, Sequence, Set, Tuple
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-05-07 11:28:54 +10:00
|
|
|
from atomicfile import AtomicFile
|
|
|
|
from PyQt5.QtCore import QObject, QStandardPaths, pyqtProperty, pyqtSlot
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-04-18 12:34:22 +10:00
|
|
|
from .html_filter import HtmlFilter
|
2019-05-03 04:31:47 +10:00
|
|
|
from .model import ListModel, ListModelMap
|
2019-05-12 05:52:56 +10:00
|
|
|
from .model.items import User
|
|
|
|
from .network_manager import NioErrorResponse
|
2019-04-19 16:07:01 +10:00
|
|
|
from .pyqt_future import futurize
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
|
|
|
class Backend(QObject):
|
2019-04-23 04:24:45 +10:00
|
|
|
def __init__(self, parent: QObject) -> None:
|
|
|
|
super().__init__(parent)
|
2019-04-19 16:07:01 +10:00
|
|
|
self.pool: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=6)
|
|
|
|
|
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-05-03 04:31:47 +10:00
|
|
|
self._html_filter: HtmlFilter = HtmlFilter(self)
|
|
|
|
|
2019-04-18 13:28:25 +10:00
|
|
|
from .client_manager import ClientManager
|
|
|
|
self._client_manager: ClientManager = ClientManager(self)
|
2019-05-03 04:31:47 +10:00
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
self._accounts: ListModel = ListModel(parent=parent)
|
|
|
|
|
|
|
|
self._room_events: ListModelMap = ListModelMap(
|
|
|
|
container = Deque,
|
|
|
|
parent = self
|
|
|
|
)
|
|
|
|
|
|
|
|
self._users: ListModel = ListModel(
|
|
|
|
default_factory = self._query_user,
|
|
|
|
parent = self
|
|
|
|
)
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
from .signal_manager import SignalManager
|
|
|
|
self._signal_manager: SignalManager = SignalManager(self)
|
|
|
|
|
2019-05-03 04:54:37 +10:00
|
|
|
self.clients.configLoad()
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
2019-05-03 04:31:47 +10:00
|
|
|
@pyqtProperty("QVariant", constant=True)
|
|
|
|
def htmlFilter(self):
|
|
|
|
return self._html_filter
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
@pyqtProperty("QVariant", constant=True)
|
2019-05-03 04:54:37 +10:00
|
|
|
def clients(self):
|
2019-04-12 18:33:09 +10:00
|
|
|
return self._client_manager
|
|
|
|
|
|
|
|
@pyqtProperty("QVariant", constant=True)
|
2019-05-03 04:31:47 +10:00
|
|
|
def accounts(self):
|
|
|
|
return self._accounts
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-04-18 00:49:54 +10:00
|
|
|
@pyqtProperty("QVariant", constant=True)
|
2019-05-03 04:31:47 +10:00
|
|
|
def roomEvents(self):
|
|
|
|
return self._room_events
|
2019-04-18 00:49:54 +10:00
|
|
|
|
2019-05-10 03:58:46 +10:00
|
|
|
@pyqtProperty("QVariant", constant=True)
|
2019-05-12 05:52:56 +10:00
|
|
|
def users(self):
|
|
|
|
return self._users
|
2019-05-10 03:58:46 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
@pyqtProperty("QVariant", constant=True)
|
|
|
|
def signals(self):
|
|
|
|
return self._signal_manager
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
def _query_user(self, user_id: str) -> User:
|
|
|
|
client = random.choice(tuple(self.clients.values())) # nosec
|
2019-04-15 02:56:30 +10:00
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
@futurize(running_value=user_id)
|
|
|
|
def get_displayname(self) -> str:
|
|
|
|
print("querying", user_id)
|
|
|
|
try:
|
|
|
|
response = client.net.talk(client.nio.get_displayname, user_id)
|
|
|
|
return response.displayname or user_id
|
|
|
|
except NioErrorResponse:
|
|
|
|
return user_id
|
2019-04-15 02:56:30 +10:00
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
return User(
|
|
|
|
userId = user_id,
|
|
|
|
displayName = get_displayname(self),
|
|
|
|
devices = ListModel(),
|
|
|
|
)
|
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:
|
2019-04-23 00:04:16 +10:00
|
|
|
# pylint:disable=no-self-use
|
|
|
|
return sum((ord(char) * 99 for char in string)) % 360 / 360
|
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-05-03 04:54:37 +10:00
|
|
|
for client in self.clients.values():
|
2019-04-18 11:08:32 +10:00
|
|
|
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
|
2019-04-21 07:36:21 +10:00
|
|
|
|
2019-05-12 07:13:53 +10:00
|
|
|
|
|
|
|
@pyqtSlot(str)
|
|
|
|
def setRoomFilter(self, pattern: str) -> None:
|
|
|
|
for account in self.accounts:
|
|
|
|
for categ in account.roomCategories:
|
|
|
|
categ.sortedRooms.filter = pattern
|
|
|
|
|
|
|
|
|
2019-05-07 11:28:54 +10:00
|
|
|
@staticmethod
|
2019-05-07 11:57:44 +10:00
|
|
|
def getDir(standard_dir: QStandardPaths.StandardLocation) -> str:
|
|
|
|
path = QStandardPaths.writableLocation(standard_dir)
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def getFile(self,
|
|
|
|
standard_dir: QStandardPaths.StandardLocation,
|
|
|
|
relative_file_path: str,
|
|
|
|
initial_content: Optional[str] = None) -> str:
|
|
|
|
|
|
|
|
relative_file_path = relative_file_path.replace("/", os.sep)
|
2019-05-07 11:28:54 +10:00
|
|
|
|
2019-05-07 11:57:44 +10:00
|
|
|
path = QStandardPaths.locate(standard_dir, relative_file_path)
|
2019-05-07 11:28:54 +10:00
|
|
|
if path:
|
|
|
|
return path
|
|
|
|
|
2019-05-07 11:57:44 +10:00
|
|
|
path = os.path.join(self.getDir(standard_dir), relative_file_path)
|
2019-05-07 11:28:54 +10:00
|
|
|
|
|
|
|
if initial_content is not None:
|
|
|
|
with AtomicFile(path, "w") as new:
|
|
|
|
new.write(initial_content)
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
@pyqtSlot()
|
|
|
|
@pyqtSlot(list)
|
|
|
|
def pdb(self, additional_data: Sequence = ()) -> None:
|
|
|
|
# pylint: disable=all
|
2019-05-07 03:07:00 +10:00
|
|
|
ad = additional_data
|
|
|
|
cl = self.clients
|
|
|
|
ac = self.accounts
|
|
|
|
re = self.roomEvents
|
2019-05-12 05:52:56 +10:00
|
|
|
us = self.users
|
2019-04-22 00:44:04 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
tcl = lambda user: cl[f"@test_{user}:matrix.org"]
|
2019-04-22 00:44:04 +10:00
|
|
|
|
2019-05-10 04:22:50 +10:00
|
|
|
import json
|
|
|
|
jd = lambda obj: print(json.dumps(obj, indent=4, ensure_ascii=False))
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
import pdb
|
|
|
|
from PyQt5.QtCore import pyqtRemoveInputHook
|
|
|
|
pyqtRemoveInputHook()
|
|
|
|
pdb.set_trace()
|