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:vodozemac/vodozemac.dart' as vod;
22 :
23 : import 'package:matrix/encryption/utils/pickle_key.dart';
24 : import 'package:matrix/matrix.dart';
25 :
26 : class OutboundGroupSession {
27 : /// The devices is a map from user id to device id to if the device is blocked.
28 : /// This way we can easily know if a new user is added, leaves, a new devices is added, and,
29 : /// very importantly, if we block a device. These are all important for determining if/when
30 : /// an outbound session needs to be rotated.
31 : Map<String, Map<String, bool>> devices = {};
32 : // Default to a date, that would get this session rotated in any case to make handling easier
33 : DateTime creationTime = DateTime.fromMillisecondsSinceEpoch(0);
34 : vod.GroupSession? outboundGroupSession;
35 9 : int? get sentMessages => outboundGroupSession?.messageIndex;
36 8 : bool get isValid => outboundGroupSession != null;
37 : final String key;
38 :
39 5 : OutboundGroupSession({
40 : required this.devices,
41 : required this.creationTime,
42 : required this.outboundGroupSession,
43 : required this.key,
44 : });
45 :
46 2 : OutboundGroupSession.fromJson(Map<String, dynamic> dbEntry, this.key) {
47 : try {
48 7 : for (final entry in json.decode(dbEntry['device_ids']).entries) {
49 5 : devices[entry.key] = Map<String, bool>.from(entry.value);
50 : }
51 : } catch (e) {
52 : // devices is bad (old data), so just not use this session
53 0 : Logs().i(
54 0 : '[OutboundGroupSession] Session in database is old, not using it. $e',
55 : );
56 : return;
57 : }
58 :
59 2 : creationTime =
60 4 : DateTime.fromMillisecondsSinceEpoch(dbEntry['creation_time']);
61 :
62 : try {
63 3 : outboundGroupSession = vod.GroupSession.fromPickleEncrypted(
64 4 : pickleKey: key.toPickleKey(),
65 2 : pickle: dbEntry['pickle'],
66 : );
67 : } catch (e, s) {
68 : try {
69 1 : outboundGroupSession = vod.GroupSession.fromOlmPickleEncrypted(
70 2 : pickleKey: utf8.encode(key),
71 1 : pickle: dbEntry['pickle'],
72 : );
73 : } catch (_) {
74 2 : Logs().e('[Vodozemac] Unable to unpickle outboundGroupSession', e, s);
75 : }
76 : }
77 : }
78 : }
|