properly persist decryption of events

This commit is contained in:
Sorunome 2020-05-22 11:15:48 +00:00 committed by Christian Pauly
parent bb75ee00be
commit 384e5962d6
3 changed files with 38 additions and 24 deletions

View file

@ -425,6 +425,25 @@ class Event {
/// if it fails and does nothing if the event was not encrypted. /// if it fails and does nothing if the event was not encrypted.
Event get decrypted => room.decryptGroupMessage(this); Event get decrypted => room.decryptGroupMessage(this);
/// Trys to decrypt this event and persists it in the database afterwards
Future<Event> decryptAndStore([String updateType = 'timeline']) async {
final newEvent = decrypted;
if (newEvent.type == EventTypes.Encrypted || room.client.database == null) {
return newEvent; // decryption failed or we don't have a database
}
await room.client.database.storeEventUpdate(
room.client.id,
EventUpdate(
eventType: newEvent.typeKey,
content: newEvent.toJson(),
roomID: newEvent.roomId,
type: updateType,
sortOrder: newEvent.sortOrder,
),
);
return newEvent;
}
/// If this event is encrypted and the decryption was not successful because /// If this event is encrypted and the decryption was not successful because
/// the session is unknown, this requests the session key from other devices /// the session is unknown, this requests the session key from other devices
/// in the room. If the event is not encrypted or the decryption failed because /// in the room. If the event is not encrypted or the decryption failed because

View file

@ -271,9 +271,9 @@ class Room {
onSessionKeyReceived.add(sessionId); onSessionKeyReceived.add(sessionId);
} }
void _tryAgainDecryptLastMessage() { Future<void> _tryAgainDecryptLastMessage() async {
if (getState('m.room.encrypted') != null) { if (getState('m.room.encrypted') != null) {
final decrypted = getState('m.room.encrypted').decrypted; final decrypted = await getState('m.room.encrypted').decryptAndStore();
if (decrypted.type != EventTypes.Encrypted) { if (decrypted.type != EventTypes.Encrypted) {
setState(decrypted); setState(decrypted);
} }
@ -1207,19 +1207,7 @@ class Room {
if (events[i].type == EventTypes.Encrypted && if (events[i].type == EventTypes.Encrypted &&
events[i].content['body'] == DecryptError.UNKNOWN_SESSION) { events[i].content['body'] == DecryptError.UNKNOWN_SESSION) {
await events[i].loadSession(); await events[i].loadSession();
events[i] = events[i].decrypted; events[i] = await events[i].decryptAndStore();
if (events[i].type != EventTypes.Encrypted) {
await client.database.storeEventUpdate(
client.id,
EventUpdate(
eventType: events[i].typeKey,
content: events[i].toJson(),
roomID: events[i].roomId,
type: 'timeline',
sortOrder: events[i].sortOrder,
),
);
}
} }
} }
}); });

View file

@ -97,18 +97,25 @@ class Timeline {
sessionIdReceivedSub?.cancel(); sessionIdReceivedSub?.cancel();
} }
void _sessionKeyReceived(String sessionId) { void _sessionKeyReceived(String sessionId) async {
var decryptAtLeastOneEvent = false; var decryptAtLeastOneEvent = false;
for (var i = 0; i < events.length; i++) { final decryptFn = () async {
if (events[i].type == EventTypes.Encrypted && for (var i = 0; i < events.length; i++) {
events[i].messageType == MessageTypes.BadEncrypted && if (events[i].type == EventTypes.Encrypted &&
events[i].content['body'] == DecryptError.UNKNOWN_SESSION && events[i].messageType == MessageTypes.BadEncrypted &&
events[i].content['session_id'] == sessionId) { events[i].content['body'] == DecryptError.UNKNOWN_SESSION &&
events[i] = events[i].decrypted; events[i].content['session_id'] == sessionId) {
if (events[i].type != EventTypes.Encrypted) { events[i] = await events[i].decryptAndStore();
decryptAtLeastOneEvent = true; if (events[i].type != EventTypes.Encrypted) {
decryptAtLeastOneEvent = true;
}
} }
} }
};
if (room.client.database != null) {
await room.client.database.transaction(decryptFn);
} else {
await decryptFn();
} }
if (decryptAtLeastOneEvent) onUpdate(); if (decryptAtLeastOneEvent) onUpdate();
} }