Preparing to change the relay settings screen

This commit is contained in:
Vitor Pamplona 2024-05-01 09:15:37 -04:00
parent 5af807cf5b
commit baf51f527b
5 changed files with 120 additions and 9 deletions

View File

@ -47,6 +47,7 @@ import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip47WalletConnect
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.events.AdvertisedRelayListEvent
import com.vitorpamplona.quartz.events.BookmarkListEvent
import com.vitorpamplona.quartz.events.ChannelCreateEvent
import com.vitorpamplona.quartz.events.ChannelMessageEvent
@ -56,6 +57,7 @@ import com.vitorpamplona.quartz.events.ClassifiedsEvent
import com.vitorpamplona.quartz.events.Contact
import com.vitorpamplona.quartz.events.ContactListEvent
import com.vitorpamplona.quartz.events.DeletionEvent
import com.vitorpamplona.quartz.events.DirectMessageRelayListEvent
import com.vitorpamplona.quartz.events.DraftEvent
import com.vitorpamplona.quartz.events.EmojiPackEvent
import com.vitorpamplona.quartz.events.EmojiPackSelectionEvent
@ -515,6 +517,30 @@ class Account(
}
}
fun sendNip65RelayList(relays: List<AdvertisedRelayListEvent.AdvertisedRelayInfo>) {
if (!isWriteable()) return
AdvertisedRelayListEvent.create(
relays,
signer,
) {
Client.send(it)
LocalCache.justConsume(it, null)
}
}
fun sendNip17RelayList(relays: List<String>) {
if (!isWriteable()) return
DirectMessageRelayListEvent.create(
relays,
signer,
) {
Client.send(it)
LocalCache.justConsume(it, null)
}
}
fun sendNewUserMetadata(
name: String? = null,
picture: String? = null,
@ -2514,12 +2540,34 @@ class Account(
.toSet()
}
fun saveRelayList(value: List<RelaySetupInfo>) {
fun saveRelayList(
value: List<RelaySetupInfo>,
nip65List: List<RelaySetupInfo>,
nip17List: List<RelaySetupInfo>,
) {
try {
localRelays = value.toSet()
return sendNewRelayList(
localRelays = value.toSet() + nip65List.toSet() + nip17List.toSet()
sendNewRelayList(
value.associate { it.url to ContactListEvent.ReadWrite(it.read, it.write) },
)
sendNip65RelayList(
nip65List.map {
val type =
if (it.read && it.write) {
AdvertisedRelayListEvent.AdvertisedRelayType.BOTH
} else if (it.read) {
AdvertisedRelayListEvent.AdvertisedRelayType.READ
} else {
AdvertisedRelayListEvent.AdvertisedRelayType.WRITE
}
AdvertisedRelayListEvent.AdvertisedRelayInfo(it.url, type)
},
)
sendNip17RelayList(
nip17List.map { it.url },
)
} finally {
saveable.invalidateData()
}

View File

@ -123,7 +123,7 @@ fun NewRelayListView(
nav: (String) -> Unit,
) {
val postViewModel: NewRelayListViewModel = viewModel()
LaunchedEffect(Unit) { postViewModel.load(accountViewModel.account) }
LaunchedEffect(accountViewModel) { postViewModel.load(accountViewModel.account) }
NewRelayListDialog(postViewModel, relayToAdd, onClose, accountViewModel, nav)
}

View File

@ -23,12 +23,15 @@ package com.vitorpamplona.amethyst.ui.actions
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.service.Nip11CachedRetriever
import com.vitorpamplona.amethyst.service.relays.Constants
import com.vitorpamplona.amethyst.service.relays.FeedType
import com.vitorpamplona.amethyst.service.relays.RelayPool
import com.vitorpamplona.quartz.events.AdvertisedRelayListEvent
import com.vitorpamplona.quartz.events.ContactListEvent
import com.vitorpamplona.quartz.events.DirectMessageRelayListEvent
import kotlinx.collections.immutable.toImmutableSet
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
@ -42,6 +45,12 @@ class NewRelayListViewModel : ViewModel() {
private val _relays = MutableStateFlow<List<RelaySetupInfo>>(emptyList())
val relays = _relays.asStateFlow()
private val _nip65Relays = MutableStateFlow<List<RelaySetupInfo>>(emptyList())
val nip65Relays = _relays.asStateFlow()
private val _nip17Relays = MutableStateFlow<List<RelaySetupInfo>>(emptyList())
val nip17Relays = _relays.asStateFlow()
fun load(account: Account) {
this.account = account
reset()
@ -49,11 +58,9 @@ class NewRelayListViewModel : ViewModel() {
}
fun create() {
relays.let {
viewModelScope.launch(Dispatchers.IO) {
account.saveRelayList(it.value)
reset()
}
viewModelScope.launch(Dispatchers.IO) {
account.saveRelayList(_relays.value, _nip65Relays.value, _nip17Relays.value)
reset()
}
}
@ -163,6 +170,52 @@ class NewRelayListViewModel : ViewModel() {
_relays.update {
currentKind3List
}
val nip65Relays = LocalCache.getOrCreateAddressableNote(AdvertisedRelayListEvent.createAddressTag(account.userProfile().pubkeyHex)).event as? AdvertisedRelayListEvent
if (nip65Relays != null) {
val currentNip65list =
nip65Relays.relays().map {
val liveRelay = RelayPool.getRelay(it.relayUrl)
RelaySetupInfo(
it.relayUrl,
it.type == AdvertisedRelayListEvent.AdvertisedRelayType.BOTH || it.type == AdvertisedRelayListEvent.AdvertisedRelayType.READ,
it.type == AdvertisedRelayListEvent.AdvertisedRelayType.BOTH || it.type == AdvertisedRelayListEvent.AdvertisedRelayType.WRITE,
liveRelay?.errorCounter ?: 0,
liveRelay?.eventDownloadCounterInBytes ?: 0,
liveRelay?.eventUploadCounterInBytes ?: 0,
liveRelay?.spamCounter ?: 0,
setOf(FeedType.FOLLOWS),
)
}
_nip65Relays.update {
currentNip65list
}
}
val nip17Relays = LocalCache.getOrCreateAddressableNote(DirectMessageRelayListEvent.createAddressTag(account.userProfile().pubkeyHex)).event as? DirectMessageRelayListEvent
if (nip17Relays != null) {
val currentNip17list =
nip17Relays.relays().map {
val liveRelay = RelayPool.getRelay(it)
RelaySetupInfo(
it,
true,
true,
liveRelay?.errorCounter ?: 0,
liveRelay?.eventDownloadCounterInBytes ?: 0,
liveRelay?.eventUploadCounterInBytes ?: 0,
liveRelay?.spamCounter ?: 0,
setOf(FeedType.FOLLOWS),
)
}
_nip17Relays.update {
currentNip17list
}
}
}
fun addRelay(relay: RelaySetupInfo) {

View File

@ -21,6 +21,7 @@
package com.vitorpamplona.quartz.events
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@ -57,6 +58,10 @@ class AdvertisedRelayListEvent(
const val KIND = 10002
const val FIXED_D_TAG = ""
fun createAddressTag(pubKey: HexKey): ATag {
return ATag(KIND, pubKey, FIXED_D_TAG, null)
}
fun create(
list: List<AdvertisedRelayInfo>,
signer: NostrSigner,

View File

@ -21,6 +21,7 @@
package com.vitorpamplona.quartz.events
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@ -50,6 +51,10 @@ class DirectMessageRelayListEvent(
const val KIND = 10050
const val FIXED_D_TAG = ""
fun createAddressTag(pubKey: HexKey): ATag {
return ATag(AdvertisedRelayListEvent.KIND, pubKey, AdvertisedRelayListEvent.FIXED_D_TAG, null)
}
fun create(
relays: List<String>,
signer: NostrSigner,