From 33f7c5c82e91273252f620c0a8710942dc292792 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Wed, 13 Sep 2023 18:06:50 -0300 Subject: [PATCH] deleted intentutils --- .../amethyst/service/AmberUtils.kt | 182 ++++++++++++++++-- .../amethyst/service/IntentUtils.kt | 167 ---------------- .../vitorpamplona/amethyst/ui/MainActivity.kt | 4 +- .../ui/screen/loggedIn/ChatroomListScreen.kt | 4 +- 4 files changed, 174 insertions(+), 183 deletions(-) delete mode 100644 app/src/main/java/com/vitorpamplona/amethyst/service/IntentUtils.kt diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/AmberUtils.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/AmberUtils.kt index c8ea573a7..51f9270f9 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/AmberUtils.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/AmberUtils.kt @@ -1,22 +1,180 @@ package com.vitorpamplona.amethyst.service +import android.app.Activity import android.content.Intent import android.net.Uri +import android.util.LruCache +import android.widget.Toast import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts +import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.ServiceManager import com.vitorpamplona.amethyst.model.Account +import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.service.relays.Client +import com.vitorpamplona.amethyst.ui.MainActivity import com.vitorpamplona.amethyst.ui.actions.SignerType import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.events.Event import com.vitorpamplona.quartz.events.EventInterface +import com.vitorpamplona.quartz.events.GiftWrapEvent import com.vitorpamplona.quartz.events.LnZapRequestEvent +import com.vitorpamplona.quartz.events.SealedGossipEvent +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch object AmberUtils { var content: String = "" var isActivityRunning: Boolean = false val cachedDecryptedContent = mutableMapOf() lateinit var account: Account + lateinit var activityResultLauncher: ActivityResultLauncher + lateinit var decryptGossipResultLauncher: ActivityResultLauncher + lateinit var blockListResultLauncher: ActivityResultLauncher + lateinit var signEventResultLauncher: ActivityResultLauncher + val eventCache = LruCache(100) + + @OptIn(DelicateCoroutinesApi::class) + fun consume(event: Event) { + if (LocalCache.justVerify(event)) { + if (event is GiftWrapEvent) { + GlobalScope.launch(Dispatchers.IO) { + val decryptedContent = cachedDecryptedContent[event.id] ?: "" + if (decryptedContent.isNotBlank()) { + event.cachedGift( + NostrAccountDataSource.account.keyPair.pubKey, + decryptedContent + )?.let { + consume(it) + } + } else { + decryptGossip(event) + } + } + } + + if (event is SealedGossipEvent) { + GlobalScope.launch(Dispatchers.IO) { + val decryptedContent = cachedDecryptedContent[event.id] ?: "" + if (decryptedContent.isNotBlank()) { + event.cachedGossip(NostrAccountDataSource.account.keyPair.pubKey, decryptedContent)?.let { + LocalCache.justConsume(it, null) + } + } else { + decryptGossip(event) + } + } + // Don't store sealed gossips to avoid rebroadcasting by mistake. + } else { + LocalCache.justConsume(event, null) + } + } + } + + @OptIn(DelicateCoroutinesApi::class) + fun start(activity: MainActivity) { + signEventResultLauncher = activity.registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { + if (it.resultCode != Activity.RESULT_OK) { + GlobalScope.launch(Dispatchers.Main) { + Toast.makeText( + Amethyst.instance, + "Sign request rejected", + Toast.LENGTH_SHORT + ).show() + } + } else { + val json = it.data?.getStringExtra("event") ?: "" + GlobalScope.launch(Dispatchers.IO) { + val signedEvent = Event.fromJson(json) + if (signedEvent.hasValidSignature()) { + Client.send(signedEvent) + LocalCache.verifyAndConsume(signedEvent, null) + } + } + } + isActivityRunning = false + ServiceManager.shouldPauseService = true + } + + activityResultLauncher = activity.registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { + isActivityRunning = false + ServiceManager.shouldPauseService = true + if (it.resultCode != Activity.RESULT_OK) { + GlobalScope.launch(Dispatchers.Main) { + Toast.makeText( + Amethyst.instance, + "Sign request rejected", + Toast.LENGTH_SHORT + ).show() + } + } else { + val event = it.data?.getStringExtra("signature") ?: "" + content = event + val id = it.data?.getStringExtra("id") ?: "" + if (id.isNotBlank()) { + cachedDecryptedContent[id] = event + } + } + } + + blockListResultLauncher = activity.registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { + if (it.resultCode != Activity.RESULT_OK) { + GlobalScope.launch(Dispatchers.Main) { + Toast.makeText( + Amethyst.instance, + "Sign request rejected", + Toast.LENGTH_SHORT + ).show() + } + } else { + val decryptedContent = it.data?.getStringExtra("signature") ?: "" + val id = it.data?.getStringExtra("id") ?: "" + if (id.isNotBlank()) { + cachedDecryptedContent[id] = decryptedContent + account.live.invalidateData() + } + } + isActivityRunning = false + ServiceManager.shouldPauseService = true + } + + decryptGossipResultLauncher = activity.registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { + if (it.resultCode != Activity.RESULT_OK) { + GlobalScope.launch(Dispatchers.Main) { + Toast.makeText( + Amethyst.instance, + "Sign request rejected", + Toast.LENGTH_SHORT + ).show() + } + } else { + val decryptedContent = it.data?.getStringExtra("signature") ?: "" + val id = it.data?.getStringExtra("id") ?: "" + if (id.isNotBlank()) { + val event = eventCache.get(id) + if (event != null) { + GlobalScope.launch(Dispatchers.IO) { + AmberUtils.cachedDecryptedContent[event.id] = decryptedContent + consume(event) + } + } + } + } + isActivityRunning = false + ServiceManager.shouldPauseService = true + } + } fun openAmber( data: String, @@ -50,12 +208,12 @@ object AmberUtils { openAmber( event.toJson(), SignerType.SIGN_EVENT, - IntentUtils.activityResultLauncher, + activityResultLauncher, "", event.id() ) while (isActivityRunning) { - // do nothing + Thread.sleep(100) } } @@ -66,7 +224,7 @@ object AmberUtils { openAmber( event.toJson(), SignerType.SIGN_EVENT, - IntentUtils.signEventResultLauncher, + signEventResultLauncher, account.keyPair.pubKey.toHexKey(), event.id() ) @@ -77,7 +235,7 @@ object AmberUtils { openAmber( encryptedContent, signerType, - IntentUtils.blockListResultLauncher, + blockListResultLauncher, pubKey, id ) @@ -89,7 +247,7 @@ object AmberUtils { openAmber( encryptedContent, signerType, - IntentUtils.activityResultLauncher, + activityResultLauncher, pubKey, id ) @@ -103,7 +261,7 @@ object AmberUtils { openAmber( encryptedContent, signerType, - IntentUtils.activityResultLauncher, + activityResultLauncher, pubKey, id ) @@ -113,21 +271,21 @@ object AmberUtils { openAmber( encryptedContent, signerType, - IntentUtils.activityResultLauncher, + activityResultLauncher, pubKey, id ) } fun decryptGossip(event: Event) { - if (IntentUtils.eventCache.get(event.id) == null) { - IntentUtils.eventCache.put(event.id, event) + if (eventCache.get(event.id) == null) { + eventCache.put(event.id, event) } isActivityRunning = true openAmber( event.content, SignerType.NIP44_DECRYPT, - IntentUtils.decryptGossipResultLauncher, + decryptGossipResultLauncher, event.pubKey, event.id ) @@ -138,7 +296,7 @@ object AmberUtils { openAmber( decryptedContent, signerType, - IntentUtils.activityResultLauncher, + activityResultLauncher, pubKey, "encrypt" ) @@ -152,7 +310,7 @@ object AmberUtils { openAmber( event.toJson(), SignerType.DECRYPT_ZAP_EVENT, - IntentUtils.activityResultLauncher, + activityResultLauncher, event.pubKey, event.id ) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/IntentUtils.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/IntentUtils.kt deleted file mode 100644 index 2047e4ed1..000000000 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/IntentUtils.kt +++ /dev/null @@ -1,167 +0,0 @@ -package com.vitorpamplona.amethyst.service - -import android.app.Activity -import android.content.Intent -import android.util.LruCache -import android.widget.Toast -import androidx.activity.result.ActivityResultLauncher -import androidx.activity.result.contract.ActivityResultContracts -import com.vitorpamplona.amethyst.Amethyst -import com.vitorpamplona.amethyst.ServiceManager -import com.vitorpamplona.amethyst.model.LocalCache -import com.vitorpamplona.amethyst.service.relays.Client -import com.vitorpamplona.amethyst.ui.MainActivity -import com.vitorpamplona.quartz.events.Event -import com.vitorpamplona.quartz.events.GiftWrapEvent -import com.vitorpamplona.quartz.events.SealedGossipEvent -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch - -object IntentUtils { - lateinit var activityResultLauncher: ActivityResultLauncher - lateinit var decryptGossipResultLauncher: ActivityResultLauncher - lateinit var blockListResultLauncher: ActivityResultLauncher - lateinit var signEventResultLauncher: ActivityResultLauncher - val eventCache = LruCache(100) - - @OptIn(DelicateCoroutinesApi::class) - fun consume(event: Event) { - if (LocalCache.justVerify(event)) { - if (event is GiftWrapEvent) { - GlobalScope.launch(Dispatchers.IO) { - val decryptedContent = AmberUtils.cachedDecryptedContent[event.id] ?: "" - if (decryptedContent.isNotBlank()) { - event.cachedGift( - NostrAccountDataSource.account.keyPair.pubKey, - decryptedContent - )?.let { - consume(it) - } - } else { - AmberUtils.decryptGossip(event) - } - } - } - - if (event is SealedGossipEvent) { - GlobalScope.launch(Dispatchers.IO) { - val decryptedContent = AmberUtils.cachedDecryptedContent[event.id] ?: "" - if (decryptedContent.isNotBlank()) { - event.cachedGossip(NostrAccountDataSource.account.keyPair.pubKey, decryptedContent)?.let { - LocalCache.justConsume(it, null) - } - } else { - AmberUtils.decryptGossip(event) - } - } - // Don't store sealed gossips to avoid rebroadcasting by mistake. - } else { - LocalCache.justConsume(event, null) - } - } - } - - @OptIn(DelicateCoroutinesApi::class) - fun start(activity: MainActivity) { - signEventResultLauncher = activity.registerForActivityResult( - ActivityResultContracts.StartActivityForResult() - ) { - if (it.resultCode != Activity.RESULT_OK) { - GlobalScope.launch(Dispatchers.Main) { - Toast.makeText( - Amethyst.instance, - "Sign request rejected", - Toast.LENGTH_SHORT - ).show() - } - } else { - val json = it.data?.getStringExtra("event") ?: "" - GlobalScope.launch(Dispatchers.IO) { - val signedEvent = Event.fromJson(json) - if (signedEvent.hasValidSignature()) { - Client.send(signedEvent) - LocalCache.verifyAndConsume(signedEvent, null) - } - } - } - AmberUtils.isActivityRunning = false - ServiceManager.shouldPauseService = true - } - - activityResultLauncher = activity.registerForActivityResult( - ActivityResultContracts.StartActivityForResult() - ) { - if (it.resultCode != Activity.RESULT_OK) { - GlobalScope.launch(Dispatchers.Main) { - Toast.makeText( - Amethyst.instance, - "Sign request rejected", - Toast.LENGTH_SHORT - ).show() - } - } else { - val event = it.data?.getStringExtra("signature") ?: "" - AmberUtils.content = event - val id = it.data?.getStringExtra("id") ?: "" - if (id.isNotBlank()) { - AmberUtils.cachedDecryptedContent[id] = event - } - } - AmberUtils.isActivityRunning = false - ServiceManager.shouldPauseService = true - } - - blockListResultLauncher = activity.registerForActivityResult( - ActivityResultContracts.StartActivityForResult() - ) { - if (it.resultCode != Activity.RESULT_OK) { - GlobalScope.launch(Dispatchers.Main) { - Toast.makeText( - Amethyst.instance, - "Sign request rejected", - Toast.LENGTH_SHORT - ).show() - } - } else { - val decryptedContent = it.data?.getStringExtra("signature") ?: "" - val id = it.data?.getStringExtra("id") ?: "" - if (id.isNotBlank()) { - AmberUtils.cachedDecryptedContent[id] = decryptedContent - AmberUtils.account.live.invalidateData() - } - } - AmberUtils.isActivityRunning = false - ServiceManager.shouldPauseService = true - } - - decryptGossipResultLauncher = activity.registerForActivityResult( - ActivityResultContracts.StartActivityForResult() - ) { - if (it.resultCode != Activity.RESULT_OK) { - GlobalScope.launch(Dispatchers.Main) { - Toast.makeText( - Amethyst.instance, - "Sign request rejected", - Toast.LENGTH_SHORT - ).show() - } - } else { - val decryptedContent = it.data?.getStringExtra("signature") ?: "" - val id = it.data?.getStringExtra("id") ?: "" - if (id.isNotBlank()) { - val event = eventCache.get(id) - if (event != null) { - GlobalScope.launch(Dispatchers.IO) { - AmberUtils.cachedDecryptedContent[event.id] = decryptedContent - consume(event) - } - } - } - } - AmberUtils.isActivityRunning = false - ServiceManager.shouldPauseService = true - } - } -} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt index 4fd53823c..8fa9659b9 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt @@ -23,7 +23,7 @@ import androidx.core.os.LocaleListCompat import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.ServiceManager -import com.vitorpamplona.amethyst.service.IntentUtils +import com.vitorpamplona.amethyst.service.AmberUtils import com.vitorpamplona.amethyst.service.connectivitystatus.ConnectivityStatus import com.vitorpamplona.amethyst.service.notifications.PushNotificationUtils import com.vitorpamplona.amethyst.ui.components.DefaultMutedSetting @@ -52,7 +52,7 @@ import java.nio.charset.StandardCharsets class MainActivity : AppCompatActivity() { @RequiresApi(Build.VERSION_CODES.R) override fun onCreate(savedInstanceState: Bundle?) { - IntentUtils.start(this) + AmberUtils.start(this) super.onCreate(savedInstanceState) 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 b2a9b2ec9..2fdd2f764 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 @@ -40,7 +40,7 @@ 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.AmberUtils import com.vitorpamplona.amethyst.service.NostrChatroomListDataSource import com.vitorpamplona.amethyst.ui.screen.ChatroomListFeedView import com.vitorpamplona.amethyst.ui.screen.FeedViewModel @@ -76,7 +76,7 @@ fun ChatroomListScreen( val gifts = LocalCache.notes.elements().toList().filter { it.event is GiftWrapEvent } gifts.forEach { it.event?.let { - IntentUtils.consume(it as Event) + AmberUtils.consume(it as Event) } } }