feat: Implement room key request parsing

This commit is contained in:
Christian Pauly 2021-01-08 09:54:11 +01:00
parent 81c86a6c05
commit fce7705243
3 changed files with 87 additions and 0 deletions

View File

@ -82,6 +82,7 @@ export 'src/model/auth/authentication_user_identifier.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';
export 'src/model/events/room_key_request_content.dart';
export 'src/model/events/secret_storage_default_key_content.dart';
export 'src/model/events/secret_storage_key_content.dart';
export 'src/model/events/tombstone_content.dart';

View File

@ -0,0 +1,58 @@
import '../basic_event.dart';
import '../../utils/try_get_map_extension.dart';
extension RoomKeyRequestContentBasicEventExtension on BasicEvent {
RoomKeyRequestContent get parsedRoomKeyRequestContent =>
RoomKeyRequestContent.fromJson(content);
}
class RoomKeyRequestContent {
RequestedKeyInfo body;
String action;
String requestingDeviceId;
String requestId;
RoomKeyRequestContent.fromJson(Map<String, dynamic> json)
: body = RequestedKeyInfo.fromJson(
json.tryGet<Map<String, dynamic>>('body')),
action = json.tryGet<String>('action', ''),
requestingDeviceId = json.tryGet<String>('requesting_device_id', ''),
requestId = json.tryGet<String>('request_id', '');
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
if (body != null) data['body'] = body.toJson();
data['action'] = action;
data['requesting_device_id'] = requestingDeviceId;
data['request_id'] = requestId;
return data;
}
}
class RequestedKeyInfo {
String algorithm;
String roomId;
String sessionId;
String senderKey;
RequestedKeyInfo();
factory RequestedKeyInfo.fromJson(Map<String, dynamic> json) {
if (json == null) return null;
final requestKeyInfo = RequestedKeyInfo();
requestKeyInfo.algorithm = json.tryGet<String>('algorithm', '');
requestKeyInfo.roomId = json.tryGet<String>('room_id', '');
requestKeyInfo.sessionId = json.tryGet<String>('session_id', '');
requestKeyInfo.senderKey = json.tryGet<String>('sender_key', '');
return requestKeyInfo;
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['algorithm'] = algorithm;
data['room_id'] = roomId;
data['session_id'] = sessionId;
data['sender_key'] = senderKey;
return data;
}
}

View File

@ -91,5 +91,33 @@ void main() {
expect(BasicEvent.fromJson(json).parsedRoomKeyContent.toJson(),
json['content']);
});
test('Room Key Request Content', () {
var json = <String, dynamic>{
'content': {
'action': 'request_cancellation',
'request_id': '1495474790150.19',
'requesting_device_id': 'RJYKSTBOIE'
},
'type': 'm.room_key_request'
};
expect(BasicEvent.fromJson(json).parsedRoomKeyRequestContent.toJson(),
json['content']);
json = <String, dynamic>{
'content': {
'action': 'request',
'body': {
'algorithm': 'm.megolm.v1.aes-sha2',
'room_id': '!Cuyf34gef24t:localhost',
'sender_key': 'RF3s+E7RkTQTGF2d8Deol0FkQvgII2aJDf3/Jp5mxVU',
'session_id': 'X3lUlvLELLYxeTx4yOVu6UDpasGEVO0Jbu+QFnm0cKQ'
},
'request_id': '1495474790150.19',
'requesting_device_id': 'RJYKSTBOIE'
},
'type': 'm.room_key_request'
};
expect(BasicEvent.fromJson(json).parsedRoomKeyRequestContent.toJson(),
json['content']);
});
});
}