famedlysdk/lib/src/utils/event_update.dart

76 lines
2.4 KiB
Dart
Raw Normal View History

2019-06-09 11:57:33 +00:00
/*
2020-06-03 10:16:01 +00:00
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-06-09 11:57:33 +00:00
*/
import '../../famedlysdk.dart';
2020-06-03 10:16:01 +00:00
import '../../matrix_api.dart';
2020-08-06 09:35:02 +00:00
import 'logs.dart';
2020-10-22 10:21:20 +00:00
enum EventUpdateType {
timeline,
state,
history,
accountData,
ephemeral,
inviteState
}
2019-06-09 10:16:48 +00:00
/// Represents a new event (e.g. a message in a room) or an update for an
/// already known event.
class EventUpdate {
/// Usually 'timeline', 'state' or whatever.
2020-10-22 10:21:20 +00:00
final EventUpdateType type;
2019-06-09 10:16:48 +00:00
/// Most events belong to a room. If not, this equals to eventType.
final String roomID;
/// See (Matrix Room Events)[https://matrix.org/docs/spec/client_server/r0.4.0.html#room-events]
/// and (Matrix Events)[https://matrix.org/docs/spec/client_server/r0.4.0.html#id89] for more
/// informations.
2019-06-21 10:18:54 +00:00
final String eventType;
2019-06-09 10:16:48 +00:00
// The json payload of the content of this event.
2019-06-26 17:03:15 +00:00
final Map<String, dynamic> content;
2019-06-09 10:16:48 +00:00
2020-05-15 18:40:17 +00:00
// the order where to stort this event
final double sortOrder;
2020-05-22 10:12:18 +00:00
EventUpdate(
{this.eventType, this.roomID, this.type, this.content, this.sortOrder});
Future<EventUpdate> decrypt(Room room, {bool store = false}) async {
if (eventType != EventTypes.Encrypted || !room.client.encryptionEnabled) {
return this;
}
try {
var decrpytedEvent = await room.client.encryption.decryptRoomEvent(
room.id, Event.fromJson(content, room, sortOrder),
store: store, updateType: type);
return EventUpdate(
2020-06-03 10:16:01 +00:00
eventType: decrpytedEvent.type,
roomID: roomID,
type: type,
content: decrpytedEvent.toJson(),
2020-05-15 18:40:17 +00:00
sortOrder: sortOrder,
);
2020-08-06 09:35:02 +00:00
} catch (e, s) {
Logs.error('[LibOlm] Could not decrypt megolm event: ' + e.toString(), s);
return this;
}
}
2019-06-09 10:16:48 +00:00
}