diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 1be91d62..acf6cf7f 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -4366,7 +4366,7 @@ class MeshCoreConnector extends ChangeNotifier { _pathOpLock = completer.future; await prev; try { - if (!isConnected) return; + if (!isConnected || !_isKnownContact(contact)) return; final width = pathHashWidth ?? @@ -4409,6 +4409,13 @@ class MeshCoreConnector extends ChangeNotifier { } } + /// Whether [contact] is still in the radio's contact list. The firmware's + /// CMD_ADD_UPDATE_CONTACT adds unknown keys, so path/flag writes for a + /// removed contact (e.g. a repeater screen still open after its old key was + /// deleted) would re-create it on the radio. + bool _isKnownContact(Contact contact) => + _contacts.any((c) => c.publicKeyHex == contact.publicKeyHex); + /// The advert timestamp the firmware holds for [contact] (Contact.lastSeen /// is parsed from that field); the stored copy is freshest. DateTime _deviceLastAdvert(Contact contact) { @@ -4425,7 +4432,7 @@ class MeshCoreConnector extends ChangeNotifier { bool? teleLoc, bool? teleEnv, }) async { - if (!isConnected) return; + if (!isConnected || !_isKnownContact(contact)) return; final latestContact = await _fetchContactSnapshotFromDevice(contact.publicKey) ?? contact; int updatedFlags = isFavorite != null diff --git a/test/connector/remove_contact_test.dart b/test/connector/remove_contact_test.dart index 6428c33b..9abb7eaf 100644 --- a/test/connector/remove_contact_test.dart +++ b/test/connector/remove_contact_test.dart @@ -1,3 +1,5 @@ +import 'dart:typed_data'; + import 'package:flutter_test/flutter_test.dart'; import 'package:meshcore_open/connector/meshcore_connector.dart'; import 'package:meshcore_open/connector/meshcore_protocol.dart'; @@ -17,6 +19,20 @@ Future _waitFor(bool Function() condition, {int seconds = 8}) async { } } +/// Review radio that records every frame the app sends. +class _RecordingReviewRadio extends ReviewRadio { + final List sent = []; + + _RecordingReviewRadio() + : super(scheduledMessageInterval: const Duration(hours: 1)); + + @override + void handle(Uint8List frame) { + sent.add(frame); + super.handle(frame); + } +} + void main() { TestWidgetsFlutterBinding.ensureInitialized(); @@ -35,8 +51,8 @@ void main() { retryService: retryService, pathHistoryService: PathHistoryService(StorageService()), ); - connector.reviewRadioFactoryForTest = () => - ReviewRadio(scheduledMessageInterval: const Duration(hours: 1)); + final radio = _RecordingReviewRadio(); + connector.reviewRadioFactoryForTest = () => radio; await connector.connectReview(); await _waitFor( @@ -66,6 +82,15 @@ void main() { final saved = await store.loadContacts(); expect(saved.any((c) => c.publicKeyHex == contact.publicKeyHex), isFalse); + // An open chat/repeater screen still syncs the path before each send, and + // the favourite toggle writes flags. On firmware CMD_ADD_UPDATE_CONTACT + // adds unknown keys, so neither may be sent for the removed contact. + radio.sent.clear(); + await connector.setContactPath(contact, Uint8List.fromList([0x42]), 1); + await connector.preparePathForContactSend(contact); + await connector.setContactFlags(contact, isFavorite: true); + expect(radio.sent.where((f) => f.first == cmdAddUpdateContact), isEmpty); + await connector.disconnect(manual: true); });