deleted intentutils

This commit is contained in:
greenart7c3
2023-09-13 18:06:50 -03:00
parent 52f17e9f46
commit 33f7c5c82e
4 changed files with 174 additions and 183 deletions

View File

@@ -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<HexKey, String>()
lateinit var account: Account
lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
lateinit var decryptGossipResultLauncher: ActivityResultLauncher<Intent>
lateinit var blockListResultLauncher: ActivityResultLauncher<Intent>
lateinit var signEventResultLauncher: ActivityResultLauncher<Intent>
val eventCache = LruCache<String, Event>(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
)

View File

@@ -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<Intent>
lateinit var decryptGossipResultLauncher: ActivityResultLauncher<Intent>
lateinit var blockListResultLauncher: ActivityResultLauncher<Intent>
lateinit var signEventResultLauncher: ActivityResultLauncher<Intent>
val eventCache = LruCache<String, Event>(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
}
}
}

View File

@@ -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)

View File

@@ -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)
}
}
}