Add checks when syncing

This commit is contained in:
Kgothatso Ngako
2026-03-31 21:36:17 +02:00
parent 79044514f8
commit 3d60b10216
4 changed files with 40 additions and 50 deletions

View File

@@ -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 = [

View File

@@ -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<String>
) {
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(

View File

@@ -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<String>
) {
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

View File

@@ -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()