Handle MeshCoreOne's emoji reactions
MeshCoreOne is a companion app that supports emoji reactions, using a different message format than ours. This commit adds support for parsing and displaying reactions received in the MeshCoreOne format. Their format is described here: https://github.com/Avi0n/MeshCoreOne/blob/main/docs/Reactions.md Here's an example: 👍@[entropy] b3je6qf7 Major differences compared to MeshCore-Open's reaction messages: - includes the emoji, rather than an index code - includes the name of who sent the message it refers to - the hash does not depend on the sender name
This commit is contained in:
1 parent
e6cea37e77
commit
97f20eaec0
4 files changed
+141
-6
No files matched your search
@@ -1,13 +1,22 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:crypto/crypto.dart';
|
||||||
|
import 'package:base32/base32.dart';
|
||||||
|
import 'package:base32/encodings.dart';
|
||||||
import '../widgets/emoji_picker.dart';
|
import '../widgets/emoji_picker.dart';
|
||||||
|
|
||||||
|
enum HashType { ours, mc1 }
|
||||||
|
|
||||||
class ReactionInfo {
|
class ReactionInfo {
|
||||||
final String targetHash;
|
final String targetHash;
|
||||||
final String emoji;
|
final String emoji;
|
||||||
String? senderName;
|
HashType hashType;
|
||||||
|
String? senderName; // Who sent the reaction
|
||||||
|
|
||||||
ReactionInfo({
|
ReactionInfo({
|
||||||
required this.targetHash,
|
required this.targetHash,
|
||||||
required this.emoji,
|
required this.emoji,
|
||||||
|
required this.hashType,
|
||||||
this.senderName,
|
this.senderName,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -40,11 +49,18 @@ class ReactionHelper {
|
|||||||
updateMessage,
|
updateMessage,
|
||||||
}) {
|
}) {
|
||||||
final targetHash = reactionInfo.targetHash;
|
final targetHash = reactionInfo.targetHash;
|
||||||
|
final hashFunc = switch (reactionInfo.hashType) {
|
||||||
|
HashType.ours => computeReactionHash,
|
||||||
|
HashType.mc1 => ((ts, senderName, text) => _concatenateHashAndSender(
|
||||||
|
computeReactionHashMC1(ts, senderName, text),
|
||||||
|
senderName,
|
||||||
|
)),
|
||||||
|
};
|
||||||
for (int i = messages.length - 1; i >= 0; i--) {
|
for (int i = messages.length - 1; i >= 0; i--) {
|
||||||
final msg = messages[i];
|
final msg = messages[i];
|
||||||
if (shouldSkip(msg)) continue;
|
if (shouldSkip(msg)) continue;
|
||||||
|
|
||||||
final msgHash = computeReactionHash(
|
final msgHash = hashFunc(
|
||||||
getTimestampSecs(msg),
|
getTimestampSecs(msg),
|
||||||
getSenderName(msg),
|
getSenderName(msg),
|
||||||
getMessageText(msg),
|
getMessageText(msg),
|
||||||
@@ -108,17 +124,72 @@ class ReactionHelper {
|
|||||||
return hash.toRadixString(16).padLeft(4, '0');
|
return hash.toRadixString(16).padLeft(4, '0');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute the type of reaction hash used by MeshCoreOne.
|
||||||
|
static String computeReactionHashMC1(
|
||||||
|
int timestampSeconds,
|
||||||
|
String? senderName,
|
||||||
|
String messageText,
|
||||||
|
) {
|
||||||
|
// TODO: unit tests
|
||||||
|
// raw hash is first 5 bytes of SHA-256(UTF-8 text + uint32-LE sender timestamp)
|
||||||
|
Uint8List messageBytes = utf8.encode(messageText);
|
||||||
|
ByteData timestampBytes = ByteData(4)
|
||||||
|
..setUint32(0, timestampSeconds, Endian.little);
|
||||||
|
final hash = sha256
|
||||||
|
.convert(messageBytes + timestampBytes.buffer.asUint8List())
|
||||||
|
.bytes
|
||||||
|
.sublist(0, 5);
|
||||||
|
|
||||||
|
// encode as 8 chars of Crockford base32
|
||||||
|
return base32
|
||||||
|
.encode(Uint8List.fromList(hash), encoding: Encoding.crockford)
|
||||||
|
.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
// MeshCoreOne-style reaction hashes don't depend on the sender name,
|
||||||
|
// and we're expected to check the hash and sender name separately.
|
||||||
|
// Our own reaction hashes include it. Rather than store the sender
|
||||||
|
// Name in ReactionInfo and complicate the logic in applyReaction(),
|
||||||
|
// we just tack the senderName onto the end of the "hash".
|
||||||
|
static String _concatenateHashAndSender(String hash, String? senderName) {
|
||||||
|
return "$hash:${senderName ?? ''}";
|
||||||
|
}
|
||||||
|
|
||||||
|
static ReactionInfo? parseReaction(String text) {
|
||||||
|
return parseReactionOurs(text) ?? parseReactionMC1(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
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'^(.{1,4})(?:@\[(.*)])?\n([a-zA-Z0-9]{8})$');
|
||||||
|
final match = regex.firstMatch(text);
|
||||||
|
if (match == null) return null;
|
||||||
|
|
||||||
|
final hash = match.group(3)?.toLowerCase();
|
||||||
|
final senderName = match.group(2);
|
||||||
|
return ReactionInfo(
|
||||||
|
targetHash: _concatenateHashAndSender(hash!, senderName),
|
||||||
|
emoji: match.group(1)!,
|
||||||
|
hashType: HashType.mc1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse reaction format: r:HASH:INDEX (where INDEX is 2-char hex emoji index)
|
/// Parse reaction format: r:HASH:INDEX (where INDEX is 2-char hex emoji index)
|
||||||
/// Returns null if text is not a valid reaction format
|
/// Returns null if text is not a valid reaction format
|
||||||
static ReactionInfo? parseReaction(String text) {
|
static ReactionInfo? parseReactionOurs(String text) {
|
||||||
final regex = RegExp(r'^r:([0-9a-f]{4}):([0-9a-f]{2})$');
|
final regex = RegExp(r'^r:([0-9a-f]{4}):([0-9a-f]{2})$');
|
||||||
final match = regex.firstMatch(text);
|
final match = regex.firstMatch(text);
|
||||||
if (match == null) return null;
|
if (match == null) return null;
|
||||||
|
|
||||||
final emoji = indexToEmoji(match.group(2)!);
|
final emoji = indexToEmoji(match.group(2)!);
|
||||||
if (emoji == null) return null;
|
if (emoji == null) return null;
|
||||||
|
return ReactionInfo(
|
||||||
return ReactionInfo(targetHash: match.group(1)!, emoji: emoji);
|
targetHash: match.group(1)!,
|
||||||
|
emoji: emoji,
|
||||||
|
hashType: HashType.ours,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode a reaction message that parseReaction() can parse.
|
/// Encode a reaction message that parseReaction() can parse.
|
||||||
|
|||||||
@@ -318,7 +318,12 @@ class ChannelMessage {
|
|||||||
final emoji = entry.key;
|
final emoji = entry.key;
|
||||||
for (final senderName in entry.value) {
|
for (final senderName in entry.value) {
|
||||||
reactionList.add(
|
reactionList.add(
|
||||||
ReactionInfo(targetHash: hash, emoji: emoji, senderName: senderName),
|
ReactionInfo(
|
||||||
|
targetHash: hash, // won't be used for anything
|
||||||
|
emoji: emoji,
|
||||||
|
senderName: senderName,
|
||||||
|
hashType: HashType.ours, // also not used for anything
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ dependencies:
|
|||||||
# the abiFilters note in android/app/build.gradle.kts; a universal APK pays
|
# the abiFilters note in android/app/build.gradle.kts; a universal APK pays
|
||||||
# ~56 MB for ORT instead of ~18 MB.
|
# ~56 MB for ORT instead of ~18 MB.
|
||||||
flutter_onnxruntime: ^1.8.3
|
flutter_onnxruntime: ^1.8.3
|
||||||
|
base32: ^2.2.0
|
||||||
|
|
||||||
hooks:
|
hooks:
|
||||||
user_defines:
|
user_defines:
|
||||||
|
|||||||
@@ -340,6 +340,64 @@ void main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('MeshCoreOne Reactions', () {
|
||||||
|
test('regex for matching emoji char', () {
|
||||||
|
// 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
|
||||||
|
// anyone interested in understanding the regex in the parsing.
|
||||||
|
final regex = RegExp(r'^(.{1,4})$');
|
||||||
|
expect(regex.firstMatch('🥳'), isNotNull);
|
||||||
|
expect(regex.firstMatch('😀'), isNotNull);
|
||||||
|
expect(regex.firstMatch('\u{1F642}'), isNotNull);
|
||||||
|
expect(regex.firstMatch('\u{1f44c}\u{1f3ff}'), isNotNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parse MeshCoreOne reaction', () {
|
||||||
|
final emoji = '🥳';
|
||||||
|
final senderName = 'entropy 📓';
|
||||||
|
final hash = 'b3je6qf7';
|
||||||
|
final info = ReactionHelper.parseReactionMC1(
|
||||||
|
'$emoji@[$senderName]\n$hash',
|
||||||
|
);
|
||||||
|
expect(info, isNotNull);
|
||||||
|
expect(info!.targetHash, equals('$hash:$senderName'));
|
||||||
|
expect(info.emoji, equals(emoji));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parse MeshCoreOne reaction - main entry', () {
|
||||||
|
final emoji = '🥳';
|
||||||
|
final senderName = 'entropy 📓';
|
||||||
|
final hash = 'b3je6qf7';
|
||||||
|
final info = ReactionHelper.parseReaction(
|
||||||
|
'$emoji@[$senderName]\n$hash',
|
||||||
|
);
|
||||||
|
expect(info, isNotNull);
|
||||||
|
expect(info!.targetHash, equals('$hash:$senderName'));
|
||||||
|
expect(info.emoji, equals(emoji));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parse MeshCoreOne reaction for DM chats', () {
|
||||||
|
final emoji = '🥳';
|
||||||
|
final hash = 'b3je6qf7';
|
||||||
|
final info = ReactionHelper.parseReaction('$emoji\n$hash');
|
||||||
|
expect(info, isNotNull);
|
||||||
|
expect(info!.targetHash, equals('$hash:'));
|
||||||
|
expect(info.emoji, equals(emoji));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('compute MeshCoreOne reaction hash', () {
|
||||||
|
const timestamp = 1234567890;
|
||||||
|
const senderName = 'Alice';
|
||||||
|
const messageText = 'Hello world!';
|
||||||
|
final hash = ReactionHelper.computeReactionHashMC1(
|
||||||
|
timestamp,
|
||||||
|
senderName,
|
||||||
|
messageText,
|
||||||
|
);
|
||||||
|
expect(hash, equals('tyvbkb33'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
group('full reaction flow', () {
|
group('full reaction flow', () {
|
||||||
test('should encode and decode reaction correctly', () {
|
test('should encode and decode reaction correctly', () {
|
||||||
// Simulate sending a reaction
|
// Simulate sending a reaction
|
||||||
|
|||||||
Reference in new issue
Block a user