feat: Relay Management (#19)
Reviewed-on: #19
This commit was merged in pull request #19.
This commit is contained in:
@@ -190,11 +190,6 @@ class Nostr {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun exit() {
|
||||
signer.switch(Keys.generate())
|
||||
deviceSigner = null
|
||||
}
|
||||
|
||||
suspend fun setSigner(new: AsyncNostrSigner) {
|
||||
try {
|
||||
signer.switch(new)
|
||||
@@ -453,9 +448,10 @@ class Nostr {
|
||||
client?.addRelay(
|
||||
url = relay,
|
||||
capabilities =
|
||||
if (metadata == RelayMetadata.READ) RelayCapabilities.read()
|
||||
else if (metadata == RelayMetadata.WRITE) RelayCapabilities.write()
|
||||
else RelayCapabilities.none()
|
||||
when (metadata) {
|
||||
RelayMetadata.READ -> RelayCapabilities.read()
|
||||
RelayMetadata.WRITE -> RelayCapabilities.write()
|
||||
}
|
||||
)
|
||||
client?.connectRelay(relay)
|
||||
}
|
||||
@@ -466,7 +462,7 @@ class Nostr {
|
||||
suspend fun getDefaultMsgRelayList(): List<RelayUrl> {
|
||||
// Construct a list of messaging relays
|
||||
val msgRelayList = listOf(
|
||||
RelayUrl.parse("wss://relay.0xchat.com"),
|
||||
RelayUrl.parse("wss://auth.nostr1.com"),
|
||||
RelayUrl.parse("wss://nip17.com"),
|
||||
)
|
||||
|
||||
@@ -649,6 +645,19 @@ class Nostr {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun fetchMsgRelays(publicKey: PublicKey): List<RelayUrl> {
|
||||
try {
|
||||
val kind = Kind.fromStd(KindStandard.INBOX_RELAYS)
|
||||
val filter = Filter().kind(kind).author(publicKey).limit(1u)
|
||||
val target = ReqTarget.auto(listOf(filter))
|
||||
val events = client?.fetchEvents(target, timeout = Duration.parse("3s"))
|
||||
|
||||
return nip17ExtractRelayList(events?.toVec()?.firstOrNull() ?: return emptyList())
|
||||
} catch (e: Exception) {
|
||||
throw IllegalStateException("Failed to fetch msg relays: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getRelayList(publicKey: PublicKey): Map<RelayUrl, RelayMetadata?> {
|
||||
try {
|
||||
val kind = Kind.fromStd(KindStandard.RELAY_LIST)
|
||||
@@ -661,6 +670,20 @@ class Nostr {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setRelaylist(relays: Map<RelayUrl, RelayMetadata?>) {
|
||||
try {
|
||||
val event = EventBuilder.relayList(relays).finalizeAsync(signer)
|
||||
|
||||
client?.sendEvent(
|
||||
event = event,
|
||||
target = SendEventTarget.broadcast(),
|
||||
ackPolicy = AckPolicy.none(),
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
throw IllegalStateException("Failed to set msg relays: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getChatRooms(): Set<Room>? {
|
||||
try {
|
||||
val userPubkey = signer.currentUser ?: throw IllegalStateException("User not signed in")
|
||||
@@ -721,33 +744,25 @@ class Nostr {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun chatRoomConnect(members: List<PublicKey>): Map<PublicKey, List<RelayUrl>> {
|
||||
suspend fun chatRoomConnect(members: List<PublicKey>) {
|
||||
try {
|
||||
val results = mutableMapOf<PublicKey, MutableList<RelayUrl>>()
|
||||
|
||||
members.forEach { member ->
|
||||
results[member] = mutableListOf<RelayUrl>()
|
||||
val kind = Kind.fromStd(KindStandard.INBOX_RELAYS)
|
||||
val filter = Filter().kind(kind).author(member).limit(1u)
|
||||
|
||||
val stream = client?.streamEvents(
|
||||
target = ReqTarget.auto(listOf(filter)),
|
||||
id = "room-${member.toBech32().substring(0, 10)}",
|
||||
id = null,
|
||||
timeout = Duration.parse("3s"),
|
||||
policy = ReqExitPolicy.ExitOnEose
|
||||
)
|
||||
|
||||
stream?.next()?.let { res ->
|
||||
if (res.event != null) {
|
||||
// Connect to the msg relays
|
||||
connectMsgRelays(res.event!!)
|
||||
// Mark the member as connected
|
||||
results[member]?.add(res.relayUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
} catch (e: Exception) {
|
||||
throw IllegalStateException("Failed to fetch relays: ${e.message}", e)
|
||||
}
|
||||
@@ -757,10 +772,8 @@ class Nostr {
|
||||
try {
|
||||
val urls = nip17ExtractRelayList(event);
|
||||
for (url in urls) {
|
||||
if (client?.relay(url) == null) {
|
||||
client?.addRelay(url)
|
||||
client?.connectRelay(url)
|
||||
}
|
||||
client?.addRelay(url, RelayCapabilities.gossip())
|
||||
client?.connectRelay(url)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
throw IllegalStateException("Failed to connect to relays: ${e.message}", e)
|
||||
|
||||
@@ -146,20 +146,6 @@ class NostrViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
private fun processIncomingEvent(event: UnsignedEvent) {
|
||||
val roomId = event.roomId()
|
||||
val existingRoom = _chatRooms.value.firstOrNull { it.id == roomId }
|
||||
|
||||
if (existingRoom == null) {
|
||||
nostr.signer.currentUser?.let { user ->
|
||||
val newRoom = Room.new(event, user)
|
||||
_chatRooms.update { (it + newRoom).sortedDescending().toSet() }
|
||||
}
|
||||
} else {
|
||||
updateRoomList(roomId, event)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun runObserver() = coroutineScope {
|
||||
// Observe new messages
|
||||
launch {
|
||||
@@ -298,13 +284,11 @@ class NostrViewModel(
|
||||
nostr.getUserMetadata()
|
||||
|
||||
// Small delay to ensure all relays are connected
|
||||
delay(3000.milliseconds)
|
||||
delay(2.seconds)
|
||||
|
||||
// Check if the relay list is empty
|
||||
val relays = nostr.getMsgRelays(pubkey)
|
||||
if (relays.isEmpty()) {
|
||||
_isRelayListEmpty.value = true
|
||||
}
|
||||
if (relays.isEmpty()) _isRelayListEmpty.value = true
|
||||
|
||||
break
|
||||
}
|
||||
@@ -540,6 +524,11 @@ class NostrViewModel(
|
||||
return externalSignerHandler?.isAvailable() == true
|
||||
}
|
||||
|
||||
suspend fun refetchMsgRelays(pubkey: PublicKey) {
|
||||
val relays = nostr.fetchMsgRelays(pubkey)
|
||||
if (relays.isNotEmpty()) dismissRelayWarning()
|
||||
}
|
||||
|
||||
suspend fun useDefaultMsgRelayList() {
|
||||
try {
|
||||
val defaultRelays = nostr.getDefaultMsgRelayList()
|
||||
@@ -558,6 +547,42 @@ class NostrViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addInboxRelay(relay: String) {
|
||||
try {
|
||||
val relayUrl = RelayUrl.parse(relay)
|
||||
val relays = currentUserRelayList().toMutableMap()
|
||||
relays[relayUrl] = RelayMetadata.WRITE
|
||||
|
||||
nostr.setRelaylist(relays)
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addOutboxRelay(relay: String) {
|
||||
try {
|
||||
val relayUrl = RelayUrl.parse(relay)
|
||||
val relays = currentUserRelayList().toMutableMap()
|
||||
relays[relayUrl] = RelayMetadata.READ
|
||||
|
||||
nostr.setRelaylist(relays)
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun removeRelay(relay: String) {
|
||||
try {
|
||||
val relayUrl = RelayUrl.parse(relay)
|
||||
val relays = currentUserRelayList().toMutableMap()
|
||||
relays.remove(relayUrl)
|
||||
|
||||
nostr.setRelaylist(relays)
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun currentUserMsgRelayList(): List<RelayUrl> {
|
||||
try {
|
||||
return nostr.getMsgRelays(nostr.signer.currentUser!!)
|
||||
@@ -567,6 +592,30 @@ class NostrViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addMsgRelay(relay: String) {
|
||||
try {
|
||||
val relayUrl = RelayUrl.parse(relay)
|
||||
val relays = currentUserMsgRelayList().toMutableSet()
|
||||
relays.add(relayUrl)
|
||||
|
||||
nostr.setMsgRelays(relays.toList())
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun removeMsgRelay(relay: String) {
|
||||
try {
|
||||
val relayUrl = RelayUrl.parse(relay)
|
||||
val relays = currentUserMsgRelayList().toMutableSet()
|
||||
relays.remove(relayUrl)
|
||||
|
||||
nostr.setMsgRelays(relays.toList())
|
||||
} 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")
|
||||
@@ -644,20 +693,16 @@ class NostrViewModel(
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
suspend fun chatRoomConnect(roomId: Long): Map<PublicKey, List<RelayUrl>> {
|
||||
try {
|
||||
val room = getChatRoom(roomId) ?: throw IllegalArgumentException("Room not found")
|
||||
val members = room.members
|
||||
fun chatRoomConnect(roomId: Long) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val room = getChatRoom(roomId) ?: throw IllegalArgumentException("Room not found")
|
||||
val members = room.members
|
||||
|
||||
return runCatching {
|
||||
nostr.chatRoomConnect(members.toList())
|
||||
}.getOrElse { e ->
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
members.associateWith { emptyList() }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
return emptyMap()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user