[Event] Remove RawEvent

This commit is contained in:
Christian Pauly 2019-08-29 11:50:57 +02:00
parent 47b708b6ba
commit 2314fa0e09
8 changed files with 138 additions and 208 deletions

View file

@ -33,7 +33,6 @@ export 'package:famedlysdk/src/utils/MxContent.dart';
export 'package:famedlysdk/src/Client.dart';
export 'package:famedlysdk/src/Connection.dart';
export 'package:famedlysdk/src/Event.dart';
export 'package:famedlysdk/src/RawEvent.dart';
export 'package:famedlysdk/src/Room.dart';
export 'package:famedlysdk/src/RoomList.dart';
export 'package:famedlysdk/src/RoomState.dart';

View file

@ -21,7 +21,7 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:famedlysdk/src/RawEvent.dart';
import 'package:famedlysdk/src/RoomState.dart';
class AccountData {
/// The json payload of the content. The content highly depends on the type.
@ -35,7 +35,7 @@ class AccountData {
/// Get a State event from a table row or from the event stream.
factory AccountData.fromJson(Map<String, dynamic> jsonPayload) {
final Map<String, dynamic> content =
RawEvent.getMapFromPayload(jsonPayload['content']);
RoomState.getMapFromPayload(jsonPayload['content']);
return AccountData(content: content, typeKey: jsonPayload['type']);
}
}

View file

@ -26,7 +26,6 @@ import 'package:famedlysdk/src/sync/EventUpdate.dart';
import 'package:famedlysdk/src/utils/ChatTime.dart';
import './Room.dart';
import './RawEvent.dart';
/// Defines a timeline event for a room.
class Event extends RoomState {
@ -66,11 +65,11 @@ class Event extends RoomState {
/// Get a State event from a table row or from the event stream.
factory Event.fromJson(Map<String, dynamic> jsonPayload, Room room) {
final Map<String, dynamic> content =
RawEvent.getMapFromPayload(jsonPayload['content']);
RoomState.getMapFromPayload(jsonPayload['content']);
final Map<String, dynamic> unsigned =
RawEvent.getMapFromPayload(jsonPayload['unsigned']);
RoomState.getMapFromPayload(jsonPayload['unsigned']);
final Map<String, dynamic> prevContent =
RawEvent.getMapFromPayload(jsonPayload['prev_content']);
RoomState.getMapFromPayload(jsonPayload['prev_content']);
return Event(
status: jsonPayload['status'] ?? defaultStatus,
content: content,

View file

@ -22,7 +22,7 @@
*/
import 'package:famedlysdk/src/AccountData.dart';
import 'package:famedlysdk/src/RawEvent.dart';
import 'package:famedlysdk/src/RoomState.dart';
class Presence extends AccountData {
/// The user who has sent this event if it is not a global account data event.
@ -34,7 +34,7 @@ class Presence extends AccountData {
/// Get a State event from a table row or from the event stream.
factory Presence.fromJson(Map<String, dynamic> jsonPayload) {
final Map<String, dynamic> content =
RawEvent.getMapFromPayload(jsonPayload['content']);
RoomState.getMapFromPayload(jsonPayload['content']);
return Presence(
content: content,
typeKey: jsonPayload['type'],

View file

@ -1,170 +0,0 @@
/*
* 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/>.
*/
import 'dart:convert';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/src/utils/ChatTime.dart';
import './Room.dart';
class RawEvent {
/// The Matrix ID for this event in the format '$localpart:server.abc'. Please not
/// that account data, presence and other events may not have an eventId.
final String eventId;
/// The json payload of the content. The content highly depends on the type.
final Map<String, dynamic> content;
/// The type String of this event. For example 'm.room.message'.
final String typeKey;
/// The ID of the room this event belongs to.
final String roomId;
/// The user who has sent this event if it is not a global account data event.
final String senderId;
User get sender => room.states[senderId]?.asUser ?? User(senderId);
/// The time this event has received at the server. May be null for events like
/// account data.
final ChatTime time;
/// Optional additional content for this event.
final Map<String, dynamic> unsigned;
/// The room this event belongs to. May be null.
final Room room;
RawEvent(
{this.content,
this.typeKey,
this.eventId,
this.roomId,
this.senderId,
this.time,
this.unsigned,
this.room});
static Map<String, dynamic> getMapFromPayload(dynamic payload) {
if (payload is String)
try {
return json.decode(payload);
} catch (e) {
return {};
}
if (payload is Map<String, dynamic>) return payload;
return {};
}
/// Get a State event from a table row or from the event stream.
factory RawEvent.fromJson(Map<String, dynamic> jsonPayload, Room room) {
final Map<String, dynamic> content =
getMapFromPayload(jsonPayload['content']);
final Map<String, dynamic> unsigned =
getMapFromPayload(jsonPayload['unsigned']);
return RawEvent(
content: content,
typeKey: jsonPayload['type'],
eventId: jsonPayload['event_id'],
roomId: jsonPayload['room_id'],
senderId: jsonPayload['sender'],
time: ChatTime(jsonPayload['origin_server_ts']),
unsigned: unsigned,
room: room);
}
/// Get the real type.
EventTypes get type {
switch (typeKey) {
case "m.room.avatar":
return EventTypes.RoomAvatar;
case "m.room.name":
return EventTypes.RoomName;
case "m.room.topic":
return EventTypes.RoomTopic;
case "m.room.Aliases":
return EventTypes.RoomAliases;
case "m.room.canonical_alias":
return EventTypes.RoomCanonicalAlias;
case "m.room.create":
return EventTypes.RoomCreate;
case "m.room.join_rules":
return EventTypes.RoomJoinRules;
case "m.room.member":
return EventTypes.RoomMember;
case "m.room.power_levels":
return EventTypes.RoomPowerLevels;
case "m.room.guest_access":
return EventTypes.GuestAccess;
case "m.room.history_visibility":
return EventTypes.HistoryVisibility;
case "m.room.message":
switch (content["msgtype"] ?? "m.text") {
case "m.text":
if (content.containsKey("m.relates_to")) {
return EventTypes.Reply;
}
return EventTypes.Text;
case "m.notice":
return EventTypes.Notice;
case "m.emote":
return EventTypes.Emote;
case "m.image":
return EventTypes.Image;
case "m.video":
return EventTypes.Video;
case "m.audio":
return EventTypes.Audio;
case "m.file":
return EventTypes.File;
case "m.location":
return EventTypes.Location;
}
}
return EventTypes.Unknown;
}
}
enum EventTypes {
Text,
Emote,
Notice,
Image,
Video,
Audio,
File,
Location,
Reply,
RoomAliases,
RoomCanonicalAlias,
RoomCreate,
RoomJoinRules,
RoomMember,
RoomPowerLevels,
RoomName,
RoomTopic,
RoomAvatar,
GuestAccess,
HistoryVisibility,
Unknown,
}

View file

@ -23,7 +23,7 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/src/AccountData.dart';
import 'package:famedlysdk/src/RawEvent.dart';
import 'package:famedlysdk/src/RoomState.dart';
class RoomAccountData extends AccountData {
/// The user who has sent this event if it is not a global account data event.
@ -39,7 +39,7 @@ class RoomAccountData extends AccountData {
factory RoomAccountData.fromJson(
Map<String, dynamic> jsonPayload, Room room) {
final Map<String, dynamic> content =
RawEvent.getMapFromPayload(jsonPayload['content']);
RoomState.getMapFromPayload(jsonPayload['content']);
return RoomAccountData(
content: content,
typeKey: jsonPayload['type'],

View file

@ -20,13 +20,41 @@
* You should have received a copy of the GNU General Public License
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:convert';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/src/utils/ChatTime.dart';
import './Room.dart';
import './RawEvent.dart';
class RoomState extends RawEvent {
class RoomState {
/// The Matrix ID for this event in the format '$localpart:server.abc'. Please not
/// that account data, presence and other events may not have an eventId.
final String eventId;
/// The json payload of the content. The content highly depends on the type.
final Map<String, dynamic> content;
/// The type String of this event. For example 'm.room.message'.
final String typeKey;
/// The ID of the room this event belongs to.
final String roomId;
/// The user who has sent this event if it is not a global account data event.
final String senderId;
User get sender => room.states[senderId]?.asUser ?? User(senderId);
/// The time this event has received at the server. May be null for events like
/// account data.
final ChatTime time;
/// Optional additional content for this event.
final Map<String, dynamic> unsigned;
/// The room this event belongs to. May be null.
final Room room;
/// 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.
@ -36,37 +64,37 @@ class RoomState extends RawEvent {
/// the overwriting semantics for this piece of room state.
final String stateKey;
User get stateKeyUser => room.states[stateKey]?.asUser ?? User(stateKey);
RoomState(
{this.prevContent,
{this.content,
this.typeKey,
this.eventId,
this.roomId,
this.senderId,
this.time,
this.unsigned,
this.prevContent,
this.stateKey,
dynamic content,
String typeKey,
String eventId,
String roomId,
String senderId,
ChatTime time,
dynamic unsigned,
Room room})
: super(
content: content,
typeKey: typeKey,
eventId: eventId,
roomId: roomId,
senderId: senderId,
time: time,
unsigned: unsigned,
room: room);
this.room});
static Map<String, dynamic> getMapFromPayload(dynamic payload) {
if (payload is String)
try {
return json.decode(payload);
} catch (e) {
return {};
}
if (payload is Map<String, dynamic>) return payload;
return {};
}
/// Get a State event from a table row or from the event stream.
factory RoomState.fromJson(Map<String, dynamic> jsonPayload, Room room) {
final Map<String, dynamic> content =
RawEvent.getMapFromPayload(jsonPayload['content']);
RoomState.getMapFromPayload(jsonPayload['content']);
final Map<String, dynamic> unsigned =
RawEvent.getMapFromPayload(jsonPayload['unsigned']);
RoomState.getMapFromPayload(jsonPayload['unsigned']);
final Map<String, dynamic> prevContent =
RawEvent.getMapFromPayload(jsonPayload['prev_content']);
RoomState.getMapFromPayload(jsonPayload['prev_content']);
return RoomState(
stateKey: jsonPayload['state_key'],
prevContent: prevContent,
@ -107,4 +135,79 @@ class RoomState extends RawEvent {
time: time,
unsigned: unsigned,
room: room);
/// Get the real type.
EventTypes get type {
switch (typeKey) {
case "m.room.avatar":
return EventTypes.RoomAvatar;
case "m.room.name":
return EventTypes.RoomName;
case "m.room.topic":
return EventTypes.RoomTopic;
case "m.room.Aliases":
return EventTypes.RoomAliases;
case "m.room.canonical_alias":
return EventTypes.RoomCanonicalAlias;
case "m.room.create":
return EventTypes.RoomCreate;
case "m.room.join_rules":
return EventTypes.RoomJoinRules;
case "m.room.member":
return EventTypes.RoomMember;
case "m.room.power_levels":
return EventTypes.RoomPowerLevels;
case "m.room.guest_access":
return EventTypes.GuestAccess;
case "m.room.history_visibility":
return EventTypes.HistoryVisibility;
case "m.room.message":
switch (content["msgtype"] ?? "m.text") {
case "m.text":
if (content.containsKey("m.relates_to")) {
return EventTypes.Reply;
}
return EventTypes.Text;
case "m.notice":
return EventTypes.Notice;
case "m.emote":
return EventTypes.Emote;
case "m.image":
return EventTypes.Image;
case "m.video":
return EventTypes.Video;
case "m.audio":
return EventTypes.Audio;
case "m.file":
return EventTypes.File;
case "m.location":
return EventTypes.Location;
}
}
return EventTypes.Unknown;
}
}
enum EventTypes {
Text,
Emote,
Notice,
Image,
Video,
Audio,
File,
Location,
Reply,
RoomAliases,
RoomCanonicalAlias,
RoomCreate,
RoomJoinRules,
RoomMember,
RoomPowerLevels,
RoomName,
RoomTopic,
RoomAvatar,
GuestAccess,
HistoryVisibility,
Unknown,
}

View file

@ -24,7 +24,6 @@
import 'dart:convert';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/src/RawEvent.dart';
import 'package:famedlysdk/src/RoomState.dart';
import 'package:flutter_test/flutter_test.dart';