Adjusts Outbox filter as soon as a new NIP-65 relay list shows up

This commit is contained in:
Vitor Pamplona
2024-08-15 15:17:41 -04:00
parent 2f5b99a1c7
commit bd18d4b4b2
2 changed files with 34 additions and 25 deletions
@@ -449,11 +449,10 @@ class Account(
val liveKind3Follows = liveKind3FollowsFlow.stateIn(scope, SharingStarted.Eagerly, LiveFollowLists(usersPlusMe = setOf(keyPair.pubKeyHex))) val liveKind3Follows = liveKind3FollowsFlow.stateIn(scope, SharingStarted.Eagerly, LiveFollowLists(usersPlusMe = setOf(keyPair.pubKeyHex)))
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
private val liveHomeList: Flow<ListNameNotePair> by lazy { private val liveHomeList: Flow<ListNameNotePair> =
defaultHomeFollowList.flatMapLatest { listName -> defaultHomeFollowList.flatMapLatest { listName ->
loadPeopleListFlowFromListName(listName) loadPeopleListFlowFromListName(listName)
} }
}
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
fun loadPeopleListFlowFromListName(listName: String): Flow<ListNameNotePair> = fun loadPeopleListFlowFromListName(listName: String): Flow<ListNameNotePair> =
@@ -490,7 +489,7 @@ class Account(
} }
val liveHomeFollowListFlow: Flow<LiveFollowLists?> by lazy { val liveHomeFollowListFlow: Flow<LiveFollowLists?> by lazy {
combinePeopleListFlows(liveKind3FollowsFlow, liveHomeList) combinePeopleListFlows(liveKind3Follows, liveHomeList)
} }
val liveHomeFollowLists: StateFlow<LiveFollowLists?> by lazy { val liveHomeFollowLists: StateFlow<LiveFollowLists?> by lazy {
@@ -544,31 +543,27 @@ class Account(
fun authorsPerRelay( fun authorsPerRelay(
followsNIP65RelayLists: Array<NoteState>, followsNIP65RelayLists: Array<NoteState>,
defaultRelayList: List<String>, defaultRelayList: List<String>,
): Map<String, List<HexKey>> { ): Map<String, List<HexKey>> =
val test = assembleAuthorsPerWriteRelay(
assembleAuthorsPerWriteRelay( followsNIP65RelayLists
followsNIP65RelayLists .mapNotNull
.mapNotNull {
{ val author = (it.note as? AddressableNote)?.address?.pubKeyHex
val author = (it.note as? AddressableNote)?.address?.pubKeyHex val event = (it.note.event as? AdvertisedRelayListEvent)
val event = (it.note.event as? AdvertisedRelayListEvent)
if (event != null) { if (event != null) {
event.pubKey to event.writeRelays() event.pubKey to event.writeRelays()
} else {
if (author != null) {
author to defaultRelayList
} else { } else {
if (author != null) { Log.e("Account", "This author should NEVER be null. Note: ${it.note.idHex}")
author to defaultRelayList null
} else {
Log.e("Account", "This author should NEVER be null. Note: ${it.note.idHex}")
null
}
} }
}.toMap(), }
hasOnionConnection = proxy != null, }.toMap(),
) hasOnionConnection = proxy != null,
)
return test
}
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
val liveHomeFollowListAdvertizedRelayListFlow: Flow<Array<NoteState>?> = val liveHomeFollowListAdvertizedRelayListFlow: Flow<Array<NoteState>?> =
@@ -55,6 +55,7 @@ object NostrHomeDataSource : AmethystNostrDataSource("HomeFeed") {
val latestEOSEs = EOSEAccount() val latestEOSEs = EOSEAccount()
var job: Job? = null var job: Job? = null
var job2: Job? = null
override fun start() { override fun start() {
job?.cancel() job?.cancel()
@@ -68,12 +69,25 @@ object NostrHomeDataSource : AmethystNostrDataSource("HomeFeed") {
} }
} }
} }
job2?.cancel()
job2 =
scope.launch(Dispatchers.IO) {
// creates cache on main
withContext(Dispatchers.Main) { account.userProfile().live() }
account.liveHomeListAuthorsPerRelay.collect {
if (this@NostrHomeDataSource::account.isInitialized) {
invalidateFilters()
}
}
}
super.start() super.start()
} }
override fun stop() { override fun stop() {
super.stop() super.stop()
job?.cancel() job?.cancel()
job2?.cancel()
} }
fun createFollowAccountsFilter(): TypedFilter { fun createFollowAccountsFilter(): TypedFilter {