From 8c4af4ac857b44813258096aa6cec665e66a83e0 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Wed, 13 Sep 2023 11:10:10 -0300 Subject: [PATCH] only decrypt giftwrap in the dms screen --- .../vitorpamplona/amethyst/model/Account.kt | 19 +++++---- .../service/NostrAccountDataSource.kt | 25 ----------- .../EventNotificationConsumer.kt | 41 ------------------- .../ui/screen/loggedIn/ChatroomListScreen.kt | 17 ++++++++ 4 files changed, 29 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 63fbcb48d..992072871 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -532,7 +532,6 @@ class Account( if (privKey != null) return zapResponseEvent.response(privKey, pubKey) - Log.d("zaps", "decrypt with amber") AmberUtils.decrypt(zapResponseEvent.content, pubKey.toHexKey(), zapResponseEvent.id) if (AmberUtils.content.isBlank()) return null return zapResponseEvent.response(AmberUtils.content) @@ -2529,9 +2528,12 @@ class Account( if (!isWriteable() && !loginWithAmber) return null if (loginWithAmber) { - AmberUtils.content = "" - AmberUtils.decrypt(event.content, event.pubKey, event.id, SignerType.NIP44_DECRYPT) - val decryptedContent = AmberUtils.cachedDecryptedContent[event.id] ?: "" + var decryptedContent = AmberUtils.cachedDecryptedContent[event.id] + if (decryptedContent == null) { + AmberUtils.content = "" + AmberUtils.decrypt(event.content, event.pubKey, event.id, SignerType.NIP44_DECRYPT) + } + decryptedContent = AmberUtils.cachedDecryptedContent[event.id] ?: "" if (decryptedContent.isEmpty()) return null return event.cachedGift(keyPair.pubKey, decryptedContent) } @@ -2543,9 +2545,12 @@ class Account( if (!isWriteable() && !loginWithAmber) return null if (loginWithAmber) { - AmberUtils.content = "" - AmberUtils.decrypt(event.content, event.pubKey, event.id, SignerType.NIP44_DECRYPT) - val decryptedContent = AmberUtils.cachedDecryptedContent[event.id] ?: "" + var decryptedContent = AmberUtils.cachedDecryptedContent[event.id] + if (decryptedContent == null) { + AmberUtils.content = "" + AmberUtils.decrypt(event.content, event.pubKey, event.id, SignerType.NIP44_DECRYPT) + } + decryptedContent = AmberUtils.cachedDecryptedContent[event.id] ?: "" if (decryptedContent.isEmpty()) return null return event.cachedGossip(keyPair.pubKey, decryptedContent) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt index 76514e917..764cc159f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt @@ -30,9 +30,6 @@ import com.vitorpamplona.quartz.events.SealedGossipEvent import com.vitorpamplona.quartz.events.StatusEvent import com.vitorpamplona.quartz.events.TextNoteEvent import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch object NostrAccountDataSource : NostrDataSource("AccountData") { lateinit var account: Account @@ -157,17 +154,6 @@ object NostrAccountDataSource : NostrDataSource("AccountData") { event.cachedGift(privateKey)?.let { this.consume(it, relay) } - } else if (account.loginWithAmber) { - GlobalScope.launch(Dispatchers.IO) { - val decryptedContent = AmberUtils.cachedDecryptedContent[event.id] ?: "" - if (decryptedContent.isNotBlank()) { - event.cachedGift(account.keyPair.pubKey, decryptedContent)?.let { - consume(it, relay) - } - } else { - AmberUtils.decryptGossip(event) - } - } } } @@ -177,17 +163,6 @@ object NostrAccountDataSource : NostrDataSource("AccountData") { event.cachedGossip(privateKey)?.let { LocalCache.justConsume(it, relay) } - } else if (account.loginWithAmber) { - GlobalScope.launch(Dispatchers.IO) { - val decryptedContent = AmberUtils.cachedDecryptedContent[event.id] ?: "" - if (decryptedContent.isNotBlank()) { - event.cachedGossip(account.keyPair.pubKey, decryptedContent)?.let { - LocalCache.justConsume(it, relay) - } - } else { - AmberUtils.decryptGossip(event) - } - } } // Don't store sealed gossips to avoid rebroadcasting by mistake. diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt index 4420c7c3c..b99dec080 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt @@ -100,27 +100,6 @@ class EventNotificationConsumer(private val applicationContext: Context) { event.cachedGift(key)?.let { unwrapAndConsume(it, account) } - } else if (account.loginWithAmber) { - var cached = AmberUtils.cachedDecryptedContent[event.id] - if (cached == null) { - AmberUtils.content = "" - AmberUtils.decrypt( - event.content, - event.pubKey, - event.id, - SignerType.NIP44_DECRYPT - ) - cached = AmberUtils.cachedDecryptedContent[event.id] ?: "" - } - - if (cached.isNotBlank()) { - event.cachedGift(account.keyPair.pubKey, cached)?.let { - LocalCache.justConsume(it, null) - it - } - } else { - null - } } else { null } @@ -133,26 +112,6 @@ class EventNotificationConsumer(private val applicationContext: Context) { LocalCache.justConsume(it, null) it } - } else if (account.loginWithAmber) { - var cached = AmberUtils.cachedDecryptedContent[event.id] - if (cached == null) { - AmberUtils.content = "" - AmberUtils.decrypt( - event.content, - event.pubKey, - event.id, - SignerType.NIP44_DECRYPT - ) - cached = AmberUtils.cachedDecryptedContent[event.id] ?: "" - } - if (cached.isNotBlank()) { - event.cachedGossip(account.keyPair.pubKey, cached)?.let { - LocalCache.justConsume(it, null) - it - } - } else { - null - } } else { null } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomListScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomListScreen.kt index 39b60a649..b2a9b2ec9 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomListScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomListScreen.kt @@ -39,6 +39,8 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.service.IntentUtils import com.vitorpamplona.amethyst.service.NostrChatroomListDataSource import com.vitorpamplona.amethyst.ui.screen.ChatroomListFeedView import com.vitorpamplona.amethyst.ui.screen.FeedViewModel @@ -46,6 +48,8 @@ import com.vitorpamplona.amethyst.ui.screen.NostrChatroomListKnownFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrChatroomListNewFeedViewModel import com.vitorpamplona.amethyst.ui.theme.TabRowHeight import com.vitorpamplona.amethyst.ui.theme.placeholderText +import com.vitorpamplona.quartz.events.Event +import com.vitorpamplona.quartz.events.GiftWrapEvent import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -66,6 +70,19 @@ fun ChatroomListScreen( WatchAccountForListScreen(knownFeedViewModel, newFeedViewModel, accountViewModel) + LaunchedEffect(Unit) { + if (accountViewModel.loggedInWithAmber()) { + coroutineScope.launch(Dispatchers.IO) { + val gifts = LocalCache.notes.elements().toList().filter { it.event is GiftWrapEvent } + gifts.forEach { + it.event?.let { + IntentUtils.consume(it as Event) + } + } + } + } + } + val lifeCycleOwner = LocalLifecycleOwner.current DisposableEffect(accountViewModel) { val observer = LifecycleEventObserver { _, event ->