display Regions in received Messages and allow reply to same region.

This commit is contained in:
ericz committed 2026-09-21 17:52:33 +02:00
1 parent d5f54dfa11
commit 20e01145e6
9 files changed
+177 -15

No files matched your search

+35 -5
View File
@@ -3572,8 +3572,10 @@ class MeshCoreConnector extends ChangeNotifier {
String? originalText,
String? translatedLanguageCode,
String? translationModelId,
String? region,
}) async {
if (!isConnected || text.isEmpty) return;
final sendRegion = region ?? getChannelRegion(channel.index);
// Check if this is a reaction - if so, process it immediately instead of adding as a message
final reactionInfo = ReactionHelper.parseReaction(text);
@@ -3631,6 +3633,7 @@ class MeshCoreConnector extends ChangeNotifier {
originalText: originalText,
translatedLanguageCode: translatedLanguageCode,
translationModelId: translationModelId,
region: sendRegion,
);
_addChannelMessage(channel.index, message);
_pendingChannelSentQueue.add(message.messageId);
@@ -3645,7 +3648,7 @@ class MeshCoreConnector extends ChangeNotifier {
expectsGenericAck: true,
successCode: respCodeSent,
);
}, region: getChannelRegion(channel.index));
}, region: sendRegion);
}
/// Minimum companion firmware version code that implements
@@ -5821,6 +5824,7 @@ class MeshCoreConnector extends ChangeNotifier {
pathHashWidth: packet.pathHashWidth,
pathBytes: packet.pathBytes,
channelIndex: channel.index,
region: _resolveTransportRegion(packet),
packetHash: pktHash,
);
@@ -6423,10 +6427,7 @@ class MeshCoreConnector extends ChangeNotifier {
final hasTransport =
routeType == _routeTransportFlood ||
routeType == _routeTransportDirect;
if (hasTransport) {
// Skip reserved bytes in transport header made up of two u16 fields
reader.skipBytes(4);
}
final transportCodes = hasTransport ? reader.readBytes(4) : null;
final pathLenRaw = reader.readByte();
final pathByteLen = _decodePathByteLen(pathLenRaw);
final pathBytes = reader.readBytes(pathByteLen);
@@ -6438,6 +6439,7 @@ class MeshCoreConnector extends ChangeNotifier {
payloadType: (header >> _phTypeShift) & _phTypeMask,
payloadVer: (header >> _phVerShift) & _phVerMask,
pathLenRaw: pathLenRaw,
transportCodes: transportCodes,
pathBytes: pathBytes,
payload: payload,
);
@@ -6452,6 +6454,30 @@ class MeshCoreConnector extends ChangeNotifier {
return digest[0];
}
String? _resolveTransportRegion(_RawPacket packet) {
if (packet.routeType != _routeTransportFlood ||
packet.transportCodes == null) {
return null;
}
final candidates = <String>{
...RegionStore().loadRegions(),
..._channelRegions.values,
}.where((region) => region.trim().isNotEmpty).toList()..sort();
String? match;
for (final region in candidates) {
final code = floodTransportCode(
scopeKey: floodScopeKeyForRegion(region),
payloadType: packet.payloadType,
payload: packet.payload,
);
if (!_pathsEqual(code, packet.transportCodes!.sublist(0, 2))) continue;
if (match != null) return null;
match = region;
}
return match;
}
/// Firmware-compatible packet hash: SHA256(payloadType + payload) -> first 8 bytes as hex.
String _computePacketHash(int payloadType, Uint8List payload) {
final input = Uint8List(1 + payload.length);
@@ -6599,6 +6625,7 @@ class MeshCoreConnector extends ChangeNotifier {
pathBytes: message.pathBytes,
pathVariants: message.pathVariants,
channelIndex: message.channelIndex,
region: message.region,
messageId: message.messageId,
replyToMessageId: originalMessage.messageId,
replyToSenderName: originalMessage.senderName,
@@ -6635,6 +6662,7 @@ class MeshCoreConnector extends ChangeNotifier {
pathHashWidth: existing.pathHashWidth ?? processedMessage.pathHashWidth,
pathBytes: mergedPathBytes,
pathVariants: mergedPathVariants,
region: existing.region ?? processedMessage.region,
packetHash: existing.packetHash ?? processedMessage.packetHash,
// Mark as sent when first repeat is heard
status: promotedFromPending
@@ -7586,6 +7614,7 @@ class _RawPacket {
final int payloadType;
final int payloadVer;
final int pathLenRaw;
final Uint8List? transportCodes;
final Uint8List pathBytes;
final Uint8List payload;
@@ -7595,6 +7624,7 @@ class _RawPacket {
required this.payloadType,
required this.payloadVer,
required this.pathLenRaw,
required this.transportCodes,
required this.pathBytes,
required this.payload,
});
+23 -3
View File
@@ -1040,9 +1040,29 @@ Uint8List buildSetFloodScopeFrame(String region) {
return Uint8List.fromList([cmdSetFloodScope, 0]);
}
final name = region.startsWith('#') ? region : '#$region';
final hash = crypto.sha256.convert(utf8.encode(name)).bytes;
final scope = Uint8List.fromList(hash.sublist(0, 16));
final scope = floodScopeKeyForRegion(region);
return Uint8List.fromList([cmdSetFloodScope, 0, ...scope]);
}
Uint8List floodScopeKeyForRegion(String region) {
final name = region.startsWith('#') ? region : '#$region';
final hash = crypto.sha256.convert(utf8.encode(name)).bytes;
return Uint8List.fromList(hash.sublist(0, 16));
}
Uint8List floodTransportCode({
required Uint8List scopeKey,
required int payloadType,
required Uint8List payload,
}) {
final input = Uint8List(1 + payload.length)
..[0] = payloadType
..setRange(1, 1 + payload.length, payload);
final code = crypto.Hmac(crypto.sha256, scopeKey).convert(input).bytes;
if (code[0] == 0 && code[1] == 0) return Uint8List.fromList([1, 0]);
if (code[0] == 0xFF && code[1] == 0xFF) {
return Uint8List.fromList([0xFE, 0xFF]);
}
return Uint8List.fromList(code.sublist(0, 2));
}
+6
View File
@@ -45,6 +45,7 @@ class ChannelMessage {
final Uint8List pathBytes;
final List<Uint8List> pathVariants;
final int? channelIndex;
final String? region;
final String messageId;
final String? packetHash;
final String? replyToMessageId;
@@ -71,6 +72,7 @@ class ChannelMessage {
Uint8List? pathBytes,
List<Uint8List>? pathVariants,
this.channelIndex,
this.region,
String? messageId,
this.packetHash,
this.replyToMessageId,
@@ -98,6 +100,7 @@ class ChannelMessage {
int? pathHashWidth,
Uint8List? pathBytes,
List<Uint8List>? pathVariants,
Object? region = _unset,
String? packetHash,
String? replyToMessageId,
String? replyToSenderName,
@@ -136,6 +139,7 @@ class ChannelMessage {
pathBytes: pathBytes ?? this.pathBytes,
pathVariants: pathVariants ?? this.pathVariants,
channelIndex: channelIndex,
region: region == _unset ? this.region : region as String?,
messageId: messageId,
packetHash: packetHash ?? this.packetHash,
replyToMessageId: replyToMessageId ?? this.replyToMessageId,
@@ -238,6 +242,7 @@ class ChannelMessage {
String? originalText,
String? translatedLanguageCode,
String? translationModelId,
String? region,
}) {
return ChannelMessage(
senderKey: null,
@@ -253,6 +258,7 @@ class ChannelMessage {
pathBytes: Uint8List(0),
pathVariants: const [],
channelIndex: channelIndex,
region: region,
);
}
+79 -3
View File
@@ -77,6 +77,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
final ChatScrollController _scrollController = ChatScrollController();
final FocusNode _textFieldFocusNode = FocusNode();
ChannelMessage? _replyingToMessage;
bool _useReplyRegion = false;
final CommunityStore _communityStore = CommunityStore();
final CommunityPskIndex _communityIndex = CommunityPskIndex();
final Map<String, GlobalKey> _messageKeys = {};
@@ -210,12 +211,14 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
void _setReplyingTo(ChannelMessage message) {
setState(() {
_replyingToMessage = message;
_useReplyRegion = message.region?.isNotEmpty ?? false;
});
}
void _cancelReply() {
setState(() {
_replyingToMessage = null;
_useReplyRegion = false;
});
}
@@ -809,9 +812,41 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
RouteChip(
isDirect: (message.pathLength ?? -1) >= 0,
hops: displayHopCount,
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
RouteChip(
isDirect: (message.pathLength ?? -1) >= 0,
hops: displayHopCount,
),
if (message.region?.isNotEmpty ?? false)
Padding(
padding: const EdgeInsets.only(top: 2),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
color: scheme.surfaceContainerHigh,
borderRadius: BorderRadius.circular(
MeshRadii.xs,
),
border: Border.all(
color: scheme.outlineVariant,
),
),
child: Text(
_displayRegion(message.region!),
style: MeshTheme.mono(
fontSize: 8.5 * textScale,
color: metaColor,
),
),
),
),
],
),
const SizedBox(width: 4),
Flexible(
@@ -1670,6 +1705,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
Widget _buildReplyBanner(double textScale) {
final message = _replyingToMessage!;
final scheme = Theme.of(context).colorScheme;
final replyRegion = message.region;
return Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
@@ -1707,6 +1743,40 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
],
),
),
if (replyRegion?.isNotEmpty ?? false)
InkWell(
borderRadius: BorderRadius.circular(MeshRadii.xs),
onTap: () => setState(() => _useReplyRegion = !_useReplyRegion),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
decoration: BoxDecoration(
color: _useReplyRegion
? scheme.primaryContainer
: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(MeshRadii.xs),
border: Border.all(
color: _useReplyRegion
? scheme.primary
: scheme.outlineVariant,
),
),
child: Text(
context.l10n.channels_regionSetTo(replyRegion!),
style:
MeshTheme.mono(
fontSize: 10 * textScale,
color: _useReplyRegion
? scheme.onPrimaryContainer
: scheme.onSurfaceVariant,
).copyWith(
decoration: _useReplyRegion
? null
: TextDecoration.lineThrough,
),
),
),
),
const SizedBox(width: 4),
IconButton(
icon: const Icon(Icons.close, size: 18),
onPressed: _cancelReply,
@@ -2032,6 +2102,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
if (_replyingToMessage != null) {
messageText = '@[${_replyingToMessage!.senderName}] $messageText';
}
final replyRegion = _useReplyRegion ? _replyingToMessage?.region : null;
final maxBytes = maxChannelMessageBytes(connector.selfName);
final outboundText = connector.prepareChannelOutboundText(
@@ -2064,6 +2135,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
originalText: originalText,
translatedLanguageCode: translatedLanguageCode,
translationModelId: translationModelId,
region: replyRegion,
);
}
@@ -2282,6 +2354,10 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
return PathHelper.splitPathBytes(pathBytes, pathHashByteWidth).length;
}
String _displayRegion(String region) {
return region.startsWith('#') ? region.substring(1) : region;
}
Future<void> openRegionSelectDialog(Channel channel) async {
// The AppBar subtitle reads the region from the connector inside a
// Consumer, so setChannelRegion's notifyListeners refreshes it directly —
@@ -211,6 +211,13 @@ class ChannelMessagePathScreen extends StatelessWidget {
observedLabel,
scheme: scheme,
),
if (message.region?.isNotEmpty ?? false)
_buildDetailRow(
context,
l10n.settings_regionName,
message.region!,
scheme: scheme,
),
],
),
);
+1 -3
View File
@@ -11,9 +11,7 @@ import '../connector/meshcore_protocol.dart';
/// do the same so the repeater always gets the phone's time.
String normalizeRepeaterClockSyncCommand(String command, {int? nowSeconds}) {
final epoch = nowSeconds ?? DateTime.now().millisecondsSinceEpoch ~/ 1000;
return command.trim().toLowerCase() == 'clock sync'
? 'time $epoch'
: command;
return command.trim().toLowerCase() == 'clock sync' ? 'time $epoch' : command;
}
class RepeaterCommandService {
+2
View File
@@ -108,6 +108,7 @@ class ChannelMessageStore {
'isOutgoing': msg.isOutgoing,
'status': msg.status.index,
'channelIndex': msg.channelIndex,
'region': msg.region,
'repeatCount': msg.repeatCount,
'pathLength': msg.pathLength,
'pathHashWidth': msg.pathHashWidth,
@@ -188,6 +189,7 @@ class ChannelMessageStore {
.toList() ??
const [],
channelIndex: json['channelIndex'] as int?,
region: json['region'] as String?,
messageId: json['messageId'] as String?,
packetHash: json['packetHash'] as String?,
replyToMessageId: json['replyToMessageId'] as String?,
+20
View File
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:crypto/crypto.dart' as crypto;
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/models/channel_message.dart';
@@ -148,6 +149,25 @@ void main() {
});
});
test('flood transport code uses the scope key HMAC', () {
final scopeKey = floodScopeKeyForRegion('Europe');
final payload = Uint8List.fromList([0xA1, 0xB2, 0xC3]);
expect(
floodTransportCode(
scopeKey: scopeKey,
payloadType: 0x05,
payload: payload,
),
orderedEquals(
crypto.Hmac(
crypto.sha256,
scopeKey,
).convert([0x05, ...payload]).bytes.sublist(0, 2),
),
);
});
group('Contact.fromFrame — pathLen mapping', () {
test('pathLen == 0 → pathLength == 0 (direct, NOT flood)', () {
final frame = _buildContactFrame(pathLen: 0);
@@ -19,7 +19,10 @@ void main() {
});
test('passes other commands through unchanged', () {
expect(normalizeRepeaterClockSyncCommand('clock', nowSeconds: 1), 'clock');
expect(
normalizeRepeaterClockSyncCommand('clock', nowSeconds: 1),
'clock',
);
expect(
normalizeRepeaterClockSyncCommand('get radio', nowSeconds: 1),
'get radio',