From 90a5c15ee9ff8f35aae35a3e5b6b0d589e12beba Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 11 Sep 2025 11:27:29 -0400 Subject: [PATCH] Moves the OTS processor from Account's decrypt process to Application since it doesn't need the account information. --- .../vitorpamplona/amethyst/model/Account.kt | 3 +- .../IncomingOtsEventVerifier.kt | 54 +++++++++++++++++++ .../loggedIn/DecryptAndIndexProcessor.kt | 21 -------- 3 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip03Timestamp/IncomingOtsEventVerifier.kt 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 cec3aa6b0..307a88ed5 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -334,11 +334,10 @@ class Account( privacyState::shouldUseTorForMoneyOperations, Amethyst.instance.otsBlockHeightCache, ) + val newNotesPreProcessor = EventProcessor(this, cache) val otsState = OtsState(signer, cache, otsResolverBuilder, scope, settings) - val newNotesPreProcessor = EventProcessor(this, otsVerifCache, cache) - val feedDecryptionCaches = FeedDecryptionCaches( peopleListCache = peopleListDecryptionCache, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip03Timestamp/IncomingOtsEventVerifier.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip03Timestamp/IncomingOtsEventVerifier.kt new file mode 100644 index 000000000..d4cc44f70 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip03Timestamp/IncomingOtsEventVerifier.kt @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.model.nip03Timestamp + +import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.model.Note +import com.vitorpamplona.quartz.nip03Timestamp.OtsEvent +import com.vitorpamplona.quartz.nip03Timestamp.VerificationStateCache +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.stateIn + +class IncomingOtsEventVerifier( + private val otsVerifCache: VerificationStateCache, + private val cache: LocalCache, + private val scope: CoroutineScope, +) { + val verifying = + cache.live.newEventBundles + .onEach { newNotes -> + newNotes.forEach(::consume) + }.stateIn( + scope, + SharingStarted.Eagerly, + null, + ) + + fun consume(note: Note) { + note.event?.let { event -> + if (event is OtsEvent) { + otsVerifCache.cacheVerify(event) + } + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/DecryptAndIndexProcessor.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/DecryptAndIndexProcessor.kt index c9a675509..cd12ea159 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/DecryptAndIndexProcessor.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/DecryptAndIndexProcessor.kt @@ -28,9 +28,6 @@ import com.vitorpamplona.amethyst.model.privateChats.ChatroomList import com.vitorpamplona.quartz.experimental.ephemChat.chat.EphemeralChatEvent import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.IEvent -import com.vitorpamplona.quartz.nip03Timestamp.OtsEvent -import com.vitorpamplona.quartz.nip03Timestamp.VerificationStateCache -import com.vitorpamplona.quartz.nip03Timestamp.ots.okhttp.OkHttpOtsResolverBuilder import com.vitorpamplona.quartz.nip17Dm.base.ChatroomKeyable import com.vitorpamplona.quartz.nip28PublicChat.message.ChannelMessageEvent import com.vitorpamplona.quartz.nip37Drafts.DraftWrapEvent @@ -44,12 +41,9 @@ import kotlinx.coroutines.CancellationException class EventProcessor( private val account: Account, - private val otsVerifCache: VerificationStateCache, private val cache: LocalCache, ) { private val chatHandler = ChatHandler(account.chatroomList) - private val otsHandler = OtsEventHandler(account.otsResolverBuilder, otsVerifCache) - private val draftHandler = DraftEventHandler(account, cache) private val giftWrapHandler = GiftWrapEventHandler(account, cache, this) @@ -75,7 +69,6 @@ class EventProcessor( ) { when (event) { is ChatroomKeyable -> chatHandler.add(event, eventNote, publicNote) - is OtsEvent -> otsHandler.add(event, eventNote, publicNote) is DraftWrapEvent -> draftHandler.add(event, eventNote, publicNote) is GiftWrapEvent -> giftWrapHandler.add(event, eventNote, publicNote) is SealedRumorEvent -> sealHandler.add(event, eventNote, publicNote) @@ -99,7 +92,6 @@ class EventProcessor( ) { when (event) { is ChatroomKeyable -> chatHandler.delete(event, note) - is OtsEvent -> otsHandler.delete(event, note) is DraftWrapEvent -> draftHandler.delete(event, note) is GiftWrapEvent -> giftWrapHandler.delete(event, note) is SealedRumorEvent -> sealHandler.delete(event, note) @@ -180,19 +172,6 @@ class ChatHandler( } } -class OtsEventHandler( - private val otsResolverBuilder: OkHttpOtsResolverBuilder, - private val otsVerifCache: VerificationStateCache, -) : EventHandler { - override suspend fun add( - event: OtsEvent, - eventNote: Note, - publicNote: Note, - ) { - otsVerifCache.cacheVerify(event, otsResolverBuilder) - } -} - class DraftEventHandler( private val account: Account, private val cache: LocalCache,