diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index af266af90..1b5e29729 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -119,7 +119,9 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combineTransform +import kotlinx.coroutines.flow.emitAll import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.transformLatest @@ -540,17 +542,14 @@ class Account( } fun authorsPerRelay( - pubkeyList: Set, + followsNIP65RelayLists: Array, defaultRelayList: List, - ): Flow>> = - combine( - pubkeyList.map { - getNIP65RelayListFlow(it) - }, - ) { followsNIP65RelayLists -> + ): Map> { + val test = assembleAuthorsPerWriteRelay( followsNIP65RelayLists - .mapNotNull { + .mapNotNull + { val author = (it.note as? AddressableNote)?.address?.pubKeyHex val event = (it.note.event as? AdvertisedRelayListEvent) @@ -567,23 +566,39 @@ class Account( }.toMap(), hasOnionConnection = proxy != null, ) - } + + return test + } @OptIn(ExperimentalCoroutinesApi::class) - val liveHomeListAuthorsPerRelayFlow: Flow>?> by lazy { - combineTransform(liveHomeFollowListFlow, connectToRelaysFlow) { followList, existing -> - if (followList != null) { - emit(authorsPerRelay(followList.usersPlusMe, existing.filter { it.feedTypes.contains(FeedType.FOLLOWS) && it.read }.map { it.url })) + val liveHomeFollowListAdvertizedRelayListFlow: Flow?> = + liveHomeFollowLists + .transformLatest { followList -> + if (followList != null) { + emitAll(combine(followList.usersPlusMe.map { getNIP65RelayListFlow(it) }) { it }) + } else { + emit(null) + } + } + + val liveHomeListAuthorsPerRelayFlow: Flow>?> by lazy { + combineTransform(liveHomeFollowListAdvertizedRelayListFlow, connectToRelays) { adverisedRelayList, existing -> + if (adverisedRelayList != null) { + emit(authorsPerRelay(adverisedRelayList, existing.filter { it.feedTypes.contains(FeedType.FOLLOWS) && it.read }.map { it.url })) } else { - emit(MutableStateFlow(null)) + emit(null) } - }.flatMapLatest { - it } } - val liveHomeListAuthorsPerRelay: StateFlow>?> by lazy { - liveHomeListAuthorsPerRelayFlow.stateIn(scope, SharingStarted.Eagerly, emptyMap()) + val liveHomeListAuthorsPerRelay: StateFlow>?> by lazy { + val currentRelays = connectToRelays.value.filter { it.feedTypes.contains(FeedType.FOLLOWS) && it.read }.map { it.url } + val default = + currentRelays.associate { relayUrl -> + relayUrl to (liveHomeFollowLists.value?.usersPlusMe?.toList() ?: emptyList()) + } + + liveHomeListAuthorsPerRelayFlow.stateIn(scope, SharingStarted.Eagerly, default) } @OptIn(ExperimentalCoroutinesApi::class) @@ -611,15 +626,23 @@ class Account( } @OptIn(ExperimentalCoroutinesApi::class) + val liveStoriesFollowListAdvertizedRelayListFlow: Flow?> = + liveStoriesFollowLists + .transformLatest { followList -> + if (followList != null) { + emitAll(combine(followList.usersPlusMe.map { getNIP65RelayListFlow(it) }) { it }) + } else { + emit(null) + } + } + val liveStoriesListAuthorsPerRelayFlow: Flow>?> by lazy { - combineTransform(liveStoriesFollowLists, connectToRelaysFlow) { followList, existing -> - if (followList != null) { - emit(authorsPerRelay(followList.usersPlusMe, existing.filter { it.feedTypes.contains(FeedType.FOLLOWS) && it.read }.map { it.url })) + combineTransform(liveStoriesFollowListAdvertizedRelayListFlow, connectToRelays) { adverisedRelayList, existing -> + if (adverisedRelayList != null) { + emit(authorsPerRelay(adverisedRelayList, existing.filter { it.feedTypes.contains(FeedType.FOLLOWS) && it.read }.map { it.url })) } else { - emit(MutableStateFlow(null)) + emit(null) } - }.flatMapLatest { - it } } @@ -640,15 +663,23 @@ class Account( } @OptIn(ExperimentalCoroutinesApi::class) + val liveDiscoveryFollowListAdvertizedRelayListFlow: Flow?> = + liveDiscoveryFollowLists + .transformLatest { followList -> + if (followList != null) { + emitAll(combine(followList.usersPlusMe.map { getNIP65RelayListFlow(it) }) { it }) + } else { + emit(null) + } + } + val liveDiscoveryListAuthorsPerRelayFlow: Flow>?> by lazy { - combineTransform(liveDiscoveryFollowLists, connectToRelaysFlow) { followList, existing -> - if (followList != null) { - emit(authorsPerRelay(followList.usersPlusMe, existing.filter { it.read }.map { it.url })) + combineTransform(liveDiscoveryFollowListAdvertizedRelayListFlow, connectToRelays) { adverisedRelayList, existing -> + if (adverisedRelayList != null) { + emit(authorsPerRelay(adverisedRelayList, existing.filter { it.read }.map { it.url })) } else { - emit(MutableStateFlow(null)) + emit(null) } - }.flatMapLatest { - it } } diff --git a/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/filters/SinceAuthorPerRelayFilter.kt b/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/filters/SinceAuthorPerRelayFilter.kt index e2ec61eb2..884401519 100644 --- a/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/filters/SinceAuthorPerRelayFilter.kt +++ b/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/filters/SinceAuthorPerRelayFilter.kt @@ -20,14 +20,15 @@ */ package com.vitorpamplona.ammolite.relays.filters +import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.events.Event /** * This is a nostr filter with per-relay authors list and since parameters */ class SinceAuthorPerRelayFilter( - val ids: List? = null, - val authors: Map>? = null, + val ids: List? = null, + val authors: Map>? = null, val kinds: List? = null, val tags: Map>? = null, val since: Map? = null,