Line data Source code
1 : /* 2 : * Famedly Matrix SDK 3 : * Copyright (C) 2019, 2020, 2021 Famedly GmbH 4 : * 5 : * This program is free software: you can redistribute it and/or modify 6 : * it under the terms of the GNU Affero General Public License as 7 : * published by the Free Software Foundation, either version 3 of the 8 : * License, or (at your option) any later version. 9 : * 10 : * This program is distributed in the hope that it will be useful, 11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 : * GNU Affero General Public License for more details. 14 : * 15 : * You should have received a copy of the GNU Affero General Public License 16 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 : */ 18 : 19 : import 'dart:convert'; 20 : 21 : import 'package:olm/olm.dart' as olm; 22 : 23 : import 'package:matrix/matrix.dart'; 24 : 25 : class OutboundGroupSession { 26 : /// The devices is a map from user id to device id to if the device is blocked. 27 : /// This way we can easily know if a new user is added, leaves, a new devices is added, and, 28 : /// very importantly, if we block a device. These are all important for determining if/when 29 : /// an outbound session needs to be rotated. 30 : Map<String, Map<String, bool>> devices = {}; 31 : // Default to a date, that would get this session rotated in any case to make handling easier 32 : DateTime creationTime = DateTime.fromMillisecondsSinceEpoch(0); 33 : olm.OutboundGroupSession? outboundGroupSession; 34 6 : int? get sentMessages => outboundGroupSession?.message_index(); 35 6 : bool get isValid => outboundGroupSession != null; 36 : final String key; 37 : 38 4 : OutboundGroupSession( 39 : {required this.devices, 40 : required this.creationTime, 41 : required this.outboundGroupSession, 42 : required this.key}); 43 : 44 2 : OutboundGroupSession.fromJson(Map<String, dynamic> dbEntry, this.key) { 45 : try { 46 7 : for (final entry in json.decode(dbEntry['device_ids']).entries) { 47 5 : devices[entry.key] = Map<String, bool>.from(entry.value); 48 : } 49 : } catch (e) { 50 : // devices is bad (old data), so just not use this session 51 0 : Logs().i( 52 0 : '[OutboundGroupSession] Session in database is old, not using it. $e'); 53 : return; 54 : } 55 4 : outboundGroupSession = olm.OutboundGroupSession(); 56 : try { 57 8 : outboundGroupSession!.unpickle(key, dbEntry['pickle']); 58 1 : creationTime = 59 2 : DateTime.fromMillisecondsSinceEpoch(dbEntry['creation_time']); 60 : } catch (e, s) { 61 1 : dispose(); 62 2 : Logs().e('[LibOlm] Unable to unpickle outboundGroupSession', e, s); 63 : } 64 : } 65 : 66 5 : void dispose() { 67 10 : outboundGroupSession?.free(); 68 5 : outboundGroupSession = null; 69 : } 70 : }