From 74cd1128bce23348cf8f7cc52950eb3b1813b6aa Mon Sep 17 00:00:00 2001 From: Seth Golub Date: Mon, 7 Sep 2026 09:56:24 -0700 Subject: [PATCH] Support updated MeshCoreOne emoji reaction format In #521, @Avi0n mentions they will be changing MeshCoreOne's reaction format in the next release. Here we add support for the new format, while retaining support for the old format as "MC1Legacy". There's some redundancy. My goal was not to maximize elegance, but to facilitate easily tearing out support for the legacy format months from now when we no longer care about it. This has not been tested in the wild, as nothing is transmitting the new format yet. --- lib/helpers/reaction_helper.dart | 24 +++++++++++- test/reaction_helper_test.dart | 63 ++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 5 deletions(-) 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';