filter out invalid CiphertextInfo instead of crashing

This commit is contained in:
Marcus Hoffmann 2021-07-01 17:15:06 +02:00
parent 1a51f813a7
commit 34b9cc0dcf
1 changed files with 19 additions and 1 deletions

View File

@ -31,6 +31,14 @@ extension RoomEncryptedContentBasicEventExtension on BasicEvent {
RoomEncryptedContent.fromJson(content); RoomEncryptedContent.fromJson(content);
} }
/// Convert Nullable iterable of MapEntries to Map
extension ToMap<T1, T2> on Iterable<MapEntry<T1, T2>>? {
Map<T1, T2>? toMap() {
final that = this;
return that != null ? Map.fromEntries(that) : null;
}
}
class RoomEncryptedContent { class RoomEncryptedContent {
String algorithm; String algorithm;
String senderKey; String senderKey;
@ -45,9 +53,19 @@ class RoomEncryptedContent {
deviceId = json.tryGet('device_id'), deviceId = json.tryGet('device_id'),
sessionId = json.tryGet('session_id'), sessionId = json.tryGet('session_id'),
ciphertextMegolm = json.tryGet('ciphertext'), ciphertextMegolm = json.tryGet('ciphertext'),
// filter out invalid/incomplete CiphertextInfos
ciphertextOlm = json ciphertextOlm = json
.tryGet<Map<String, dynamic>>('ciphertext') .tryGet<Map<String, dynamic>>('ciphertext')
?.map((k, v) => MapEntry(k, CiphertextInfo.fromJson(v))); ?.entries
.map((e) {
try {
return MapEntry(e.key, CiphertextInfo.fromJson(e.value));
} catch (_) {
return null;
}
})
.whereType<MapEntry<String, CiphertextInfo>>()
.toMap();
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final data = <String, dynamic>{}; final data = <String, dynamic>{};