Streamlines Global filter for performance

This commit is contained in:
Vitor Pamplona
2023-04-07 14:26:23 -04:00
parent 3a082d131f
commit a5da4ea53d

View File

@@ -3,10 +3,7 @@ package com.vitorpamplona.amethyst.ui.dal
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.service.model.*
import com.vitorpamplona.amethyst.service.model.LongTextNoteEvent
import com.vitorpamplona.amethyst.service.model.PollNoteEvent
import com.vitorpamplona.amethyst.service.model.TextNoteEvent
object GlobalFeedFilter : FeedFilter<Note>() { object GlobalFeedFilter : FeedFilter<Note>() {
lateinit var account: Account lateinit var account: Account
@@ -14,41 +11,43 @@ object GlobalFeedFilter : FeedFilter<Note>() {
override fun feed(): List<Note> { override fun feed(): List<Note> {
val followChannels = account.followingChannels() val followChannels = account.followingChannels()
val followUsers = account.followingKeySet() val followUsers = account.followingKeySet()
val now = System.currentTimeMillis() / 1000
val notes = LocalCache.notes.values val notes = LocalCache.notes.values
.asSequence() .asSequence()
.filter { .filter {
(it.event is TextNoteEvent || it.event is LongTextNoteEvent || it.event is PollNoteEvent || it.event is ChannelMessageEvent) && it.event is BaseTextNoteEvent && it.replyTo.isNullOrEmpty()
it.replyTo.isNullOrEmpty()
} }
.filter { .filter {
val channel = it.channel()
// 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 followChannels) && (channel == null || channel !in followChannels) &&
// does not show people the user already follows // does not show people the user already follows
(it.author?.pubkeyHex !in followUsers) (it.author?.pubkeyHex !in followUsers)
} }
.filter { account.isAcceptable(it) } .filter { account.isAcceptable(it) }
.filter { .filter {
// Do not show notes with the creation time exceeding the current time, as they will always stay at the top of the global feed, which is cheating. // Do not show notes with the creation time exceeding the current time, as they will always stay at the top of the global feed, which is cheating.
it.createdAt()!! <= System.currentTimeMillis() / 1000 it.createdAt()!! <= now
} }
.toList() .toList()
val longFormNotes = LocalCache.addressables.values val longFormNotes = LocalCache.addressables.values
.asSequence() .asSequence()
.filter { .filter {
(it.event is LongTextNoteEvent) && it.replyTo.isNullOrEmpty() it.event is LongTextNoteEvent && it.replyTo.isNullOrEmpty()
} }
.filter { .filter {
val channel = it.channel()
// 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 followChannels) && (channel == null || channel !in followChannels) &&
// does not show people the user already follows // does not show people the user already follows
(it.author?.pubkeyHex !in followUsers) (it.author?.pubkeyHex !in followUsers)
} }
.filter { account.isAcceptable(it) } .filter { account.isAcceptable(it) }
.filter { .filter {
// Do not show notes with the creation time exceeding the current time, as they will always stay at the top of the global feed, which is cheating. // Do not show notes with the creation time exceeding the current time, as they will always stay at the top of the global feed, which is cheating.
it.createdAt()!! <= System.currentTimeMillis() / 1000 it.createdAt()!! <= now
} }
.toList() .toList()