fix: only/number emotes in a reply

This commit is contained in:
Jindřich Pikora 2021-09-14 09:21:49 +00:00
parent ecbdb32c55
commit 03f82d84b9
2 changed files with 93 additions and 12 deletions

View File

@ -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>.*<\/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>.*<\/mx-reply>',
caseSensitive: false, multiLine: false, dotAll: true),
'');
return _countEmojiEmoteRegex.allMatches(formattedTextStripped).length;
} else {
return _countEmojiRegex.allMatches(plaintextBody).length;
}
}
}

View File

@ -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':
'\<mx-reply><blockquote><a href="https://fakeserver.notexisting/\$jEsUZKDJdhlrceRyVU">In reply to</a> <a href="https://fakeserver.notexisting/@alice:example.org">@alice:example.org</a><br>😒😒</blockquote></mx-reply>❤❤❤'
},
'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':
'\<mx-reply><blockquote><a href="https://fakeserver.notexisting/\$jEsUZKDJdhlrceRyVU">In reply to</a> <a href="https://fakeserver.notexisting/@alice:example.org">@alice:example.org</a><br>A 😒</blockquote></mx-reply>❤❤'
},
'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':
'\<mx-reply><blockquote><a href="https://fakeserver.notexisting/\$jEsUZKDJdhlrceRyVU">In reply to</a> <a href="https://fakeserver.notexisting/@alice:example.org">@alice:example.org</a><br>😒😒😒</blockquote></mx-reply>❤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':
'\<mx-reply><blockquote><a href="https://fakeserver.notexisting/\$jEsUZKDJdhlrceRyVU">In reply to</a> <a href="https://fakeserver.notexisting/@alice:example.org">@alice:example.org</a><br>A😒</blockquote></mx-reply>❤A❤'
},
'event_id': '\$edit2',
'sender': '@alice:example.org',
}, null);
expect(event.onlyEmotes, false);
expect(event.numberEmotes, 2);
});
});
}