@ -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 capture s from MeshCore One peer s , 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 + 21 FF , 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 ;
}