From f37ec2c90478bbb6be65e0a28762552388961fde Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Mon, 30 Mar 2026 01:51:39 +0200 Subject: [PATCH] Bug fix on npub login (which should hopefully cover nsec login as it's the same outside of first saving the private key) --- .../ac/aux/compose/database/dao/NostrDao.kt | 2 + .../dao/SynchronizeNostrEventRequestDao.kt | 3 +- .../repository/DatabaseNostrRepository.kt | 1 - .../ui/view/model/NavigationViewModel.kt | 42 +++++++++++-------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/ac/aux/compose/database/dao/NostrDao.kt b/composeApp/src/commonMain/kotlin/ac/aux/compose/database/dao/NostrDao.kt index 530be560..0108817a 100644 --- a/composeApp/src/commonMain/kotlin/ac/aux/compose/database/dao/NostrDao.kt +++ b/composeApp/src/commonMain/kotlin/ac/aux/compose/database/dao/NostrDao.kt @@ -84,6 +84,7 @@ abstract class NostrDao( ) { logger.i("Store Nostr Event: $nostrEvent") if (nostrEvent.unsignedNostrEventId == null) { + logger.i("Store Nostr Event: $nostrEvent") // Find or create profile with the pubKey... if not found submit a sync request... val profile = database.profileDao().getProfileByPublicKey(nostrEvent.pubKey) @@ -105,6 +106,7 @@ abstract class NostrDao( database.profileDao().upsert(placeHolderProfile) synchronizationRelayURLs.forEach { synchronizationRelayURL -> + logger.i("Sync Profile with PubKey: ${nostrEvent.pubKey}") database.synchronizeNostrEventRequestDao().upsert( SynchronizeNostrEventRequest( authorPublicKeys = arrayOf(nostrEvent.pubKey), diff --git a/composeApp/src/commonMain/kotlin/ac/aux/compose/database/dao/SynchronizeNostrEventRequestDao.kt b/composeApp/src/commonMain/kotlin/ac/aux/compose/database/dao/SynchronizeNostrEventRequestDao.kt index f8f5ca83..572ea8c4 100644 --- a/composeApp/src/commonMain/kotlin/ac/aux/compose/database/dao/SynchronizeNostrEventRequestDao.kt +++ b/composeApp/src/commonMain/kotlin/ac/aux/compose/database/dao/SynchronizeNostrEventRequestDao.kt @@ -2,6 +2,7 @@ package ac.aux.compose.database.dao import ac.aux.compose.database.model.SynchronizeNostrEventRequest import androidx.room.Dao +import androidx.room.Insert import androidx.room.Query import androidx.room.Upsert import kotlinx.coroutines.flow.Flow @@ -14,6 +15,6 @@ interface SynchronizeNostrEventRequestDao { @Upsert fun upsert(synchronizeNostrEventRequest: SynchronizeNostrEventRequest) - @Upsert + @Insert fun insert(synchronizeNostrEventRequests: List) } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/ac/aux/compose/database/repository/DatabaseNostrRepository.kt b/composeApp/src/commonMain/kotlin/ac/aux/compose/database/repository/DatabaseNostrRepository.kt index 4e640ed1..3843f2d0 100644 --- a/composeApp/src/commonMain/kotlin/ac/aux/compose/database/repository/DatabaseNostrRepository.kt +++ b/composeApp/src/commonMain/kotlin/ac/aux/compose/database/repository/DatabaseNostrRepository.kt @@ -168,7 +168,6 @@ class DatabaseNostrRepository( updatedAt = Clock.System.now() ) ) - logger.i("Processed: $synchronizeNostrEventRequest") } override suspend fun saveBroadcastReceipt(broadcastNostrEventReceipt: BroadcastNostrEventReceipt) { diff --git a/composeApp/src/commonMain/kotlin/ac/aux/compose/ui/view/model/NavigationViewModel.kt b/composeApp/src/commonMain/kotlin/ac/aux/compose/ui/view/model/NavigationViewModel.kt index d5e6abbe..8a7ff6a2 100644 --- a/composeApp/src/commonMain/kotlin/ac/aux/compose/ui/view/model/NavigationViewModel.kt +++ b/composeApp/src/commonMain/kotlin/ac/aux/compose/ui/view/model/NavigationViewModel.kt @@ -137,10 +137,13 @@ class NavigationViewModel( private fun observePendingSyncNostrEventRequests() { logger.i { "observePendingSyncNostrEventRequests" } + val syncEventCache = mutableMapOf() + scope.launch(Dispatchers.IO) { nostrRepository.observePendingSynchronizeNostrEventRequests().collect { synchronizeNostrEventRequestOrNull -> synchronizeNostrEventRequestOrNull?.let { synchronizeNostrEventRequest -> try { + logger.i("synchronizeNostrEventRequest: $synchronizeNostrEventRequest") val reqCommand = ReqCmd( subId = synchronizeNostrEventRequest.id, filters = listOf( @@ -189,22 +192,27 @@ class NavigationViewModel( jsonText ) - val nostrEvent = NostrEvent( - id = event.id, - pubKey = event.pubKey, - kind = event.kind, - content = event.content, - tags = event.tags, - createdAt = Instant.fromEpochMilliseconds(event.createdAt), - unsignedNostrEventId = synchronizeNostrEventRequest.unsignedNostrEventId, - sig = event.sig - ) + // TODO: Do profile specific cache (to avoid wiping an updated profile) + syncEventCache.getOrPut(event.id, { + val nostrEvent = NostrEvent( + id = event.id, + pubKey = event.pubKey, + kind = event.kind, + content = event.content, + tags = event.tags, + createdAt = Instant.fromEpochMilliseconds(event.createdAt), + unsignedNostrEventId = synchronizeNostrEventRequest.unsignedNostrEventId, + sig = event.sig + ) - nostrRepository.saveNostrEvent( - nostrEvent = nostrEvent, - synchronizeNostrEventRequest, - synchronizationRelayURLs = Relays.eventPublishRelaySet.map { normalizedRelayUrl -> normalizedRelayUrl.url } - ) + nostrRepository.saveNostrEvent( + nostrEvent = nostrEvent, + synchronizeNostrEventRequest, + synchronizationRelayURLs = Relays.eventPublishRelaySet.map { normalizedRelayUrl -> normalizedRelayUrl.url } + ) + + nostrEvent + }) } } @@ -309,7 +317,7 @@ class NavigationViewModel( NavigationUIState.ProfileLoaded( publicKey = localProfile.unsignedNostrEvent.pubKey ) - } else if (localProfile.unsignedNostrEvent.signedAt != null && localProfile.profile!= null) { + } else if (localProfile.unsignedNostrEvent.signedAt != null && localProfile.profile != null) { logger.i("We have successfully synced a profile: ${localProfile.profile}") NavigationUIState.ProfileLoaded( publicKey = localProfile.unsignedNostrEvent.pubKey @@ -334,7 +342,7 @@ class NavigationViewModel( NavigationUIState.UnqueuedProfileSynchronization( unsignedNostrEvent = localProfile.unsignedNostrEvent ) - } else if (localProfile.unsignedNostrEvent.signedAt != null) { + } else if (localProfile.unsignedNostrEvent.signedAt != null && localProfile.synchronizeNostrEventRequests.isEmpty()) { logger.i("We should be syncing the profile: ${localProfile.unsignedNostrEvent}") NavigationUIState.UnsyncedProfile( unsignedNostrEvent = localProfile.unsignedNostrEvent