diff --git a/pantalaimon/client.py b/pantalaimon/client.py index 1707125..d9c53db 100644 --- a/pantalaimon/client.py +++ b/pantalaimon/client.py @@ -11,9 +11,9 @@ from nio.crypto import Sas from nio.store import SqliteStore from pantalaimon.log import logger -from pantalaimon.thread_messages import (DaemonResponse, DevicesMessage, - InviteSasSignal, SasDoneSignal, - ShowSasSignal) +from pantalaimon.thread_messages import (DaemonResponse, InviteSasSignal, + SasDoneSignal, ShowSasSignal, + UpdateDevicesMessage) class PanClient(AsyncClient): @@ -74,6 +74,10 @@ class PanClient(AsyncClient): """Send a thread message to the UI thread.""" await self.queue.put(message) + async def send_update_devcies(self): + message = UpdateDevicesMessage() + await self.queue.put(message) + async def sync_tasks(self, response): try: await asyncio.gather(*self.key_verificatins_tasks) @@ -97,8 +101,7 @@ class PanClient(AsyncClient): "user {}".format(device.id, user_id)) self.verify_device(device) - message = DevicesMessage(self.user_id, response.changed) - await self.queue.put(message) + await self.send_update_devcies() def undecrypted_event_cb(self, room, event): loop = asyncio.get_event_loop() @@ -170,6 +173,8 @@ class PanClient(AsyncClient): ) )) self.key_verificatins_tasks.append(task) + task = loop.create_task(self.send_update_devcies()) + self.key_verificatins_tasks.append(task) def start_loop(self): """Start a loop that runs forever and keeps on syncing with the server. @@ -327,6 +332,7 @@ class PanClient(AsyncClient): device = sas.other_olm_device if sas.verified: + await self.send_update_devcies() await self.send_message( SasDoneSignal( self.user_id, diff --git a/pantalaimon/daemon.py b/pantalaimon/daemon.py index 536047b..9dacfbd 100755 --- a/pantalaimon/daemon.py +++ b/pantalaimon/daemon.py @@ -17,13 +17,16 @@ from nio import EncryptionError, LoginResponse, SendRetryError from pantalaimon.client import PanClient from pantalaimon.log import logger from pantalaimon.store import ClientInfo, PanStore -from pantalaimon.thread_messages import (AcceptSasMessage, DaemonResponse, - ConfirmSasMessage, +from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage, + ConfirmSasMessage, DaemonResponse, + DeviceBlacklistMessage, + DeviceUnblacklistMessage, DeviceUnverifyMessage, DeviceVerifyMessage, ExportKeysMessage, ImportKeysMessage, SasMessage, StartSasMessage, - CancelSasMessage) + UpdateDevicesMessage, + UpdateUsersMessage) @attr.s @@ -91,10 +94,11 @@ class ProxyDaemon: if ret: msg = (f"Device {device.id} of user " - f"{device.user_id} succesfully verified") + f"{device.user_id} succesfully verified.") + await self.send_update_devcies() else: msg = (f"Device {device.id} of user " - f"{device.user_id} already verified") + f"{device.user_id} already verified.") logger.info(msg) await self.send_response(message_id, client.user_id, "m.ok", msg) @@ -104,7 +108,8 @@ class ProxyDaemon: if ret: msg = (f"Device {device.id} of user " - f"{device.user_id} succesfully unverified") + f"{device.user_id} succesfully unverified.") + await self.send_update_devcies() else: msg = (f"Device {device.id} of user " f"{device.user_id} already unverified") @@ -117,6 +122,10 @@ class ProxyDaemon: message = DaemonResponse(message_id, pan_user, code, message) await self.send_queue.put(message) + async def send_update_devcies(self): + message = UpdateDevicesMessage() + await self.send_queue.put(message) + async def receive_message(self, message): client = self.pan_clients.get(message.pan_user) diff --git a/pantalaimon/thread_messages.py b/pantalaimon/thread_messages.py index c19c2f6..3075712 100644 --- a/pantalaimon/thread_messages.py +++ b/pantalaimon/thread_messages.py @@ -15,9 +15,13 @@ class DaemonResponse(Message): @attr.s -class DevicesMessage(Message): - user_id = attr.ib() - devices = attr.ib() +class UpdateUsersMessage(Message): + pass + + +@attr.s +class UpdateDevicesMessage(Message): + pass @attr.s diff --git a/pantalaimon/ui.py b/pantalaimon/ui.py index c60c587..07c568d 100644 --- a/pantalaimon/ui.py +++ b/pantalaimon/ui.py @@ -1,21 +1,24 @@ +from collections import defaultdict from queue import Empty import attr from gi.repository import GLib -from nio.store import TrustState from pydbus import SessionBus from pydbus.generic import signal from pantalaimon.log import logger from pantalaimon.store import PanStore -from pantalaimon.thread_messages import (AcceptSasMessage, DaemonResponse, - ConfirmSasMessage, - DevicesMessage, DeviceUnverifyMessage, +from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage, + ConfirmSasMessage, DaemonResponse, + DeviceBlacklistMessage, + DeviceUnblacklistMessage, + DeviceUnverifyMessage, DeviceVerifyMessage, ExportKeysMessage, ImportKeysMessage, InviteSasSignal, SasDoneSignal, ShowSasSignal, StartSasMessage, - CancelSasMessage) + UpdateDevicesMessage, + UpdateUsersMessage) class IdCounter: @@ -180,10 +183,12 @@ class Devices: VerificationString = signal() VerificationDone = signal() - def __init__(self, queue, device_list, id_counter): - self.device_list = device_list + def __init__(self, queue, store, id_counter): + self.store = store + self.device_list = None self.queue = queue self.id_counter = id_counter + self.update_devices() @property def message_id(self): @@ -275,21 +280,8 @@ class Devices: self.queue.put(message) return message.message_id - def update_devices(self, message): - device_store = self.device_list[message.user_id] - - for user_id, device_dict in message.devices.items(): - for device in device_dict.values(): - if device.deleted: - device_store[user_id].remove(device.id, None) - else: - device_store[user_id][device.id] = { - "user_id": device.user_id, - "device_id": device.id, - "e225519": device.ed25519, - "curve25519": device.curve25519, - "trust_state": TrustState.unset.name, - } + def update_devices(self): + self.device_list = self.store.load_all_devices() @attr.s @@ -311,12 +303,11 @@ class GlibT: self.store = PanStore(self.data_dir) self.users = self.store.load_all_users() - self.devices = self.store.load_all_devices() id_counter = IdCounter() self.control_if = Control(self.send_queue, self.users, id_counter) - self.device_if = Devices(self.send_queue, self.devices, id_counter) + self.device_if = Devices(self.send_queue, self.store, id_counter) self.bus = SessionBus() self.bus.publish("org.pantalaimon1", self.control_if, self.device_if) @@ -329,8 +320,8 @@ class GlibT: logger.debug(f"UI loop received message {message}") - if isinstance(message, DevicesMessage): - self.device_if.update_devices(message) + if isinstance(message, UpdateDevicesMessage): + self.device_if.update_devices() elif isinstance(message, InviteSasSignal): self.device_if.VerificationInvite(