feat: Implement plaintext parsing
This commit is contained in:
parent
618d42a448
commit
ae34041f29
|
|
@ -80,6 +80,7 @@ export 'src/model/auth/authentication_token.dart';
|
|||
export 'src/model/auth/authentication_types.dart';
|
||||
export 'src/model/auth/authentication_user_identifier.dart';
|
||||
export 'src/model/events/forwarded_room_key_content.dart';
|
||||
export 'src/model/events/olm_plaintext_payload.dart';
|
||||
export 'src/model/events/room_encrypted_content.dart';
|
||||
export 'src/model/events/room_encryption_content.dart';
|
||||
export 'src/model/events/room_key_content.dart';
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
import '../../utils/try_get_map_extension.dart';
|
||||
|
||||
class OlmPlaintextPayload {
|
||||
String type;
|
||||
Map<String, dynamic> content;
|
||||
String sender;
|
||||
String recipient;
|
||||
Map<String, String> recipientKeys;
|
||||
Map<String, String> keys;
|
||||
|
||||
OlmPlaintextPayload({
|
||||
this.type,
|
||||
this.content,
|
||||
this.sender,
|
||||
this.recipient,
|
||||
this.recipientKeys,
|
||||
this.keys,
|
||||
}) : super();
|
||||
|
||||
factory OlmPlaintextPayload.fromJson(Map<String, dynamic> json) =>
|
||||
OlmPlaintextPayload(
|
||||
sender: json.tryGet<String>('sender'),
|
||||
type: json.tryGet<String>('type'),
|
||||
content: json.tryGetMap<String, dynamic>('content'),
|
||||
recipient: json.tryGet<String>('recipient'),
|
||||
recipientKeys: json.tryGetMap<String, String>('recipient_keys'),
|
||||
keys: json.tryGetMap<String, String>('keys'),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
if (type != null) data['type'] = type;
|
||||
if (sender != null) data['sender'] = sender;
|
||||
if (content != null) data['content'] = content;
|
||||
if (recipient != null) data['recipient'] = recipient;
|
||||
if (recipientKeys != null) data['recipient_keys'] = recipientKeys;
|
||||
if (keys != null) data['keys'] = keys;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ extension TryGetMapExtension on Map<String, dynamic> {
|
|||
final value = this[key];
|
||||
if (value != null && !(value is List)) {
|
||||
Logs().w(
|
||||
'Expected "${T.runtimeType}" in event content for the key "$key" but got "${value.runtimeType}".');
|
||||
'Expected "List<${T.runtimeType}>" in event content for the key "$key" but got "${value.runtimeType}".');
|
||||
return fallbackValue;
|
||||
}
|
||||
if (value == null && fallbackValue != null) {
|
||||
|
|
@ -29,10 +29,31 @@ extension TryGetMapExtension on Map<String, dynamic> {
|
|||
return fallbackValue;
|
||||
}
|
||||
try {
|
||||
return List<T>.from(value as List<dynamic>);
|
||||
return List<T>.from(value);
|
||||
} catch (_) {
|
||||
Logs().w(
|
||||
'Unable to create List<${T.runtimeType}> in event content for the key "$key');
|
||||
'Unable to create "List<${T.runtimeType}>" in event content for the key "$key"');
|
||||
return fallbackValue;
|
||||
}
|
||||
}
|
||||
|
||||
Map<A, B> tryGetMap<A, B>(String key, [Map<A, B> fallbackValue]) {
|
||||
final value = this[key];
|
||||
if (value != null && !(value is Map)) {
|
||||
Logs().w(
|
||||
'Expected "Map<${A.runtimeType},${B.runtimeType}>" in event content for the key "$key" but got "${value.runtimeType}".');
|
||||
return fallbackValue;
|
||||
}
|
||||
if (value == null && fallbackValue != null) {
|
||||
Logs().w(
|
||||
'Required field in event content for the key "$key" is null. Set to "$fallbackValue".');
|
||||
return fallbackValue;
|
||||
}
|
||||
try {
|
||||
return Map<A, B>.from(value);
|
||||
} catch (_) {
|
||||
Logs().w(
|
||||
'Unable to create "Map<${A.runtimeType},${B.runtimeType}>" in event content for the key "$key"');
|
||||
return fallbackValue;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -146,5 +146,20 @@ void main() {
|
|||
expect(BasicEvent.fromJson(json).parsedForwardedRoomKeyContent.toJson(),
|
||||
json['content']);
|
||||
});
|
||||
test('OLM Plaintext Payload', () {
|
||||
var json = <String, dynamic>{
|
||||
'type': '<type of the plaintext event>',
|
||||
'content': <String, dynamic>{
|
||||
'msgtype': 'm.text',
|
||||
'body': 'Hello world',
|
||||
},
|
||||
'sender': '<sender_user_id>',
|
||||
'recipient': '<recipient_user_id>',
|
||||
'recipient_keys': {'ed25519': '<our_ed25519_key>'},
|
||||
'keys': {'ed25519': '<sender_ed25519_key>'}
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
expect(OlmPlaintextPayload.fromJson(json).toJson(), json);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue