Line data Source code
1 : /* MIT License
2 : *
3 : * Copyright (C) 2019, 2020, 2021 Famedly GmbH
4 : *
5 : * Permission is hereby granted, free of charge, to any person obtaining a copy
6 : * of this software and associated documentation files (the "Software"), to deal
7 : * in the Software without restriction, including without limitation the rights
8 : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : * copies of the Software, and to permit persons to whom the Software is
10 : * furnished to do so, subject to the following conditions:
11 : *
12 : * The above copyright notice and this permission notice shall be included in all
13 : * copies or substantial portions of the Software.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 : * SOFTWARE.
22 : */
23 :
24 : import 'package:matrix/matrix_api_lite.dart';
25 :
26 : extension RoomEncryptedContentBasicEventExtension on BasicEvent {
27 24 : RoomEncryptedContent get parsedRoomEncryptedContent =>
28 48 : RoomEncryptedContent.fromJson(content);
29 : }
30 :
31 : class RoomEncryptedContent {
32 : String algorithm;
33 : String senderKey;
34 : String? deviceId;
35 : String? sessionId;
36 : String? ciphertextMegolm;
37 : Map<String, CiphertextInfo>? ciphertextOlm;
38 :
39 24 : RoomEncryptedContent.fromJson(Map<String, Object?> json)
40 24 : : algorithm = json.tryGet('algorithm', TryGet.required) ?? '',
41 24 : senderKey = json.tryGet('sender_key', TryGet.required) ?? '',
42 24 : deviceId = json.tryGet('device_id'),
43 24 : sessionId = json.tryGet('session_id'),
44 24 : ciphertextMegolm = json.tryGet('ciphertext', TryGet.silent),
45 : // filter out invalid/incomplete CiphertextInfos
46 : ciphertextOlm = json
47 24 : .tryGet<Map<String, Object?>>('ciphertext', TryGet.silent)
48 72 : ?.catchMap((k, v) => MapEntry(
49 24 : k, CiphertextInfo.fromJson(v as Map<String, Object?>)));
50 :
51 0 : Map<String, Object?> toJson() {
52 0 : final data = <String, Object?>{};
53 0 : data['algorithm'] = algorithm;
54 0 : data['sender_key'] = senderKey;
55 0 : if (deviceId != null) {
56 0 : data['device_id'] = deviceId;
57 : }
58 0 : if (sessionId != null) {
59 0 : data['session_id'] = sessionId;
60 : }
61 0 : if (ciphertextMegolm != null) {
62 0 : data['ciphertext'] = ciphertextMegolm;
63 : }
64 0 : if (ciphertextOlm != null) {
65 0 : data['ciphertext'] =
66 0 : ciphertextOlm!.map((k, v) => MapEntry(k, v.toJson()));
67 0 : if (ciphertextMegolm != null) {
68 0 : Logs().wtf(
69 : 'ciphertextOlm and ciphertextMegolm are both set, which should never happen!');
70 : }
71 : }
72 : return data;
73 : }
74 : }
75 :
76 : class CiphertextInfo {
77 : String body;
78 : int type;
79 :
80 24 : CiphertextInfo.fromJson(Map<String, Object?> json)
81 24 : : body = json['body'] as String,
82 24 : type = json['type'] as int;
83 :
84 0 : Map<String, Object?> toJson() {
85 0 : final data = <String, Object?>{};
86 0 : data['body'] = body;
87 0 : data['type'] = type;
88 : return data;
89 : }
90 : }
|