From 4f398a07df665213f1502170acb80500c6c91665 Mon Sep 17 00:00:00 2001 From: kenny lopez Date: Thu, 20 Aug 2026 08:31:46 +0100 Subject: [PATCH] fix(theme): ignore active mobile publish echoes Signed-off-by: kenny lopez --- .../shared/theme/community_theme_sync.dart | 28 ++++++++++++++++-- .../theme/community_theme_sync_test.dart | 29 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/mobile/lib/shared/theme/community_theme_sync.dart b/mobile/lib/shared/theme/community_theme_sync.dart index a8da2e1b77..73a6da6795 100644 --- a/mobile/lib/shared/theme/community_theme_sync.dart +++ b/mobile/lib/shared/theme/community_theme_sync.dart @@ -62,6 +62,9 @@ class CommunityThemeSyncManager { int _publishRetryAttempt = 0; bool _publishInFlight = false; bool _publishRequestedWhileInFlight = false; + int? _activePublishCreatedAt; + String? _activePublishEventId; + int? _activePublishRemoteRevision; int _remoteRevision = 0; bool _hydrationObserved = false; bool _disposed = false; @@ -345,6 +348,7 @@ class CommunityThemeSyncManager { _lastCreatedAt + 1, ); NostrEvent? signed; + _activePublishRemoteRevision = remoteRevision; await signedEventRelay.submit( kind: EventKind.readState, content: content, @@ -353,7 +357,11 @@ class CommunityThemeSyncManager { ['t', communityThemeDTag], ], createdAt: createdAt, - onSigned: (event) => signed = event, + onSigned: (event) { + signed = event; + _activePublishCreatedAt = event.createdAt; + _activePublishEventId = event.id; + }, ); if (_disposed) return; final published = signed; @@ -395,6 +403,9 @@ class CommunityThemeSyncManager { _schedulePublish(Duration(milliseconds: retryMs)); } finally { _publishInFlight = false; + _activePublishCreatedAt = null; + _activePublishEventId = null; + _activePublishRemoteRevision = null; if (!_disposed && _pending != null && (_publishRequestedWhileInFlight || _pending != preference) && @@ -432,10 +443,21 @@ class CommunityThemeSyncManager { remote.eventId.compareTo(_lastEventId) >= 0)) { return; } + final isActivePublishEcho = + _publishInFlight && + remote.createdAt == _activePublishCreatedAt && + remote.eventId == _activePublishEventId; + final competingRemoteSeen = + _activePublishRemoteRevision != null && + _remoteRevision != _activePublishRemoteRevision; _lastCreatedAt = remote.createdAt; _lastEventId = remote.eventId; - _lastRemote = remote; - _remoteRevision++; + if (!isActivePublishEcho || !competingRemoteSeen) { + _lastRemote = remote; + } + if (!isActivePublishEcho) { + _remoteRevision++; + } _observeHydration(); if (_pending != null) { _lastPublished = null; diff --git a/mobile/test/shared/theme/community_theme_sync_test.dart b/mobile/test/shared/theme/community_theme_sync_test.dart index 01b6652b4f..32ab98ae98 100644 --- a/mobile/test/shared/theme/community_theme_sync_test.dart +++ b/mobile/test/shared/theme/community_theme_sync_test.dart @@ -308,11 +308,12 @@ void main() { 're-merges desktop fields when remote advances during submission', () async { final firstSubmission = Completer(); + final session = _FakeSession(); final relay = _FakeSignedRelay( eventId: 'a-mobile', firstSubmissionGate: firstSubmission.future, + onBeforeAcknowledge: session.emit, ); - final session = _FakeSession(); final acknowledgements = []; final manager = _manager( session, @@ -362,6 +363,29 @@ void main() { }, ); + test( + 'matching self echo before acknowledgement does not republish', + () async { + final session = _FakeSession(); + final relay = _FakeSignedRelay(onBeforeAcknowledge: session.emit); + final acknowledgements = []; + final manager = _manager( + session, + relay, + onPublished: acknowledgements.add, + ); + await manager.initialize(); + manager.publish(local); + + await manager.flush(); + await Future.delayed(const Duration(milliseconds: 10)); + + expect(relay.attempts, 1); + expect(acknowledgements, [local]); + expect(manager.pending, isNull); + }, + ); + test('remote coordinate advances pending local publish timestamp', () async { final relay = _FakeSignedRelay(); final session = _FakeSession(); @@ -762,10 +786,12 @@ class _FakeSignedRelay implements SignedEventRelay { this.failuresRemaining = 0, this.eventId = 'event', this.firstSubmissionGate, + this.onBeforeAcknowledge, }); int failuresRemaining; final String eventId; final Future? firstSubmissionGate; + final void Function(NostrEvent)? onBeforeAcknowledge; int attempts = 0; final startedCreatedAts = []; final submissions = <_Submission>[]; @@ -797,6 +823,7 @@ class _FakeSignedRelay implements SignedEventRelay { ); onSigned?.call(event); submittedEvents.add(event); + onBeforeAcknowledge?.call(event); return event; } }