From 2cea10b01a7b11254a79f4af96309a3c13739a73 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 7 Aug 2019 09:53:22 +0200 Subject: [PATCH] [State] Add state event class --- lib/src/State.dart | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lib/src/State.dart diff --git a/lib/src/State.dart b/lib/src/State.dart new file mode 100644 index 0000000..0eaae0a --- /dev/null +++ b/lib/src/State.dart @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * This file is part of famedlysdk. + * + * famedlysdk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * famedlysdk 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with famedlysdk. If not, see . + */ +import 'package:famedlysdk/src/utils/ChatTime.dart'; + +import './Room.dart'; +import './RawEvent.dart'; + +class State extends RawEvent { + /// Optional. The previous content for this state. + /// This will be present only for state events appearing in the timeline. + /// If this is not a state event, or there is no previous content, this key will be null. + final Map prevContent; + + /// Optional. This key will only be present for state events. A unique key which defines + /// the overwriting semantics for this piece of room state. + final String stateKey; + + State( + {this.prevContent, + this.stateKey, + dynamic content, + String typeKey, + String eventId, + String roomId, + String sender, + ChatTime time, + dynamic unsigned, + Room room}) + : super( + content: content, + typeKey: typeKey, + eventId: eventId, + roomId: roomId, + sender: sender, + time: time, + unsigned: unsigned, + room: room); + + /// Get a State event from a table row or from the event stream. + factory State.fromJson(Map jsonPayload, Room room) { + final Map content = + RawEvent.getMapFromPayload(jsonPayload['content']); + final Map unsigned = + RawEvent.getMapFromPayload(jsonPayload['unsigned']); + final Map prevContent = + RawEvent.getMapFromPayload(jsonPayload['prev_content']); + return State( + stateKey: jsonPayload['state_key'], + prevContent: prevContent, + content: content, + typeKey: jsonPayload['type'], + eventId: jsonPayload['event_id'], + roomId: jsonPayload['room_id'], + sender: jsonPayload['sender'], + time: ChatTime(jsonPayload['origin_server_ts']), + unsigned: unsigned, + room: room); + } + + /// The unique key of this event. For events with a [stateKey], it will be the + /// stateKey. Otherwise it will be the [type] as a string. + String get key => stateKey != null || stateKey.isEmpty ? type : stateKey; +}