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 9 : int? get sentMessages => outboundGroupSession?.message_index(); 35 8 : bool get isValid => outboundGroupSession != null; 36 : final String key; 37 : 38 5 : OutboundGroupSession({ 39 : required this.devices, 40 : required this.creationTime, 41 : required this.outboundGroupSession, 42 : required this.key, 43 : }); 44 : 45 2 : OutboundGroupSession.fromJson(Map<String, dynamic> dbEntry, this.key) { 46 : try { 47 7 : for (final entry in json.decode(dbEntry['device_ids']).entries) { 48 5 : devices[entry.key] = Map<String, bool>.from(entry.value); 49 : } 50 : } catch (e) { 51 : // devices is bad (old data), so just not use this session 52 0 : Logs().i( 53 0 : '[OutboundGroupSession] Session in database is old, not using it. $e', 54 : ); 55 : return; 56 : } 57 4 : outboundGroupSession = olm.OutboundGroupSession(); 58 : try { 59 8 : outboundGroupSession!.unpickle(key, dbEntry['pickle']); 60 1 : creationTime = 61 2 : DateTime.fromMillisecondsSinceEpoch(dbEntry['creation_time']); 62 : } catch (e, s) { 63 1 : dispose(); 64 2 : Logs().e('[LibOlm] Unable to unpickle outboundGroupSession', e, s); 65 : } 66 : } 67 : 68 6 : void dispose() { 69 12 : outboundGroupSession?.free(); 70 6 : outboundGroupSession = null; 71 : } 72 : }