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 'package:matrix/matrix.dart'; 20 : 21 : enum EventUpdateType { 22 : /// Newly received events from /sync 23 : timeline, 24 : 25 : /// A state update not visible in the timeline currently 26 : state, 27 : 28 : /// Messages that have been fetched when requesting past history 29 : history, 30 : 31 : /// Updates to account data 32 : accountData, 33 : 34 : /// Ephemeral events like receipts 35 : ephemeral, 36 : 37 : /// The state of an invite 38 : inviteState, 39 : 40 : /// Events that came down timeline, but we only received the keys for it later so we send a second update for them in the decrypted state 41 : decryptedTimelineQueue, 42 : } 43 : 44 : /// Represents a new event (e.g. a message in a room) or an update for an 45 : /// already known event. 46 : class EventUpdate { 47 : /// Usually 'timeline', 'state' or whatever. 48 : final EventUpdateType type; 49 : 50 : /// Most events belong to a room. If not, this equals to eventType. 51 : final String roomID; 52 : 53 : // The json payload of the content of this event. 54 : final Map<String, dynamic> content; 55 : 56 36 : EventUpdate({ 57 : required this.roomID, 58 : required this.type, 59 : required this.content, 60 : }); 61 : 62 2 : Future<EventUpdate> decrypt(Room room, {bool store = false}) async { 63 4 : final encryption = room.client.encryption; 64 6 : if (content['type'] != EventTypes.Encrypted || 65 4 : !room.client.encryptionEnabled || 66 : encryption == null) { 67 : return this; 68 : } 69 : try { 70 2 : final decrpytedEvent = await encryption.decryptRoomEvent( 71 2 : room.id, 72 4 : Event.fromJson(content, room), 73 : store: store, 74 2 : updateType: type, 75 : ); 76 2 : return EventUpdate( 77 2 : roomID: roomID, 78 2 : type: type, 79 2 : content: decrpytedEvent.toJson(), 80 : ); 81 : } catch (e, s) { 82 0 : Logs().e('[LibOlm] Could not decrypt megolm event', e, s); 83 : return this; 84 : } 85 : } 86 : }