Merge branch 'event-fix-types' into 'master'

[Event] Fix types

See merge request famedly/famedlysdk!146
This commit is contained in:
Christian Pauly 2020-01-04 09:57:30 +00:00
commit d8f43e6858
2 changed files with 54 additions and 32 deletions

View file

@ -26,6 +26,7 @@ import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/src/utils/Receipt.dart'; import 'package:famedlysdk/src/utils/Receipt.dart';
import './Room.dart'; import './Room.dart';
/// All data exchanged over Matrix is expressed as an "event". Typically each client action (e.g. sending a message) correlates with exactly one event.
class Event { class Event {
/// The Matrix ID for this event in the format '$localpart:server.abc'. Please not /// 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. /// that account data, presence and other events may not have an eventId.
@ -215,32 +216,46 @@ class Event {
return EventTypes.GuestAccess; return EventTypes.GuestAccess;
case "m.room.history_visibility": case "m.room.history_visibility":
return EventTypes.HistoryVisibility; return EventTypes.HistoryVisibility;
case "m.sticker":
return EventTypes.Sticker;
case "m.room.message": case "m.room.message":
switch (content["msgtype"] ?? "m.text") { return EventTypes.Message;
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; return EventTypes.Unknown;
} }
///
MessageTypes get messageType {
switch (content["msgtype"] ?? "m.text") {
case "m.text":
if (content.containsKey("m.relates_to")) {
return MessageTypes.Reply;
}
return MessageTypes.Text;
case "m.notice":
return MessageTypes.Notice;
case "m.emote":
return MessageTypes.Emote;
case "m.image":
return MessageTypes.Image;
case "m.video":
return MessageTypes.Video;
case "m.audio":
return MessageTypes.Audio;
case "m.file":
return MessageTypes.File;
case "m.sticker":
return MessageTypes.Sticker;
case "m.location":
return MessageTypes.Location;
default:
if (type == EventTypes.Message) {
return MessageTypes.Text;
}
return MessageTypes.None;
}
}
void setRedactionEvent(Event redactedBecause) { void setRedactionEvent(Event redactedBecause) {
unsigned = { unsigned = {
"redacted_because": redactedBecause.toJson(), "redacted_because": redactedBecause.toJson(),
@ -350,17 +365,24 @@ class Event {
room.redactEvent(eventId, reason: reason, txid: txid); room.redactEvent(eventId, reason: reason, txid: txid);
} }
enum EventTypes { enum MessageTypes {
Text, Text,
Emote, Emote,
Notice, Notice,
Image, Image,
Video, Video,
Audio, Audio,
Redaction,
File, File,
Location, Location,
Reply, Reply,
Sticker,
None,
}
enum EventTypes {
Message,
Sticker,
Redaction,
RoomAliases, RoomAliases,
RoomCanonicalAlias, RoomCanonicalAlias,
RoomCreate, RoomCreate,

View file

@ -66,7 +66,7 @@ void main() {
expect(event.text, body); expect(event.text, body);
expect(event.formattedText, formatted_body); expect(event.formattedText, formatted_body);
expect(event.getBody(), body); expect(event.getBody(), body);
expect(event.type, EventTypes.Text); expect(event.type, EventTypes.Message);
jsonObj["state_key"] = ""; jsonObj["state_key"] = "";
Event state = Event.fromJson(jsonObj, null); Event state = Event.fromJson(jsonObj, null);
expect(state.eventId, id); expect(state.eventId, id);
@ -125,31 +125,31 @@ void main() {
jsonObj["content"]["msgtype"] = "m.notice"; jsonObj["content"]["msgtype"] = "m.notice";
event = Event.fromJson(jsonObj, null); event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Notice); expect(event.messageType, MessageTypes.Notice);
jsonObj["content"]["msgtype"] = "m.emote"; jsonObj["content"]["msgtype"] = "m.emote";
event = Event.fromJson(jsonObj, null); event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Emote); expect(event.messageType, MessageTypes.Emote);
jsonObj["content"]["msgtype"] = "m.image"; jsonObj["content"]["msgtype"] = "m.image";
event = Event.fromJson(jsonObj, null); event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Image); expect(event.messageType, MessageTypes.Image);
jsonObj["content"]["msgtype"] = "m.video"; jsonObj["content"]["msgtype"] = "m.video";
event = Event.fromJson(jsonObj, null); event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Video); expect(event.messageType, MessageTypes.Video);
jsonObj["content"]["msgtype"] = "m.audio"; jsonObj["content"]["msgtype"] = "m.audio";
event = Event.fromJson(jsonObj, null); event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Audio); expect(event.messageType, MessageTypes.Audio);
jsonObj["content"]["msgtype"] = "m.file"; jsonObj["content"]["msgtype"] = "m.file";
event = Event.fromJson(jsonObj, null); event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.File); expect(event.messageType, MessageTypes.File);
jsonObj["content"]["msgtype"] = "m.location"; jsonObj["content"]["msgtype"] = "m.location";
event = Event.fromJson(jsonObj, null); event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Location); expect(event.messageType, MessageTypes.Location);
jsonObj["type"] = "m.room.message"; jsonObj["type"] = "m.room.message";
jsonObj["content"]["msgtype"] = "m.text"; jsonObj["content"]["msgtype"] = "m.text";
@ -158,7 +158,7 @@ void main() {
"event_id": "1234", "event_id": "1234",
}; };
event = Event.fromJson(jsonObj, null); event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Reply); expect(event.messageType, MessageTypes.Reply);
}); });
test("redact", () async { test("redact", () async {