fix: don't assume msgtype is a string

This commit is contained in:
Sorunome 2020-10-07 16:17:05 +02:00 committed by Christian Pauly
parent 4d62a79e96
commit c571fe4dd5
2 changed files with 13 additions and 10 deletions

View File

@ -234,7 +234,7 @@ class Event extends MatrixEvent {
String get messageType => type == EventTypes.Sticker
? MessageTypes.Sticker
: content['msgtype'] ?? MessageTypes.Text;
: (content['msgtype'] is String ? content['msgtype'] : MessageTypes.Text);
void setRedactionEvent(Event redactedBecause) {
unsigned = {
@ -281,10 +281,11 @@ class Event extends MatrixEvent {
}
/// Returns the body of this event if it has a body.
String get text => content['body'] ?? '';
String get text => content['body'] is String ? content['body'] : '';
/// Returns the formatted boy of this event if it has a formatted body.
String get formattedText => content['formatted_body'] ?? '';
String get formattedText =>
content['formatted_body'] is String ? content['formatted_body'] : '';
/// Use this to get the body.
String get body {
@ -800,13 +801,13 @@ class Event extends MatrixEvent {
/// Returns if a given event only has emotes, emojis or whitespace as content.
/// This is useful to determine if stand-alone emotes should be displayed bigger.
bool get onlyEmotes => isRichMessage
? _onlyEmojiEmoteRegex.hasMatch(content['formatted_body'])
: _onlyEmojiRegex.hasMatch(content['body'] ?? '');
? _onlyEmojiEmoteRegex.hasMatch(formattedText)
: _onlyEmojiRegex.hasMatch(text);
/// Gets the number of emotes in a given message. This is useful to determine if
/// emotes should be displayed bigger. WARNING: This does **not** test if there are
/// only emotes. Use `event.onlyEmotes` for that!
int get numberEmotes => isRichMessage
? _countEmojiEmoteRegex.allMatches(content['formatted_body']).length
: _countEmojiRegex.allMatches(content['body'] ?? '').length;
? _countEmojiEmoteRegex.allMatches(formattedText).length
: _countEmojiRegex.allMatches(text).length;
}

View File

@ -174,7 +174,8 @@ class Room {
StreamController.broadcast();
/// The name of the room if set by a participant.
String get name => states[EventTypes.RoomName] != null
String get name => states[EventTypes.RoomName] != null &&
states[EventTypes.RoomName].content['name'] is String
? states[EventTypes.RoomName].content['name']
: '';
@ -205,14 +206,15 @@ class Room {
}
/// The topic of the room if set by a participant.
String get topic => states[EventTypes.RoomTopic] != null
String get topic => states[EventTypes.RoomTopic] != null &&
states[EventTypes.RoomTopic].content['topic'] is String
? states[EventTypes.RoomTopic].content['topic']
: '';
/// The avatar of the room if set by a participant.
Uri get avatar {
if (states[EventTypes.RoomAvatar] != null &&
states[EventTypes.RoomAvatar].content['url'] != null) {
states[EventTypes.RoomAvatar].content['url'] is String) {
return Uri.parse(states[EventTypes.RoomAvatar].content['url']);
}
if (mHeroes != null && mHeroes.length == 1 && states[mHeroes[0]] != null) {