Decrypt events in get_event. Fixes #123

This commit is contained in:
Tulir Asokan 2021-06-11 11:04:22 +03:00
parent 543f8dbf78
commit b2018d9ae9

View File

@ -23,7 +23,8 @@ from mautrix.client import Client as MatrixClient, SyncStream
from mautrix.util.formatter import MatrixParser, MarkdownString, EntityType from mautrix.util.formatter import MatrixParser, MarkdownString, EntityType
from mautrix.util import markdown from mautrix.util import markdown
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent, from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
MessageType, TextMessageEventContent, Format, RelatesTo) MessageType, TextMessageEventContent, Format, RelatesTo, EncryptedEvent)
from mautrix.errors import DecryptionError
class HumanReadableString(MarkdownString): class HumanReadableString(MarkdownString):
@ -132,10 +133,19 @@ class MaubotMatrixClient(MatrixClient):
return super().dispatch_event(event, source) return super().dispatch_event(event, source)
async def get_event(self, room_id: RoomID, event_id: EventID) -> Event: async def get_event(self, room_id: RoomID, event_id: EventID) -> Event:
event = await super().get_event(room_id, event_id) evt = await super().get_event(room_id, event_id)
if isinstance(event, MessageEvent): if isinstance(evt, EncryptedEvent) and self.crypto:
event.content.trim_reply_fallback() try:
return MaubotMessageEvent(event, self) self.crypto_log.trace(f"get_event: Decrypting {evt.event_id} in {evt.room_id}...")
decrypted = await self.crypto.decrypt_megolm_event(evt)
except DecryptionError as e:
self.crypto_log.warning(f"get_event: Failed to decrypt {evt.event_id}: {e}")
return
self.crypto_log.trace(f"get_event: Decrypted {evt.event_id}: {decrypted}")
evt = decrypted
if isinstance(evt, MessageEvent):
evt.content.trim_reply_fallback()
return MaubotMessageEvent(evt, self)
else: else:
event.client = self evt.client = self
return event return evt