fix: crash when getting all cached metadata (#12)
Reviewed-on: #12
This commit was merged in pull request #12.
This commit is contained in:
@@ -500,18 +500,23 @@ class Nostr {
|
|||||||
|
|
||||||
suspend fun getAllCacheMetadata(): Map<PublicKey, Metadata> {
|
suspend fun getAllCacheMetadata(): Map<PublicKey, Metadata> {
|
||||||
try {
|
try {
|
||||||
val filter = Filter().kind(Kind.fromStd(KindStandard.METADATA)).limit(200u)
|
val filter = Filter().kind(Kind.fromStd(KindStandard.METADATA)).limit(100u)
|
||||||
val events = client?.database()?.query(filter)
|
val events = client?.database()?.query(filter)
|
||||||
val results = mutableMapOf<PublicKey, Metadata>()
|
val results = mutableMapOf<PublicKey, Metadata>()
|
||||||
|
|
||||||
events?.toVec()?.forEach { event ->
|
events?.toVec()?.forEach { event ->
|
||||||
val metadata = Metadata.fromJson(event.content())
|
try {
|
||||||
results[event.author()] = metadata
|
val metadata = Metadata.fromJson(event.content())
|
||||||
|
results[event.author()] = metadata
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Failed to parse metadata: $e")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return results
|
return results
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
throw IllegalStateException("Failed to get cache metadata: ${e.message}", e)
|
println("Failed to get all cache metadata: ${e.message}")
|
||||||
|
return emptyMap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import rust.nostr.sdk.UnsignedEvent
|
|||||||
import su.reya.coop.blossom.BlossomClient
|
import su.reya.coop.blossom.BlossomClient
|
||||||
import su.reya.coop.storage.SecretStorage
|
import su.reya.coop.storage.SecretStorage
|
||||||
import kotlin.time.Clock
|
import kotlin.time.Clock
|
||||||
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
class NostrViewModel(
|
class NostrViewModel(
|
||||||
@@ -177,7 +178,7 @@ class NostrViewModel(
|
|||||||
val lastFlushTime = Clock.System.now().toEpochMilliseconds()
|
val lastFlushTime = Clock.System.now().toEpochMilliseconds()
|
||||||
|
|
||||||
while (batch.isNotEmpty()) {
|
while (batch.isNotEmpty()) {
|
||||||
val nextKey = withTimeoutOrNull(timeout) {
|
val nextKey = withTimeoutOrNull(timeout.milliseconds) {
|
||||||
metadataRequestChannel.receive()
|
metadataRequestChannel.receive()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,7 +256,7 @@ class NostrViewModel(
|
|||||||
nostr.getUserMetadata()
|
nostr.getUserMetadata()
|
||||||
|
|
||||||
// Small delay to ensure all relays are connected
|
// Small delay to ensure all relays are connected
|
||||||
delay(3000)
|
delay(3000.milliseconds)
|
||||||
|
|
||||||
// Check if the relay list is empty
|
// Check if the relay list is empty
|
||||||
val relays = nostr.getMsgRelays(pubkey)
|
val relays = nostr.getMsgRelays(pubkey)
|
||||||
@@ -266,7 +267,7 @@ class NostrViewModel(
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(500)
|
delay(500.milliseconds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user