properly persist decryption of events
This commit is contained in:
parent
bb75ee00be
commit
384e5962d6
|
@ -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
|
||||||
|
|
|
@ -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,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -97,19 +97,26 @@ class Timeline {
|
||||||
sessionIdReceivedSub?.cancel();
|
sessionIdReceivedSub?.cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _sessionKeyReceived(String sessionId) {
|
void _sessionKeyReceived(String sessionId) async {
|
||||||
var decryptAtLeastOneEvent = false;
|
var decryptAtLeastOneEvent = false;
|
||||||
|
final decryptFn = () async {
|
||||||
for (var i = 0; i < events.length; i++) {
|
for (var i = 0; i < events.length; i++) {
|
||||||
if (events[i].type == EventTypes.Encrypted &&
|
if (events[i].type == EventTypes.Encrypted &&
|
||||||
events[i].messageType == MessageTypes.BadEncrypted &&
|
events[i].messageType == MessageTypes.BadEncrypted &&
|
||||||
events[i].content['body'] == DecryptError.UNKNOWN_SESSION &&
|
events[i].content['body'] == DecryptError.UNKNOWN_SESSION &&
|
||||||
events[i].content['session_id'] == sessionId) {
|
events[i].content['session_id'] == sessionId) {
|
||||||
events[i] = events[i].decrypted;
|
events[i] = await events[i].decryptAndStore();
|
||||||
if (events[i].type != EventTypes.Encrypted) {
|
if (events[i].type != EventTypes.Encrypted) {
|
||||||
decryptAtLeastOneEvent = true;
|
decryptAtLeastOneEvent = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
if (room.client.database != null) {
|
||||||
|
await room.client.database.transaction(decryptFn);
|
||||||
|
} else {
|
||||||
|
await decryptFn();
|
||||||
|
}
|
||||||
if (decryptAtLeastOneEvent) onUpdate();
|
if (decryptAtLeastOneEvent) onUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue