famedlysdk/lib/src/Event.dart

219 lines
6 KiB
Dart
Raw Normal View History

2019-06-09 11:57:33 +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-06-09 11:57:33 +00:00
*/
2019-06-09 10:16:48 +00:00
import 'dart:convert';
import 'package:famedlysdk/src/sync/EventUpdate.dart';
2019-06-09 10:16:48 +00:00
import 'package:famedlysdk/src/utils/ChatTime.dart';
import 'package:famedlysdk/src/Client.dart';
2019-06-09 11:57:33 +00:00
import './User.dart';
2019-06-11 09:13:14 +00:00
import './Room.dart';
2019-06-09 10:16:48 +00:00
2019-06-09 12:33:25 +00:00
/// A single Matrix event, e.g. a message in a chat.
2019-06-09 10:16:48 +00:00
class Event {
2019-06-11 09:23:57 +00:00
/// The Matrix ID for this event in the format '$localpart:server.abc'.
2019-06-09 10:16:48 +00:00
final String id;
2019-06-11 09:23:57 +00:00
/// The room this event belongs to.
final Room room;
/// The time this event has received at the server.
2019-06-09 10:16:48 +00:00
final ChatTime time;
2019-06-11 09:23:57 +00:00
/// The user who has sent this event.
2019-06-09 10:16:48 +00:00
final User sender;
2019-06-11 09:23:57 +00:00
/// The user who is the target of this event e.g. for a m.room.member event.
2019-06-09 10:16:48 +00:00
final User stateKey;
2019-06-11 09:23:57 +00:00
/// The type of this event. Mostly this is 'timeline'.
2019-06-09 10:16:48 +00:00
final String environment;
2019-06-11 09:23:57 +00:00
/// The status of this event.
/// -1=ERROR
/// 0=SENDING
/// 1=SENT
/// 2=RECEIVED
int status;
/// The json payload of the content. The content highly depends on the type.
final Map<String, dynamic> content;
2019-06-09 10:16:48 +00:00
Event(
this.id,
this.sender,
this.time, {
2019-06-11 09:23:57 +00:00
this.room,
2019-06-09 10:16:48 +00:00
this.stateKey,
this.status = 2,
2019-06-11 09:23:57 +00:00
this.environment,
2019-06-09 10:16:48 +00:00
this.content,
});
2019-06-11 09:23:57 +00:00
/// Returns the body of this event if it has a body.
String get text => content["body"] ?? "";
/// Returns the formatted boy of this event if it has a formatted body.
String get formattedText => content["formatted_body"] ?? "";
/// Use this to get the body.
String getBody() {
2019-06-12 06:22:30 +00:00
if (text != "") return text;
2019-06-12 07:07:07 +00:00
if (formattedText != "") return formattedText;
2019-06-12 06:22:30 +00:00
return "*** Unable to parse Content ***";
}
2019-06-09 10:16:48 +00:00
2019-06-11 09:23:57 +00:00
/// Get the real type.
2019-06-09 10:16:48 +00:00
EventTypes get type {
switch (environment) {
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;
2019-06-09 10:16:48 +00:00
case "m.room.message":
switch (content["msgtype"] ?? "m.text") {
case "m.text":
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;
2019-06-09 10:16:48 +00:00
}
}
2019-06-27 10:33:02 +00:00
return EventTypes.Text;
2019-06-09 10:16:48 +00:00
}
2019-06-11 09:23:57 +00:00
/// Generate a new Event object from a json string, mostly a table row.
2019-07-03 09:42:19 +00:00
static Event fromJson(Map<String, dynamic> jsonObj, Room room,
{User senderUser, User stateKeyUser}) {
2019-06-21 10:18:54 +00:00
Map<String, dynamic> content = jsonObj["content"];
2019-07-03 09:19:45 +00:00
if (content == null && jsonObj["content_json"] != null)
2019-06-21 10:18:54 +00:00
try {
content = json.decode(jsonObj["content_json"]);
} catch (e) {
2019-07-03 09:19:45 +00:00
print("jsonObj decode of event content failed: ${e.toString()}");
2019-06-21 10:18:54 +00:00
content = {};
}
2019-07-03 09:42:19 +00:00
else if (content == null) content = {};
2019-07-03 11:07:56 +00:00
if (senderUser == null) senderUser = User.fromJson(jsonObj, room);
2019-07-03 09:42:19 +00:00
if (stateKeyUser == null) stateKeyUser = User(jsonObj["state_key"]);
2019-06-21 10:18:54 +00:00
2019-06-09 10:16:48 +00:00
return Event(
2019-06-27 08:15:46 +00:00
jsonObj["event_id"] ?? jsonObj["id"],
2019-07-03 09:42:19 +00:00
senderUser,
2019-06-09 10:16:48 +00:00
ChatTime(jsonObj["origin_server_ts"]),
2019-07-03 09:42:19 +00:00
stateKey: stateKeyUser,
2019-06-09 10:16:48 +00:00
environment: jsonObj["type"],
status: jsonObj["status"] ?? 2,
2019-06-09 10:16:48 +00:00
content: content,
2019-06-11 09:23:57 +00:00
room: room,
2019-06-09 10:16:48 +00:00
);
}
/// Removes this event if the status is < 1. This event will just be removed
/// from the database and the timelines.
Future<dynamic> remove() async {
if (status < 1) {
2019-06-27 08:33:43 +00:00
if (room.client.store != null)
room.client.store.db.rawDelete("DELETE FROM Events WHERE id=?", [id]);
room.client.connection.onEvent.add(EventUpdate(
roomID: room.id,
type: "timeline",
eventType: environment,
2019-06-27 08:20:47 +00:00
content: {
"event_id": id,
"status": -2,
"content": {"body": "Removed..."}
}));
}
}
/// Try to send this event again. Only works with events of status -1.
Future<String> sendAgain({String txid}) async {
if (status != -1) return null;
remove();
final String eventID = await room.sendTextEvent(text, txid: txid);
return eventID;
}
2019-06-11 09:23:57 +00:00
@Deprecated("Use [client.store.getEventList(Room room)] instead!")
static Future<List<Event>> getEventList(Client matrix, Room room) async {
2019-06-11 09:23:57 +00:00
List<Event> eventList = await matrix.store.getEventList(room);
2019-06-09 10:16:48 +00:00
return eventList;
}
}
enum EventTypes {
Text,
Emote,
Notice,
Image,
Video,
Audio,
File,
Location,
RoomAliases,
RoomCanonicalAlias,
RoomCreate,
RoomJoinRules,
RoomMember,
RoomPowerLevels,
RoomName,
RoomTopic,
RoomAvatar,
}
final Map<String, int> StatusTypes = {
"REMOVE": -2,
2019-06-09 10:16:48 +00:00
"ERROR": -1,
"SENDING": 0,
"SENT": 1,
"RECEIVED": 2,
};