2019-04-12 18:33:09 +10:00
|
|
|
# Copyright 2019 miruka
|
|
|
|
# This file is part of harmonyqml, licensed under GPLv3.
|
|
|
|
|
2019-04-15 04:49:26 +10:00
|
|
|
from threading import Lock
|
2019-04-15 06:12:07 +10:00
|
|
|
from typing import Any, Deque, Dict, List, Optional
|
2019-04-13 22:59:10 +10:00
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
from PyQt5.QtCore import QDateTime, QObject, pyqtBoundSignal
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
from .backend import Backend
|
|
|
|
from .client import Client
|
2019-04-15 02:56:30 +10:00
|
|
|
from .model.items import Room, RoomEvent, User
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
|
|
|
class SignalManager(QObject):
|
2019-04-15 04:49:26 +10:00
|
|
|
_duplicate_check_lock: Lock = Lock()
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
def __init__(self, backend: Backend) -> None:
|
2019-04-15 02:56:30 +10:00
|
|
|
super().__init__(parent=backend)
|
2019-04-12 18:33:09 +10:00
|
|
|
self.backend = backend
|
|
|
|
|
2019-04-15 04:49:26 +10:00
|
|
|
self.last_room_events: Deque[str] = Deque(maxlen=1000)
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
cm = self.backend.clientManager
|
|
|
|
cm.clientAdded.connect(self.onClientAdded)
|
|
|
|
cm.clientDeleted.connect(self.onClientDeleted)
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
|
|
|
def onClientAdded(self, client: Client) -> None:
|
2019-04-13 03:18:46 +10:00
|
|
|
self.connectClient(client)
|
2019-04-12 18:33:09 +10:00
|
|
|
self.backend.models.accounts.append(User(
|
2019-04-13 03:18:46 +10:00
|
|
|
user_id = client.userID,
|
|
|
|
display_name = client.userID.lstrip("@").split(":")[0],
|
2019-04-12 18:33:09 +10:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
def onClientDeleted(self, user_id: str) -> None:
|
|
|
|
accs = self.backend.models.accounts
|
|
|
|
del accs[accs.indexWhere("user_id", user_id)]
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
|
|
|
def connectClient(self, client: Client) -> None:
|
2019-04-15 02:56:30 +10:00
|
|
|
for name in dir(client):
|
|
|
|
attr = getattr(client, name)
|
|
|
|
|
|
|
|
if isinstance(attr, pyqtBoundSignal):
|
|
|
|
def onSignal(*args, name=name) -> None:
|
|
|
|
func = getattr(self, f"on{name[0].upper()}{name[1:]}")
|
|
|
|
func(client, *args)
|
|
|
|
|
|
|
|
attr.connect(onSignal)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
|
|
|
def onRoomInvited(self, client: Client, room_id: str) -> None:
|
|
|
|
pass # TODO
|
|
|
|
|
|
|
|
|
|
|
|
def onRoomJoined(self, client: Client, room_id: str) -> None:
|
2019-04-13 22:59:10 +10:00
|
|
|
model = self.backend.models.rooms[client.userID]
|
|
|
|
room = client.nio.rooms[room_id]
|
|
|
|
|
|
|
|
def group_name() -> Optional[str]:
|
|
|
|
name = room.group_name()
|
|
|
|
return None if name == "Empty room?" else name
|
2019-04-13 03:18:46 +10:00
|
|
|
|
2019-04-13 22:59:10 +10:00
|
|
|
item = Room(
|
2019-04-13 03:18:46 +10:00
|
|
|
room_id = room_id,
|
2019-04-13 22:59:10 +10:00
|
|
|
display_name = room.name or room.canonical_alias or group_name(),
|
2019-04-13 03:18:46 +10:00
|
|
|
description = getattr(room, "topic", ""), # FIXME: outside init
|
2019-04-13 22:59:10 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
index = model.indexWhere("room_id", room_id)
|
|
|
|
except ValueError:
|
|
|
|
model.append(item)
|
|
|
|
else:
|
|
|
|
model[index] = item
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
|
|
|
def onRoomLeft(self, client: Client, room_id: str) -> None:
|
|
|
|
rooms = self.backend.models.rooms[client.userID]
|
|
|
|
del rooms[rooms.indexWhere("room_id", room_id)]
|
2019-04-15 02:56:30 +10:00
|
|
|
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
def onRoomSyncPrevBatchTokenReceived(
|
|
|
|
self, _: Client, room_id: str, token: str
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
if room_id not in self.backend.past_tokens:
|
|
|
|
self.backend.past_tokens[room_id] = token
|
|
|
|
|
|
|
|
|
|
|
|
def onRoomPastPrevBatchTokenReceived(
|
|
|
|
self, _: Client, room_id: str, token: str
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
self.backend.past_tokens[room_id] = token
|
|
|
|
|
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
def onRoomEventReceived(
|
|
|
|
self, _: Client, room_id: str, etype: str, edict: Dict[str, Any]
|
|
|
|
) -> None:
|
2019-04-15 04:49:26 +10:00
|
|
|
|
|
|
|
# Prevent duplicate events in models due to multiple accounts
|
|
|
|
with self._duplicate_check_lock:
|
|
|
|
if edict["event_id"] in self.last_room_events:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.last_room_events.appendleft(edict["event_id"])
|
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
model = self.backend.models.roomEvents[room_id]
|
|
|
|
date_time = QDateTime.fromMSecsSinceEpoch(edict["server_timestamp"])
|
|
|
|
new_event = RoomEvent(type=etype, date_time=date_time, dict=edict)
|
|
|
|
|
2019-04-18 06:43:18 +10:00
|
|
|
# Model is sorted from newest to oldest message
|
|
|
|
insert_at = None
|
|
|
|
for i, event in enumerate(model):
|
|
|
|
if new_event.date_time > event.date_time:
|
|
|
|
insert_at = i
|
|
|
|
break
|
|
|
|
|
|
|
|
if insert_at is None:
|
2019-04-15 02:56:30 +10:00
|
|
|
model.append(new_event)
|
|
|
|
else:
|
2019-04-18 06:43:18 +10:00
|
|
|
model.insert(insert_at, new_event)
|
2019-04-15 06:12:07 +10:00
|
|
|
|
|
|
|
|
|
|
|
def onRoomTypingUsersUpdated(
|
|
|
|
self, client: Client, room_id: str, users: List[str]
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
rooms = self.backend.models.rooms[client.userID]
|
|
|
|
rooms[rooms.indexWhere("room_id", room_id)].typing_users = users
|