Caching followingChannels and followingUsers to speed Global filter up

This commit is contained in:
Vitor Pamplona
2023-03-15 13:39:47 -04:00
parent dbc155454f
commit 83a8558f23

View File

@@ -10,7 +10,11 @@ import com.vitorpamplona.amethyst.service.model.TextNoteEvent
object GlobalFeedFilter : FeedFilter<Note>() { object GlobalFeedFilter : FeedFilter<Note>() {
lateinit var account: Account lateinit var account: Account
override fun feed() = LocalCache.notes.values override fun feed(): List<Note> {
val followChannels = account.followingChannels()
val followUsers = account.followingKeySet()
return LocalCache.notes.values
.asSequence() .asSequence()
.filter { .filter {
(it.event is TextNoteEvent || it.event is LongTextNoteEvent || it.event is ChannelMessageEvent) && (it.event is TextNoteEvent || it.event is LongTextNoteEvent || it.event is ChannelMessageEvent) &&
@@ -18,12 +22,13 @@ object GlobalFeedFilter : FeedFilter<Note>() {
} }
.filter { .filter {
// does not show events already in the public chat list // does not show events already in the public chat list
(it.channel() == null || it.channel() !in account.followingChannels()) && (it.channel() == null || it.channel() !in followChannels) &&
// does not show people the user already follows // does not show people the user already follows
(it.author?.pubkeyHex !in account.followingKeySet()) (it.author?.pubkeyHex !in followUsers)
} }
.filter { account.isAcceptable(it) } .filter { account.isAcceptable(it) }
.sortedBy { it.createdAt() } .sortedBy { it.createdAt() }
.toList() .toList()
.reversed() .reversed()
}
} }