feat(#1265): accept mention-first MeshCore One reactions

MeshCore One 1.4.1+ (their commit 5d63d5fc) sends channel reactions as @[sender]emoji followed by the hash line. Offband only accepted emoji-first, so these showed up as chat text. 53 such reactions are in the owner's logs, still arriving.

- channel form accepts both orders: text starting with @[ is mention-first, the sender runs to the first ] and the emoji is the rest of the line, with no separator; otherwise the existing emoji-first path

- the emoji must be exactly one grapheme cluster on all forms, matching the reference parser, so two emoji or a mention followed by text stay chat

Hash, DM form, connector and notification code are unchanged; all three call sites already go through PocketMeshReaction.parse. Tests use three real captures from #690 and were shown failing (6 of 36) before the change.

Epic #659. Agent: JadePond (session ccf4efac)
pull/1349/head
diffwireauto 3 days ago
parent ce8da46764
commit 19270a781f

@ -1,14 +1,17 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:characters/characters.dart';
import 'package:crypto/crypto.dart';
import 'crockford_base32.dart';
/// A reaction in the PocketMesh / MeshCore One wire format.
///
/// Channel form: `{emoji}@[{targetSenderName}]\n{hash}`
/// Direct form: `{emoji}\n{hash}`
/// Channel form, either order:
/// `@[{targetSenderName}]{emoji}\n{hash}` (MeshCore One 1.4.1+, GH #659)
/// `{emoji}@[{targetSenderName}]\n{hash}` (earlier MeshCore One, PocketMesh)
/// Direct form: `{emoji}\n{hash}`
///
/// The hash identifies the target message as sha256 over the target's body
/// text (UTF-8) followed by its sender-claimed timestamp as a little-endian
@ -16,8 +19,8 @@ import 'crockford_base32.dart';
/// Crockford Base32 characters. The channel `SenderName: ` prefix is NOT part
/// of the hashed text, which is why the sender travels in `@[...]` instead.
///
/// Confirmed against a live capture from a MeshCore One peer, see GH #378.
/// Receive-only: Offband still sends its own `r:hhhh:ii` format (GH #379).
/// Confirmed against live captures from MeshCore One peers, see GH #378 and
/// GH #690. Receive-only: Offband still sends its own `r:hhhh:ii` format.
class PocketMeshReaction {
final String emoji;
@ -51,9 +54,9 @@ class PocketMeshReaction {
/// Parse [text] as a reaction, or null if it is an ordinary message.
///
/// Mirrors the reference parser: the last line must be exactly 8 valid
/// Crockford Base32 characters, and the part before it must start with an
/// emoji. Both checks matter, since anything accepted here is swallowed
/// instead of being shown as a message.
/// Crockford Base32 characters, and the emoji must be exactly one emoji.
/// Both checks matter, since anything accepted here is swallowed instead of
/// being shown as a message.
static PocketMeshReaction? parse(String text, {required bool isDm}) {
final newline = text.lastIndexOf('\n');
if (newline < 0) return null;
@ -70,6 +73,22 @@ class PocketMeshReaction {
final bracket = head.indexOf('@[');
if (bracket < 0) return null;
if (bracket == 0) {
// Mention-first: the sender runs to the first `]` and the emoji is the
// rest of the line, with no separator.
final close = head.indexOf(']', 2);
if (close < 0) return null;
final sender = head.substring(2, close);
if (sender.isEmpty) return null;
return _build(
emoji: head.substring(close + 1),
sender: sender,
hash: hash,
);
}
// Emoji-first.
final afterBracket = head.substring(bracket + 2);
if (!afterBracket.endsWith(']')) return null;
final sender = afterBracket.substring(0, afterBracket.length - 1);
@ -95,18 +114,8 @@ class PocketMeshReaction {
);
}
/// A reaction is one emoji, possibly with a variation selector, a skin-tone
/// modifier or ZWJ joins. Eight runes is well clear of the longest such
/// sequence and nowhere near a sentence.
///
/// This cap is the guard that matters: without it, any multi-line message
/// starting with a symbol and ending in eight Crockford characters would be
/// swallowed whole and shown as the reaction "emoji".
static const int _maxEmojiRunes = 8;
/// Deliberately conservative: a missed emoji only means the reaction renders
/// as text, which is the behaviour we have today, while a false positive
/// would swallow a real message.
/// as text, while a false positive would swallow a real message.
///
/// Arrows (U+2190-U+21FF, U+2934-U+2935) are excluded on purpose even though
/// they carry the Unicode Emoji property. They are ordinary punctuation in
@ -121,10 +130,12 @@ class PocketMeshReaction {
[0x3297, 0x3299],
];
/// Exactly one grapheme cluster, so a skin tone, a variation selector or a
/// ZWJ sequence still counts as one emoji, while two emoji or an emoji
/// followed by text stays chat. This is the reference rule.
static bool _isReactionEmoji(String text) {
final runes = text.runes;
if (runes.isEmpty || runes.length > _maxEmojiRunes) return false;
final first = runes.first;
if (text.characters.length != 1) return false;
final first = text.runes.first;
for (final range in _emojiRanges) {
if (first >= range[0] && first <= range[1]) return true;
}

@ -89,6 +89,132 @@ void main() {
isNull,
);
});
test('rejects two emoji, which MeshCore One treats as chat', () {
expect(
PocketMeshReaction.parse(
'\u{1F44D}\u{1F44D}@[Node]\ndyps6yf0',
isDm: false,
),
isNull,
);
});
});
// MeshCore One 1.4.1+ (their commit 5d63d5fc) sends channel reactions
// mention-first, with no separator after the `]`. Vectors are real
// captures from the owner's frame logs, GH #690.
group('parse, channel form, mention-first (GH #659)', () {
const captures = [
(
wire: '@[Shepherd]\u{1F44D}\nf8pv8797',
sender: 'Shepherd',
emoji: '\u{1F44D}',
targetText: '\u{2615}\u{1F441}\u{2615}',
targetTs: 1790594378,
),
(
wire: '@[MilkWeed\u{1FAB4}]\u{2615}\u{FE0F}\n3b028mg2',
sender: 'MilkWeed\u{1FAB4}',
emoji: '\u{2615}\u{FE0F}',
targetText:
'Coffee \u{2615}\u{FE0F} \u{2615}\u{FE0F} and good morning '
'from Kettering\u{2757}\u{FE0F}',
targetTs: 1790594340,
),
(
wire: '@[Tom]\u{1F44B}\n5zsea456',
sender: 'Tom',
emoji: '\u{1F44B}',
targetText: 'I just went past Tipp City. Hello Tipp City!',
targetTs: 1790594712,
),
];
for (final c in captures) {
test('parses the capture ${c.wire.split('\n').last}', () {
final parsed = PocketMeshReaction.parse(c.wire, isDm: false);
expect(parsed, isNotNull);
expect(parsed!.emoji, c.emoji);
expect(parsed.targetSenderName, c.sender);
expect(
parsed.targetHash,
PocketMeshReaction.computeHash(c.targetText, c.targetTs),
reason: 'the hash must still identify the target message',
);
});
}
test('accepts an emoji with a variation selector', () {
final parsed = PocketMeshReaction.parse(
'@[Shepherd]\u{1F44D}\u{FE0F}\nf8pv8797',
isDm: false,
);
expect(parsed?.emoji, '\u{1F44D}\u{FE0F}');
});
test('rejects a mention followed by text', () {
expect(
PocketMeshReaction.parse(
'@[Bob]\u{1F44D} nice one\ndyps6yf0',
isDm: false,
),
isNull,
);
});
test('rejects a mention followed by two emoji', () {
expect(
PocketMeshReaction.parse(
'@[Bob]\u{1F44D}\u{1F44D}\ndyps6yf0',
isDm: false,
),
isNull,
);
});
test(
'rejects a space before the emoji, as the reference parser does',
() {
expect(
PocketMeshReaction.parse('@[Bob] \u{1F44D}\ndyps6yf0', isDm: false),
isNull,
);
},
);
test('rejects an empty sender', () {
expect(
PocketMeshReaction.parse('@[]\u{1F44D}\ndyps6yf0', isDm: false),
isNull,
);
});
test('rejects nothing after the mention', () {
expect(
PocketMeshReaction.parse('@[Bob]\ndyps6yf0', isDm: false),
isNull,
);
});
test('rejects an unterminated mention', () {
expect(
PocketMeshReaction.parse('@[Bob\u{1F44D}\ndyps6yf0', isDm: false),
isNull,
);
});
test('an ordinary message that starts with a mention stays chat', () {
expect(PocketMeshReaction.parse('@[You] hi', isDm: false), isNull);
});
test('is rejected in a direct conversation', () {
expect(
PocketMeshReaction.parse('@[Tom]\u{1F44B}\n5zsea456', isDm: true),
isNull,
);
});
});
group('parse, direct form', () {
@ -110,6 +236,13 @@ void main() {
isNull,
);
});
test('rejects two emoji, which MeshCore One treats as chat', () {
expect(
PocketMeshReaction.parse('\u{1F44D}\u{1F44D}\ndyps6yf0', isDm: true),
isNull,
);
});
});
group('parse rejects ordinary messages', () {

Loading…
Cancel
Save

Powered by TurnKey Linux.