From bf5b194aa89dbe2035193dff9e1288c63d58357e Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 27 Jun 2026 15:15:33 +0700 Subject: [PATCH 1/3] extract rumor can be failed --- shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt index d5427a0..63bab7d 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt @@ -57,7 +57,6 @@ import rust.nostr.sdk.extractRelayList import rust.nostr.sdk.initLogger import rust.nostr.sdk.nip17ExtractRelayList import rust.nostr.sdk.nip59MakeGiftWrapAsync -import kotlin.coroutines.cancellation.CancellationException import kotlin.time.Duration object NostrManager { @@ -114,7 +113,7 @@ class Nostr { suspend fun init( dbPath: String, - logLevel: LogLevel = LogLevel.WARN + logLevel: LogLevel = LogLevel.INFO ) { try { if (isInitialized.value) return @@ -286,12 +285,14 @@ class Nostr { launch(Dispatchers.Default) { for (event in giftWrapQueue) { - val rumor = extractRumor(event) ?: continue + val rumor = extractRumor(event) processedCount++ // Trigger new message notification - if (rumor.createdAt().asSecs() >= now.asSecs()) { - onNewMessage(rumor) + if (rumor != null) { + if (rumor.createdAt().asSecs() >= now.asSecs()) { + onNewMessage(rumor) + } } // Update sync state @@ -485,8 +486,6 @@ class Nostr { setCachedRumor(event.id(), unsignedEvent) return unsignedEvent - } catch (e: CancellationException) { - throw e } catch (e: Throwable) { println("Failed to unwrap gift ${event.id().toHex()}: ${e.message}") return null -- 2.49.1 From b2d99157eaff535e05abe581a8aad5ab0527c149 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sun, 28 Jun 2026 07:40:02 +0700 Subject: [PATCH 2/3] update nostr sdk --- composeApp/build.gradle.kts | 2 +- shared/build.gradle.kts | 2 +- shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index b6f2e0e..f6be6ff 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -24,7 +24,7 @@ kotlin { implementation(libs.jetbrains.navigation3.ui) implementation(libs.jetbrains.lifecycle.viewmodelNavigation3) implementation(libs.androidx.core.splashscreen) - implementation("su.reya:nostr-sdk-kmp:0.3.1") + implementation("su.reya:nostr-sdk-kmp:0.3.2") implementation("io.coil-kt.coil3:coil-compose:3.4.0") implementation("io.coil-kt.coil3:coil-network-okhttp:3.4.0") implementation("io.github.kalinjul.easyqrscan:scanner:0.7.0") diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 89d957d..b9d19bd 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -33,7 +33,7 @@ kotlin { implementation(libs.androidx.lifecycle.runtimeCompose) implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.8.0") - implementation("su.reya:nostr-sdk-kmp:0.3.1") + implementation("su.reya:nostr-sdk-kmp:0.3.2") implementation("com.squareup.okio:okio:3.16.2") } androidMain.dependencies { diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt index 63bab7d..fbd1b8a 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt @@ -113,7 +113,7 @@ class Nostr { suspend fun init( dbPath: String, - logLevel: LogLevel = LogLevel.INFO + logLevel: LogLevel = LogLevel.DEBUG ) { try { if (isInitialized.value) return @@ -391,7 +391,7 @@ class Nostr { private suspend fun getMutualContacts(pubkeys: List) { try { val kind = Kind.fromStd(KindStandard.CONTACT_LIST) - val filter = Filter().kind(kind).authors(pubkeys).limit(200u) + val filter = Filter().kind(kind).authors(pubkeys).limit(pubkeys.size.toULong()) val opts = SubscribeAutoCloseOptions().exitPolicy(ReqExitPolicy.ExitOnEose) val target = mutableMapOf>() -- 2.49.1 From 4f736e741551da84e296658701d4cd368f643229 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sun, 28 Jun 2026 08:27:41 +0700 Subject: [PATCH 3/3] update bootstrap relays --- .../commonMain/kotlin/su/reya/coop/Nostr.kt | 22 +++++++++---------- .../kotlin/su/reya/coop/NostrViewModel.kt | 8 +++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt index fbd1b8a..f6b81ee 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt @@ -64,7 +64,8 @@ object NostrManager { val BOOTSTRAP_RELAYS = listOf( "wss://relay.primal.net", - "wss://purplepag.es" + "wss://relay.ditto.pub", + "wss://user.kindpag.es", ) val INDEXER_RELAY = listOf( @@ -113,7 +114,7 @@ class Nostr { suspend fun init( dbPath: String, - logLevel: LogLevel = LogLevel.DEBUG + logLevel: LogLevel = LogLevel.WARN ) { try { if (isInitialized.value) return @@ -646,7 +647,7 @@ class Nostr { suspend fun fetchMetadataBatch(keys: List) { try { - val limit = keys.size.toULong() * 4u + val limit = keys.size.toULong() * 2u val opts = SubscribeAutoCloseOptions().exitPolicy(ReqExitPolicy.ExitOnEose) // Construct a filter for metadata events @@ -655,16 +656,13 @@ class Nostr { .authors(keys) .limit(limit) - // Construct a target that includes all filters - val target = - ReqTarget.manual( - mapOf( - RelayUrl.parse("wss://purplepag.es") to listOf(filter), - RelayUrl.parse("wss://relay.primal.net") to listOf(filter), - ) - ) + // Construct request target + val target = mutableMapOf>() + NostrManager.BOOTSTRAP_RELAYS.forEach { relay -> + target[RelayUrl.parse(relay)] = listOf(filter) + } - client?.subscribe(target = target, closeOn = opts) + client?.subscribe(target = ReqTarget.manual(target), closeOn = opts) } catch (e: Exception) { throw IllegalStateException("Failed to fetch metadata batch: ${e.message}", e) } diff --git a/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt b/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt index ff6e71c..fee7464 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt @@ -82,7 +82,7 @@ class NostrViewModel( val errorEvents = _errorEvents.receiveAsFlow() private val _metadataStore = mutableMapOf>() - + private val metadataRequestChannel = Channel(Channel.UNLIMITED) private val seenPublicKeys = mutableSetOf() @@ -213,16 +213,20 @@ class NostrViewModel( val timeout = 500L // 500ms timeout for batching while (true) { + // Get the first pubkey val firstKey = metadataRequestChannel.receive() batch.add(firstKey) + + // Get current time val lastFlushTime = Clock.System.now().toEpochMilliseconds() while (batch.isNotEmpty()) { + // Get the next pubkey val nextKey = withTimeoutOrNull(timeout.milliseconds) { metadataRequestChannel.receive() } - // Only add the key if it's not null + // Only add the pubkey if it's not null if (nextKey != null) batch.add(nextKey) // Get current time -- 2.49.1