Separates index relays from the default and put it into the hint-based queries for unkown users.

This commit is contained in:
Vitor Pamplona
2025-09-24 12:36:39 -04:00
parent 0ba3385771
commit ccf64687a8
6 changed files with 26 additions and 105 deletions

View File

@@ -80,7 +80,6 @@ import com.vitorpamplona.amethyst.model.nip96FileStorage.FileStorageServerListSt
import com.vitorpamplona.amethyst.model.nipB7Blossom.BlossomServerListState import com.vitorpamplona.amethyst.model.nipB7Blossom.BlossomServerListState
import com.vitorpamplona.amethyst.model.serverList.MergedFollowListsState import com.vitorpamplona.amethyst.model.serverList.MergedFollowListsState
import com.vitorpamplona.amethyst.model.serverList.MergedFollowPlusMineRelayListsState import com.vitorpamplona.amethyst.model.serverList.MergedFollowPlusMineRelayListsState
import com.vitorpamplona.amethyst.model.serverList.MergedFollowPlusMineWithIndexAndSearchRelayListsState
import com.vitorpamplona.amethyst.model.serverList.MergedFollowPlusMineWithIndexRelayListsState import com.vitorpamplona.amethyst.model.serverList.MergedFollowPlusMineWithIndexRelayListsState
import com.vitorpamplona.amethyst.model.serverList.MergedFollowPlusMineWithSearchRelayListsState import com.vitorpamplona.amethyst.model.serverList.MergedFollowPlusMineWithSearchRelayListsState
import com.vitorpamplona.amethyst.model.serverList.MergedServerListState import com.vitorpamplona.amethyst.model.serverList.MergedServerListState
@@ -312,7 +311,6 @@ class Account(
val followOutboxesOrProxy = FollowListOutboxOrProxyRelays(kind3FollowList, blockedRelayList, proxyRelayList, cache, scope) val followOutboxesOrProxy = FollowListOutboxOrProxyRelays(kind3FollowList, blockedRelayList, proxyRelayList, cache, scope)
val followPlusAllMineWithIndex = MergedFollowPlusMineWithIndexRelayListsState(followOutboxesOrProxy, nip65RelayList, privateStorageRelayList, localRelayList, indexerRelayList, scope) val followPlusAllMineWithIndex = MergedFollowPlusMineWithIndexRelayListsState(followOutboxesOrProxy, nip65RelayList, privateStorageRelayList, localRelayList, indexerRelayList, scope)
val followPlusAllMineWithSearch = MergedFollowPlusMineWithSearchRelayListsState(followOutboxesOrProxy, nip65RelayList, privateStorageRelayList, localRelayList, searchRelayList, scope) val followPlusAllMineWithSearch = MergedFollowPlusMineWithSearchRelayListsState(followOutboxesOrProxy, nip65RelayList, privateStorageRelayList, localRelayList, searchRelayList, scope)
val followPlusAllMineWithIndexAndSearch = MergedFollowPlusMineWithIndexAndSearchRelayListsState(followOutboxesOrProxy, nip65RelayList, privateStorageRelayList, localRelayList, indexerRelayList, searchRelayList, scope)
val defaultGlobalRelays = MergedFollowPlusMineRelayListsState(followOutboxesOrProxy, nip65RelayList, privateStorageRelayList, localRelayList, scope) val defaultGlobalRelays = MergedFollowPlusMineRelayListsState(followOutboxesOrProxy, nip65RelayList, privateStorageRelayList, localRelayList, scope)
// keeps a cache of the outbox relays for each author // keeps a cache of the outbox relays for each author

View File

@@ -1,92 +0,0 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.model.serverList
import com.vitorpamplona.amethyst.model.edits.PrivateStorageRelayListState
import com.vitorpamplona.amethyst.model.localRelays.LocalRelayListState
import com.vitorpamplona.amethyst.model.nip02FollowLists.FollowListOutboxOrProxyRelays
import com.vitorpamplona.amethyst.model.nip51Lists.indexerRelays.IndexerRelayListState
import com.vitorpamplona.amethyst.model.nip51Lists.searchRelays.SearchRelayListState
import com.vitorpamplona.amethyst.model.nip65RelayList.Nip65RelayListState
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
class MergedFollowPlusMineWithIndexAndSearchRelayListsState(
val followsOutboxOrProxyRelayList: FollowListOutboxOrProxyRelays,
val nip65RelayList: Nip65RelayListState,
val privateOutboxRelayList: PrivateStorageRelayListState,
val localRelayList: LocalRelayListState,
val indexerRelayList: IndexerRelayListState,
val searchRelayListsState: SearchRelayListState,
val scope: CoroutineScope,
) {
fun mergeLists(lists: Array<Set<NormalizedRelayUrl>>): Set<NormalizedRelayUrl> = lists.reduce { acc, set -> acc + set }
val flow: StateFlow<Set<NormalizedRelayUrl>> =
combine(
listOf(
followsOutboxOrProxyRelayList.flow,
nip65RelayList.outboxFlow,
nip65RelayList.inboxFlow,
privateOutboxRelayList.flow,
localRelayList.flow,
indexerRelayList.flow,
searchRelayListsState.flow,
),
::mergeLists,
).onStart {
emit(
mergeLists(
arrayOf(
followsOutboxOrProxyRelayList.flow.value,
nip65RelayList.outboxFlow.value,
nip65RelayList.inboxFlow.value,
privateOutboxRelayList.flow.value,
localRelayList.flow.value,
indexerRelayList.flow.value,
searchRelayListsState.flow.value,
),
),
)
}.flowOn(Dispatchers.Default)
.stateIn(
scope,
SharingStarted.Eagerly,
mergeLists(
arrayOf(
followsOutboxOrProxyRelayList.flow.value,
nip65RelayList.outboxFlow.value,
nip65RelayList.inboxFlow.value,
privateOutboxRelayList.flow.value,
localRelayList.flow.value,
indexerRelayList.flow.value,
searchRelayListsState.flow.value,
),
),
)
}

View File

@@ -38,14 +38,16 @@ val MetadataAndRelayListKinds =
fun filterFindUserMetadataForKey( fun filterFindUserMetadataForKey(
author: HexKey, author: HexKey,
indexRelays: Set<NormalizedRelayUrl>,
defaultRelays: Set<NormalizedRelayUrl>, defaultRelays: Set<NormalizedRelayUrl>,
): List<RelayBasedFilter> = ): List<RelayBasedFilter> =
LocalCache.checkGetOrCreateUser(author)?.let { LocalCache.checkGetOrCreateUser(author)?.let {
filterFindUserMetadataForKey(setOf(it), defaultRelays) filterFindUserMetadataForKey(setOf(it), indexRelays, defaultRelays)
} ?: emptyList() } ?: emptyList()
fun filterFindUserMetadataForKey( fun filterFindUserMetadataForKey(
authors: Set<User>, authors: Set<User>,
indexRelays: Set<NormalizedRelayUrl>,
defaultRelays: Set<NormalizedRelayUrl>, defaultRelays: Set<NormalizedRelayUrl>,
): List<RelayBasedFilter> { ): List<RelayBasedFilter> {
val perRelayKeys = val perRelayKeys =
@@ -53,8 +55,10 @@ fun filterFindUserMetadataForKey(
authors.forEach { key -> authors.forEach { key ->
val relays = val relays =
key.authorRelayList()?.writeRelaysNorm() key.authorRelayList()?.writeRelaysNorm()
?: LocalCache.relayHints.hintsForKey(key.pubkeyHex).ifEmpty { null } ?: (key.relaysBeingUsed.keys + LocalCache.relayHints.hintsForKey(key.pubkeyHex) + indexRelays)
?: (key.relaysBeingUsed.keys + defaultRelays).toList() .ifEmpty { null }
?.also { println("Using relay hints ${it.size} for ${key.pubkeyHex}") }
?: defaultRelays.toList()
relays.forEach { relays.forEach {
add(it, key.pubkeyHex) add(it, key.pubkeyHex)

View File

@@ -20,6 +20,7 @@
*/ */
package com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.loaders package com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.loaders
import com.vitorpamplona.amethyst.model.DefaultIndexerRelayList
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.relayClient.eoseManagers.SingleSubNoEoseCacheEoseManager import com.vitorpamplona.amethyst.service.relayClient.eoseManagers.SingleSubNoEoseCacheEoseManager
@@ -33,6 +34,7 @@ class UserLoaderSubAssembler(
allKeys: () -> Set<UserFinderQueryState>, allKeys: () -> Set<UserFinderQueryState>,
) : SingleSubNoEoseCacheEoseManager<UserFinderQueryState>(client, allKeys, invalidateAfterEose = true) { ) : SingleSubNoEoseCacheEoseManager<UserFinderQueryState>(client, allKeys, invalidateAfterEose = true) {
override fun updateFilter(keys: List<UserFinderQueryState>): List<RelayBasedFilter>? { override fun updateFilter(keys: List<UserFinderQueryState>): List<RelayBasedFilter>? {
println("01f6901bc401e87962fa8da15acfe16ef72b17ed965114384d69aa857a21fbfc updating user assembly filter 2")
val firstTimers = mutableSetOf<User>() val firstTimers = mutableSetOf<User>()
keys.forEach { keys.forEach {
@@ -43,10 +45,15 @@ class UserLoaderSubAssembler(
} }
} }
val indexRelays = mutableSetOf<NormalizedRelayUrl>()
val defaultRelays = mutableSetOf<NormalizedRelayUrl>() val defaultRelays = mutableSetOf<NormalizedRelayUrl>()
keys.mapTo(mutableSetOf()) { it.account }.forEach { keys.mapTo(mutableSetOf()) { it.account }.forEach {
defaultRelays.addAll(it.followPlusAllMineWithIndexAndSearch.flow.value) indexRelays.addAll(
it.indexerRelayList.flow.value
.ifEmpty { DefaultIndexerRelayList },
)
defaultRelays.addAll(it.followPlusAllMineWithSearch.flow.value)
it.kind3FollowList.flow.value.authors.forEach { it.kind3FollowList.flow.value.authors.forEach {
val user = LocalCache.getOrCreateUser(it) val user = LocalCache.getOrCreateUser(it)
@@ -60,7 +67,7 @@ class UserLoaderSubAssembler(
if (firstTimers.isEmpty()) return null if (firstTimers.isEmpty()) return null
return filterFindUserMetadataForKey(firstTimers, defaultRelays) return filterFindUserMetadataForKey(firstTimers, indexRelays, defaultRelays)
} }
override fun distinct(key: UserFinderQueryState) = key.user override fun distinct(key: UserFinderQueryState) = key.user

View File

@@ -26,5 +26,6 @@ import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
fun filterByAuthor( fun filterByAuthor(
pubKey: HexKey, pubKey: HexKey,
indexRelays: Set<NormalizedRelayUrl>,
defaultRelays: Set<NormalizedRelayUrl>, defaultRelays: Set<NormalizedRelayUrl>,
) = filterFindUserMetadataForKey(pubKey, defaultRelays) ) = filterFindUserMetadataForKey(pubKey, indexRelays, defaultRelays)

View File

@@ -20,6 +20,7 @@
*/ */
package com.vitorpamplona.amethyst.service.relayClient.searchCommand.subassemblies package com.vitorpamplona.amethyst.service.relayClient.searchCommand.subassemblies
import com.vitorpamplona.amethyst.model.DefaultIndexerRelayList
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.relayClient.eoseManagers.PerUniqueIdEoseManager import com.vitorpamplona.amethyst.service.relayClient.eoseManagers.PerUniqueIdEoseManager
import com.vitorpamplona.amethyst.service.relayClient.searchCommand.SearchQueryState import com.vitorpamplona.amethyst.service.relayClient.searchCommand.SearchQueryState
@@ -55,23 +56,25 @@ class SearchWatcherSubAssembler(
if (mySearchString.isBlank()) return null if (mySearchString.isBlank()) return null
val defaultRelaysWithIndexAndSearch = key.account.followPlusAllMineWithIndexAndSearch.flow.value val indexRelays =
key.account.indexerRelayList.flow.value
.ifEmpty { DefaultIndexerRelayList }
val defaultRelaysWithSearch = key.account.followPlusAllMineWithSearch.flow.value val defaultRelaysWithSearch = key.account.followPlusAllMineWithSearch.flow.value
val directFilters = val directFilters =
runCatching { runCatching {
if (Hex.isHex(mySearchString)) { if (Hex.isHex(mySearchString)) {
val hexKey = Hex.decode(mySearchString).toHexKey() val hexKey = Hex.decode(mySearchString).toHexKey()
filterByAuthor(hexKey, defaultRelaysWithIndexAndSearch) + filterByEvent(hexKey, defaultRelaysWithSearch) filterByAuthor(hexKey, indexRelays, defaultRelaysWithSearch) + filterByEvent(hexKey, defaultRelaysWithSearch)
} else { } else {
val parsed = Nip19Parser.uriToRoute(mySearchString)?.entity val parsed = Nip19Parser.uriToRoute(mySearchString)?.entity
if (parsed != null) { if (parsed != null) {
cache.consume(parsed) cache.consume(parsed)
when (parsed) { when (parsed) {
is NSec -> filterByAuthor(parsed.toPubKeyHex(), defaultRelaysWithIndexAndSearch) is NSec -> filterByAuthor(parsed.toPubKeyHex(), indexRelays, defaultRelaysWithSearch)
is NPub -> filterByAuthor(parsed.hex, defaultRelaysWithIndexAndSearch) is NPub -> filterByAuthor(parsed.hex, indexRelays, defaultRelaysWithSearch)
is NProfile -> filterByAuthor(parsed.hex, defaultRelaysWithIndexAndSearch) is NProfile -> filterByAuthor(parsed.hex, indexRelays, defaultRelaysWithSearch)
is NNote -> filterByEvent(parsed.hex, defaultRelaysWithSearch) is NNote -> filterByEvent(parsed.hex, defaultRelaysWithSearch)
is NEvent -> filterByEvent(parsed.hex, defaultRelaysWithSearch) is NEvent -> filterByEvent(parsed.hex, defaultRelaysWithSearch)
is NEmbed -> emptyList() is NEmbed -> emptyList()