Speeding up userprofile, hidden user search and notification filter time

This commit is contained in:
Vitor Pamplona 2023-02-21 16:40:27 -05:00
parent 110c074993
commit a2f14c3671
4 changed files with 26 additions and 18 deletions

View File

@ -61,9 +61,16 @@ class Account(
var latestContactList: ContactListEvent? = null
) {
var transientHiddenUsers: Set<String> = setOf()
@Transient
var userProfile: User? = null
fun userProfile(): User {
return LocalCache.getOrCreateUser(loggedIn.pubKey.toHexKey())
userProfile?.let { return it }
val newUser = LocalCache.getOrCreateUser(loggedIn.pubKey.toHexKey())
userProfile = newUser
return newUser
}
fun followingChannels(): List<Channel> {
@ -483,10 +490,10 @@ class Account(
val liveLanguages: AccountLiveData = AccountLiveData(this)
val saveable: AccountLiveData = AccountLiveData(this)
fun isHidden(user: User) = user in hiddenUsers()
fun isHidden(user: User) = user.pubkeyHex in hiddenUsers || user.pubkeyHex in transientHiddenUsers
fun isAcceptable(user: User): Boolean {
return user !in hiddenUsers() // if user hasn't hided this author
return !isHidden(user) // if user hasn't hided this author
&& user.reportsBy( userProfile() ).isEmpty() // if user has not reported this post
&& user.reportAuthorsBy( userProfile().follows ).size < 5
}

View File

@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.ui.dal
import android.util.Log
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.UrlCachedPreviewer
import kotlin.time.ExperimentalTime
@ -10,8 +11,14 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
abstract class FeedFilter<T>() {
@OptIn(ExperimentalTime::class)
fun loadTop(): List<T> {
return feed().take(1000)
val (feed, elapsed) = measureTimedValue {
feed().take(1000)
}
Log.d("Time","${this.javaClass.simpleName} Feed in ${elapsed}")
return feed
}
abstract fun feed(): List<T>

View File

@ -1,11 +1,7 @@
package com.vitorpamplona.amethyst.ui.dal
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User
import nostr.postr.JsonFilter
import nostr.postr.events.TextNoteEvent
object HiddenAccountsFeedFilter: FeedFilter<User>() {
lateinit var account: Account

View File

@ -109,7 +109,7 @@ private fun homeHasNewItems(account: Account, cache: NotificationCache, context:
HomeNewThreadFeedFilter.account = account
return HomeNewThreadFeedFilter.feed().any {(it.event?.createdAt ?: 0) > lastTime }
return (HomeNewThreadFeedFilter.feed().firstOrNull { it.event?.createdAt != null }?.event?.createdAt ?: 0) > lastTime
}
private fun notificationHasNewItems(account: Account, cache: NotificationCache, context: Context): Boolean {
@ -117,19 +117,17 @@ private fun notificationHasNewItems(account: Account, cache: NotificationCache,
NotificationFeedFilter.account = account
return NotificationFeedFilter.feed().any {(it.event?.createdAt ?: 0) > lastTime }
return (NotificationFeedFilter.feed().firstOrNull { it.event?.createdAt != null }?.event?.createdAt ?: 0) > lastTime
}
private fun messagesHasNewItems(account: Account, cache: NotificationCache, context: Context): Boolean {
ChatroomListKnownFeedFilter.account = account
return ChatroomListKnownFeedFilter.feed().any {
if (it.channel == null && it.author != account.userProfile()) {
val lastTime = cache.load("Room/${it.author?.pubkeyHex}", context)
val note = ChatroomListKnownFeedFilter.feed().firstOrNull {
it.event?.createdAt != null && it.channel == null && it.author != account.userProfile()
} ?: return false
(it.event?.createdAt ?: 0) > lastTime
} else {
false
}
}
val lastTime = cache.load("Room/${note.author?.pubkeyHex}", context)
return (note.event?.createdAt ?: 0) > lastTime
}