famedlysdk/lib/src/RoomState.dart

111 lines
3.5 KiB
Dart
Raw Normal View History

2019-08-07 07:53:22 +00:00
/*
* Copyright (c) 2019 Zender & Kurtz GbR.
*
* Authors:
* Christian Pauly <krille@famedly.com>
* Marcel Radzio <mtrnord@famedly.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
2019-08-07 08:46:59 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2019-08-07 07:53:22 +00:00
import 'package:famedlysdk/src/utils/ChatTime.dart';
import './Room.dart';
import './RawEvent.dart';
2019-08-08 10:51:07 +00:00
class RoomState extends RawEvent {
2019-08-07 07:53:22 +00:00
/// 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<String, dynamic> 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;
2019-08-29 08:29:00 +00:00
User get stateKeyUser => room.states[stateKey].asUser ?? User(stateKey);
2019-08-07 08:46:59 +00:00
2019-08-08 10:51:07 +00:00
RoomState(
2019-08-07 07:53:22 +00:00
{this.prevContent,
this.stateKey,
dynamic content,
String typeKey,
String eventId,
String roomId,
2019-08-07 08:46:59 +00:00
String senderId,
2019-08-07 07:53:22 +00:00
ChatTime time,
dynamic unsigned,
Room room})
: super(
content: content,
typeKey: typeKey,
eventId: eventId,
roomId: roomId,
2019-08-07 08:46:59 +00:00
senderId: senderId,
2019-08-07 07:53:22 +00:00
time: time,
unsigned: unsigned,
room: room);
/// Get a State event from a table row or from the event stream.
2019-08-08 10:51:07 +00:00
factory RoomState.fromJson(Map<String, dynamic> jsonPayload, Room room) {
2019-08-07 07:53:22 +00:00
final Map<String, dynamic> content =
RawEvent.getMapFromPayload(jsonPayload['content']);
final Map<String, dynamic> unsigned =
RawEvent.getMapFromPayload(jsonPayload['unsigned']);
final Map<String, dynamic> prevContent =
RawEvent.getMapFromPayload(jsonPayload['prev_content']);
2019-08-08 10:51:07 +00:00
return RoomState(
2019-08-07 07:53:22 +00:00
stateKey: jsonPayload['state_key'],
prevContent: prevContent,
content: content,
typeKey: jsonPayload['type'],
eventId: jsonPayload['event_id'],
roomId: jsonPayload['room_id'],
2019-08-07 08:46:59 +00:00
senderId: jsonPayload['sender'],
2019-08-07 07:53:22 +00:00
time: ChatTime(jsonPayload['origin_server_ts']),
unsigned: unsigned,
room: room);
}
2019-08-07 10:27:02 +00:00
Event get timelineEvent => Event(
content: content,
typeKey: typeKey,
eventId: eventId,
room: room,
roomId: roomId,
senderId: senderId,
time: time,
unsigned: unsigned,
status: 1,
);
2019-08-07 07:53:22 +00:00
/// 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.
2019-08-07 10:27:02 +00:00
String get key => stateKey == null || stateKey.isEmpty ? typeKey : stateKey;
2019-08-08 08:31:39 +00:00
2019-08-08 09:54:39 +00:00
User get asUser => User.fromState(
2019-08-08 08:31:39 +00:00
stateKey: stateKey,
prevContent: prevContent,
content: content,
typeKey: typeKey,
eventId: eventId,
roomId: roomId,
senderId: senderId,
time: time,
unsigned: unsigned,
room: room);
2019-08-07 07:53:22 +00:00
}