Improvements to MeshCore-One emoji reaction processing
- Made Crockford-matching regex slighlty stricter (not allowing U) - Match arbitrarily long multi-code-point joined emoji - Added more validation checks for potential emoji, including that it's a single grapheme - Added more unit tests for emoji parsing - For reactions whose targets we can't find, display MC1-style reactions as normal messages. Continue old beavior of silently dropping unmatched MC-Open reactions. Part of PR #537
This commit is contained in:
1 parent
97f20eaec0
commit
9d3b4a40d9
3 files changed
+143
-45
No files matched your search
@@ -3563,27 +3563,31 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
|
|
||||||
// Process reaction locally to update the UI immediately
|
// Process reaction locally to update the UI immediately
|
||||||
appLogger.info('Adding sent channel reaction, id: $reactionIdentifier');
|
appLogger.info('Adding sent channel reaction, id: $reactionIdentifier');
|
||||||
_processReaction(messages, reactionInfo);
|
final reactionApplied = _processReaction(messages, reactionInfo);
|
||||||
await _channelMessageStore.saveChannelMessages(channel.index, messages);
|
if (reactionApplied) {
|
||||||
|
await _channelMessageStore.saveChannelMessages(channel.index, messages);
|
||||||
|
|
||||||
// Mark this reaction as processed
|
// Mark this reaction as processed
|
||||||
_processedChannelReactions[channel.index]!.add(reactionIdentifier);
|
_processedChannelReactions[channel.index]!.add(reactionIdentifier);
|
||||||
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
||||||
// Send the reaction to the device (don't add as a visible message)
|
// Send the reaction to the device (don't add as a visible message)
|
||||||
final reactionQueueId = _nextReactionSendQueueId();
|
final reactionQueueId = _nextReactionSendQueueId();
|
||||||
_pendingChannelSentQueue.add(reactionQueueId);
|
_pendingChannelSentQueue.add(reactionQueueId);
|
||||||
await _runScopedChannelSend(() async {
|
await _runScopedChannelSend(() async {
|
||||||
await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime);
|
await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime);
|
||||||
await _sendFrameAndWaitForCommandAck(
|
await _sendFrameAndWaitForCommandAck(
|
||||||
buildSendChannelTextMsgFrame(channel.index, text),
|
buildSendChannelTextMsgFrame(channel.index, text),
|
||||||
channelSendQueueId: reactionQueueId,
|
channelSendQueueId: reactionQueueId,
|
||||||
expectsGenericAck: true,
|
expectsGenericAck: true,
|
||||||
successCode: respCodeSent,
|
successCode: respCodeSent,
|
||||||
);
|
);
|
||||||
}, region: getChannelRegion(channel.index));
|
}, region: getChannelRegion(channel.index));
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
// It looks like a reaction, but we did not find its target, so
|
||||||
|
// we continue to process it normally.
|
||||||
}
|
}
|
||||||
|
|
||||||
final message = ChannelMessage.outgoing(
|
final message = ChannelMessage.outgoing(
|
||||||
@@ -6227,27 +6231,39 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
final isDuplicate = _processedContactReactions[pubKeyHex]!.contains(
|
final isDuplicate = _processedContactReactions[pubKeyHex]!.contains(
|
||||||
reactionIdentifier,
|
reactionIdentifier,
|
||||||
);
|
);
|
||||||
|
if (isDuplicate) return;
|
||||||
|
|
||||||
if (!isDuplicate) {
|
// New reaction - process it
|
||||||
// New reaction - process it
|
// For hashing and dup checking, we use null for sender names in
|
||||||
|
// normal 1:1 chats. This way if we and they have different ideas
|
||||||
|
// about what their name is, reactions still work. But for reaction
|
||||||
|
// reports, we want to know who sent what, rather than infer it later,
|
||||||
|
// so we'll add it to the reactionInfo before storing it.
|
||||||
|
reactionInfo.senderName ??= message.isOutgoing
|
||||||
|
? selfName
|
||||||
|
: getContactByPubKeyHex(pubKeyHex)?.name ?? '???';
|
||||||
|
final reactionApplied = _processContactReaction(
|
||||||
|
messages,
|
||||||
|
reactionInfo,
|
||||||
|
pubKeyHex,
|
||||||
|
);
|
||||||
|
if (reactionApplied) {
|
||||||
appLogger.info('Adding reaction, id: $reactionIdentifier');
|
appLogger.info('Adding reaction, id: $reactionIdentifier');
|
||||||
// For hashing and dup checking, we use null for sender names in
|
|
||||||
// normal 1:1 chats. This way if we and they have different ideas
|
|
||||||
// about what their name is, reactions still work. But for reaction
|
|
||||||
// reports, we want to know who sent what, rather than infer it later,
|
|
||||||
// so we'll add it to the reactionInfo before storing it.
|
|
||||||
reactionInfo.senderName ??= message.isOutgoing
|
|
||||||
? selfName
|
|
||||||
: getContactByPubKeyHex(pubKeyHex)?.name ?? '???';
|
|
||||||
_processContactReaction(messages, reactionInfo, pubKeyHex);
|
|
||||||
_messageStore.saveMessages(pubKeyHex, messages);
|
_messageStore.saveMessages(pubKeyHex, messages);
|
||||||
|
|
||||||
// Mark as processed
|
// Mark as processed
|
||||||
_processedContactReactions[pubKeyHex]!.add(reactionIdentifier);
|
_processedContactReactions[pubKeyHex]!.add(reactionIdentifier);
|
||||||
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
return; // Don't add reaction as a visible message
|
||||||
}
|
}
|
||||||
return; // Don't add reaction as a visible message
|
|
||||||
|
// Looks like a reaction, but didn't match any message we have.
|
||||||
|
// for reactions matching our own format, because those are not very
|
||||||
|
// informative on their own, but the MC1 format is useful even if
|
||||||
|
// in raw form, so we'll let those through.
|
||||||
|
appLogger.info('No match for reaction, id: $reactionIdentifier');
|
||||||
|
if (reactionInfo.hashType == HashType.ours) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
messages.add(message);
|
messages.add(message);
|
||||||
@@ -6258,7 +6274,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _processContactReaction(
|
bool _processContactReaction(
|
||||||
List<Message> messages,
|
List<Message> messages,
|
||||||
ReactionInfo reactionInfo,
|
ReactionInfo reactionInfo,
|
||||||
String contactPubKeyHex,
|
String contactPubKeyHex,
|
||||||
@@ -6266,7 +6282,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
final contact = getContactByPubKeyHex(contactPubKeyHex);
|
final contact = getContactByPubKeyHex(contactPubKeyHex);
|
||||||
final isRoomServer = contact?.type == advTypeRoom;
|
final isRoomServer = contact?.type == advTypeRoom;
|
||||||
|
|
||||||
ReactionHelper.applyReaction<Message>(
|
return ReactionHelper.applyReaction<Message>(
|
||||||
messages: messages,
|
messages: messages,
|
||||||
reactionInfo: reactionInfo,
|
reactionInfo: reactionInfo,
|
||||||
// Incoming reactions in 1:1: match against outgoing messages only
|
// Incoming reactions in 1:1: match against outgoing messages only
|
||||||
@@ -6481,17 +6497,26 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
reactionIdentifier,
|
reactionIdentifier,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!isDuplicate) {
|
if (isDuplicate) return false;
|
||||||
// New reaction - process it
|
|
||||||
|
// New reaction - process it
|
||||||
|
final reactionApplied = _processReaction(messages, reactionInfo);
|
||||||
|
if (reactionApplied) {
|
||||||
appLogger.info('Adding channel reaction, id: $reactionIdentifier');
|
appLogger.info('Adding channel reaction, id: $reactionIdentifier');
|
||||||
_processReaction(messages, reactionInfo);
|
|
||||||
// Save updated messages
|
// Save updated messages
|
||||||
_channelMessageStore.saveChannelMessages(channelIndex, messages);
|
_channelMessageStore.saveChannelMessages(channelIndex, messages);
|
||||||
|
|
||||||
// Mark as processed
|
// Mark as processed
|
||||||
_processedChannelReactions[channelIndex]!.add(reactionIdentifier);
|
_processedChannelReactions[channelIndex]!.add(reactionIdentifier);
|
||||||
|
return false; // Don't add reaction as a visible message
|
||||||
}
|
}
|
||||||
return false; // Don't add reaction as a visible message
|
// Looks like a reaction, but didn't match any message we have.
|
||||||
|
// Old behavior is to silently drop these. We'll continue doing that
|
||||||
|
// for reactions matching our own format, because those are not very
|
||||||
|
// informative on their own, but the MC1 format is useful even if
|
||||||
|
// in raw form, so we'll let those through.
|
||||||
|
appLogger.info('No match for reaction, id: $reactionIdentifier');
|
||||||
|
if (reactionInfo.hashType == HashType.ours) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse reply info from message text
|
// Parse reply info from message text
|
||||||
@@ -6593,11 +6618,11 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _processReaction(
|
bool _processReaction(
|
||||||
List<ChannelMessage> messages,
|
List<ChannelMessage> messages,
|
||||||
ReactionInfo reactionInfo,
|
ReactionInfo reactionInfo,
|
||||||
) {
|
) {
|
||||||
ReactionHelper.applyReaction<ChannelMessage>(
|
return ReactionHelper.applyReaction<ChannelMessage>(
|
||||||
messages: messages,
|
messages: messages,
|
||||||
reactionInfo: reactionInfo,
|
reactionInfo: reactionInfo,
|
||||||
shouldSkip: (_) => false,
|
shouldSkip: (_) => false,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'package:characters/characters.dart';
|
||||||
import 'package:crypto/crypto.dart';
|
import 'package:crypto/crypto.dart';
|
||||||
import 'package:base32/base32.dart';
|
import 'package:base32/base32.dart';
|
||||||
import 'package:base32/encodings.dart';
|
import 'package:base32/encodings.dart';
|
||||||
@@ -21,7 +22,7 @@ class ReactionInfo {
|
|||||||
});
|
});
|
||||||
|
|
||||||
String identifier() {
|
String identifier() {
|
||||||
return '$targetHash:$emoji:${senderName ?? ""}';
|
return '$targetHash:$emoji:$hashType:${senderName ?? ""}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +131,6 @@ class ReactionHelper {
|
|||||||
String? senderName,
|
String? senderName,
|
||||||
String messageText,
|
String messageText,
|
||||||
) {
|
) {
|
||||||
// TODO: unit tests
|
|
||||||
// raw hash is first 5 bytes of SHA-256(UTF-8 text + uint32-LE sender timestamp)
|
// raw hash is first 5 bytes of SHA-256(UTF-8 text + uint32-LE sender timestamp)
|
||||||
Uint8List messageBytes = utf8.encode(messageText);
|
Uint8List messageBytes = utf8.encode(messageText);
|
||||||
ByteData timestampBytes = ByteData(4)
|
ByteData timestampBytes = ByteData(4)
|
||||||
@@ -144,6 +144,11 @@ class ReactionHelper {
|
|||||||
return base32
|
return base32
|
||||||
.encode(Uint8List.fromList(hash), encoding: Encoding.crockford)
|
.encode(Uint8List.fromList(hash), encoding: Encoding.crockford)
|
||||||
.toLowerCase();
|
.toLowerCase();
|
||||||
|
// Note: Crockford32 defines permissive decoding and allows some
|
||||||
|
// substitutions (e.g. I, i, l -> 1) but we never decode the hashes, we
|
||||||
|
// just compare them. If someone sends a miscoded hash that should be
|
||||||
|
// equivalent, we won't match it and it won't work. But it's not worth
|
||||||
|
// accommodating, because no one is going to be hand copying hashes.
|
||||||
}
|
}
|
||||||
|
|
||||||
// MeshCoreOne-style reaction hashes don't depend on the sender name,
|
// MeshCoreOne-style reaction hashes don't depend on the sender name,
|
||||||
@@ -159,19 +164,43 @@ class ReactionHelper {
|
|||||||
return parseReactionOurs(text) ?? parseReactionMC1(text);
|
return parseReactionOurs(text) ?? parseReactionMC1(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool _looksLikeEmoji(String emoji) {
|
||||||
|
// Make sure it's a single grapheme.
|
||||||
|
if (Characters(emoji).length > 1) return false;
|
||||||
|
|
||||||
|
// Below are some emoji-validating tests, copied from MeshTrax.
|
||||||
|
// https://github.com/venamartin/meshtrax/
|
||||||
|
// It's nice to avoid false positives just to save us some processing,
|
||||||
|
// but it's not crucial, as we will either apply a reaction or show it
|
||||||
|
// as a message. Text that is not actually a hash is unlikely to match
|
||||||
|
// a message.
|
||||||
|
if (emoji.isEmpty) return false;
|
||||||
|
// Emoji, not prose: nearly all emoji start at U+2000 or above; the
|
||||||
|
// exceptions (keycaps, ©/®) carry a variation selector U+FE0F or a
|
||||||
|
// combining keycap U+20E3. Ordinary ASCII text fails both tests.
|
||||||
|
if (!(emoji.runes.first >= 0x2000 ||
|
||||||
|
emoji.runes.any((r) => r == 0xFE0F || r == 0x20E3))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static ReactionInfo? parseReactionMC1(String text) {
|
static ReactionInfo? parseReactionMC1(String text) {
|
||||||
// See https://github.com/Avi0n/MeshCoreOne/blob/main/docs/Reactions.md
|
// See https://github.com/Avi0n/MeshCoreOne/blob/main/docs/Reactions.md
|
||||||
// This regex matches both the channel format, which includes the sender name,
|
// This regex matches both the channel format, which includes the sender name,
|
||||||
// and the chat (DM) format, which omits it.
|
// and the chat (DM) format, which omits it.
|
||||||
final regex = RegExp(r'^(.{1,4})(?:@\[(.*)])?\n([a-zA-Z0-9]{8})$');
|
final regex = RegExp(r'^(.+?)(?:@\[(.*)])?\n([a-tv-zA-TV-Z0-9]{8})$');
|
||||||
final match = regex.firstMatch(text);
|
final match = regex.firstMatch(text);
|
||||||
if (match == null) return null;
|
if (match == null) return null;
|
||||||
|
|
||||||
final hash = match.group(3)?.toLowerCase();
|
final hash = match.group(3)?.toLowerCase();
|
||||||
final senderName = match.group(2);
|
final senderName = match.group(2);
|
||||||
|
final emoji = match.group(1)!;
|
||||||
|
if (!_looksLikeEmoji(emoji)) return null;
|
||||||
|
|
||||||
return ReactionInfo(
|
return ReactionInfo(
|
||||||
targetHash: _concatenateHashAndSender(hash!, senderName),
|
targetHash: _concatenateHashAndSender(hash!, senderName),
|
||||||
emoji: match.group(1)!,
|
emoji: emoji,
|
||||||
hashType: HashType.mc1,
|
hashType: HashType.mc1,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:characters/characters.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:meshcore_open/helpers/reaction_helper.dart';
|
import 'package:meshcore_open/helpers/reaction_helper.dart';
|
||||||
import 'package:meshcore_open/widgets/emoji_picker.dart';
|
import 'package:meshcore_open/widgets/emoji_picker.dart';
|
||||||
@@ -341,15 +342,30 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
group('MeshCoreOne Reactions', () {
|
group('MeshCoreOne Reactions', () {
|
||||||
test('regex for matching emoji char', () {
|
test('emoji validation sanity checks', () {
|
||||||
// This is more a test of expectations than of app code, but it was
|
// This is more a test of expectations than of app code, but it was
|
||||||
// a good place to get this sorted out cleanly, and I leave it for
|
// a good place to get this sorted out cleanly, and I leave it for
|
||||||
// anyone interested in understanding the regex in the parsing.
|
// anyone interested in understanding the emoji validation and tests.
|
||||||
|
expect('🥳'.length, equals(2));
|
||||||
|
expect('\u{1f44c}\u{1f3ff}'.length, equals(4));
|
||||||
|
final oneChar = RegExp(r'^(.)$');
|
||||||
|
final twoChar = RegExp(r'^(..)$');
|
||||||
|
expect(oneChar.firstMatch('🥳'), isNull);
|
||||||
|
expect(oneChar.firstMatch('\u{1F642}'), isNull);
|
||||||
|
expect(oneChar.firstMatch('\u{1f44c}\u{1f3ff}'), isNull);
|
||||||
|
expect(twoChar.firstMatch('\u{1f44c}\u{1f3ff}'), isNull);
|
||||||
|
|
||||||
final regex = RegExp(r'^(.{1,4})$');
|
final regex = RegExp(r'^(.{1,4})$');
|
||||||
expect(regex.firstMatch('🥳'), isNotNull);
|
expect(regex.firstMatch('🥳'), isNotNull);
|
||||||
expect(regex.firstMatch('😀'), isNotNull);
|
expect(regex.firstMatch('😀'), isNotNull);
|
||||||
expect(regex.firstMatch('\u{1F642}'), isNotNull);
|
expect(regex.firstMatch('\u{1F642}'), isNotNull);
|
||||||
expect(regex.firstMatch('\u{1f44c}\u{1f3ff}'), isNotNull);
|
expect(regex.firstMatch('\u{1f44c}\u{1f3ff}'), isNotNull);
|
||||||
|
|
||||||
|
expect(Characters('🥳').length, equals(1));
|
||||||
|
expect(Characters('\u{1F642}').length, equals(1));
|
||||||
|
expect(Characters('\u{1F642}').length, equals(1));
|
||||||
|
expect(Characters('\u{1f44c}\u{1f3ff}').length, equals(1));
|
||||||
|
expect(Characters('🧑🧑🧒🧒').length, equals(1));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('parse MeshCoreOne reaction', () {
|
test('parse MeshCoreOne reaction', () {
|
||||||
@@ -362,6 +378,34 @@ void main() {
|
|||||||
expect(info, isNotNull);
|
expect(info, isNotNull);
|
||||||
expect(info!.targetHash, equals('$hash:$senderName'));
|
expect(info!.targetHash, equals('$hash:$senderName'));
|
||||||
expect(info.emoji, equals(emoji));
|
expect(info.emoji, equals(emoji));
|
||||||
|
|
||||||
|
List<String> 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.parseReactionMC1(txt);
|
||||||
|
expect(info, isNotNull, reason: "$txt did not parse");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parse MeshCoreOne reaction - avoid false positives', () {
|
||||||
|
List<String> 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.parseReactionMC1(txt);
|
||||||
|
expect(info, isNull, reason: "$txt should not have parsed");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('parse MeshCoreOne reaction - main entry', () {
|
test('parse MeshCoreOne reaction - main entry', () {
|
||||||
@@ -385,7 +429,7 @@ void main() {
|
|||||||
expect(info.emoji, equals(emoji));
|
expect(info.emoji, equals(emoji));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('compute MeshCoreOne reaction hash', () {
|
test('computeq MeshCoreOne reaction hash', () {
|
||||||
const timestamp = 1234567890;
|
const timestamp = 1234567890;
|
||||||
const senderName = 'Alice';
|
const senderName = 'Alice';
|
||||||
const messageText = 'Hello world!';
|
const messageText = 'Hello world!';
|
||||||
|
|||||||
Reference in new issue
Block a user