diff --git a/lib/helpers/reaction_helper.dart b/lib/helpers/reaction_helper.dart index da669761..58928284 100644 --- a/lib/helpers/reaction_helper.dart +++ b/lib/helpers/reaction_helper.dart @@ -161,7 +161,9 @@ class ReactionHelper { } static ReactionInfo? parseReaction(String text) { - return parseReactionOurs(text) ?? parseReactionMC1(text); + return parseReactionOurs(text) ?? + parseReactionMC1(text) ?? + parseReactionMC1Legacy(text); } static bool _looksLikeEmoji(String emoji) { @@ -186,6 +188,26 @@ class ReactionHelper { } static ReactionInfo? parseReactionMC1(String text) { + // See https://github.com/Avi0n/MeshCoreOne/blob/main/docs/Reactions.md + // This regex matches both the channel format, which includes the sender name, + // and the chat (DM) format, which omits it. + final regex = RegExp(r'^(?:@\[(.*)])?(.+?)\n([a-tv-zA-TV-Z0-9]{8})$'); + final match = regex.firstMatch(text); + if (match == null) return null; + + final senderName = match.group(1); + final emoji = match.group(2)!; + final hash = match.group(3)?.toLowerCase(); + if (!_looksLikeEmoji(emoji)) return null; + + return ReactionInfo( + targetHash: _concatenateHashAndSender(hash!, senderName), + emoji: emoji, + hashType: HashType.mc1, + ); + } + + static ReactionInfo? parseReactionMC1Legacy(String text) { // See https://github.com/Avi0n/MeshCoreOne/blob/main/docs/Reactions.md // This regex matches both the channel format, which includes the sender name, // and the chat (DM) format, which omits it. diff --git a/test/reaction_helper_test.dart b/test/reaction_helper_test.dart index 409dedf5..d3a952d8 100644 --- a/test/reaction_helper_test.dart +++ b/test/reaction_helper_test.dart @@ -373,7 +373,7 @@ void main() { final senderName = 'entropy 📓'; final hash = 'b3je6qf7'; final info = ReactionHelper.parseReactionMC1( - '$emoji@[$senderName]\n$hash', + '@[$senderName]$emoji\n$hash', ); expect(info, isNotNull); expect(info!.targetHash, equals('$hash:$senderName')); @@ -381,10 +381,10 @@ void main() { List messages = [ '🥳\n01234567', - '🥳@[entropy]\n01234567', + '@[entropy]🥳\n01234567', '\u{1f44c}\u{1f3ff}\n01234567', - '\u{1f44c}\u{1f3ff}@[entropy]\n01234567', - '🧑‍🧑‍🧒@[entropy]\n01234567', // long composite emoji - 21 bytes + '@[entropy]\u{1f44c}\u{1f3ff}\n01234567', + '@[entropy]🧑‍🧑‍🧒\n01234567', // long composite emoji - 21 bytes '🧑‍🧑‍🧒‍🧒\n01234567', // long composite emoji - 25 bytes ]; for (String txt in messages) { @@ -408,7 +408,62 @@ void main() { } }); + test('parse MeshCoreOne reaction - Legacy format', () { + final emoji = '🥳'; + final senderName = 'entropy 📓'; + final hash = 'b3je6qf7'; + final info = ReactionHelper.parseReactionMC1Legacy( + '$emoji@[$senderName]\n$hash', + ); + expect(info, isNotNull); + expect(info!.targetHash, equals('$hash:$senderName')); + expect(info.emoji, equals(emoji)); + + List messages = [ + '🥳\n01234567', + '🥳@[entropy]\n01234567', + '\u{1f44c}\u{1f3ff}\n01234567', + '\u{1f44c}\u{1f3ff}@[entropy]\n01234567', + '🧑‍🧑‍🧒@[entropy]\n01234567', // long composite emoji - 21 bytes + '🧑‍🧑‍🧒‍🧒\n01234567', // long composite emoji - 25 bytes + ]; + for (String txt in messages) { + final info = ReactionHelper.parseReactionMC1Legacy(txt); + expect(info, isNotNull, reason: "$txt did not parse"); + } + }); + + test( + 'parse MeshCoreOne reaction - Legacy format - avoid false positives', + () { + List messages = [ + 'test\nabcdefgh', + '🥳\nhashistoolong', + '🥳\nshort', + '🥳🥳\n01234567', // two emojis not composed into one grapheme + 'X\n01234567', + '🥳\nabcuuxyz', // u is not valid as we don't support checksums + ]; + for (String txt in messages) { + final info = ReactionHelper.parseReactionMC1Legacy(txt); + expect(info, isNull, reason: "$txt should not have parsed"); + } + }, + ); + test('parse MeshCoreOne reaction - main entry', () { + final emoji = '🥳'; + final senderName = 'entropy 📓'; + final hash = 'b3je6qf7'; + final info = ReactionHelper.parseReaction( + '@[$senderName]$emoji\n$hash', + ); + expect(info, isNotNull); + expect(info!.targetHash, equals('$hash:$senderName')); + expect(info.emoji, equals(emoji)); + }); + + test('parse MeshCoreOne reaction Legacy format - main entry', () { final emoji = '🥳'; final senderName = 'entropy 📓'; final hash = 'b3je6qf7';