chore: refactor the internal structure #33
@@ -79,6 +79,8 @@ class Nostr {
|
|||||||
// Initialize configurations for nostr client
|
// Initialize configurations for nostr client
|
||||||
val lmdb = NostrDatabase.lmdb(dbPath)
|
val lmdb = NostrDatabase.lmdb(dbPath)
|
||||||
val gossip = NostrGossip.inMemory()
|
val gossip = NostrGossip.inMemory()
|
||||||
|
|
||||||
|
// Initialize the authenticator
|
||||||
val authenticator = SignerAuthenticator(signer)
|
val authenticator = SignerAuthenticator(signer)
|
||||||
val idleTimeout = Duration.parse("5m")
|
val idleTimeout = Duration.parse("5m")
|
||||||
|
|
||||||
@@ -208,7 +210,7 @@ class Nostr {
|
|||||||
if (isSignedByUser(event = event)) {
|
if (isSignedByUser(event = event)) {
|
||||||
val pubkeys = event.tags().publicKeys()
|
val pubkeys = event.tags().publicKeys()
|
||||||
// Get mutual contacts
|
// Get mutual contacts
|
||||||
profiles.getMutualContacts(pubkeys)
|
profiles.syncMutualContacts(pubkeys)
|
||||||
// Emit contact list update
|
// Emit contact list update
|
||||||
onContactListUpdate(pubkeys)
|
onContactListUpdate(pubkeys)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,22 +74,13 @@ class ProfileManager(private val nostr: Nostr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getMutualContacts(pubkeys: List<PublicKey>) {
|
suspend fun syncMutualContacts(pubkeys: List<PublicKey>) {
|
||||||
try {
|
try {
|
||||||
val kind = Kind.fromStd(KindStandard.CONTACT_LIST)
|
val kind = Kind.fromStd(KindStandard.CONTACT_LIST)
|
||||||
val filter = Filter().kind(kind).authors(pubkeys).limit(pubkeys.size.toULong())
|
val filter = Filter().kind(kind).authors(pubkeys).limit(pubkeys.size.toULong())
|
||||||
val opts = SubscribeAutoCloseOptions().exitPolicy(ReqExitPolicy.ExitOnEose)
|
val relays = NostrManager.BOOTSTRAP_RELAYS.map { RelayUrl.parse(it) }
|
||||||
|
|
||||||
val target = mutableMapOf<RelayUrl, List<Filter>>()
|
client?.sync(filter, relays)
|
||||||
NostrManager.BOOTSTRAP_RELAYS.forEach { relay ->
|
|
||||||
target[RelayUrl.parse(relay)] = listOf(filter)
|
|
||||||
}
|
|
||||||
|
|
||||||
client?.subscribe(
|
|
||||||
target = ReqTarget.manual(target),
|
|
||||||
id = "mutual-contacts",
|
|
||||||
closeOn = opts,
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
throw IllegalStateException("Failed to fetch mutual contacts: ${e.message}", e)
|
throw IllegalStateException("Failed to fetch mutual contacts: ${e.message}", e)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,8 +134,9 @@ class RelayManager(private val nostr: Nostr) {
|
|||||||
val kind = Kind.fromStd(KindStandard.INBOX_RELAYS)
|
val kind = Kind.fromStd(KindStandard.INBOX_RELAYS)
|
||||||
val filter = Filter().kind(kind).author(publicKey).limit(1u)
|
val filter = Filter().kind(kind).author(publicKey).limit(1u)
|
||||||
val events = client?.database()?.query(filter)
|
val events = client?.database()?.query(filter)
|
||||||
|
val event = events?.first() ?: return emptyList()
|
||||||
|
|
||||||
return nip17ExtractRelayList(events?.toVec()?.firstOrNull() ?: return emptyList())
|
return nip17ExtractRelayList(event)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
throw IllegalStateException("Failed to get msg relays: ${e.message}", e)
|
throw IllegalStateException("Failed to get msg relays: ${e.message}", e)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import kotlinx.coroutines.flow.SharingStarted
|
|||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
|
import kotlinx.coroutines.flow.filterNotNull
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.flatMapLatest
|
import kotlinx.coroutines.flow.flatMapLatest
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.flowOf
|
import kotlinx.coroutines.flow.flowOf
|
||||||
@@ -159,8 +161,7 @@ class NostrViewModel(private val nostr: Nostr) : BaseViewModel() {
|
|||||||
// Wait until the client is ready
|
// Wait until the client is ready
|
||||||
nostr.waitUntilInitialized()
|
nostr.waitUntilInitialized()
|
||||||
|
|
||||||
val results = nostr.profiles.getAllCacheMetadata()
|
nostr.profiles.getAllCacheMetadata().forEach { (pubkey, metadata) ->
|
||||||
results.forEach { (pubkey, metadata) ->
|
|
||||||
// Update the metadata state
|
// Update the metadata state
|
||||||
updateMetadata(pubkey, Profile(pubkey, metadata))
|
updateMetadata(pubkey, Profile(pubkey, metadata))
|
||||||
// Update seenPublicKeys to avoid duplicate requests
|
// Update seenPublicKeys to avoid duplicate requests
|
||||||
@@ -171,25 +172,21 @@ class NostrViewModel(private val nostr: Nostr) : BaseViewModel() {
|
|||||||
|
|
||||||
private fun observeSignerAndCheckRelays() {
|
private fun observeSignerAndCheckRelays() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
while (true) {
|
// Wait until the client is ready
|
||||||
val currentUser = nostr.signer.getPublicKeyAsync()
|
nostr.waitUntilInitialized()
|
||||||
|
|
||||||
if (currentUser != null) {
|
// Wait until a signer is explicitly set (which updates publicKeyFlow)
|
||||||
// Get all metadata for the current user
|
val currentUser = nostr.signer.publicKeyFlow.filterNotNull().first()
|
||||||
nostr.profiles.getUserMetadata()
|
|
||||||
|
|
||||||
// Small delay to ensure all relays are connected
|
// Get all metadata for the current user
|
||||||
delay(2.seconds)
|
nostr.profiles.getUserMetadata()
|
||||||
|
|
||||||
// Check if the relay list is empty
|
// Small delay to ensure all relays are connected
|
||||||
val relays = nostr.relays.getMsgRelays(currentUser)
|
delay(2.seconds)
|
||||||
if (relays.isEmpty()) _appState.update { it.copy(isRelayListEmpty = true) }
|
|
||||||
|
|
||||||
break
|
// Check if the relay list is empty
|
||||||
}
|
val relays = nostr.relays.getMsgRelays(currentUser)
|
||||||
|
if (relays.isEmpty()) _appState.update { it.copy(isRelayListEmpty = true) }
|
||||||
delay(500.milliseconds)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user