diff --git a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/dao/NegentropySynchronizeRequestDao.kt b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/dao/NegentropySynchronizeRequestDao.kt index ad70e337..6b976f1b 100644 --- a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/dao/NegentropySynchronizeRequestDao.kt +++ b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/dao/NegentropySynchronizeRequestDao.kt @@ -4,6 +4,7 @@ import ac.cord.auxiliary.compose.database.model.NegentropySynchronizeRequest import ac.cord.auxiliary.compose.database.model.SynchronizeNostrEventRequest import androidx.room3.Dao import androidx.room3.Insert +import androidx.room3.OnConflictStrategy.Companion.IGNORE import androidx.room3.Query import androidx.room3.Upsert import kotlinx.coroutines.flow.Flow @@ -21,6 +22,8 @@ interface NegentropySynchronizeRequestDao { @Upsert fun upsert(negentropySynchronizeRequest: NegentropySynchronizeRequest) - @Insert + @Insert( + onConflict = IGNORE + ) fun insert(negentropySynchronizeRequests: List) } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/network/relays/RelayPool.kt b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/network/relays/RelayPool.kt index 671f4c41..68f14a98 100644 --- a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/network/relays/RelayPool.kt +++ b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/network/relays/RelayPool.kt @@ -38,6 +38,8 @@ import com.vitorpamplona.quartz.nip77Negentropy.NegOpenCmd import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.transformWhile +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock /** * As seen in Primal @@ -58,6 +60,8 @@ class RelayPool( val relays: MutableSet = mutableSetOf() + private val relayMutex = Mutex() + @VisibleForTesting var socketClients = setOf() @@ -117,27 +121,28 @@ class RelayPool( this.relays.removeAll(relays) } - fun addRelaysIfMissing(relays: Set) { - logger.d("addRelaysIfMissing: ${relays.map { it.url }}") - val existingRelayUrls = socketClients.map { NormalizedRelayUrl(it.socketUrl).displayUrl() } - logger.d("Existing Relays: $existingRelayUrls") - val newRelayUrls = relays.map { it.url } + suspend fun addRelaysIfMissing(relays: Set) { + relayMutex.withLock { + logger.d("addRelaysIfMissing: ${relays.map { it.url }}") + val existingRelayUrls = socketClients.map { NormalizedRelayUrl(it.socketUrl).displayUrl() } + logger.d("Existing Relays: $existingRelayUrls") + val newRelayUrls = relays.map { it.url } - val toAddRelayUrls = newRelayUrls.filter { NormalizedRelayUrl(it).displayUrl() !in existingRelayUrls }.toSet() - logger.d("toAddRelayUrls: $toAddRelayUrls") - val toAddSocketClients = relays.filter { it.url in toAddRelayUrls }.mapAsNostrSocketClient() - logger.d("toAddSocketClients: ${toAddSocketClients.map { it.socketUrl }}" ) - if (toAddSocketClients.isNotEmpty()) { - val newSocketClients = socketClients.toMutableSet().apply { - addAll(toAddSocketClients) + val toAddRelayUrls = newRelayUrls.filter { NormalizedRelayUrl(it).displayUrl() !in existingRelayUrls }.toSet() + logger.d("toAddRelayUrls: $toAddRelayUrls") + val toAddSocketClients = relays.filter { it.url in toAddRelayUrls }.mapAsNostrSocketClient() + logger.d("toAddSocketClients: ${toAddSocketClients.map { it.socketUrl }}" ) + if (toAddSocketClients.isNotEmpty()) { + val newSocketClients = socketClients.toMutableSet().apply { + 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() { diff --git a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/nostr/Relays.kt b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/nostr/Relays.kt index 58347877..924e326b 100644 --- a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/nostr/Relays.kt +++ b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/nostr/Relays.kt @@ -35,10 +35,12 @@ object Relays { val bootstrapInboxRelaySet = setOf(damus, primal, mom, nos, bitcoiner, oxtr, yabu) 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 = listOf( diff --git a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/ui/view/model/FeedListViewModel.kt b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/ui/view/model/FeedListViewModel.kt index b160fa31..4de0b39d 100755 --- a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/ui/view/model/FeedListViewModel.kt +++ b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/ui/view/model/FeedListViewModel.kt @@ -51,6 +51,16 @@ class FeedListViewModel( val isSynchronizationPending: MutableState = mutableStateOf(false) + val negentropySynchronizeRequests = Relays.eventPublishRelaySet.take(1).map { normalizedRelayUrl -> + NegentropySynchronizeRequest( + purpose = "feed", + synchronizationFilter = synchronizationFilter, + relayURL = normalizedRelayUrl.url, + level = 0 + ) + } + + fun initiate() { logger.d("init") scheduleSynchronization() @@ -76,14 +86,7 @@ class FeedListViewModel( viewModelScope.launch(Dispatchers.IO) { // Sync Notifications... might want to also run this in the background nostrRepository.queueNegentropySynchronizeRequest( - Relays.eventPublishRelaySet.take(1).map { normalizedRelayUrl -> - NegentropySynchronizeRequest( - purpose = "feed", - synchronizationFilter = synchronizationFilter, - relayURL = normalizedRelayUrl.url, - level = 0 - ) - } + negentropySynchronizeRequests ) } }