update
This commit is contained in:
@@ -677,6 +677,21 @@ class Nostr {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setContactList(contacts: List<PublicKey>) {
|
||||
try {
|
||||
val contacts = contacts.map { Contact(it) }
|
||||
val event = EventBuilder.contactList(contacts).finalizeAsync(signer)
|
||||
|
||||
client?.sendEvent(
|
||||
event = event,
|
||||
target = SendEventTarget.broadcast(),
|
||||
ackPolicy = AckPolicy.none(),
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
throw IllegalStateException("Failed to set contact list: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getChatRooms(): Set<Room>? {
|
||||
try {
|
||||
val userPubkey =
|
||||
|
||||
@@ -616,6 +616,54 @@ class NostrViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun newContact(publicKey: PublicKey) {
|
||||
if (publicKey in contactList.value) return
|
||||
|
||||
try {
|
||||
val updated = contactList.value + publicKey
|
||||
// Publish new event
|
||||
nostr.setContactList(updated.toList())
|
||||
// Optimistic local update
|
||||
_contactList.update { it + publicKey }
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addContact(address: String): Boolean {
|
||||
val pubkey = try {
|
||||
if (address.contains("@")) {
|
||||
nostr.searchByAddress(address)
|
||||
} else {
|
||||
PublicKey.parse(address)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
showError("Invalid contact address: ${e.message}")
|
||||
return false
|
||||
}
|
||||
|
||||
return run {
|
||||
newContact(pubkey)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fun removeContact(publicKey: PublicKey) {
|
||||
viewModelScope.launch {
|
||||
if (publicKey !in contactList.value) return@launch
|
||||
|
||||
try {
|
||||
val updated = contactList.value - publicKey
|
||||
// Publish new event
|
||||
nostr.setContactList(updated.toList())
|
||||
// Optimistic local update
|
||||
_contactList.update { it - publicKey }
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createChatRoom(to: List<PublicKey>): Long {
|
||||
try {
|
||||
if (nostr.signer.currentUser == null) throw IllegalStateException("User not signed in")
|
||||
|
||||
Reference in New Issue
Block a user