From c571fe4dd5a121b1807d5d05da2279dbdcb4cb2c Mon Sep 17 00:00:00 2001 From: Sorunome Date: Wed, 7 Oct 2020 16:17:05 +0200 Subject: [PATCH] fix: don't assume msgtype is a string --- lib/src/event.dart | 15 ++++++++------- lib/src/room.dart | 8 +++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index af7bc54..6ec4df4 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -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; } diff --git a/lib/src/room.dart b/lib/src/room.dart index aba01d2..80f34ba 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -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) {