LCOV - code coverage report
Current view: top level - lib/matrix_api_lite/model/events - room_encrypted_content.dart (source / functions) Coverage Total Hit
Test: merged.info Lines: 44.1 % 34 15
Test Date: 2025-01-14 13:39:53 Functions: - 0 0

            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           24 :             ?.catchMap(
      49           48 :               (k, v) => MapEntry(
      50              :                 k,
      51           24 :                 CiphertextInfo.fromJson(v as Map<String, Object?>),
      52              :               ),
      53              :             );
      54              : 
      55            0 :   Map<String, Object?> toJson() {
      56            0 :     final data = <String, Object?>{};
      57            0 :     data['algorithm'] = algorithm;
      58            0 :     data['sender_key'] = senderKey;
      59            0 :     if (deviceId != null) {
      60            0 :       data['device_id'] = deviceId;
      61              :     }
      62            0 :     if (sessionId != null) {
      63            0 :       data['session_id'] = sessionId;
      64              :     }
      65            0 :     if (ciphertextMegolm != null) {
      66            0 :       data['ciphertext'] = ciphertextMegolm;
      67              :     }
      68            0 :     if (ciphertextOlm != null) {
      69            0 :       data['ciphertext'] =
      70            0 :           ciphertextOlm!.map((k, v) => MapEntry(k, v.toJson()));
      71            0 :       if (ciphertextMegolm != null) {
      72            0 :         Logs().wtf(
      73              :           'ciphertextOlm and ciphertextMegolm are both set, which should never happen!',
      74              :         );
      75              :       }
      76              :     }
      77              :     return data;
      78              :   }
      79              : }
      80              : 
      81              : class CiphertextInfo {
      82              :   String body;
      83              :   int type;
      84              : 
      85           24 :   CiphertextInfo.fromJson(Map<String, Object?> json)
      86           24 :       : body = json['body'] as String,
      87           24 :         type = json['type'] as int;
      88              : 
      89            0 :   Map<String, Object?> toJson() {
      90            0 :     final data = <String, Object?>{};
      91            0 :     data['body'] = body;
      92            0 :     data['type'] = type;
      93              :     return data;
      94              :   }
      95              : }
        

Generated by: LCOV version 2.0-1