From 3d60b10216d15f6804dc82c74267c634725288fc Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Tue, 31 Mar 2026 21:36:17 +0200 Subject: [PATCH] Add checks when syncing --- .../ac/aux/compose/database/AuxDatabase.kt | 3 + .../ac/aux/compose/database/dao/NostrDao.kt | 55 +++++++++++-------- .../repository/DatabaseNostrRepository.kt | 5 +- .../ui/view/model/FeedListViewModel.kt | 27 +-------- 4 files changed, 40 insertions(+), 50 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/ac/aux/compose/database/AuxDatabase.kt b/composeApp/src/commonMain/kotlin/ac/aux/compose/database/AuxDatabase.kt index 4cb61076..54834d2d 100644 --- a/composeApp/src/commonMain/kotlin/ac/aux/compose/database/AuxDatabase.kt +++ b/composeApp/src/commonMain/kotlin/ac/aux/compose/database/AuxDatabase.kt @@ -29,6 +29,9 @@ import androidx.room.RoomDatabase import androidx.room.TypeConverters import androidx.room.immediateTransaction import androidx.room.useWriterConnection +import kotlin.time.Instant + +val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L) @Database( entities = [ 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 f415c516..b6872069 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 @@ -1,6 +1,7 @@ package ac.aux.compose.database.dao import ac.aux.compose.database.AuxDatabase +import ac.aux.compose.database.GENESIS_AT import ac.aux.compose.database.model.BroadcastNostrEventRequest import ac.aux.compose.database.model.NostrEvent import ac.aux.compose.database.model.Post @@ -16,6 +17,7 @@ import com.vitorpamplona.quartz.nip01Core.tags.events.taggedEvents import com.vitorpamplona.quartz.nip01Core.tags.people.taggedUsers import com.vitorpamplona.quartz.nip37Drafts.DraftWrapEvent import kotlin.time.Clock +import kotlin.time.Instant @Dao abstract class NostrDao( @@ -37,27 +39,27 @@ abstract class NostrDao( ) ) // Find or create profile with the pubKey... if not found TODO: submit a sync request... - val profile = database.profileDao().getProfileByPublicKey(nostrEvent.pubKey) - - if (profile == null) { - val placeHolderProfile = Profile( - publicKey = nostrEvent.pubKey, - nostrEventId = nostrEvent.pubKey // This is illegal... because the nostrEventId should be the one with all the profile information - ) - val placeHolderProfileNostrEvent = NostrEvent( - id = placeHolderProfile.publicKey, - kind = DraftWrapEvent.KIND, - pubKey = placeHolderProfile.publicKey, - content = placeHolderProfile.publicKey, - createdAt = Clock.System.now(), - sig = placeHolderProfile.publicKey, - tags = emptyArray(), - ) - database.nostrEventDao().upsert(placeHolderProfileNostrEvent) - database.profileDao().upsert(placeHolderProfile) - - // TODO: Sync to find and update profile - } +// val profile = database.profileDao().getProfileByPublicKey(nostrEvent.pubKey) +// +// if (profile == null) { +// val placeHolderProfile = Profile( +// publicKey = nostrEvent.pubKey, +// nostrEventId = nostrEvent.pubKey // This is illegal... because the nostrEventId should be the one with all the profile information +// ) +// val placeHolderProfileNostrEvent = NostrEvent( +// id = placeHolderProfile.publicKey, +// kind = DraftWrapEvent.KIND, +// pubKey = placeHolderProfile.publicKey, +// content = placeHolderProfile.publicKey, +// createdAt = GENESIS_AT, +// sig = placeHolderProfile.publicKey, +// tags = emptyArray(), +// ) +// database.nostrEventDao().upsert(placeHolderProfileNostrEvent) +// database.profileDao().upsert(placeHolderProfile) +// +// // TODO: Sync to find and update profile +// } database.nostrEventDao().upsert(nostrEvent) @@ -83,7 +85,16 @@ abstract class NostrDao( synchronizationRelayURLs: List ) { logger.i("Store Nostr Event: $nostrEvent") - database.nostrEventDao().upsert(nostrEvent) + + val storedNostrEvent = database.nostrEventDao().getNostrEventById(nostrEvent.id) + + if (storedNostrEvent == null) { + // Add new nostr event + database.nostrEventDao().insert(nostrEvent) + } else if (nostrEvent.createdAt > storedNostrEvent.createdAt) { + // Update nostr event + database.nostrEventDao().upsert(nostrEvent) + } // Index nostrEvent indexNostrEvent( 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 12507d74..6ea2293c 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 @@ -1,6 +1,7 @@ package ac.aux.compose.database.repository import ac.aux.compose.database.AuxDatabase +import ac.aux.compose.database.GENESIS_AT import ac.aux.compose.database.model.BroadcastNostrEventReceipt import ac.aux.compose.database.model.BroadcastNostrEventRequest import ac.aux.compose.database.model.NostrEvent @@ -77,7 +78,7 @@ class DatabaseNostrRepository( val signedAt = if (name == null && biography == null) { // We are most likely try to sign in to an already existing block... - Instant.fromEpochMilliseconds(1231006505000L) + GENESIS_AT } else { null } @@ -275,7 +276,6 @@ class DatabaseNostrRepository( synchronizeNostrEventRequest: SynchronizeNostrEventRequest, synchronizationRelayURLs: List ) { - database.nostrDao().storeNostrEvent( nostrEvent, synchronizationRelayURLs = synchronizationRelayURLs @@ -303,7 +303,6 @@ class DatabaseNostrRepository( return database.nostrEventDao().observeNostrEvents( kinds = arrayOf( - MetadataEvent.KIND, TextNoteEvent.KIND, RepostEvent.KIND, LnZapEvent.KIND diff --git a/composeApp/src/commonMain/kotlin/ac/aux/compose/ui/view/model/FeedListViewModel.kt b/composeApp/src/commonMain/kotlin/ac/aux/compose/ui/view/model/FeedListViewModel.kt index 33456590..9c029e8d 100755 --- a/composeApp/src/commonMain/kotlin/ac/aux/compose/ui/view/model/FeedListViewModel.kt +++ b/composeApp/src/commonMain/kotlin/ac/aux/compose/ui/view/model/FeedListViewModel.kt @@ -38,30 +38,7 @@ class FeedListViewModel( fun loadNostrFeed() { viewModelScope.launch(Dispatchers.IO) { - // Sync Feed -// nostrRepository.queueSynchronizeNostrEvent( -// Relays.eventPublishRelaySet.map { normalizedRelay -> -// SynchronizeNostrEventRequest( -// synchronizationFilters = arrayOf( -// SynchronizationFilter( -// kinds = arrayOf( -// TextNoteEvent.KIND, -// RepostEvent.KIND, -// ReactionEvent.KIND, -// LnZapEvent.KIND, -// ), -// tags = mapOf( -// Pair("p", listOf(SeedManager.activePublicKey().toHexKey())) -// ), -// limit = 50 -// ) -// ), -// relayURL = normalizedRelay.url -// ) -// } -// ) - - // Sync Notifications + // Sync Notifications... might want to also run this in the background nostrRepository.queueSynchronizeNostrEvent( Relays.eventPublishRelaySet.map { normalizedRelay -> SynchronizeNostrEventRequest( @@ -84,7 +61,7 @@ class FeedListViewModel( } ) - + // Get contact list and sync feed... val localProfile = nostrRepository.observeProfile(SeedManager.activePublicKey().toHexKey()).firstOrNull()