2019-04-12 18:33:09 +10:00
|
|
|
# Copyright 2019 miruka
|
|
|
|
# This file is part of harmonyqml, licensed under GPLv3.
|
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2019-04-15 04:49:26 +10:00
|
|
|
from threading import Lock
|
2019-05-03 04:20:21 +10:00
|
|
|
from typing import Any, Deque, Dict, List, Optional
|
2019-04-13 22:59:10 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
from PyQt5.QtCore import QDateTime, QObject, pyqtBoundSignal, pyqtSignal
|
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-05-10 03:58:46 +10:00
|
|
|
from .model.items import (
|
2019-05-12 05:52:56 +10:00
|
|
|
Account, Device, ListModel, Room, RoomCategory, RoomEvent, User
|
2019-05-10 03:58:46 +10:00
|
|
|
)
|
2019-05-07 03:07:00 +10:00
|
|
|
from .model.sort_filter_proxy import SortFilterProxy
|
2019-05-12 05:52:56 +10:00
|
|
|
from .pyqt_future import futurize
|
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-05-07 04:06:28 +10:00
|
|
|
roomCategoryChanged = pyqtSignal(str, str, str, str)
|
|
|
|
|
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-05-12 05:52:56 +10:00
|
|
|
self.pool: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=6)
|
|
|
|
|
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-05-03 04:54:37 +10:00
|
|
|
self.backend.clients.clientAdded.connect(self.onClientAdded)
|
|
|
|
self.backend.clients.clientDeleted.connect(self.onClientDeleted)
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
|
|
|
def onClientAdded(self, client: Client) -> None:
|
2019-05-12 05:52:56 +10:00
|
|
|
if client.userId in self.backend.accounts:
|
|
|
|
return
|
|
|
|
|
|
|
|
# An user might already exist in the model, e.g. if another account
|
|
|
|
# was in a room with the account that we just connected to
|
|
|
|
self.backend.users.upsert(
|
|
|
|
where_main_key_is = client.userId,
|
|
|
|
update_with = User(
|
|
|
|
userId = client.userId,
|
|
|
|
displayName = self.backend.users[client.userId].displayName,
|
|
|
|
# Devices are added later, we might need to upload keys before
|
|
|
|
# but we want to show the accounts ASAP in the client side pane
|
|
|
|
devices = ListModel(),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Backend.accounts
|
2019-05-07 03:07:00 +10:00
|
|
|
room_categories_kwargs: List[Dict[str, Any]] = [
|
|
|
|
{"name": "Invites", "rooms": ListModel()},
|
|
|
|
{"name": "Rooms", "rooms": ListModel()},
|
|
|
|
{"name": "Left", "rooms": ListModel()},
|
|
|
|
]
|
|
|
|
|
|
|
|
for i, _ in enumerate(room_categories_kwargs):
|
|
|
|
proxy = SortFilterProxy(
|
2019-05-12 07:13:53 +10:00
|
|
|
source_model = room_categories_kwargs[i]["rooms"],
|
|
|
|
sort_by_role = "lastEventDateTime",
|
|
|
|
filter_by_role = "displayName",
|
|
|
|
ascending = False,
|
2019-05-07 03:07:00 +10:00
|
|
|
)
|
|
|
|
room_categories_kwargs[i]["sortedRooms"] = proxy
|
|
|
|
|
2019-05-03 04:31:47 +10:00
|
|
|
self.backend.accounts.append(Account(
|
2019-05-03 04:20:21 +10:00
|
|
|
userId = client.userId,
|
|
|
|
roomCategories = ListModel([
|
2019-05-07 03:07:00 +10:00
|
|
|
RoomCategory(**kws) for kws in room_categories_kwargs
|
2019-05-03 04:20:21 +10:00
|
|
|
]),
|
2019-04-12 18:33:09 +10:00
|
|
|
))
|
|
|
|
|
2019-05-10 03:58:46 +10:00
|
|
|
# Upload our E2E keys to the matrix server if needed
|
|
|
|
if not client.nio.olm_account_shared:
|
|
|
|
client.uploadE2EKeys()
|
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
# Add all devices nio knows for this account
|
2019-05-10 03:58:46 +10:00
|
|
|
store = client.nio.device_store
|
|
|
|
|
|
|
|
for user_id in store.users:
|
2019-05-12 05:52:56 +10:00
|
|
|
user = self.backend.users.get(user_id, None)
|
|
|
|
if not user:
|
|
|
|
self.backend.users.append(
|
|
|
|
User(userId=user_id, devices=ListModel())
|
|
|
|
)
|
|
|
|
|
|
|
|
for device in store.active_user_devices(user_id):
|
|
|
|
self.backend.users[client.userId].devices.upsert(
|
|
|
|
where_main_key_is = device.id,
|
|
|
|
update_with = Device(
|
|
|
|
deviceId = device.id,
|
|
|
|
ed25519Key = device.ed25519,
|
|
|
|
trust = client.getDeviceTrust(device),
|
|
|
|
)
|
|
|
|
)
|
2019-05-10 03:58:46 +10:00
|
|
|
|
|
|
|
# Finally, connect all client signals
|
|
|
|
self.connectClient(client)
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
def onClientDeleted(self, user_id: str) -> None:
|
2019-05-03 04:31:47 +10:00
|
|
|
del self.backend.accounts[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-05-03 04:20:21 +10:00
|
|
|
@staticmethod
|
|
|
|
def _get_room_displayname(nio_room: MatrixRoom) -> Optional[str]:
|
|
|
|
name = nio_room.name or nio_room.canonical_alias
|
|
|
|
if name:
|
|
|
|
return name
|
|
|
|
|
|
|
|
name = nio_room.group_name()
|
|
|
|
return None if name == "Empty room?" else name
|
|
|
|
|
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
def _add_users_from_nio_room(self, room: nio.rooms.MatrixRoom) -> None:
|
|
|
|
for user in room.users.values():
|
|
|
|
@futurize(running_value=user.display_name)
|
|
|
|
def get_displayname(self, user) -> str:
|
|
|
|
# pylint:disable=unused-argument
|
|
|
|
return user.display_name
|
|
|
|
|
|
|
|
self.backend.users.upsert(
|
|
|
|
where_main_key_is = user.user_id,
|
|
|
|
update_with = User(
|
|
|
|
userId = user.user_id,
|
|
|
|
displayName = get_displayname(self, user),
|
|
|
|
devices = ListModel()
|
|
|
|
),
|
|
|
|
ignore_roles = ("devices",),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
nio_room = client.nio.invited_rooms[room_id]
|
|
|
|
self._add_users_from_nio_room(nio_room)
|
|
|
|
|
2019-05-03 04:31:47 +10:00
|
|
|
categories = self.backend.accounts[client.userId].roomCategories
|
2019-05-03 04:20:21 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
previous_room = categories["Rooms"].rooms.pop(room_id, None)
|
|
|
|
previous_left = categories["Left"].rooms.pop(room_id, None)
|
2019-05-03 04:20:21 +10:00
|
|
|
|
2019-05-07 03:07:00 +10:00
|
|
|
categories["Invites"].rooms.upsert(
|
|
|
|
where_main_key_is = room_id,
|
|
|
|
update_with = Room(
|
|
|
|
roomId = room_id,
|
|
|
|
displayName = self._get_room_displayname(nio_room),
|
|
|
|
topic = nio_room.topic,
|
|
|
|
inviter = inviter,
|
|
|
|
lastEventDateTime = QDateTime.currentDateTime(), # FIXME
|
2019-05-12 05:52:56 +10:00
|
|
|
members = list(nio_room.users.keys()),
|
2019-05-07 03:07:00 +10:00
|
|
|
),
|
2019-05-12 05:52:56 +10:00
|
|
|
ignore_roles = ("typingMembers"),
|
2019-05-07 03:07:00 +10:00
|
|
|
)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
signal = self.roomCategoryChanged
|
|
|
|
if previous_room:
|
|
|
|
signal.emit(client.userId, room_id, "Rooms", "Invites")
|
|
|
|
elif previous_left:
|
|
|
|
signal.emit(client.userId, room_id, "Left", "Invites")
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
def onRoomJoined(self, client: Client, room_id: str) -> None:
|
2019-05-12 05:52:56 +10:00
|
|
|
nio_room = client.nio.rooms[room_id]
|
|
|
|
self._add_users_from_nio_room(nio_room)
|
|
|
|
|
2019-05-03 04:31:47 +10:00
|
|
|
categories = self.backend.accounts[client.userId].roomCategories
|
2019-05-03 04:20:21 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
previous_invite = categories["Invites"].rooms.pop(room_id, None)
|
|
|
|
previous_left = categories["Left"].rooms.pop(room_id, None)
|
2019-05-03 04:20:21 +10:00
|
|
|
|
2019-05-07 03:07:00 +10:00
|
|
|
categories["Rooms"].rooms.upsert(
|
|
|
|
where_main_key_is = room_id,
|
|
|
|
update_with = Room(
|
|
|
|
roomId = room_id,
|
|
|
|
displayName = self._get_room_displayname(nio_room),
|
|
|
|
topic = nio_room.topic,
|
2019-05-12 05:52:56 +10:00
|
|
|
members = list(nio_room.users.keys()),
|
2019-05-07 03:07:00 +10:00
|
|
|
),
|
2019-05-12 05:52:56 +10:00
|
|
|
ignore_roles = ("typingMembers", "lastEventDateTime"),
|
2019-05-07 03:07:00 +10:00
|
|
|
)
|
2019-04-22 05:20:20 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
signal = self.roomCategoryChanged
|
|
|
|
if previous_invite:
|
|
|
|
signal.emit(client.userId, room_id, "Invites", "Rooms")
|
|
|
|
elif previous_left:
|
|
|
|
signal.emit(client.userId, room_id, "Left", "Rooms")
|
|
|
|
|
2019-04-22 05:20:20 +10:00
|
|
|
|
|
|
|
def onRoomLeft(self,
|
|
|
|
client: Client,
|
|
|
|
room_id: str,
|
|
|
|
left_event: LeftEvent = None) -> None:
|
2019-05-03 04:31:47 +10:00
|
|
|
categories = self.backend.accounts[client.userId].roomCategories
|
2019-04-22 05:20:20 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
previous_room = categories["Rooms"].rooms.pop(room_id, None)
|
|
|
|
previous_invite = categories["Invites"].rooms.pop(room_id, None)
|
|
|
|
previous = previous_room or previous_invite or \
|
|
|
|
categories["Left"].rooms.get(room_id, None)
|
2019-04-22 10:55:24 +10:00
|
|
|
|
2019-05-07 03:07:00 +10:00
|
|
|
left_time = left_event.get("server_timestamp") if left_event else None
|
|
|
|
|
|
|
|
categories["Left"].rooms.upsert(
|
|
|
|
where_main_key_is = room_id,
|
|
|
|
update_with = Room(
|
|
|
|
roomId = room_id,
|
|
|
|
displayName = previous.displayName if previous else None,
|
|
|
|
topic = previous.topic if previous else None,
|
|
|
|
leftEvent = left_event,
|
|
|
|
lastEventDateTime = (
|
|
|
|
QDateTime.fromMSecsSinceEpoch(left_time)
|
|
|
|
if left_time else QDateTime.currentDateTime()
|
|
|
|
),
|
|
|
|
),
|
2019-05-12 05:52:56 +10:00
|
|
|
ignore_roles = ("members", "lastEventDateTime"),
|
2019-05-07 03:07:00 +10:00
|
|
|
)
|
2019-04-15 02:56:30 +10:00
|
|
|
|
2019-05-07 04:06:28 +10:00
|
|
|
signal = self.roomCategoryChanged
|
|
|
|
if previous_room:
|
|
|
|
signal.emit(client.userId, room_id, "Rooms", "Left")
|
|
|
|
elif previous_invite:
|
|
|
|
signal.emit(client.userId, room_id, "Invites", "Left")
|
|
|
|
|
|
|
|
|
|
|
|
|
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-05-07 03:07:00 +10:00
|
|
|
def _set_room_last_event(self, user_id: str, room_id: str, event: RoomEvent
|
|
|
|
) -> None:
|
|
|
|
for categ in self.backend.accounts[user_id].roomCategories:
|
|
|
|
if room_id in categ.rooms:
|
2019-05-14 03:20:39 +10:00
|
|
|
|
|
|
|
last = categ.rooms[room_id].lastEventDateTime
|
|
|
|
if last and last > event.dateTime:
|
|
|
|
continue
|
|
|
|
|
2019-05-07 03:07:00 +10:00
|
|
|
# Use setProperty to make sure to trigger model changed signals
|
|
|
|
categ.rooms.setProperty(
|
|
|
|
room_id, "lastEventDateTime", event.dateTime
|
|
|
|
)
|
2019-05-03 06:10:41 +10:00
|
|
|
|
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
def onRoomEventReceived(self,
|
2019-05-03 06:10:41 +10:00
|
|
|
client: Client,
|
2019-04-22 10:55:24 +10:00
|
|
|
room_id: str,
|
|
|
|
etype: str,
|
|
|
|
edict: Dict[str, Any]) -> None:
|
2019-05-03 06:10:41 +10:00
|
|
|
|
|
|
|
def process() -> Optional[RoomEvent]:
|
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:
|
2019-05-03 06:10:41 +10:00
|
|
|
return None
|
2019-04-15 04:49:26 +10:00
|
|
|
|
|
|
|
self.last_room_events.appendleft(edict["event_id"])
|
|
|
|
|
2019-05-03 04:31:47 +10:00
|
|
|
model = self.backend.roomEvents[room_id]
|
2019-04-19 03:46:39 +10:00
|
|
|
date_time = QDateTime\
|
|
|
|
.fromMSecsSinceEpoch(edict["server_timestamp"])
|
2019-05-15 09:39:31 +10:00
|
|
|
new_event = RoomEvent(
|
|
|
|
eventId = edict["event_id"],
|
|
|
|
type = etype,
|
|
|
|
dateTime = date_time,
|
|
|
|
dict = edict,
|
|
|
|
)
|
2019-04-19 03:46:39 +10:00
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
event_is_our_profile_changed = (
|
|
|
|
etype == "RoomMemberEvent" and
|
2019-05-03 04:54:37 +10:00
|
|
|
edict.get("sender") in self.backend.clients and
|
2019-05-03 04:20:21 +10:00
|
|
|
((edict.get("content") or {}).get("membership") ==
|
|
|
|
(edict.get("prev_content") or {}).get("membership"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if event_is_our_profile_changed:
|
2019-05-03 06:10:41 +10:00
|
|
|
return None
|
2019-05-03 04:20:21 +10:00
|
|
|
|
2019-04-27 06:52:26 +10:00
|
|
|
if etype == "RoomCreateEvent":
|
|
|
|
self.backend.fully_loaded_rooms.add(room_id)
|
|
|
|
|
2019-04-19 03:46:39 +10:00
|
|
|
if self._events_in_transfer:
|
|
|
|
local_echoes_met: int = 0
|
2019-05-07 03:07:00 +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
|
|
|
|
|
2019-05-14 03:15:03 +10:00
|
|
|
sb = (event.dict.get("sender"), event.dict.get("body"))
|
|
|
|
new_sb = (new_event.dict.get("sender"),
|
|
|
|
new_event.dict.get("body"))
|
2019-04-19 03:46:39 +10:00
|
|
|
|
|
|
|
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:
|
2019-05-15 09:06:59 +10:00
|
|
|
model.updateItem(update_at, new_event)
|
2019-04-19 03:46:39 +10:00
|
|
|
self._events_in_transfer -= 1
|
2019-05-03 06:10:41 +10:00
|
|
|
return new_event
|
2019-04-19 03:46:39 +10:00
|
|
|
|
|
|
|
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)
|
2019-05-03 06:10:41 +10:00
|
|
|
return new_event
|
2019-04-18 06:43:18 +10:00
|
|
|
|
2019-04-15 02:56:30 +10:00
|
|
|
model.append(new_event)
|
2019-05-03 06:10:41 +10:00
|
|
|
return new_event
|
2019-04-15 06:12:07 +10:00
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
with self._lock:
|
2019-05-03 06:10:41 +10:00
|
|
|
new_event = process()
|
|
|
|
if new_event:
|
2019-05-07 03:07:00 +10:00
|
|
|
self._set_room_last_event(client.userId, room_id, new_event)
|
2019-04-22 10:55:24 +10:00
|
|
|
|
2019-04-15 06:12:07 +10:00
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
def onRoomTypingMembersUpdated(self,
|
|
|
|
client: Client,
|
|
|
|
room_id: str,
|
|
|
|
users: List[str]) -> None:
|
2019-05-03 04:31:47 +10:00
|
|
|
categories = self.backend.accounts[client.userId].roomCategories
|
2019-05-03 04:20:21 +10:00
|
|
|
for categ in categories:
|
|
|
|
try:
|
2019-05-12 05:52:56 +10:00
|
|
|
categ.rooms.setProperty(room_id, "typingMembers", users)
|
2019-05-03 04:20:21 +10:00
|
|
|
break
|
|
|
|
except ValueError:
|
|
|
|
pass
|
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:
|
|
|
|
|
2019-05-15 05:03:12 +10:00
|
|
|
date_time = QDateTime.currentDateTime()
|
|
|
|
|
2019-04-22 10:55:24 +10:00
|
|
|
with self._lock:
|
2019-05-03 04:31:47 +10:00
|
|
|
model = self.backend.roomEvents[room_id]
|
2019-04-19 03:46:39 +10:00
|
|
|
nio_event = nio.events.RoomMessage.parse_event({
|
|
|
|
"event_id": "",
|
2019-04-21 07:36:21 +10:00
|
|
|
"sender": client.userId,
|
2019-05-15 05:03:12 +10:00
|
|
|
"origin_server_ts": date_time.toMSecsSinceEpoch(),
|
2019-04-19 03:46:39 +10:00
|
|
|
"content": content,
|
|
|
|
})
|
|
|
|
event = RoomEvent(
|
2019-05-15 09:39:31 +10:00
|
|
|
eventId = f"localEcho.{self._events_in_transfer + 1}",
|
2019-04-21 07:36:21 +10:00
|
|
|
type = type(nio_event).__name__,
|
|
|
|
dict = nio_event.__dict__,
|
2019-05-15 05:03:12 +10:00
|
|
|
dateTime = date_time,
|
2019-04-21 07:36:21 +10:00
|
|
|
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
|
|
|
|
2019-05-07 03:07:00 +10:00
|
|
|
self._set_room_last_event(client.userId, room_id, event)
|
2019-04-27 06:02:20 +10:00
|
|
|
|
|
|
|
|
|
|
|
def onRoomAboutToBeForgotten(self, client: Client, room_id: str) -> None:
|
2019-05-03 04:31:47 +10:00
|
|
|
categories = self.backend.accounts[client.userId].roomCategories
|
2019-05-03 04:20:21 +10:00
|
|
|
|
|
|
|
for categ in categories:
|
|
|
|
categ.rooms.pop(room_id, None)
|
|
|
|
|
2019-05-03 04:31:47 +10:00
|
|
|
self.backend.roomEvents[room_id].clear()
|
2019-05-10 05:54:31 +10:00
|
|
|
|
|
|
|
|
|
|
|
def onDeviceIsPresent(self,
|
|
|
|
client: Client,
|
|
|
|
user_id: str,
|
|
|
|
device_id: str,
|
|
|
|
ed25519_key: str) -> None:
|
2019-05-12 05:52:56 +10:00
|
|
|
|
2019-05-10 05:54:31 +10:00
|
|
|
nio_device = client.nio.device_store[user_id][device_id]
|
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
user = self.backend.users.get(user_id, None)
|
|
|
|
if not user:
|
|
|
|
self.backend.users.append(
|
|
|
|
User(userId=user_id, devices=ListModel())
|
|
|
|
)
|
|
|
|
|
|
|
|
self.backend.users[user_id].devices.upsert(
|
2019-05-10 05:54:31 +10:00
|
|
|
where_main_key_is = device_id,
|
|
|
|
update_with = Device(
|
|
|
|
deviceId = device_id,
|
|
|
|
ed25519Key = ed25519_key,
|
|
|
|
trust = client.getDeviceTrust(nio_device),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def onDeviceIsDeleted(self, _: Client, user_id: str, device_id: str
|
|
|
|
) -> None:
|
2019-05-12 05:52:56 +10:00
|
|
|
try:
|
|
|
|
del self.backend.users[user_id].devices[device_id]
|
|
|
|
except ValueError:
|
|
|
|
pass
|