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 34 : EventUpdate( 57 : {required this.roomID, required this.type, required this.content}); 58 : 59 2 : Future<EventUpdate> decrypt(Room room, {bool store = false}) async { 60 4 : final encryption = room.client.encryption; 61 6 : if (content['type'] != EventTypes.Encrypted || 62 4 : !room.client.encryptionEnabled || 63 : encryption == null) { 64 : return this; 65 : } 66 : try { 67 2 : final decrpytedEvent = await encryption.decryptRoomEvent( 68 6 : room.id, Event.fromJson(content, room), 69 2 : store: store, updateType: type); 70 2 : return EventUpdate( 71 6 : roomID: roomID, type: type, content: decrpytedEvent.toJson()); 72 : } catch (e, s) { 73 0 : Logs().e('[LibOlm] Could not decrypt megolm event', e, s); 74 : return this; 75 : } 76 : } 77 : }