fix: Auto-trust all devices for E2E decryption

Bot now trusts all room member devices on each sync, enabling
Megolm key exchange. Logs undecryptable events for debugging.

CF-1147

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Christian Gick
2026-02-15 08:01:14 +02:00
parent cbc61f1646
commit d7044b613c

19
bot.py
View File

@@ -7,6 +7,8 @@ from nio import (
AsyncClientConfig, AsyncClientConfig,
LoginResponse, LoginResponse,
InviteMemberEvent, InviteMemberEvent,
MegolmEvent,
SyncResponse,
KeyVerificationStart, KeyVerificationStart,
KeyVerificationCancel, KeyVerificationCancel,
KeyVerificationKey, KeyVerificationKey,
@@ -57,6 +59,8 @@ class Bot:
self.lkapi = api.LiveKitAPI(LK_URL, LK_KEY, LK_SECRET) self.lkapi = api.LiveKitAPI(LK_URL, LK_KEY, LK_SECRET)
self.client.add_event_callback(self.on_invite, InviteMemberEvent) self.client.add_event_callback(self.on_invite, InviteMemberEvent)
self.client.add_event_callback(self.on_megolm, MegolmEvent)
self.client.add_response_callback(self.on_sync, SyncResponse)
self.client.add_to_device_callback(self.on_key_verification, KeyVerificationStart) self.client.add_to_device_callback(self.on_key_verification, KeyVerificationStart)
self.client.add_to_device_callback(self.on_key_verification, KeyVerificationKey) self.client.add_to_device_callback(self.on_key_verification, KeyVerificationKey)
self.client.add_to_device_callback(self.on_key_verification, KeyVerificationMac) self.client.add_to_device_callback(self.on_key_verification, KeyVerificationMac)
@@ -84,6 +88,21 @@ class Bot:
except Exception: except Exception:
logger.exception("Dispatch failed for %s", room.room_id) logger.exception("Dispatch failed for %s", room.room_id)
async def on_sync(self, response: SyncResponse):
"""After each sync, trust all devices in our rooms."""
for user_id in self.client.device_store.users:
for device in self.client.device_store.active_user_devices(user_id):
if not device.verified:
self.client.verify_device(device)
logger.info("Auto-trusted device %s of %s", device.device_id, user_id)
async def on_megolm(self, room, event: MegolmEvent):
"""Handle undecryptable messages by requesting keys."""
logger.warning(
"Can't decrypt event %s in %s from %s (session %s)",
event.event_id, room.room_id, event.sender, event.session_id,
)
async def on_key_verification(self, event): async def on_key_verification(self, event):
"""Auto-accept key verification requests.""" """Auto-accept key verification requests."""
if isinstance(event, KeyVerificationStart): if isinstance(event, KeyVerificationStart):