From caad5066ff7d245c3f397ccb4c307dddec005a41 Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Sun, 27 Oct 2024 17:48:52 +0100 Subject: [PATCH] Refactor followSetNotesFlow() and introduce FollowSetFeedFilter. --- .../vitorpamplona/amethyst/model/Account.kt | 8 +-- .../amethyst/ui/dal/FollowSetFeedFilter.kt | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/FollowSetFeedFilter.kt 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 baadee1fa..8ad155b96 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -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 = 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 = getMuteListNote().flow().metadata.stateFlow fun getBlockList(): PeopleListEvent? = getBlockListNote().event as? PeopleListEvent diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/FollowSetFeedFilter.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/FollowSetFeedFilter.kt new file mode 100644 index 000000000..188fe6a74 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/FollowSetFeedFilter.kt @@ -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() { + 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() + 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 = + account + .followSetNotesFlow() + .value + .user + .followSets + .map { setEntry -> mapEventToSet(setEntry.value) } +}