From 03f82d84b9955f406c9fed4330bb8cc48b428b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20Pikora?= Date: Tue, 14 Sep 2021 09:21:49 +0000 Subject: [PATCH] fix: only/number emotes in a reply --- lib/src/event.dart | 36 +++++++++++++++++------ test/event_test.dart | 69 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index 1389cf7b..20142ace 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -748,15 +748,33 @@ class Event extends MatrixEvent { multiLine: false); /// Returns if a given event only has emotes, emojis or whitespace as content. + /// If the body contains a reply then it is stripped. /// This is useful to determine if stand-alone emotes should be displayed bigger. - bool get onlyEmotes => isRichMessage - ? _onlyEmojiEmoteRegex.hasMatch(formattedText) - : _onlyEmojiRegex.hasMatch(text); + bool get onlyEmotes { + if (isRichMessage) { + final formattedTextStripped = formattedText.replaceAll( + RegExp('.*<\/mx-reply>', + caseSensitive: false, multiLine: false, dotAll: true), + ''); + return _onlyEmojiEmoteRegex.hasMatch(formattedTextStripped); + } else { + return _onlyEmojiRegex.hasMatch(plaintextBody); + } + } - /// 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(formattedText).length - : _countEmojiRegex.allMatches(text).length; + /// Gets the number of emotes in a given message. This is useful to determine + /// if the emotes should be displayed bigger. + /// If the body contains a reply then it is stripped. + /// WARNING: This does **not** test if there are only emotes. Use `event.onlyEmotes` for that! + int get numberEmotes { + if (isRichMessage) { + final formattedTextStripped = formattedText.replaceAll( + RegExp('.*<\/mx-reply>', + caseSensitive: false, multiLine: false, dotAll: true), + ''); + return _countEmojiEmoteRegex.allMatches(formattedTextStripped).length; + } else { + return _countEmojiRegex.allMatches(plaintextBody).length; + } + } } diff --git a/test/event_test.dart b/test/event_test.dart index 61b2f4aa..ac599f53 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -20,12 +20,11 @@ import 'dart:convert'; import 'dart:typed_data'; -import 'package:matrix/matrix.dart'; import 'package:matrix/encryption.dart'; +import 'package:matrix/matrix.dart'; import 'package:matrix/src/event.dart'; - -import 'package:test/test.dart'; import 'package:olm/olm.dart' as olm; +import 'package:test/test.dart'; import 'fake_client.dart'; import 'fake_matrix_api.dart'; @@ -1483,6 +1482,70 @@ void main() { }, null); expect(event.onlyEmotes, true); expect(event.numberEmotes, 1); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '''> <@alice:example.org> 😒😒 + + ❤❤❤''', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '\
In reply to @alice:example.org
😒😒
❤❤❤' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, null); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 3); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '''> <@alice:example.org> A 😒 + + ❤❤''', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '\
In reply to @alice:example.org
A 😒
❤❤' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, null); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 2); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '''> <@alice:example.org> 😒😒😒 + + ❤A❤''', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '\
In reply to @alice:example.org
😒😒😒
❤A❤' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, null); + expect(event.onlyEmotes, false); + expect(event.numberEmotes, 2); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '''> <@alice:example.org> A😒 + + ❤A❤''', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '\
In reply to @alice:example.org
A😒
❤A❤' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, null); + expect(event.onlyEmotes, false); + expect(event.numberEmotes, 2); }); }); }