Skip to content

Commit

Permalink
Fixes miscaching flows of the relay lists from follows.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Aug 14, 2024
1 parent 6b19389 commit 7478899
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 32 deletions.
91 changes: 61 additions & 30 deletions amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -540,17 +542,14 @@ class Account(
}

fun authorsPerRelay(
pubkeyList: Set<HexKey>,
followsNIP65RelayLists: Array<NoteState>,
defaultRelayList: List<String>,
): Flow<Map<String, List<String>>> =
combine(
pubkeyList.map {
getNIP65RelayListFlow(it)
},
) { followsNIP65RelayLists ->
): Map<String, List<HexKey>> {
val test =
assembleAuthorsPerWriteRelay(
followsNIP65RelayLists
.mapNotNull {
.mapNotNull
{
val author = (it.note as? AddressableNote)?.address?.pubKeyHex
val event = (it.note.event as? AdvertisedRelayListEvent)

Expand All @@ -567,23 +566,39 @@ class Account(
}.toMap(),
hasOnionConnection = proxy != null,
)
}

return test
}

@OptIn(ExperimentalCoroutinesApi::class)
val liveHomeListAuthorsPerRelayFlow: Flow<Map<String, List<String>>?> 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<Array<NoteState>?> =
liveHomeFollowLists
.transformLatest { followList ->
if (followList != null) {
emitAll(combine(followList.usersPlusMe.map { getNIP65RelayListFlow(it) }) { it })
} else {
emit(null)
}
}

val liveHomeListAuthorsPerRelayFlow: Flow<Map<String, List<HexKey>>?> 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<Map<String, List<String>>?> by lazy {
liveHomeListAuthorsPerRelayFlow.stateIn(scope, SharingStarted.Eagerly, emptyMap())
val liveHomeListAuthorsPerRelay: StateFlow<Map<String, List<HexKey>>?> 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)
Expand Down Expand Up @@ -611,15 +626,23 @@ class Account(
}

@OptIn(ExperimentalCoroutinesApi::class)
val liveStoriesFollowListAdvertizedRelayListFlow: Flow<Array<NoteState>?> =
liveStoriesFollowLists
.transformLatest { followList ->
if (followList != null) {
emitAll(combine(followList.usersPlusMe.map { getNIP65RelayListFlow(it) }) { it })
} else {
emit(null)
}
}

val liveStoriesListAuthorsPerRelayFlow: Flow<Map<String, List<String>>?> 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
}
}

Expand All @@ -640,15 +663,23 @@ class Account(
}

@OptIn(ExperimentalCoroutinesApi::class)
val liveDiscoveryFollowListAdvertizedRelayListFlow: Flow<Array<NoteState>?> =
liveDiscoveryFollowLists
.transformLatest { followList ->
if (followList != null) {
emitAll(combine(followList.usersPlusMe.map { getNIP65RelayListFlow(it) }) { it })
} else {
emit(null)
}
}

val liveDiscoveryListAuthorsPerRelayFlow: Flow<Map<String, List<String>>?> 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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>? = null,
val authors: Map<String, List<String>>? = null,
val ids: List<HexKey>? = null,
val authors: Map<String, List<HexKey>>? = null,
val kinds: List<Int>? = null,
val tags: Map<String, List<String>>? = null,
val since: Map<String, EOSETime>? = null,
Expand Down

0 comments on commit 7478899

Please sign in to comment.