Improve sync initialization

This commit is contained in:
Kgothatso Ngako
2026-05-04 20:08:52 +02:00
parent b44bc7df5b
commit 4fbbfc72f7
4 changed files with 42 additions and 29 deletions

View File

@@ -4,6 +4,7 @@ import ac.cord.auxiliary.compose.database.model.NegentropySynchronizeRequest
import ac.cord.auxiliary.compose.database.model.SynchronizeNostrEventRequest import ac.cord.auxiliary.compose.database.model.SynchronizeNostrEventRequest
import androidx.room3.Dao import androidx.room3.Dao
import androidx.room3.Insert import androidx.room3.Insert
import androidx.room3.OnConflictStrategy.Companion.IGNORE
import androidx.room3.Query import androidx.room3.Query
import androidx.room3.Upsert import androidx.room3.Upsert
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@@ -21,6 +22,8 @@ interface NegentropySynchronizeRequestDao {
@Upsert @Upsert
fun upsert(negentropySynchronizeRequest: NegentropySynchronizeRequest) fun upsert(negentropySynchronizeRequest: NegentropySynchronizeRequest)
@Insert @Insert(
onConflict = IGNORE
)
fun insert(negentropySynchronizeRequests: List<NegentropySynchronizeRequest>) fun insert(negentropySynchronizeRequests: List<NegentropySynchronizeRequest>)
} }

View File

@@ -38,6 +38,8 @@ import com.vitorpamplona.quartz.nip77Negentropy.NegOpenCmd
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.transformWhile import kotlinx.coroutines.flow.transformWhile
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
/** /**
* As seen in Primal * As seen in Primal
@@ -58,6 +60,8 @@ class RelayPool(
val relays: MutableSet<RelayDTO> = mutableSetOf() val relays: MutableSet<RelayDTO> = mutableSetOf()
private val relayMutex = Mutex()
@VisibleForTesting @VisibleForTesting
var socketClients = setOf<NostrSocketClient>() var socketClients = setOf<NostrSocketClient>()
@@ -117,27 +121,28 @@ class RelayPool(
this.relays.removeAll(relays) this.relays.removeAll(relays)
} }
fun addRelaysIfMissing(relays: Set<RelayDTO>) { suspend fun addRelaysIfMissing(relays: Set<RelayDTO>) {
logger.d("addRelaysIfMissing: ${relays.map { it.url }}") relayMutex.withLock {
val existingRelayUrls = socketClients.map { NormalizedRelayUrl(it.socketUrl).displayUrl() } logger.d("addRelaysIfMissing: ${relays.map { it.url }}")
logger.d("Existing Relays: $existingRelayUrls") val existingRelayUrls = socketClients.map { NormalizedRelayUrl(it.socketUrl).displayUrl() }
val newRelayUrls = relays.map { it.url } logger.d("Existing Relays: $existingRelayUrls")
val newRelayUrls = relays.map { it.url }
val toAddRelayUrls = newRelayUrls.filter { NormalizedRelayUrl(it).displayUrl() !in existingRelayUrls }.toSet() val toAddRelayUrls = newRelayUrls.filter { NormalizedRelayUrl(it).displayUrl() !in existingRelayUrls }.toSet()
logger.d("toAddRelayUrls: $toAddRelayUrls") logger.d("toAddRelayUrls: $toAddRelayUrls")
val toAddSocketClients = relays.filter { it.url in toAddRelayUrls }.mapAsNostrSocketClient() val toAddSocketClients = relays.filter { it.url in toAddRelayUrls }.mapAsNostrSocketClient()
logger.d("toAddSocketClients: ${toAddSocketClients.map { it.socketUrl }}" ) logger.d("toAddSocketClients: ${toAddSocketClients.map { it.socketUrl }}" )
if (toAddSocketClients.isNotEmpty()) { if (toAddSocketClients.isNotEmpty()) {
val newSocketClients = socketClients.toMutableSet().apply { val newSocketClients = socketClients.toMutableSet().apply {
addAll(toAddSocketClients) addAll(toAddSocketClients)
}
logger.d("newSocketClients: ${newSocketClients.map { it.socketUrl }}")
socketClients = newSocketClients
this.relays.addAll(relays)
} }
logger.d("newSocketClients: ${newSocketClients.map { it.socketUrl }}")
socketClients = newSocketClients
this.relays.addAll(relays)
} }
} }
fun closePool() { fun closePool() {

View File

@@ -35,10 +35,12 @@ object Relays {
val bootstrapInboxRelaySet = setOf(damus, primal, mom, nos, bitcoiner, oxtr, yabu) val bootstrapInboxRelaySet = setOf(damus, primal, mom, nos, bitcoiner, oxtr, yabu)
val eventFinderRelaySet = setOf(wine, damus, mom, primal, nos, bitcoiner, oxtr) val eventFinderRelaySet = setOf(wine, damus, mom, primal, nos, bitcoiner, oxtr)
val eventPublishRelaySet = setOf(damus, mom, nos, primal, bitcoiner, oxtr) val eventPublishRelaySet = setOf(oxtr, damus, wine, mom, primal, nos, bitcoiner)
val negentropicRelaySet = setOf(damus, yabu, oxtr, bitcoiner, nos)
val searchQuerableRelaySet = setOf(primal)
val DefaultNIP65RelaySet = setOf(mom, nos, bitcoiner) val DefaultNIP65RelaySet = setOf(mom, nos, bitcoiner, wine)
val DefaultNIP65List = val DefaultNIP65List =
listOf( listOf(

View File

@@ -51,6 +51,16 @@ class FeedListViewModel(
val isSynchronizationPending: MutableState<Boolean> = mutableStateOf(false) val isSynchronizationPending: MutableState<Boolean> = mutableStateOf(false)
val negentropySynchronizeRequests = Relays.eventPublishRelaySet.take(1).map { normalizedRelayUrl ->
NegentropySynchronizeRequest(
purpose = "feed",
synchronizationFilter = synchronizationFilter,
relayURL = normalizedRelayUrl.url,
level = 0
)
}
fun initiate() { fun initiate() {
logger.d("init") logger.d("init")
scheduleSynchronization() scheduleSynchronization()
@@ -76,14 +86,7 @@ class FeedListViewModel(
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
// Sync Notifications... might want to also run this in the background // Sync Notifications... might want to also run this in the background
nostrRepository.queueNegentropySynchronizeRequest( nostrRepository.queueNegentropySynchronizeRequest(
Relays.eventPublishRelaySet.take(1).map { normalizedRelayUrl -> negentropySynchronizeRequests
NegentropySynchronizeRequest(
purpose = "feed",
synchronizationFilter = synchronizationFilter,
relayURL = normalizedRelayUrl.url,
level = 0
)
}
) )
} }
} }