mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
Prevent duplicate hash channels
This commit is contained in:
@@ -830,6 +830,14 @@ class ConnectionProvider with ChangeNotifier {
|
|||||||
return secret.any((byte) => byte != 0);
|
return secret.any((byte) => byte != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
static bool isDuplicateChannelName({
|
||||||
|
required String requestedName,
|
||||||
|
required String existingName,
|
||||||
|
}) {
|
||||||
|
return requestedName.startsWith('#') && existingName == requestedName;
|
||||||
|
}
|
||||||
|
|
||||||
bool _channelSlotIsOccupied(Object channel) {
|
bool _channelSlotIsOccupied(Object channel) {
|
||||||
final channelName = (channel as dynamic).name as String?;
|
final channelName = (channel as dynamic).name as String?;
|
||||||
final secret = (channel as dynamic).secret;
|
final secret = (channel as dynamic).secret;
|
||||||
@@ -986,8 +994,9 @@ class ConnectionProvider with ChangeNotifier {
|
|||||||
// Determine channel type
|
// Determine channel type
|
||||||
final bool isHashChannel = channelName.startsWith('#');
|
final bool isHashChannel = channelName.startsWith('#');
|
||||||
|
|
||||||
// Check for duplicate channels
|
// Match meshcore-open behavior:
|
||||||
int? existingSlot;
|
// - deterministic hash channels (#name) cannot be duplicated
|
||||||
|
// - private channels always use the next empty slot, even if the name matches
|
||||||
if (getChannelInfo != null) {
|
if (getChannelInfo != null) {
|
||||||
final maxChannels = _deviceInfo.maxChannels ?? 40;
|
final maxChannels = _deviceInfo.maxChannels ?? 40;
|
||||||
for (int i = 1; i < maxChannels; i++) {
|
for (int i = 1; i < maxChannels; i++) {
|
||||||
@@ -995,8 +1004,12 @@ class ConnectionProvider with ChangeNotifier {
|
|||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
final existingName = (channel as dynamic).name as String?;
|
final existingName = (channel as dynamic).name as String?;
|
||||||
if (existingName != null && existingName.isNotEmpty) {
|
if (existingName != null && existingName.isNotEmpty) {
|
||||||
// For hash channels (#name), check exact match to prevent duplicates
|
if (
|
||||||
if (isHashChannel && existingName == channelName) {
|
isDuplicateChannelName(
|
||||||
|
requestedName: channelName,
|
||||||
|
existingName: existingName,
|
||||||
|
)
|
||||||
|
) {
|
||||||
debugPrint(
|
debugPrint(
|
||||||
' ⚠️ Hash channel "$channelName" already exists in slot $i',
|
' ⚠️ Hash channel "$channelName" already exists in slot $i',
|
||||||
);
|
);
|
||||||
@@ -1004,27 +1017,12 @@ class ConnectionProvider with ChangeNotifier {
|
|||||||
'Channel "$channelName" already exists. Hash channels cannot be duplicated.',
|
'Channel "$channelName" already exists. Hash channels cannot be duplicated.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// For private channels, check name match to allow overwrite
|
|
||||||
else if (!isHashChannel && existingName == channelName) {
|
|
||||||
debugPrint(
|
|
||||||
' ℹ️ Private channel "$channelName" found in slot $i - will overwrite',
|
|
||||||
);
|
|
||||||
existingSlot = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine slot to use
|
// Find next empty slot for any new channel.
|
||||||
final int slotIdx;
|
|
||||||
if (existingSlot != null) {
|
|
||||||
// Overwrite existing private channel
|
|
||||||
slotIdx = existingSlot;
|
|
||||||
debugPrint(' Using existing slot: $slotIdx (overwrite mode)');
|
|
||||||
} else {
|
|
||||||
// Find next empty slot for new channel
|
|
||||||
final maxChannels = _deviceInfo.maxChannels ?? 40;
|
final maxChannels = _deviceInfo.maxChannels ?? 40;
|
||||||
final maxCustomChannels = maxChannels > 0 ? maxChannels - 1 : 0;
|
final maxCustomChannels = maxChannels > 0 ? maxChannels - 1 : 0;
|
||||||
final emptySlot = await findNextEmptyChannelSlot();
|
final emptySlot = await findNextEmptyChannelSlot();
|
||||||
@@ -1033,9 +1031,8 @@ class ConnectionProvider with ChangeNotifier {
|
|||||||
'All channel slots are in use (maximum $maxCustomChannels custom channels)',
|
'All channel slots are in use (maximum $maxCustomChannels custom channels)',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
slotIdx = emptySlot;
|
final slotIdx = emptySlot;
|
||||||
debugPrint(' Using empty slot: $slotIdx (new channel)');
|
debugPrint(' Using empty slot: $slotIdx (new channel)');
|
||||||
}
|
|
||||||
|
|
||||||
// Generate secret
|
// Generate secret
|
||||||
final List<int> secretBytes;
|
final List<int> secretBytes;
|
||||||
@@ -1076,7 +1073,7 @@ class ConnectionProvider with ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
debugPrint(
|
debugPrint(
|
||||||
'✅ [Provider] Channel ${existingSlot != null ? 'updated' : 'created'} successfully in slot $slotIdx',
|
'✅ [Provider] Channel created successfully in slot $slotIdx',
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_error = 'Failed to create channel: $e';
|
_error = 'Failed to create channel: $e';
|
||||||
|
|||||||
@@ -17,5 +17,25 @@ void main() {
|
|||||||
isTrue,
|
isTrue,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('treats duplicate hashtag channels as conflicts', () {
|
||||||
|
expect(
|
||||||
|
ConnectionProvider.isDuplicateChannelName(
|
||||||
|
requestedName: '#sar',
|
||||||
|
existingName: '#sar',
|
||||||
|
),
|
||||||
|
isTrue,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('allows private channels with the same name to coexist', () {
|
||||||
|
expect(
|
||||||
|
ConnectionProvider.isDuplicateChannelName(
|
||||||
|
requestedName: 'Ops',
|
||||||
|
existingName: 'Ops',
|
||||||
|
),
|
||||||
|
isFalse,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user