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-22 10:55:24 +10:00
|
|
|
from typing import Any, Deque, Dict, List, Optional, Sequence
|
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
|
|
|
|
2019-04-19 03:46:39 +10:00
|
|
|
import nio
|
2019-04-21 20:56:59 +10:00
|
|
|
from nio.rooms import MatrixRoom
|
2019-04-19 03:46:39 +10:00
|
|
|
|
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
|
|
|
|
2019-04-22 05:20:20 +10:00
|
|
|
Inviter = Optional[Dict[str, str]]
|
|
|
|
LeftEvent = Optional[Dict[str, str]]
|
2019-04-22 01:15:03 +10:00
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
class SignalManager(QObject):
|
2019-04-22 10:55:24 +10:00
|
|
|
_lock: Lock = Lock()
|
2019-04-15 04:49:26 +10:00
|
|
|
|
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-22 05:20:20 +10:00
|
|
|
self.last_room_events: Deque[str] = Deque(maxlen=1000)
|
|
|
|
self._events_in_transfer: int = 0
|
2019-04-15 04:49:26 +10:00
|
|
|
|
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-21 07:36:21 +10:00
|
|
|
userId = client.userId,
|
|
|
|
displayName = self.backend.getUserDisplayName(client.userId),
|
2019-04-12 18:33:09 +10:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
def onClientDeleted(self, user_id: str) -> None:
|
|
|
|
accs = self.backend.models.accounts
|
2019-04-21 07:36:21 +10:00
|
|
|
del accs[accs.indexWhere("userId", 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
|
|
|
|
|
|
|
|
2019-04-22 01:15:03 +10:00
|
|
|
def onRoomInvited(self,
|
|
|
|
client: Client,
|
|
|
|
room_id: str,
|
|
|
|
inviter: Inviter = None) -> None:
|
2019-04-22 05:20:20 +10:00
|
|
|
|
|
|
|
self._add_room(client, room_id, client.nio.invited_rooms[room_id],
|
|
|
|
"Invites", inviter=inviter)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
|
|
|
def onRoomJoined(self, client: Client, room_id: str) -> None:
|
2019-04-22 05:20:20 +10:00
|
|
|
self._add_room(client, room_id, client.nio.rooms[room_id], "Rooms")
|
|
|
|
|
|
|
|
|
|
|
|
def onRoomLeft(self,
|
|
|
|
client: Client,
|
|
|
|
room_id: str,
|
|
|
|
left_event: LeftEvent = None) -> None:
|
|
|
|
|
|
|
|
self._add_room(client, room_id, client.nio.rooms.get(room_id), "Left",
|
|
|
|
left_event=left_event)
|
2019-04-21 20:56:59 +10:00
|
|
|
|
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
def _move_room(self, account_id: str, room_id: str) -> None:
|
|
|
|
def get_newest_event_date_time(room_id: str) -> QDateTime:
|
|
|
|
for ev in self.backend.models.roomEvents[room_id]:
|
|
|
|
if not self.backend.EventIsOurProfileChanged(ev, account_id):
|
|
|
|
return ev.dateTime
|
|
|
|
|
|
|
|
return QDateTime.fromMSecsSinceEpoch(0)
|
|
|
|
|
|
|
|
rooms_model = self.backend.models.rooms[account_id]
|
|
|
|
room_index = rooms_model.indexWhere("roomId", room_id)
|
|
|
|
category = rooms_model[room_index].category
|
|
|
|
timestamp = get_newest_event_date_time(room_id)
|
|
|
|
|
|
|
|
def get_index(put_before_categories: Sequence[str],
|
|
|
|
put_after_categories: Sequence[str]) -> int:
|
|
|
|
for i, room in enumerate(rooms_model):
|
|
|
|
if room.category not in put_after_categories and \
|
|
|
|
(room.category in put_before_categories or
|
|
|
|
timestamp >= get_newest_event_date_time(room.roomId)):
|
|
|
|
return i
|
|
|
|
|
|
|
|
return len(rooms_model) - 1
|
|
|
|
|
|
|
|
to = 0
|
|
|
|
|
|
|
|
if category == "Invites":
|
|
|
|
to = get_index(["Rooms", "Left"], [])
|
|
|
|
|
|
|
|
if category == "Rooms":
|
|
|
|
to = get_index(["Left"], ["Invites"])
|
|
|
|
|
|
|
|
elif category == "Left":
|
|
|
|
to = get_index([], ["Invites", "Rooms", "Left"])
|
|
|
|
|
|
|
|
rooms_model.move(room_index, to)
|
|
|
|
|
|
|
|
|
2019-04-22 01:15:03 +10:00
|
|
|
def _add_room(self,
|
2019-04-22 05:20:20 +10:00
|
|
|
client: Client,
|
|
|
|
room_id: str,
|
|
|
|
room: MatrixRoom,
|
|
|
|
category: str,
|
|
|
|
inviter: Inviter = None,
|
|
|
|
left_event: LeftEvent = None) -> None:
|
|
|
|
|
|
|
|
assert not (inviter and left_event)
|
|
|
|
|
|
|
|
model = self.backend.models.rooms[client.userId]
|
|
|
|
no_update = []
|
|
|
|
|
|
|
|
def get_displayname() -> Optional[str]:
|
|
|
|
if not room:
|
|
|
|
no_update.append("displayName")
|
|
|
|
return room_id
|
|
|
|
|
|
|
|
name = room.name or room.canonical_alias
|
|
|
|
if name:
|
|
|
|
return name
|
2019-04-13 22:59:10 +10:00
|
|
|
|
|
|
|
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-22 05:20:20 +10:00
|
|
|
roomId = room_id,
|
2019-04-21 20:56:59 +10:00
|
|
|
category = category,
|
2019-04-22 05:20:20 +10:00
|
|
|
displayName = get_displayname(),
|
|
|
|
topic = room.topic if room else "",
|
2019-04-22 01:15:03 +10:00
|
|
|
inviter = inviter,
|
2019-04-22 05:20:20 +10:00
|
|
|
leftEvent = left_event,
|
|
|
|
no_update = no_update,
|
2019-04-13 22:59:10 +10:00
|
|
|
)
|
|
|
|
|
2019-04-22 05:20:20 +10:00
|
|
|
model.updateOrAppendWhere("roomId", room_id, item)
|
2019-04-22 10:55:24 +10:00
|
|
|
with self._lock:
|
|
|
|
self._move_room(client.userId, room_id)
|
2019-04-22 06:53:16 +10:00
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
def onRoomSyncPrevBatchTokenReceived(self,
|
|
|
|
_: Client,
|
|
|
|
room_id: str,
|
|
|
|
token: str) -> None:
|
2019-04-18 11:08:32 +10:00
|
|
|
|
|
|
|
if room_id not in self.backend.past_tokens:
|
|
|
|
self.backend.past_tokens[room_id] = token
|
|
|
|
|
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
def onRoomPastPrevBatchTokenReceived(self,
|
|
|
|
_: Client,
|
|
|
|
room_id: str,
|
|
|
|
token: str) -> None:
|
2019-04-18 11:08:32 +10:00
|
|
|
|
2019-04-18 12:34:22 +10:00
|
|
|
if self.backend.past_tokens[room_id] == token:
|
|
|
|
self.backend.fully_loaded_rooms.add(room_id)
|
|
|
|
|
2019-04-18 11:08:32 +10:00
|
|
|
self.backend.past_tokens[room_id] = token
|
|
|
|
|
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
def onRoomEventReceived(self,
|
|
|
|
client: Client,
|
|
|
|
room_id: str,
|
|
|
|
etype: str,
|
|
|
|
edict: Dict[str, Any]) -> None:
|
|
|
|
def process() -> None:
|
2019-04-19 03:46:39 +10:00
|
|
|
# Prevent duplicate events in models due to multiple accounts
|
2019-04-15 04:49:26 +10:00
|
|
|
if edict["event_id"] in self.last_room_events:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.last_room_events.appendleft(edict["event_id"])
|
|
|
|
|
2019-04-19 03:46:39 +10:00
|
|
|
model = self.backend.models.roomEvents[room_id]
|
|
|
|
date_time = QDateTime\
|
|
|
|
.fromMSecsSinceEpoch(edict["server_timestamp"])
|
2019-04-21 07:36:21 +10:00
|
|
|
new_event = RoomEvent(type=etype, dateTime=date_time, dict=edict)
|
2019-04-19 03:46:39 +10:00
|
|
|
|
|
|
|
if self._events_in_transfer:
|
|
|
|
local_echoes_met: int = 0
|
2019-04-21 07:36:21 +10:00
|
|
|
update_at: Optional[int] = None
|
2019-04-19 03:46:39 +10:00
|
|
|
|
|
|
|
# Find if any locally echoed event corresponds to new_event
|
|
|
|
for i, event in enumerate(model):
|
2019-04-21 07:36:21 +10:00
|
|
|
if not event.isLocalEcho:
|
2019-04-19 03:46:39 +10:00
|
|
|
continue
|
|
|
|
|
|
|
|
sb = (event.dict["sender"], event.dict["body"])
|
|
|
|
new_sb = (new_event.dict["sender"], new_event.dict["body"])
|
|
|
|
|
|
|
|
if sb == new_sb:
|
|
|
|
# The oldest matching local echo shall be replaced
|
2019-04-21 07:36:21 +10:00
|
|
|
update_at = max(update_at or 0, i)
|
2019-04-19 03:46:39 +10:00
|
|
|
|
|
|
|
local_echoes_met += 1
|
|
|
|
if local_echoes_met >= self._events_in_transfer:
|
|
|
|
break
|
2019-04-15 02:56:30 +10:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
if update_at is not None:
|
|
|
|
model.update(update_at, new_event)
|
2019-04-19 03:46:39 +10:00
|
|
|
self._events_in_transfer -= 1
|
|
|
|
return
|
|
|
|
|
|
|
|
for i, event in enumerate(model):
|
2019-04-21 07:36:21 +10:00
|
|
|
if event.isLocalEcho:
|
2019-04-19 03:46:39 +10:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Model is sorted from newest to oldest message
|
2019-04-21 07:36:21 +10:00
|
|
|
if new_event.dateTime > event.dateTime:
|
2019-04-19 03:46:39 +10:00
|
|
|
model.insert(i, new_event)
|
|
|
|
return
|
2019-04-18 06:43:18 +10:00
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
model.append(new_event)
|
2019-04-15 06:12:07 +10:00
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
with self._lock:
|
|
|
|
process()
|
|
|
|
self._move_room(client.userId, room_id)
|
|
|
|
|
2019-04-15 06:12:07 +10:00
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
def onRoomTypingUsersUpdated(self,
|
|
|
|
client: Client,
|
|
|
|
room_id: str,
|
|
|
|
users: List[str]) -> None:
|
2019-04-15 06:12:07 +10:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
rooms = self.backend.models.rooms[client.userId]
|
|
|
|
rooms[rooms.indexWhere("roomId", room_id)].typingUsers = users
|
2019-04-19 03:46:39 +10:00
|
|
|
|
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
def onMessageAboutToBeSent(self,
|
|
|
|
client: Client,
|
|
|
|
room_id: str,
|
|
|
|
content: Dict[str, str]) -> None:
|
|
|
|
|
|
|
|
with self._lock:
|
2019-04-19 03:46:39 +10:00
|
|
|
timestamp = QDateTime.currentMSecsSinceEpoch()
|
|
|
|
model = self.backend.models.roomEvents[room_id]
|
|
|
|
nio_event = nio.events.RoomMessage.parse_event({
|
|
|
|
"event_id": "",
|
2019-04-21 07:36:21 +10:00
|
|
|
"sender": client.userId,
|
2019-04-19 03:46:39 +10:00
|
|
|
"origin_server_ts": timestamp,
|
|
|
|
"content": content,
|
|
|
|
})
|
|
|
|
event = RoomEvent(
|
2019-04-21 07:36:21 +10:00
|
|
|
type = type(nio_event).__name__,
|
|
|
|
dateTime = QDateTime.fromMSecsSinceEpoch(timestamp),
|
|
|
|
dict = nio_event.__dict__,
|
|
|
|
isLocalEcho = True,
|
2019-04-19 03:46:39 +10:00
|
|
|
)
|
|
|
|
model.insert(0, event)
|
|
|
|
self._events_in_transfer += 1
|
2019-04-22 10:55:24 +10:00
|
|
|
|
|
|
|
self._move_room(client.userId, room_id)
|