From bc22b78f197a53b254f0964c50d59950364ed5c3 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 7 May 2025 13:47:34 -0400 Subject: [PATCH] Refactors EOSE management for Chatroom List --- .../datasource/ChatroomListFilterAssembler.kt | 102 +++++++++--------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/datasource/ChatroomListFilterAssembler.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/datasource/ChatroomListFilterAssembler.kt index 32fc70d29..2610ab331 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/datasource/ChatroomListFilterAssembler.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/datasource/ChatroomListFilterAssembler.kt @@ -49,37 +49,48 @@ class ChatroomListFilterAssembler( val latestEOSEs = EOSEAccount() val chatRoomListKey = "ChatroomList" - fun createMessagesToMeFilter(account: Account) = + fun since(key: ChatroomListState) = + latestEOSEs.users[key.account.userProfile()] + ?.followList + ?.get(chatRoomListKey) + ?.relayList + + fun newEose( + key: ChatroomListState, + relayUrl: String, + time: Long, + ) { + latestEOSEs.addOrUpdate( + key.account.userProfile(), + chatRoomListKey, + relayUrl, + time, + ) + } + + fun createMessagesToMeFilter(key: ChatroomListState) = TypedFilter( types = setOf(FeedType.PRIVATE_DMS), filter = SincePerRelayFilter( kinds = listOf(PrivateDmEvent.KIND), - tags = mapOf("p" to listOf(account.userProfile().pubkeyHex)), - since = - latestEOSEs.users[account.userProfile()] - ?.followList - ?.get(chatRoomListKey) - ?.relayList, + tags = mapOf("p" to listOf(key.account.userProfile().pubkeyHex)), + since = since(key), ), ) - fun createMessagesFromMeFilter(account: Account) = + fun createMessagesFromMeFilter(key: ChatroomListState) = TypedFilter( types = setOf(FeedType.PRIVATE_DMS), filter = SincePerRelayFilter( kinds = listOf(PrivateDmEvent.KIND), - authors = listOf(account.userProfile().pubkeyHex), - since = - latestEOSEs.users[account.userProfile()] - ?.followList - ?.get(chatRoomListKey) - ?.relayList, + authors = listOf(key.account.userProfile().pubkeyHex), + since = since(key), ), ) - fun createChannelsCreatedbyMeFilter(account: Account) = + fun createChannelsCreatedbyMeFilter(key: ChatroomListState) = TypedFilter( types = setOf(FeedType.PUBLIC_CHATS), filter = @@ -89,17 +100,13 @@ class ChatroomListFilterAssembler( ChannelCreateEvent.KIND, ChannelMetadataEvent.KIND, ), - authors = listOf(account.userProfile().pubkeyHex), - since = - latestEOSEs.users[account.userProfile()] - ?.followList - ?.get(chatRoomListKey) - ?.relayList, + authors = listOf(key.account.userProfile().pubkeyHex), + since = since(key), ), ) - fun createMyChannelsFilter(account: Account): TypedFilter? { - val followingEvents = account.publicChatList.livePublicChatEventIdSet.value + fun createMyChannelsFilter(key: ChatroomListState): TypedFilter? { + val followingEvents = key.account.publicChatList.livePublicChatEventIdSet.value if (followingEvents.isEmpty()) return null @@ -110,18 +117,14 @@ class ChatroomListFilterAssembler( SincePerRelayFilter( kinds = listOf(ChannelCreateEvent.KIND), ids = followingEvents.toList(), - since = - latestEOSEs.users[account.userProfile()] - ?.followList - ?.get(chatRoomListKey) - ?.relayList, + since = since(key), ), ) } - fun createLastChannelInfoFilter(account: Account): List? { + fun createLastChannelInfoFilter(key: ChatroomListState): List? { val followingEvents = - account.publicChatList.livePublicChatEventIdSet.value + key.account.publicChatList.livePublicChatEventIdSet.value if (followingEvents.isEmpty()) return null @@ -133,15 +136,16 @@ class ChatroomListFilterAssembler( SincePerRelayFilter( kinds = listOf(ChannelMetadataEvent.KIND), tags = mapOf("e" to listOf(it)), + since = since(key), limit = 1, ), ) } } - fun createLastMessageOfEachChannelFilter(account: Account): List? { + fun createLastMessageOfEachChannelFilter(key: ChatroomListState): List? { val followingEvents = - account.publicChatList.livePublicChatEventIdSet.value + key.account.publicChatList.livePublicChatEventIdSet.value if (followingEvents.isEmpty()) return null @@ -152,11 +156,7 @@ class ChatroomListFilterAssembler( SincePerRelayFilter( kinds = listOf(ChannelMessageEvent.KIND), tags = mapOf("e" to listOf(it)), - since = - latestEOSEs.users[account.userProfile()] - ?.followList - ?.get(chatRoomListKey) - ?.relayList, + since = since(key), // Remember to consider spam that is being removed from the UI limit = 50, ), @@ -164,9 +164,9 @@ class ChatroomListFilterAssembler( } } - fun createLastMessageOfEachEphemeralChatFilter(account: Account): List? { + fun createLastMessageOfEachEphemeralChatFilter(key: ChatroomListState): List? { val followingEvents = - account.ephemeralChatList.liveEphemeralChatList.value + key.account.ephemeralChatList.liveEphemeralChatList.value .map { it.id } if (followingEvents.isEmpty()) return null @@ -179,11 +179,7 @@ class ChatroomListFilterAssembler( SincePerRelayFilter( kinds = listOf(EphemeralChatEvent.KIND), tags = mapOf("d" to followingEvents), - since = - latestEOSEs.users[account.userProfile()] - ?.followList - ?.get(chatRoomListKey) - ?.relayList, + since = since(key), // Remember to consider spam that is being removed from the UI limit = 50, ), @@ -191,21 +187,21 @@ class ChatroomListFilterAssembler( ) } - fun mergeAllFilters(account: Account): List? = + fun mergeAllFilters(key: ChatroomListState): List? = listOfNotNull( listOfNotNull( - createMessagesToMeFilter(account), - createMessagesFromMeFilter(account), - createMyChannelsFilter(account), + createMessagesToMeFilter(key), + createMessagesFromMeFilter(key), + createMyChannelsFilter(key), ), - createLastChannelInfoFilter(account), - createLastMessageOfEachChannelFilter(account), - createLastMessageOfEachEphemeralChatFilter(account), + createLastChannelInfoFilter(key), + createLastMessageOfEachChannelFilter(key), + createLastMessageOfEachEphemeralChatFilter(key), ).flatten().ifEmpty { null } fun newSub(key: ChatroomListState): Subscription = requestNewSubscription { time, relayUrl -> - latestEOSEs.addOrUpdate(key.account.userProfile(), chatRoomListKey, relayUrl, time) + newEose(key, relayUrl, time) } val userSubscriptionMap = mutableMapOf() @@ -228,7 +224,7 @@ class ChatroomListFilterAssembler( uniqueSubscribedAccounts.forEach { val user = it.account.userProfile() val sub = findOrCreateSubFor(it) - sub.typedFilters = mergeAllFilters(it.account) + sub.typedFilters = mergeAllFilters(it) updated.add(user) }