ui: Update the device list in the ui thread dynamically.

This updates the copy of the device list in the ui thread every time the
device list or the verification of devices changes in the ui thread.
This commit is contained in:
Damir Jelić 2019-05-17 14:31:38 +02:00
parent 2ca850bf72
commit 2d487c55f5
4 changed files with 50 additions and 40 deletions

View File

@ -11,9 +11,9 @@ from nio.crypto import Sas
from nio.store import SqliteStore from nio.store import SqliteStore
from pantalaimon.log import logger from pantalaimon.log import logger
from pantalaimon.thread_messages import (DaemonResponse, DevicesMessage, from pantalaimon.thread_messages import (DaemonResponse, InviteSasSignal,
InviteSasSignal, SasDoneSignal, SasDoneSignal, ShowSasSignal,
ShowSasSignal) UpdateDevicesMessage)
class PanClient(AsyncClient): class PanClient(AsyncClient):
@ -74,6 +74,10 @@ class PanClient(AsyncClient):
"""Send a thread message to the UI thread.""" """Send a thread message to the UI thread."""
await self.queue.put(message) await self.queue.put(message)
async def send_update_devcies(self):
message = UpdateDevicesMessage()
await self.queue.put(message)
async def sync_tasks(self, response): async def sync_tasks(self, response):
try: try:
await asyncio.gather(*self.key_verificatins_tasks) await asyncio.gather(*self.key_verificatins_tasks)
@ -97,8 +101,7 @@ class PanClient(AsyncClient):
"user {}".format(device.id, user_id)) "user {}".format(device.id, user_id))
self.verify_device(device) self.verify_device(device)
message = DevicesMessage(self.user_id, response.changed) await self.send_update_devcies()
await self.queue.put(message)
def undecrypted_event_cb(self, room, event): def undecrypted_event_cb(self, room, event):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
@ -170,6 +173,8 @@ class PanClient(AsyncClient):
) )
)) ))
self.key_verificatins_tasks.append(task) self.key_verificatins_tasks.append(task)
task = loop.create_task(self.send_update_devcies())
self.key_verificatins_tasks.append(task)
def start_loop(self): def start_loop(self):
"""Start a loop that runs forever and keeps on syncing with the server. """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 device = sas.other_olm_device
if sas.verified: if sas.verified:
await self.send_update_devcies()
await self.send_message( await self.send_message(
SasDoneSignal( SasDoneSignal(
self.user_id, self.user_id,

View File

@ -17,13 +17,16 @@ from nio import EncryptionError, LoginResponse, SendRetryError
from pantalaimon.client import PanClient from pantalaimon.client import PanClient
from pantalaimon.log import logger from pantalaimon.log import logger
from pantalaimon.store import ClientInfo, PanStore from pantalaimon.store import ClientInfo, PanStore
from pantalaimon.thread_messages import (AcceptSasMessage, DaemonResponse, from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage,
ConfirmSasMessage, ConfirmSasMessage, DaemonResponse,
DeviceBlacklistMessage,
DeviceUnblacklistMessage,
DeviceUnverifyMessage, DeviceUnverifyMessage,
DeviceVerifyMessage, DeviceVerifyMessage,
ExportKeysMessage, ImportKeysMessage, ExportKeysMessage, ImportKeysMessage,
SasMessage, StartSasMessage, SasMessage, StartSasMessage,
CancelSasMessage) UpdateDevicesMessage,
UpdateUsersMessage)
@attr.s @attr.s
@ -91,10 +94,11 @@ class ProxyDaemon:
if ret: if ret:
msg = (f"Device {device.id} of user " 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: else:
msg = (f"Device {device.id} of user " msg = (f"Device {device.id} of user "
f"{device.user_id} already verified") f"{device.user_id} already verified.")
logger.info(msg) logger.info(msg)
await self.send_response(message_id, client.user_id, "m.ok", msg) await self.send_response(message_id, client.user_id, "m.ok", msg)
@ -104,7 +108,8 @@ class ProxyDaemon:
if ret: if ret:
msg = (f"Device {device.id} of user " 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: else:
msg = (f"Device {device.id} of user " msg = (f"Device {device.id} of user "
f"{device.user_id} already unverified") f"{device.user_id} already unverified")
@ -117,6 +122,10 @@ class ProxyDaemon:
message = DaemonResponse(message_id, pan_user, code, message) message = DaemonResponse(message_id, pan_user, code, message)
await self.send_queue.put(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): async def receive_message(self, message):
client = self.pan_clients.get(message.pan_user) client = self.pan_clients.get(message.pan_user)

View File

@ -15,9 +15,13 @@ class DaemonResponse(Message):
@attr.s @attr.s
class DevicesMessage(Message): class UpdateUsersMessage(Message):
user_id = attr.ib() pass
devices = attr.ib()
@attr.s
class UpdateDevicesMessage(Message):
pass
@attr.s @attr.s

View File

@ -1,21 +1,24 @@
from collections import defaultdict
from queue import Empty from queue import Empty
import attr import attr
from gi.repository import GLib from gi.repository import GLib
from nio.store import TrustState
from pydbus import SessionBus from pydbus import SessionBus
from pydbus.generic import signal from pydbus.generic import signal
from pantalaimon.log import logger from pantalaimon.log import logger
from pantalaimon.store import PanStore from pantalaimon.store import PanStore
from pantalaimon.thread_messages import (AcceptSasMessage, DaemonResponse, from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage,
ConfirmSasMessage, ConfirmSasMessage, DaemonResponse,
DevicesMessage, DeviceUnverifyMessage, DeviceBlacklistMessage,
DeviceUnblacklistMessage,
DeviceUnverifyMessage,
DeviceVerifyMessage, DeviceVerifyMessage,
ExportKeysMessage, ImportKeysMessage, ExportKeysMessage, ImportKeysMessage,
InviteSasSignal, SasDoneSignal, InviteSasSignal, SasDoneSignal,
ShowSasSignal, StartSasMessage, ShowSasSignal, StartSasMessage,
CancelSasMessage) UpdateDevicesMessage,
UpdateUsersMessage)
class IdCounter: class IdCounter:
@ -180,10 +183,12 @@ class Devices:
VerificationString = signal() VerificationString = signal()
VerificationDone = signal() VerificationDone = signal()
def __init__(self, queue, device_list, id_counter): def __init__(self, queue, store, id_counter):
self.device_list = device_list self.store = store
self.device_list = None
self.queue = queue self.queue = queue
self.id_counter = id_counter self.id_counter = id_counter
self.update_devices()
@property @property
def message_id(self): def message_id(self):
@ -275,21 +280,8 @@ class Devices:
self.queue.put(message) self.queue.put(message)
return message.message_id return message.message_id
def update_devices(self, message): def update_devices(self):
device_store = self.device_list[message.user_id] self.device_list = self.store.load_all_devices()
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,
}
@attr.s @attr.s
@ -311,12 +303,11 @@ class GlibT:
self.store = PanStore(self.data_dir) self.store = PanStore(self.data_dir)
self.users = self.store.load_all_users() self.users = self.store.load_all_users()
self.devices = self.store.load_all_devices()
id_counter = IdCounter() id_counter = IdCounter()
self.control_if = Control(self.send_queue, self.users, id_counter) 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 = SessionBus()
self.bus.publish("org.pantalaimon1", self.control_if, self.device_if) 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}") logger.debug(f"UI loop received message {message}")
if isinstance(message, DevicesMessage): if isinstance(message, UpdateDevicesMessage):
self.device_if.update_devices(message) self.device_if.update_devices()
elif isinstance(message, InviteSasSignal): elif isinstance(message, InviteSasSignal):
self.device_if.VerificationInvite( self.device_if.VerificationInvite(