Renames the MinimumRelayList to RecommendationProcessor

This commit is contained in:
Vitor Pamplona
2024-08-07 11:39:14 -04:00
parent c17e1f0e1f
commit 2b1e3cfc93
4 changed files with 24 additions and 25 deletions
@@ -108,7 +108,7 @@ import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.signers.NostrSignerExternal import com.vitorpamplona.quartz.signers.NostrSignerExternal
import com.vitorpamplona.quartz.signers.NostrSignerInternal import com.vitorpamplona.quartz.signers.NostrSignerInternal
import com.vitorpamplona.quartz.utils.DualCase import com.vitorpamplona.quartz.utils.DualCase
import com.vitorpamplona.quartz.utils.MinimumRelayListProcessor import com.vitorpamplona.quartz.utils.RelayListRecommendationProcessor
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -121,7 +121,6 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.combineTransform import kotlinx.coroutines.flow.combineTransform
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.transformLatest import kotlinx.coroutines.flow.transformLatest
@@ -500,13 +499,13 @@ class Account(
fun relaysFromPeopleListFlows( fun relaysFromPeopleListFlows(
currentFollowList: LiveFollowLists, currentFollowList: LiveFollowLists,
relayUrlsToIgnore: Set<String>, relayUrlsToIgnore: Set<String>,
): Flow<List<MinimumRelayListProcessor.RelayRecommendation>> = ): Flow<List<RelayListRecommendationProcessor.RelayRecommendation>> =
combine( combine(
currentFollowList.users.map { currentFollowList.users.map {
getNIP65RelayListFlow(it) getNIP65RelayListFlow(it)
}, },
) { followsNIP65RelayLists -> ) { followsNIP65RelayLists ->
MinimumRelayListProcessor RelayListRecommendationProcessor
.reliableRelaySetFor( .reliableRelaySetFor(
followsNIP65RelayLists.mapNotNull { followsNIP65RelayLists.mapNotNull {
(it.note.event as? AdvertisedRelayListEvent) (it.note.event as? AdvertisedRelayListEvent)
@@ -517,7 +516,7 @@ class Account(
} }
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
val liveHomeFollowRelayFlow: Flow<List<MinimumRelayListProcessor.RelayRecommendation>> by lazy { val liveHomeFollowRelayFlow: Flow<List<RelayListRecommendationProcessor.RelayRecommendation>> by lazy {
combineTransform(liveHomeFollowListFlow, connectToRelaysFlow) { followList, existing -> combineTransform(liveHomeFollowListFlow, connectToRelaysFlow) { followList, existing ->
if (followList != null) { if (followList != null) {
emit( emit(
@@ -540,7 +539,7 @@ class Account(
} }
} }
val liveHomeFollowRelays: StateFlow<List<MinimumRelayListProcessor.RelayRecommendation>> by lazy { val liveHomeFollowRelays: StateFlow<List<RelayListRecommendationProcessor.RelayRecommendation>> by lazy {
liveHomeFollowRelayFlow.stateIn(scope, SharingStarted.Eagerly, emptyList()) liveHomeFollowRelayFlow.stateIn(scope, SharingStarted.Eagerly, emptyList())
} }
@@ -31,7 +31,7 @@ import com.vitorpamplona.ammolite.relays.FeedType
import com.vitorpamplona.ammolite.relays.RelaySetupInfo import com.vitorpamplona.ammolite.relays.RelaySetupInfo
import com.vitorpamplona.ammolite.relays.RelayStats import com.vitorpamplona.ammolite.relays.RelayStats
import com.vitorpamplona.quartz.encoders.RelayUrlFormatter import com.vitorpamplona.quartz.encoders.RelayUrlFormatter
import com.vitorpamplona.quartz.utils.MinimumRelayListProcessor import com.vitorpamplona.quartz.utils.RelayListRecommendationProcessor
import kotlinx.collections.immutable.toImmutableSet import kotlinx.collections.immutable.toImmutableSet
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@@ -140,7 +140,7 @@ class Kind3RelayListViewModel : ViewModel() {
private fun refreshProposals() { private fun refreshProposals() {
_proposedRelays.update { _proposedRelays.update {
val proposed = val proposed =
MinimumRelayListProcessor RelayListRecommendationProcessor
.reliableRelaySetFor( .reliableRelaySetFor(
account.liveKind3Follows.value.users.mapNotNull { account.liveKind3Follows.value.users.mapNotNull {
account.getNIP65RelayList(it) account.getNIP65RelayList(it)
@@ -24,9 +24,9 @@ import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.RelayUrlFormatter import com.vitorpamplona.quartz.encoders.RelayUrlFormatter
import com.vitorpamplona.quartz.events.AdvertisedRelayListEvent import com.vitorpamplona.quartz.events.AdvertisedRelayListEvent
class MinimumRelayListProcessor { class RelayListRecommendationProcessor {
companion object { companion object {
fun transpose( private fun transpose(
userList: Map<HexKey, MutableSet<String>>, userList: Map<HexKey, MutableSet<String>>,
ignore: Set<String> = setOf(), ignore: Set<String> = setOf(),
): Map<String, MutableSet<HexKey>> { ): Map<String, MutableSet<HexKey>> {
@@ -52,7 +52,7 @@ class MinimumRelayListProcessor {
* filter onion and local host from write relays * filter onion and local host from write relays
* for each user pubkey, a list of valid relays. * for each user pubkey, a list of valid relays.
*/ */
fun filterValidRelays( private fun filterValidRelays(
userList: List<AdvertisedRelayListEvent>, userList: List<AdvertisedRelayListEvent>,
hasOnionConnection: Boolean = false, hasOnionConnection: Boolean = false,
): MutableMap<HexKey, MutableSet<String>> { ): MutableMap<HexKey, MutableSet<String>> {
@@ -76,17 +76,7 @@ class MinimumRelayListProcessor {
return validWriteRelayUrls return validWriteRelayUrls
} }
fun reliableRelaySetFor( private fun reliableRelaySetFor(
userList: List<AdvertisedRelayListEvent>,
relayUrlsToIgnore: Set<String> = emptySet(),
hasOnionConnection: Boolean = false,
): Set<RelayRecommendation> =
reliableRelaySetFor(
filterValidRelays(userList, hasOnionConnection),
relayUrlsToIgnore,
)
fun reliableRelaySetFor(
usersAndRelays: MutableMap<HexKey, MutableSet<String>>, usersAndRelays: MutableMap<HexKey, MutableSet<String>>,
relayUrlsToIgnore: Set<String> = emptySet(), relayUrlsToIgnore: Set<String> = emptySet(),
): Set<RelayRecommendation> { ): Set<RelayRecommendation> {
@@ -154,6 +144,16 @@ class MinimumRelayListProcessor {
return returningSet return returningSet
} }
fun reliableRelaySetFor(
userList: List<AdvertisedRelayListEvent>,
relayUrlsToIgnore: Set<String> = emptySet(),
hasOnionConnection: Boolean = false,
): Set<RelayRecommendation> =
reliableRelaySetFor(
filterValidRelays(userList, hasOnionConnection),
relayUrlsToIgnore,
)
} }
class RelayRecommendation( class RelayRecommendation(
@@ -24,7 +24,7 @@ import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue import junit.framework.TestCase.assertTrue
import org.junit.Test import org.junit.Test
class MinimumRelayListProcessorTest { class RelayListRecommendationProcessorTest {
val userList = val userList =
mutableMapOf( mutableMapOf(
"User1" to mutableSetOf("wss://relay1.com", "wss://relay2.com", "wss://relay3.com"), "User1" to mutableSetOf("wss://relay1.com", "wss://relay2.com", "wss://relay3.com"),
@@ -44,13 +44,13 @@ class MinimumRelayListProcessorTest {
"wss://relay5.com" to listOf("User2"), "wss://relay5.com" to listOf("User2"),
"wss://relay6.com" to listOf("User2", "User3"), "wss://relay6.com" to listOf("User2", "User3"),
).toString(), ).toString(),
MinimumRelayListProcessor.transpose(userList).toString(), RelayListRecommendationProcessor.transpose(userList).toString(),
) )
} }
@Test @Test
fun testProcessor() { fun testProcessor() {
val recommendations = MinimumRelayListProcessor.reliableRelaySetFor(userList).toList() val recommendations = RelayListRecommendationProcessor.reliableRelaySetFor(userList).toList()
val rec1 = recommendations[0] val rec1 = recommendations[0]