Stamp legacy images inside the migration; hold early channel frames

- Image entries are stamped with their channel ID before the migration is
  marked done, so an interrupted run repeats it instead of leaving them
  unstamped (and invisible) forever.
- Channel message/data frames that arrive before this session's slot map
  is confirmed are held and replayed once it is. Current firmware only
  sends them in reply to SYNC_NEXT_MESSAGE, which already waits; this
  guards against a stale saved map filing them under the wrong channel.
This commit is contained in:
zjs81 committed 2026-10-11 18:04:53 -07:00
1 parent 679116ed03
commit 6a8df764bb
4 files changed
+87 -5

No files matched your search

+31 -4
View File
@@ -379,6 +379,12 @@ class MeshCoreConnector extends ChangeNotifier {
bool _isProcessingDeferredQueuedContactMessages = false;
bool _queuedMessageSyncInFlight = false;
final List<Uint8List> _deferredQueuedContactMessageFrames = [];
/// Channel frames that arrived before this session's slot map was
/// confirmed. The firmware only sends them in reply to SYNC_NEXT_MESSAGE,
/// which waits for the map, so this is a guard: their slot index cannot be
/// resolved yet, and they are replayed once it can.
final List<Uint8List> _channelFramesAwaitingMap = [];
bool _pendingQueueSync = false;
// CMD_APP_START stops the firmware's contact iterator (END_OF_CONTACTS is
// then never sent), so APP_START requested during a contact sync is held
@@ -3431,6 +3437,7 @@ class MeshCoreConnector extends ChangeNotifier {
_isProcessingDeferredQueuedContactMessages = false;
_queuedMessageSyncInFlight = false;
_deferredQueuedContactMessageFrames.clear();
_channelFramesAwaitingMap.clear();
_pendingQueueSync = false;
_resetSyncFlowState();
_queueSyncTimeout?.cancel();
@@ -5682,6 +5689,19 @@ class MeshCoreConnector extends ChangeNotifier {
_finishChannelSync();
}
void _replayChannelFramesAwaitingMap() {
if (_channelFramesAwaitingMap.isEmpty) return;
final frames = List<Uint8List>.of(_channelFramesAwaitingMap);
_channelFramesAwaitingMap.clear();
for (final frame in frames) {
if (frame[0] != respCodeChannelDataRecv) {
_handleIncomingChannelMessage(frame);
} else if (_appSettingsService?.settings.imageMessagesEnabled ?? false) {
_handleIncomingChannelData(frame);
}
}
}
/// A channel whose key changed in place keeps its position in the custom
/// order: [after] takes [before]'s place when no slot holds [before] any
/// more. Returns true when the order changed.
@@ -5714,6 +5734,7 @@ class MeshCoreConnector extends ChangeNotifier {
}
void _finishChannelSync() {
if (_channelMapConfirmed) _replayChannelFramesAwaitingMap();
final completer = _channelSyncCompleter;
_channelSyncCompleter = null;
if (completer != null && !completer.isCompleted) completer.complete();
@@ -5745,12 +5766,11 @@ class MeshCoreConnector extends ChangeNotifier {
// First: the channel sync is already running and carries unread counts
// over from these. The migration never touches the channel cache.
await loadCachedChannels();
final migrated = await ChannelStorageMigration.migrate(
await ChannelStorageMigration.migrate(
_channelStore.publicKeyHex,
beforeDone: (slotIds) async =>
_receivedImageStore?.stampLegacyChannelIds(slotIds),
);
if (migrated.isNotEmpty) {
await _receivedImageStore?.stampLegacyChannelIds(migrated);
}
await _loadChannelOrder();
final saved = await _channelStore.loadSlotChannelIds();
_slotChannelIds
@@ -5967,6 +5987,13 @@ class MeshCoreConnector extends ChangeNotifier {
unawaited(_handleIncomingMessage(frame));
}
break;
case respCodeChannelMsgRecv ||
respCodeChannelMsgRecvV3 ||
respCodeChannelDataRecv
when !_channelMapConfirmed:
if (_isSyncingQueuedMessages) _handleQueuedMessageReceived();
_channelFramesAwaitingMap.add(frame);
break;
case respCodeChannelMsgRecv:
case respCodeChannelMsgRecvV3:
_handleIncomingChannelMessage(frame);