Compare commits
3 Commits
master
...
fix-extern
| Author | SHA1 | Date | |
|---|---|---|---|
| 5142e2cbc1 | |||
| 4449242581 | |||
| 8e7a1e6d7c |
@@ -61,7 +61,7 @@ class AndroidExternalSigner(
|
|||||||
): String? {
|
): String? {
|
||||||
// Try Content Resolver first
|
// Try Content Resolver first
|
||||||
queryContentResolver(type, payload, pubkey, currentUser)?.let {
|
queryContentResolver(type, payload, pubkey, currentUser)?.let {
|
||||||
return it.result
|
return if (resultKey == "event") it.event else it.result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fall back to Intent
|
// Fall back to Intent
|
||||||
|
|||||||
@@ -4,23 +4,30 @@ import android.content.Intent
|
|||||||
import androidx.activity.result.ActivityResult
|
import androidx.activity.result.ActivityResult
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class ExternalSignerLauncher {
|
class ExternalSignerLauncher {
|
||||||
private var launcher: ActivityResultLauncher<Intent>? = null
|
private var launcher: ActivityResultLauncher<Intent>? = null
|
||||||
private var pendingResult: CompletableDeferred<ActivityResult>? = null
|
private var pendingResult: CompletableDeferred<ActivityResult>? = null
|
||||||
|
private val mutex = Mutex()
|
||||||
|
|
||||||
fun register(launcher: ActivityResultLauncher<Intent>) {
|
fun register(launcher: ActivityResultLauncher<Intent>) {
|
||||||
this.launcher = launcher
|
this.launcher = launcher
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun launch(intent: Intent): ActivityResult {
|
suspend fun launch(intent: Intent): ActivityResult = mutex.withLock {
|
||||||
val deferred = CompletableDeferred<ActivityResult>()
|
withContext(Dispatchers.Main) {
|
||||||
pendingResult = deferred
|
val deferred = CompletableDeferred<ActivityResult>()
|
||||||
launcher?.launch(intent)
|
pendingResult = deferred
|
||||||
?: throw IllegalStateException("ExternalSignerLauncher not registered")
|
launcher?.launch(intent) ?: throw IllegalStateException("Signer not registered")
|
||||||
return deferred.await()
|
deferred.await()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun onResult(result: ActivityResult) {
|
fun onResult(result: ActivityResult) {
|
||||||
pendingResult?.complete(result)
|
pendingResult?.complete(result)
|
||||||
pendingResult = null
|
pendingResult = null
|
||||||
|
|||||||
@@ -110,17 +110,17 @@ fun ChatScreen(id: Long) {
|
|||||||
// Start loading spinner
|
// Start loading spinner
|
||||||
loading = true
|
loading = true
|
||||||
|
|
||||||
// Get msg relays for each member
|
|
||||||
viewModel.chatRoomConnect(id)
|
|
||||||
|
|
||||||
// Get messages
|
// Get messages
|
||||||
val initialMessages = viewModel.getChatRoomMessages(id)
|
val initialMessages = viewModel.getChatRoomMessages(id)
|
||||||
messages.clear()
|
messages.clear()
|
||||||
messages.addAll(initialMessages)
|
messages.addAll(initialMessages)
|
||||||
|
|
||||||
// Stop loading spinner
|
// Stop loading spinner
|
||||||
loading = false
|
loading = false
|
||||||
|
|
||||||
|
// Get msg relays for each member
|
||||||
|
viewModel.chatRoomConnect(id)
|
||||||
|
|
||||||
// Handle new messages
|
// Handle new messages
|
||||||
viewModel.newEvents.collect { event ->
|
viewModel.newEvents.collect { event ->
|
||||||
if (event.roomId() == id) {
|
if (event.roomId() == id) {
|
||||||
|
|||||||
@@ -379,9 +379,6 @@ class Nostr {
|
|||||||
|
|
||||||
private suspend fun setCachedRumor(giftId: EventId, rumor: UnsignedEvent) {
|
private suspend fun setCachedRumor(giftId: EventId, rumor: UnsignedEvent) {
|
||||||
try {
|
try {
|
||||||
val currentUser =
|
|
||||||
signer.currentUser ?: throw IllegalStateException("User not signed in")
|
|
||||||
|
|
||||||
// Construct the room id
|
// Construct the room id
|
||||||
val roomId = rumor.roomId()
|
val roomId = rumor.roomId()
|
||||||
|
|
||||||
@@ -389,17 +386,17 @@ class Nostr {
|
|||||||
val tags = listOf(
|
val tags = listOf(
|
||||||
Tag.identifier(giftId.toHex()),
|
Tag.identifier(giftId.toHex()),
|
||||||
Tag.event(rumor.id()!!),
|
Tag.event(rumor.id()!!),
|
||||||
Tag.custom("a", listOf(roomId.toString())),
|
Tag.custom("r", listOf(roomId.toString())),
|
||||||
Tag.custom("k", listOf("14"))
|
Tag.custom("k", listOf("14"))
|
||||||
)
|
)
|
||||||
|
|
||||||
// Set event kind
|
// Set event kind
|
||||||
val kind = Kind.fromStd(KindStandard.APPLICATION_SPECIFIC_DATA);
|
val kind = Kind.fromStd(KindStandard.APPLICATION_SPECIFIC_DATA);
|
||||||
|
|
||||||
|
// Construct event
|
||||||
val event = EventBuilder(kind, rumor.asJson())
|
val event = EventBuilder(kind, rumor.asJson())
|
||||||
.tags(tags)
|
.tags(tags)
|
||||||
.finalizeUnsigned(currentUser)
|
.finalizeAsync(Keys.generate())
|
||||||
.signAsync(Keys.generate())
|
|
||||||
|
|
||||||
client?.database()?.saveEvent(event)
|
client?.database()?.saveEvent(event)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -412,23 +409,16 @@ class Nostr {
|
|||||||
val cachedRumor = getCachedRumor(event.id())
|
val cachedRumor = getCachedRumor(event.id())
|
||||||
if (cachedRumor != null) return cachedRumor
|
if (cachedRumor != null) return cachedRumor
|
||||||
|
|
||||||
// Get all signers
|
|
||||||
val signers = listOfNotNull(signer, deviceSigner)
|
|
||||||
if (signers.isEmpty()) return null
|
|
||||||
|
|
||||||
// Try to unwrap the gift with each signer
|
// Try to unwrap the gift with each signer
|
||||||
for (signer in signers) {
|
try {
|
||||||
try {
|
val gift = UnwrappedGift.fromGiftWrapAsync(signer = signer, giftWrap = event)
|
||||||
val gift = UnwrappedGift.fromGiftWrapAsync(signer = signer, giftWrap = event)
|
val rumor = gift.rumor()
|
||||||
val rumor = gift.rumor()
|
// Save the rumor to the database
|
||||||
// Save the rumor to the database
|
setCachedRumor(event.id(), rumor)
|
||||||
setCachedRumor(event.id(), rumor)
|
// Return the rumor
|
||||||
// Return the rumor
|
return rumor
|
||||||
return rumor
|
} catch (e: Exception) {
|
||||||
} catch (e: Exception) {
|
println("Failed to unwrap gift: ${e.message}")
|
||||||
println("Failed to unwrap gift: ${e.message}")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
|
|||||||
Reference in New Issue
Block a user