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 9c335667e6..e16e4622e8 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -25,6 +25,7 @@ import androidx.compose.runtime.Stable import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.BuildConfig import com.vitorpamplona.amethyst.commons.richtext.RichTextParser +import com.vitorpamplona.amethyst.logTime import com.vitorpamplona.amethyst.model.edits.PrivateStorageRelayListDecryptionCache import com.vitorpamplona.amethyst.model.edits.PrivateStorageRelayListState import com.vitorpamplona.amethyst.model.emphChat.EphemeralChatChannel @@ -91,6 +92,7 @@ import com.vitorpamplona.amethyst.model.torState.TorRelayState import com.vitorpamplona.amethyst.service.location.LocationState import com.vitorpamplona.amethyst.service.relayClient.reqCommand.nwc.NWCPaymentFilterAssembler import com.vitorpamplona.amethyst.service.uploads.FileHeader +import com.vitorpamplona.amethyst.ui.screen.loggedIn.EventProcessor import com.vitorpamplona.quartz.experimental.bounties.BountyAddValueEvent import com.vitorpamplona.quartz.experimental.edits.TextNoteModificationEvent import com.vitorpamplona.quartz.experimental.interactiveStories.InteractiveStoryBaseEvent @@ -331,6 +333,8 @@ class Account( val otsState = OtsState(signer, cache, otsResolverBuilder, scope, settings) + val newNotesPreProcessor = EventProcessor(this, LocalCache) + val feedDecryptionCaches = FeedDecryptionCaches( peopleListCache = peopleListDecryptionCache, @@ -828,7 +832,7 @@ class Account( } } - suspend fun updateAttestations() = sendAutomatic(otsState.updateAttestations()) + fun upgradeAttestations() = otsState.upgradeAttestationsIfNeeded(::sendAutomatic) suspend fun follow(user: User) = sendMyPublicAndPrivateOutbox(kind3FollowList.follow(user)) @@ -1765,7 +1769,7 @@ class Account( init { Log.d("AccountRegisterObservers", "Init") - scope.launch(Dispatchers.Default) { + scope.launch { cache.antiSpam.flowSpam.collect { it.cache.spamMessages.snapshot().values.forEach { spammer -> if (!hiddenUsers.isHidden(spammer.pubkeyHex) && spammer.shouldHide()) { @@ -1776,5 +1780,22 @@ class Account( } } } + + scope.launch { + LocalCache.live.newEventBundles.collect { newNotes -> + logTime("Account ${userProfile()} newEventBundle Update with ${newNotes.size} new notes") { + upgradeAttestations() + newNotesPreProcessor.runNew(newNotes) + } + } + } + + scope.launch { + LocalCache.live.deletedEventBundles.collect { newNotes -> + logTime("Account ${userProfile()} deletedEventBundle Update with ${newNotes.size} new notes") { + newNotesPreProcessor.runDeleted(newNotes) + } + } + } } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip03Timestamp/OtsState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip03Timestamp/OtsState.kt index 763d724194..4087896f07 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip03Timestamp/OtsState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip03Timestamp/OtsState.kt @@ -28,8 +28,10 @@ import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner import com.vitorpamplona.quartz.nip03Timestamp.OtsEvent import com.vitorpamplona.quartz.nip03Timestamp.ots.okhttp.OkHttpOtsResolverBuilder +import com.vitorpamplona.quartz.utils.TimeUtils import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch import java.util.Base64 class OtsState( @@ -39,6 +41,19 @@ class OtsState( val scope: CoroutineScope, val settings: AccountSettings, ) { + var lastTimeItTriedToUpdateAttestations: Long = 0 + + fun upgradeAttestationsIfNeeded(onReady: (List) -> Unit) { + // only tries to upgrade every hour + val now = TimeUtils.now() + if (now - lastTimeItTriedToUpdateAttestations > TimeUtils.ONE_HOUR) { + lastTimeItTriedToUpdateAttestations = now + scope.launch { + onReady(updateAttestations()) + } + } + } + suspend fun updateAttestations(): List { Log.d("Pending Attestations", "Updating ${settings.pendingAttestations.value.size} pending attestations") diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index 14018abdb9..3905df8fb7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -43,7 +43,6 @@ import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.compose.GenericBaseCache import com.vitorpamplona.amethyst.commons.compose.GenericBaseCacheAsync -import com.vitorpamplona.amethyst.isDebug import com.vitorpamplona.amethyst.logTime import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.AccountSettings @@ -168,8 +167,6 @@ class AccountViewModel( val app: Amethyst, ) : ViewModel(), Dao { - val newNotesPreProcessor = EventProcessor(account, LocalCache) - var firstRoute: Route? = null val toastManager = ToastManager() @@ -708,17 +705,6 @@ class AccountViewModel( fun timestamp(note: Note) = runIOCatching { account.otsState.timestamp(note) } - var lastTimeItTriedToUpdateAttestations: Long = 0 - - fun upgradeAttestations() { - // only tries to upgrade every hour - val now = TimeUtils.now() - if (now - lastTimeItTriedToUpdateAttestations > TimeUtils.ONE_HOUR) { - lastTimeItTriedToUpdateAttestations = now - runIOCatching { account.updateAttestations() } - } - } - fun delete(notes: List) = runIOCatching { account.delete(notes) } fun delete(note: Note) = runIOCatching { account.delete(note) } @@ -1146,32 +1132,15 @@ class AccountViewModel( feedStates.init() // awaits for init to finish before starting to capture new events. LocalCache.live.newEventBundles.collect { newNotes -> - if (isDebug) { - Log.d( - "Rendering Metrics", - "Update feeds ${this@AccountViewModel} for ${account.userProfile().toBestDisplayName()} with ${newNotes.size} new notes", - ) - } logTime("AccountViewModel newEventBundle Update with ${newNotes.size} new notes") { feedStates.updateFeedsWith(newNotes) - upgradeAttestations() - viewModelScope.launch(Dispatchers.Default) { - newNotesPreProcessor.runNew(newNotes) - } } } } viewModelScope.launch(Dispatchers.Default) { LocalCache.live.deletedEventBundles.collect { newNotes -> - if (isDebug) { - Log.d( - "Rendering Metrics", - "Delete feeds ${this@AccountViewModel} for ${account.userProfile().toBestDisplayName()} with ${newNotes.size} new notes", - ) - } logTime("AccountViewModel deletedEventBundle Update with ${newNotes.size} new notes") { - newNotesPreProcessor.runDeleted(newNotes) feedStates.deleteNotes(newNotes) } }