Refactor followSetNotesFlow() and introduce FollowSetFeedFilter.

This commit is contained in:
KotlinGeekDev
2024-10-27 17:48:52 +01:00
parent c86ce1b1ce
commit caad5066ff
2 changed files with 66 additions and 6 deletions
@@ -121,7 +121,6 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.combineTransform
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
@@ -2899,11 +2898,6 @@ class Account(
fun getFollowSetNotes(): List<AddressableNote> = LocalCache.getFollowSetsFor(userProfile())
suspend fun followSetNotesFlow() =
flowOf(getFollowSetNotes())
.flowOn(Dispatchers.IO)
.stateIn(scope)
fun loadUserFollowSets() {
val sets =
getFollowSetNotes()
@@ -2913,6 +2907,8 @@ class Account(
userProfile().followSets = sets
}
fun followSetNotesFlow() = userProfile().flow().followSets.stateFlow
fun getMuteListFlow(): StateFlow<NoteState> = getMuteListNote().flow().metadata.stateFlow
fun getBlockList(): PeopleListEvent? = getBlockListNote().event as? PeopleListEvent
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2024 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.ui.dal
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.quartz.events.PeopleListEvent
class FollowSetFeedFilter(
val account: Account,
) : FeedFilter<PeopleListEvent.FollowSet>() {
override fun feedKey(): String = account.userProfile().pubkeyHex
private fun mapEventToSet(event: PeopleListEvent): PeopleListEvent.FollowSet {
val dTag = event.dTag()
val listTitle = event.title() ?: dTag
val listDescription = event.description() ?: ""
val publicFollows = event.filterTagList("p", null)
val privateFollows = mutableListOf<String>()
event.privateTaggedUsers(account.signer) { userList -> privateFollows.addAll(userList) }
return if (publicFollows.isEmpty() && privateFollows.isNotEmpty()) {
PeopleListEvent.FollowSet(
isPrivate = true,
title = listTitle,
description = listDescription,
profileList = privateFollows.toSet(),
)
} else if (publicFollows.isNotEmpty() && privateFollows.isEmpty()) {
PeopleListEvent.FollowSet(
isPrivate = false,
title = listTitle,
description = listDescription,
profileList = publicFollows.toSet(),
)
} else {
throw Exception("Mixed follow sets are not yet supported.")
}
}
override fun feed(): List<PeopleListEvent.FollowSet> =
account
.followSetNotesFlow()
.value
.user
.followSets
.map { setEntry -> mapEventToSet(setEntry.value) }
}