Unify outbox calculation in the RelayOutbox loader.
This commit is contained in:
+6
-31
@@ -20,18 +20,14 @@
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.model.nip02FollowLists
|
||||
|
||||
import com.vitorpamplona.amethyst.model.AddressableNote
|
||||
import com.vitorpamplona.amethyst.model.LocalCache
|
||||
import com.vitorpamplona.amethyst.model.NoteState
|
||||
import com.vitorpamplona.amethyst.model.nip51Lists.blockedRelays.BlockedRelayListState
|
||||
import com.vitorpamplona.amethyst.model.nip51Lists.proxyRelays.ProxyRelayListState
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.amethyst.model.topNavFeeds.OutboxRelayLoader
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import com.vitorpamplona.quartz.nip65RelayList.AdvertisedRelayListEvent
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
@@ -52,37 +48,16 @@ class FollowListOutboxOrProxyRelays(
|
||||
val cache: LocalCache,
|
||||
scope: CoroutineScope,
|
||||
) {
|
||||
fun getNIP65RelayListAddress(pubkey: HexKey) = AdvertisedRelayListEvent.createAddress(pubkey)
|
||||
|
||||
fun getNIP65RelayListNote(pubkey: HexKey): AddressableNote = cache.getOrCreateAddressableNote(getNIP65RelayListAddress(pubkey))
|
||||
|
||||
fun getNIP65RelayListFlow(pubkey: HexKey): StateFlow<NoteState> = getNIP65RelayListNote(pubkey).flow().metadata.stateFlow
|
||||
|
||||
fun getNIP65RelayList(pubkey: HexKey): AdvertisedRelayListEvent? = getNIP65RelayListNote(pubkey).event as? AdvertisedRelayListEvent
|
||||
|
||||
fun allRelayListFlows(followList: Set<HexKey>): List<StateFlow<NoteState>> = followList.map { getNIP65RelayListFlow(it) }
|
||||
|
||||
fun combineAllFlows(flows: List<StateFlow<NoteState>>): Flow<Set<NormalizedRelayUrl>> =
|
||||
combine(flows) { relayListNotes: Array<NoteState> ->
|
||||
relayListNotes
|
||||
.mapNotNull {
|
||||
(it.note.event as? AdvertisedRelayListEvent)?.writeRelaysNorm()
|
||||
}.flatten()
|
||||
.toSet()
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val outboxRelayFlow: StateFlow<Set<NormalizedRelayUrl>> =
|
||||
kind3Follows.flow
|
||||
.transformLatest {
|
||||
emitAll(combineAllFlows(allRelayListFlows(it.authors)))
|
||||
.transformLatest { follows ->
|
||||
emitAll(
|
||||
OutboxRelayLoader(true).toAuthorsPerRelayFlow(follows.authors, cache) { it.keys },
|
||||
)
|
||||
}.onStart {
|
||||
emit(
|
||||
kind3Follows.flow.value.authors
|
||||
.mapNotNull {
|
||||
getNIP65RelayList(it)?.writeRelaysNorm()
|
||||
}.flatten()
|
||||
.toSet(),
|
||||
OutboxRelayLoader(true).authorsPerRelaySnapshot(kind3Follows.flow.value.authors, cache) { it.keys },
|
||||
)
|
||||
}.distinctUntilChanged()
|
||||
.flowOn(Dispatchers.Default)
|
||||
|
||||
+5
-45
@@ -20,20 +20,15 @@
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.model.nip02FollowLists
|
||||
|
||||
import com.vitorpamplona.amethyst.model.AddressableNote
|
||||
import com.vitorpamplona.amethyst.model.Constants
|
||||
import com.vitorpamplona.amethyst.model.LocalCache
|
||||
import com.vitorpamplona.amethyst.model.NoteState
|
||||
import com.vitorpamplona.amethyst.model.nip51Lists.blockedRelays.BlockedRelayListState
|
||||
import com.vitorpamplona.amethyst.model.nip51Lists.proxyRelays.ProxyRelayListState
|
||||
import com.vitorpamplona.amethyst.model.topNavFeeds.OutboxRelayLoader
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import com.vitorpamplona.quartz.nip65RelayList.AdvertisedRelayListEvent
|
||||
import com.vitorpamplona.quartz.utils.mapOfSet
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
@@ -53,51 +48,16 @@ class FollowsPerOutboxRelay(
|
||||
val cache: LocalCache,
|
||||
scope: CoroutineScope,
|
||||
) {
|
||||
fun getNIP65RelayListAddress(pubkey: HexKey) = AdvertisedRelayListEvent.createAddress(pubkey)
|
||||
|
||||
fun getNIP65RelayListNote(pubkey: HexKey): AddressableNote = cache.getOrCreateAddressableNote(getNIP65RelayListAddress(pubkey))
|
||||
|
||||
fun getNIP65RelayListFlow(pubkey: HexKey): StateFlow<NoteState> = getNIP65RelayListNote(pubkey).flow().metadata.stateFlow
|
||||
|
||||
fun getNIP65RelayList(pubkey: HexKey): AdvertisedRelayListEvent? = getNIP65RelayListNote(pubkey).event as? AdvertisedRelayListEvent
|
||||
|
||||
fun allRelayListFlows(followList: Set<HexKey>): List<StateFlow<NoteState>> = followList.map { getNIP65RelayListFlow(it) }
|
||||
|
||||
fun combineAllRelayListFlows(flows: List<StateFlow<NoteState>>): Flow<Map<NormalizedRelayUrl, Set<HexKey>>> =
|
||||
combine(flows) { relayListNotes: Array<NoteState> ->
|
||||
mapOfSet {
|
||||
relayListNotes.forEach { noteState ->
|
||||
noteState.note.author?.pubkeyHex?.let { authorHex ->
|
||||
val outboxRelayList =
|
||||
getNIP65RelayList(authorHex)?.writeRelaysNorm()
|
||||
?: LocalCache.relayHints.hintsForKey(authorHex).ifEmpty { null }
|
||||
?: Constants.eventFinderRelays
|
||||
outboxRelayList.forEach { relay ->
|
||||
add(relay, authorHex)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val outboxPerRelayFlow: StateFlow<Map<NormalizedRelayUrl, Set<HexKey>>> =
|
||||
kind3Follows.flow
|
||||
.transformLatest {
|
||||
emitAll(combineAllRelayListFlows(allRelayListFlows(it.authors)))
|
||||
emitAll(
|
||||
OutboxRelayLoader().toAuthorsPerRelayFlow(it.authors, cache) { it },
|
||||
)
|
||||
}.onStart {
|
||||
emit(
|
||||
mapOfSet {
|
||||
kind3Follows.flow.value.authors.map { authorHex ->
|
||||
val outboxRelayList =
|
||||
getNIP65RelayList(authorHex)?.writeRelaysNorm()
|
||||
?: LocalCache.relayHints.hintsForKey(authorHex).ifEmpty { null }
|
||||
?: Constants.eventFinderRelays
|
||||
outboxRelayList.forEach { relay ->
|
||||
add(relay, authorHex)
|
||||
}
|
||||
}
|
||||
},
|
||||
OutboxRelayLoader().authorsPerRelaySnapshot(kind3Follows.flow.value.authors, cache) { it },
|
||||
)
|
||||
}.distinctUntilChanged()
|
||||
.flowOn(Dispatchers.Default)
|
||||
|
||||
+60
-56
@@ -32,75 +32,79 @@ import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
|
||||
class OutboxRelayLoader {
|
||||
companion object {
|
||||
private fun authorsPerRelay(
|
||||
outboxRelayNotes: Array<NoteState>,
|
||||
cache: LocalCache,
|
||||
): Map<NormalizedRelayUrl, Set<HexKey>> =
|
||||
mapOfSet {
|
||||
outboxRelayNotes.forEach { outboxNote ->
|
||||
val note = outboxNote.note
|
||||
class OutboxRelayLoader(
|
||||
val rawOutboxRelays: Boolean = false,
|
||||
) {
|
||||
fun authorsPerRelay(
|
||||
outboxRelayNotes: Array<NoteState>,
|
||||
cache: LocalCache,
|
||||
): Map<NormalizedRelayUrl, Set<HexKey>> =
|
||||
mapOfSet {
|
||||
outboxRelayNotes.forEach { outboxNote ->
|
||||
val note = outboxNote.note
|
||||
|
||||
val authorHex =
|
||||
if (note is AddressableNote) {
|
||||
note.address.pubKeyHex
|
||||
val authorHex =
|
||||
if (note is AddressableNote) {
|
||||
note.address.pubKeyHex
|
||||
} else {
|
||||
note.author?.pubkeyHex
|
||||
}
|
||||
|
||||
if (authorHex != null) {
|
||||
val relays =
|
||||
if (rawOutboxRelays) {
|
||||
(outboxNote.note.event as? AdvertisedRelayListEvent)?.writeRelaysNorm() ?: emptySet()
|
||||
} else {
|
||||
note.author?.pubkeyHex
|
||||
}
|
||||
|
||||
if (authorHex != null) {
|
||||
val relays =
|
||||
(outboxNote.note.event as? AdvertisedRelayListEvent)?.writeRelaysNorm()
|
||||
?: cache.relayHints.hintsForKey(authorHex).ifEmpty { null }
|
||||
?: Constants.eventFinderRelays
|
||||
}
|
||||
|
||||
relays.forEach {
|
||||
if (!it.url.startsWith("wss://feeds.nostr.band") &&
|
||||
!it.url.startsWith("wss://filter.nostr.wine") &&
|
||||
!it.url.startsWith("wss://nwc.primal.net") &&
|
||||
!it.url.startsWith("wss://relay.getalby.com")
|
||||
) {
|
||||
add(it, authorHex)
|
||||
}
|
||||
relays.forEach {
|
||||
if (!it.url.startsWith("wss://feeds.nostr.band") &&
|
||||
!it.url.startsWith("wss://filter.nostr.wine") &&
|
||||
!it.url.startsWith("wss://nwc.primal.net") &&
|
||||
!it.url.startsWith("wss://relay.getalby.com")
|
||||
) {
|
||||
add(it, authorHex)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> authorsPerRelaySnapshot(
|
||||
authors: Set<HexKey>,
|
||||
cache: LocalCache,
|
||||
transformation: (Map<NormalizedRelayUrl, Set<HexKey>>) -> T,
|
||||
): T {
|
||||
val noteMetadata =
|
||||
authors
|
||||
.map { pubkeyHex ->
|
||||
cache
|
||||
.getOrCreateAddressableNote(AdvertisedRelayListEvent.createAddress(pubkeyHex))
|
||||
.flow()
|
||||
.metadata.stateFlow.value
|
||||
}.toTypedArray()
|
||||
return transformation(authorsPerRelay(noteMetadata, cache))
|
||||
}
|
||||
|
||||
fun <T> toAuthorsPerRelayFlow(
|
||||
authors: Set<HexKey>,
|
||||
cache: LocalCache,
|
||||
transformation: (Map<NormalizedRelayUrl, Set<HexKey>>) -> T,
|
||||
): Flow<T> {
|
||||
val noteMetadataFlows =
|
||||
authors.map { pubkeyHex ->
|
||||
val note = cache.getOrCreateAddressableNote(AdvertisedRelayListEvent.createAddress(pubkeyHex))
|
||||
note.flow().metadata.stateFlow
|
||||
}
|
||||
fun <T> authorsPerRelaySnapshot(
|
||||
authors: Set<HexKey>,
|
||||
cache: LocalCache,
|
||||
transformation: (Map<NormalizedRelayUrl, Set<HexKey>>) -> T,
|
||||
): T {
|
||||
val noteMetadata =
|
||||
authors
|
||||
.map { pubkeyHex ->
|
||||
cache
|
||||
.getOrCreateAddressableNote(AdvertisedRelayListEvent.createAddress(pubkeyHex))
|
||||
.flow()
|
||||
.metadata.stateFlow.value
|
||||
}.toTypedArray()
|
||||
return transformation(authorsPerRelay(noteMetadata, cache))
|
||||
}
|
||||
|
||||
return if (noteMetadataFlows.isEmpty()) {
|
||||
MutableStateFlow(transformation(emptyMap()))
|
||||
} else {
|
||||
combine(noteMetadataFlows) { outboxRelays ->
|
||||
transformation(authorsPerRelay(outboxRelays, cache))
|
||||
}
|
||||
fun <T> toAuthorsPerRelayFlow(
|
||||
authors: Set<HexKey>,
|
||||
cache: LocalCache,
|
||||
transformation: (Map<NormalizedRelayUrl, Set<HexKey>>) -> T,
|
||||
): Flow<T> {
|
||||
val noteMetadataFlows =
|
||||
authors.map { pubkeyHex ->
|
||||
val note = cache.getOrCreateAddressableNote(AdvertisedRelayListEvent.createAddress(pubkeyHex))
|
||||
note.flow().metadata.stateFlow
|
||||
}
|
||||
|
||||
return if (noteMetadataFlows.isEmpty()) {
|
||||
MutableStateFlow(transformation(emptyMap()))
|
||||
} else {
|
||||
combine(noteMetadataFlows) { outboxRelays ->
|
||||
transformation(authorsPerRelay(outboxRelays, cache))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -81,7 +81,7 @@ class AllFollowsByOutboxTopNavFilter(
|
||||
override fun toPerRelayFlow(cache: LocalCache): Flow<AllFollowsTopNavPerRelayFilterSet> {
|
||||
val authorsPerRelay =
|
||||
if (authors != null) {
|
||||
OutboxRelayLoader.toAuthorsPerRelayFlow(authors, cache) { it }
|
||||
OutboxRelayLoader().toAuthorsPerRelayFlow(authors, cache) { it }
|
||||
} else {
|
||||
MutableStateFlow(emptyMap())
|
||||
}
|
||||
@@ -112,7 +112,7 @@ class AllFollowsByOutboxTopNavFilter(
|
||||
override fun startValue(cache: LocalCache): AllFollowsTopNavPerRelayFilterSet {
|
||||
val authorsPerRelay =
|
||||
if (authors != null) {
|
||||
OutboxRelayLoader.authorsPerRelaySnapshot(authors, cache) { it }
|
||||
OutboxRelayLoader().authorsPerRelaySnapshot(authors, cache) { it }
|
||||
} else {
|
||||
emptyMap()
|
||||
}
|
||||
|
||||
+2
-2
@@ -52,7 +52,7 @@ class AuthorsByOutboxTopNavFilter(
|
||||
)
|
||||
|
||||
override fun toPerRelayFlow(cache: LocalCache): Flow<AuthorsTopNavPerRelayFilterSet> {
|
||||
val authorsPerRelay = OutboxRelayLoader.toAuthorsPerRelayFlow(authors, cache) { it }
|
||||
val authorsPerRelay = OutboxRelayLoader().toAuthorsPerRelayFlow(authors, cache) { it }
|
||||
|
||||
return combine(authorsPerRelay, blockedRelays) { authors, blocked ->
|
||||
convert(authors.minus(blocked))
|
||||
@@ -60,7 +60,7 @@ class AuthorsByOutboxTopNavFilter(
|
||||
}
|
||||
|
||||
override fun startValue(cache: LocalCache): AuthorsTopNavPerRelayFilterSet {
|
||||
val authorsPerRelay = OutboxRelayLoader.authorsPerRelaySnapshot(authors, cache) { it }
|
||||
val authorsPerRelay = OutboxRelayLoader().authorsPerRelaySnapshot(authors, cache) { it }
|
||||
|
||||
return convert(authorsPerRelay.minus(blockedRelays.value))
|
||||
}
|
||||
|
||||
+2
-2
@@ -67,7 +67,7 @@ class SingleCommunityTopNavFilter(
|
||||
|
||||
if (authors != null) {
|
||||
// go by authors
|
||||
val authorsPerRelay = OutboxRelayLoader.toAuthorsPerRelayFlow(authors, cache) { it }
|
||||
val authorsPerRelay = OutboxRelayLoader().toAuthorsPerRelayFlow(authors, cache) { it }
|
||||
|
||||
return combine(authorsPerRelay, blockedRelays) { authorsPerRelay, blocked ->
|
||||
SingleCommunityTopNavPerRelayFilterSet(
|
||||
@@ -100,7 +100,7 @@ class SingleCommunityTopNavFilter(
|
||||
|
||||
if (authors != null) {
|
||||
// go by authors
|
||||
val authorsPerRelay = OutboxRelayLoader.authorsPerRelaySnapshot(authors, cache) { it }
|
||||
val authorsPerRelay = OutboxRelayLoader().authorsPerRelaySnapshot(authors, cache) { it }
|
||||
|
||||
return SingleCommunityTopNavPerRelayFilterSet(
|
||||
authorsPerRelay.minus(blockedRelays.value).mapValues {
|
||||
|
||||
+2
-2
@@ -52,7 +52,7 @@ class MutedAuthorsByOutboxTopNavFilter(
|
||||
)
|
||||
|
||||
override fun toPerRelayFlow(cache: LocalCache): Flow<MutedAuthorsTopNavPerRelayFilterSet> {
|
||||
val authorsPerRelay = OutboxRelayLoader.toAuthorsPerRelayFlow(authors, cache) { it }
|
||||
val authorsPerRelay = OutboxRelayLoader().toAuthorsPerRelayFlow(authors, cache) { it }
|
||||
|
||||
return combine(authorsPerRelay, blockedRelays) { authors, blocked ->
|
||||
convert(authors.minus(blocked))
|
||||
@@ -60,7 +60,7 @@ class MutedAuthorsByOutboxTopNavFilter(
|
||||
}
|
||||
|
||||
override fun startValue(cache: LocalCache): MutedAuthorsTopNavPerRelayFilterSet {
|
||||
val authorsPerRelay = OutboxRelayLoader.authorsPerRelaySnapshot(authors, cache) { it }
|
||||
val authorsPerRelay = OutboxRelayLoader().authorsPerRelaySnapshot(authors, cache) { it }
|
||||
|
||||
return convert(authorsPerRelay.minus(blockedRelays.value))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user