Fixes relay drops and difficulty in connecting.

Refactors the use of relay objects to avoid creating full state relay connections only once
pull/906/head
Vitor Pamplona 2024-06-07 20:13:15 -04:00
rodzic 723e575b4b
commit 0db6a0d27b
8 zmienionych plików z 57 dodań i 52 usunięć

Wyświetl plik

@ -258,7 +258,7 @@ class Account(
var mappedRelaySet =
baseRelaySet.map {
if (newDMRelaySet.contains(it.url)) {
Relay(it.url, true, true, it.activeTypes + FeedType.PRIVATE_DMS)
RelaySetupInfo(it.url, true, true, it.feedTypes + FeedType.PRIVATE_DMS)
} else {
it
}
@ -266,7 +266,7 @@ class Account(
newDMRelaySet.forEach { newUrl ->
if (mappedRelaySet.none { it.url == newUrl }) {
mappedRelaySet = mappedRelaySet + Relay(newUrl, true, true, setOf(FeedType.PRIVATE_DMS))
mappedRelaySet = mappedRelaySet + RelaySetupInfo(newUrl, true, true, setOf(FeedType.PRIVATE_DMS))
}
}
@ -277,7 +277,7 @@ class Account(
mappedRelaySet =
mappedRelaySet.map {
if (searchRelaySet.contains(it.url)) {
Relay(it.url, true, it.write || false, it.activeTypes + FeedType.SEARCH)
RelaySetupInfo(it.url, true, it.write || false, it.feedTypes + FeedType.SEARCH)
} else {
it
}
@ -285,7 +285,7 @@ class Account(
searchRelaySet.forEach { newUrl ->
if (mappedRelaySet.none { it.url == newUrl }) {
mappedRelaySet = mappedRelaySet + Relay(newUrl, true, false, setOf(FeedType.SEARCH))
mappedRelaySet = mappedRelaySet + RelaySetupInfo(newUrl, true, false, setOf(FeedType.SEARCH))
}
}
@ -296,7 +296,7 @@ class Account(
mappedRelaySet =
mappedRelaySet.map {
if (privateOutboxRelaySet.contains(it.url)) {
Relay(it.url, true, true, it.activeTypes + setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.GLOBAL, FeedType.PRIVATE_DMS))
RelaySetupInfo(it.url, true, true, it.feedTypes + setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.GLOBAL, FeedType.PRIVATE_DMS))
} else {
it
}
@ -304,7 +304,7 @@ class Account(
privateOutboxRelaySet.forEach { newUrl ->
if (mappedRelaySet.none { it.url == newUrl }) {
mappedRelaySet = mappedRelaySet + Relay(newUrl, true, true, setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.GLOBAL, FeedType.PRIVATE_DMS))
mappedRelaySet = mappedRelaySet + RelaySetupInfo(newUrl, true, true, setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.GLOBAL, FeedType.PRIVATE_DMS))
}
}
@ -315,7 +315,7 @@ class Account(
mappedRelaySet =
mappedRelaySet.map {
if (localRelayServers.contains(it.url)) {
Relay(it.url, true, true, it.activeTypes + setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.GLOBAL, FeedType.PRIVATE_DMS))
RelaySetupInfo(it.url, true, true, it.feedTypes + setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.GLOBAL, FeedType.PRIVATE_DMS))
} else {
it
}
@ -323,7 +323,7 @@ class Account(
localRelayServers.forEach { newUrl ->
if (mappedRelaySet.none { it.url == newUrl }) {
mappedRelaySet = mappedRelaySet + Relay(newUrl, true, true, setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.GLOBAL, FeedType.PRIVATE_DMS))
mappedRelaySet = mappedRelaySet + RelaySetupInfo(newUrl, true, true, setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.GLOBAL, FeedType.PRIVATE_DMS))
}
}
@ -337,7 +337,7 @@ class Account(
if (nip65setup != null) {
val write = nip65setup.type == AdvertisedRelayListEvent.AdvertisedRelayType.BOTH || nip65setup.type == AdvertisedRelayListEvent.AdvertisedRelayType.READ
Relay(relay.url, true, relay.write || write, relay.activeTypes + setOf(FeedType.FOLLOWS, FeedType.GLOBAL, FeedType.PUBLIC_CHATS))
RelaySetupInfo(relay.url, true, relay.write || write, relay.feedTypes + setOf(FeedType.FOLLOWS, FeedType.GLOBAL, FeedType.PUBLIC_CHATS))
} else {
relay
}
@ -347,7 +347,7 @@ class Account(
if (mappedRelaySet.none { it.url == newNip65Setup.relayUrl }) {
val write = newNip65Setup.type == AdvertisedRelayListEvent.AdvertisedRelayType.BOTH || newNip65Setup.type == AdvertisedRelayListEvent.AdvertisedRelayType.READ
mappedRelaySet = mappedRelaySet + Relay(newNip65Setup.relayUrl, true, write, setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS))
mappedRelaySet = mappedRelaySet + RelaySetupInfo(newNip65Setup.relayUrl, true, write, setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS))
}
}
@ -1316,7 +1316,7 @@ class Account(
fun consumeAndSendNip95(
data: FileStorageEvent,
signedEvent: FileStorageHeaderEvent,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
): Note? {
if (!isWriteable()) return null
@ -1342,7 +1342,7 @@ class Account(
fun sendNip95(
data: FileStorageEvent,
signedEvent: FileStorageHeaderEvent,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
) {
Client.send(data, relayList = relayList)
Client.send(signedEvent, relayList = relayList)
@ -1350,7 +1350,7 @@ class Account(
fun sendHeader(
signedEvent: FileHeaderEvent,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
onReady: (Note) -> Unit,
) {
Client.send(signedEvent, relayList = relayList)
@ -1394,7 +1394,7 @@ class Account(
alt: String?,
sensitiveContent: Boolean,
originalHash: String? = null,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
onReady: (Note) -> Unit,
) {
if (!isWriteable()) return
@ -1429,7 +1429,7 @@ class Account(
zapReceiver: List<ZapSplitSetup>? = null,
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
geohash: String? = null,
nip94attachments: List<Event>? = null,
draftTag: String?,
@ -1502,7 +1502,7 @@ class Account(
root: String?,
directMentions: Set<HexKey>,
forkedFrom: Event?,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
draftTag: String?,
@ -1591,7 +1591,7 @@ class Account(
root: String?,
directMentions: Set<HexKey>,
forkedFrom: Event?,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
draftTag: String?,
@ -1659,7 +1659,7 @@ class Account(
originalNote: Note,
notify: HexKey?,
summary: String? = null,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
) {
if (!isWriteable()) return
@ -1689,7 +1689,7 @@ class Account(
zapReceiver: List<ZapSplitSetup>? = null,
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
draftTag: String?,
@ -2605,7 +2605,7 @@ class Account(
}
// Takes a User's relay list and adds the types of feeds they are active for.
fun activeRelays(): Array<Relay>? {
fun activeRelays(): Array<RelaySetupInfo>? {
val usersRelayList =
userProfile().latestContactList?.relays()?.map {
val url = RelayUrlFormatter.normalize(it.key)
@ -2618,24 +2618,24 @@ class Account(
?.feedTypes
?: FeedType.values().toSet()
Relay(url, it.value.read, it.value.write, localFeedTypes)
RelaySetupInfo(url, it.value.read, it.value.write, localFeedTypes)
} ?: return null
return usersRelayList.toTypedArray()
}
fun convertLocalRelays(): Array<Relay> {
return localRelays.map { Relay(RelayUrlFormatter.normalize(it.url), it.read, it.write, it.feedTypes) }.toTypedArray()
fun convertLocalRelays(): Array<RelaySetupInfo> {
return localRelays.map { RelaySetupInfo(RelayUrlFormatter.normalize(it.url), it.read, it.write, it.feedTypes) }.toTypedArray()
}
fun activeGlobalRelays(): Array<String> {
return connectToRelays.value
.filter { it.activeTypes.contains(FeedType.GLOBAL) }
.filter { it.feedTypes.contains(FeedType.GLOBAL) }
.map { it.url }
.toTypedArray()
}
fun activeWriteRelays(): List<Relay> {
fun activeWriteRelays(): List<RelaySetupInfo> {
return connectToRelays.value.filter { it.write }
}

Wyświetl plik

@ -21,6 +21,7 @@
package com.vitorpamplona.amethyst.service.relays
import android.util.Log
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.EventInterface
@ -41,10 +42,10 @@ object Client : RelayPool.Listener {
@Synchronized
fun reconnect(
relays: Array<Relay>?,
relays: Array<RelaySetupInfo>?,
onlyIfChanged: Boolean = false,
) {
Log.d("Relay", "Relay Pool Reconnecting to ${relays?.size} relays: \n${relays?.joinToString("\n") { it.url + " " + it.read + " " + it.write + " " + it.activeTypes.joinToString(",") { it.name } }}")
Log.d("Relay", "Relay Pool Reconnecting to ${relays?.size} relays: \n${relays?.joinToString("\n") { it.url + " " + it.read + " " + it.write + " " + it.feedTypes.joinToString(",") { it.name } }}")
checkNotInMainThread()
if (onlyIfChanged) {
@ -56,10 +57,11 @@ object Client : RelayPool.Listener {
}
if (relays != null) {
val newRelays = relays.map { Relay(it.url, it.read, it.write, it.feedTypes) }
RelayPool.register(this)
RelayPool.loadRelays(relays.toList())
RelayPool.loadRelays(newRelays)
RelayPool.requestAndWatch()
this.relays = relays
this.relays = newRelays.toTypedArray()
}
}
} else {
@ -70,15 +72,16 @@ object Client : RelayPool.Listener {
}
if (relays != null) {
val newRelays = relays.map { Relay(it.url, it.read, it.write, it.feedTypes) }
RelayPool.register(this)
RelayPool.loadRelays(relays.toList())
RelayPool.loadRelays(newRelays)
RelayPool.requestAndWatch()
this.relays = relays
this.relays = newRelays.toTypedArray()
}
}
}
fun isSameRelaySetConfig(newRelayConfig: Array<Relay>?): Boolean {
fun isSameRelaySetConfig(newRelayConfig: Array<RelaySetupInfo>?): Boolean {
if (relays.size != newRelayConfig?.size) return false
relays.forEach { oldRelayInfo ->
@ -142,7 +145,7 @@ object Client : RelayPool.Listener {
signedEvent: EventInterface,
relay: String? = null,
feedTypes: Set<FeedType>? = null,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
onDone: (() -> Unit)? = null,
) {
checkNotInMainThread()

Wyświetl plik

@ -23,6 +23,7 @@ package com.vitorpamplona.amethyst.service.relays
import android.util.Log
import com.vitorpamplona.amethyst.BuildConfig
import com.vitorpamplona.amethyst.model.RelayBriefInfoCache
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.service.HttpClientManager
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.quartz.encoders.HexKey
@ -523,11 +524,11 @@ class Relay(
writeToSocket("""["CLOSE","$subscriptionId"]""")
}
fun isSameRelayConfig(other: Relay): Boolean {
fun isSameRelayConfig(other: RelaySetupInfo): Boolean {
return url == other.url &&
write == other.write &&
read == other.read &&
activeTypes == other.activeTypes
activeTypes == other.feedTypes
}
enum class StateType {

Wyświetl plik

@ -21,6 +21,7 @@
package com.vitorpamplona.amethyst.service.relays
import androidx.compose.runtime.Immutable
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.EventInterface
@ -148,7 +149,7 @@ object RelayPool : Relay.Listener {
}
fun sendToSelectedRelays(
list: List<Relay>,
list: List<RelaySetupInfo>,
signedEvent: EventInterface,
) {
list.forEach { relay -> relays.filter { it.url == relay.url }.forEach { it.sendOverride(signedEvent) } }

Wyświetl plik

@ -36,11 +36,11 @@ import com.vitorpamplona.amethyst.commons.richtext.RichTextParser
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.FileHeader
import com.vitorpamplona.amethyst.service.Nip96Uploader
import com.vitorpamplona.amethyst.service.NostrSearchEventOrUserDataSource
import com.vitorpamplona.amethyst.service.relays.Relay
import com.vitorpamplona.amethyst.ui.components.MediaCompressor
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.quartz.events.FileHeaderEvent
@ -109,11 +109,11 @@ open class EditPostViewModel() : ViewModel() {
editedFromNote = edit
}
fun sendPost(relayList: List<Relay>? = null) {
fun sendPost(relayList: List<RelaySetupInfo>? = null) {
viewModelScope.launch(Dispatchers.IO) { innerSendPost(relayList) }
}
suspend fun innerSendPost(relayList: List<Relay>? = null) {
suspend fun innerSendPost(relayList: List<RelaySetupInfo>? = null) {
if (accountViewModel == null) {
cancel()
return

Wyświetl plik

@ -31,10 +31,10 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.service.FileHeader
import com.vitorpamplona.amethyst.service.Nip96MediaServers
import com.vitorpamplona.amethyst.service.Nip96Uploader
import com.vitorpamplona.amethyst.service.relays.Relay
import com.vitorpamplona.amethyst.ui.components.MediaCompressor
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
@ -80,7 +80,7 @@ open class NewMediaModel : ViewModel() {
fun upload(
context: Context,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
) {
isUploadingImage = true
@ -188,7 +188,7 @@ open class NewMediaModel : ViewModel() {
localContentType: String?,
alt: String,
sensitiveContent: Boolean,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
context: Context,
) {
uploadingPercentage.value = 0.40f
@ -272,7 +272,7 @@ open class NewMediaModel : ViewModel() {
mimeType: String?,
alt: String,
sensitiveContent: Boolean,
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
context: Context,
) {
if (bytes.size > 80000) {

Wyświetl plik

@ -40,12 +40,12 @@ import com.vitorpamplona.amethyst.commons.richtext.RichTextParser
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.FileHeader
import com.vitorpamplona.amethyst.service.LocationUtil
import com.vitorpamplona.amethyst.service.Nip96Uploader
import com.vitorpamplona.amethyst.service.NostrSearchEventOrUserDataSource
import com.vitorpamplona.amethyst.service.relays.Relay
import com.vitorpamplona.amethyst.ui.components.MediaCompressor
import com.vitorpamplona.amethyst.ui.components.Split
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
@ -438,7 +438,7 @@ open class NewPostViewModel() : ViewModel() {
urlPreview = findUrlInMessage()
}
fun sendPost(relayList: List<Relay>? = null) {
fun sendPost(relayList: List<RelaySetupInfo>? = null) {
viewModelScope.launch(Dispatchers.IO) {
innerSendPost(relayList, null)
accountViewModel?.deleteDraft(draftTag)
@ -446,18 +446,18 @@ open class NewPostViewModel() : ViewModel() {
}
}
fun sendDraft(relayList: List<Relay>? = null) {
fun sendDraft(relayList: List<RelaySetupInfo>? = null) {
viewModelScope.launch {
sendDraftSync(relayList)
}
}
suspend fun sendDraftSync(relayList: List<Relay>? = null) {
suspend fun sendDraftSync(relayList: List<RelaySetupInfo>? = null) {
innerSendPost(relayList, draftTag)
}
private suspend fun innerSendPost(
relayList: List<Relay>? = null,
relayList: List<RelaySetupInfo>? = null,
localDraft: String?,
) = withContext(Dispatchers.IO) {
if (accountViewModel == null) {

Wyświetl plik

@ -47,8 +47,8 @@ import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.RelayBriefInfoCache
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.service.Nip11Retriever
import com.vitorpamplona.amethyst.service.relays.Relay
import com.vitorpamplona.amethyst.ui.actions.relays.RelayInformationDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
@ -57,7 +57,7 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
data class RelayList(
val relay: Relay,
val relay: RelaySetupInfo,
val relayInfo: RelayBriefInfoCache.RelayBriefInfo,
val isSelected: Boolean,
)
@ -69,9 +69,9 @@ data class RelayInfoDialog(
@Composable
fun RelaySelectionDialog(
preSelectedList: ImmutableList<Relay>,
preSelectedList: ImmutableList<RelaySetupInfo>,
onClose: () -> Unit,
onPost: (list: ImmutableList<Relay>) -> Unit,
onPost: (list: ImmutableList<RelaySetupInfo>) -> Unit,
accountViewModel: AccountViewModel,
nav: (String) -> Unit,
) {