diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index afd7dccaa..10a4f58e6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -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? = null, + relayList: List? = null, ): Note? { if (!isWriteable()) return null @@ -1342,7 +1342,7 @@ class Account( fun sendNip95( data: FileStorageEvent, signedEvent: FileStorageHeaderEvent, - relayList: List? = null, + relayList: List? = null, ) { Client.send(data, relayList = relayList) Client.send(signedEvent, relayList = relayList) @@ -1350,7 +1350,7 @@ class Account( fun sendHeader( signedEvent: FileHeaderEvent, - relayList: List? = null, + relayList: List? = null, onReady: (Note) -> Unit, ) { Client.send(signedEvent, relayList = relayList) @@ -1394,7 +1394,7 @@ class Account( alt: String?, sensitiveContent: Boolean, originalHash: String? = null, - relayList: List? = null, + relayList: List? = null, onReady: (Note) -> Unit, ) { if (!isWriteable()) return @@ -1429,7 +1429,7 @@ class Account( zapReceiver: List? = null, wantsToMarkAsSensitive: Boolean, zapRaiserAmount: Long? = null, - relayList: List? = null, + relayList: List? = null, geohash: String? = null, nip94attachments: List? = null, draftTag: String?, @@ -1502,7 +1502,7 @@ class Account( root: String?, directMentions: Set, forkedFrom: Event?, - relayList: List? = null, + relayList: List? = null, geohash: String? = null, nip94attachments: List? = null, draftTag: String?, @@ -1591,7 +1591,7 @@ class Account( root: String?, directMentions: Set, forkedFrom: Event?, - relayList: List? = null, + relayList: List? = null, geohash: String? = null, nip94attachments: List? = null, draftTag: String?, @@ -1659,7 +1659,7 @@ class Account( originalNote: Note, notify: HexKey?, summary: String? = null, - relayList: List? = null, + relayList: List? = null, ) { if (!isWriteable()) return @@ -1689,7 +1689,7 @@ class Account( zapReceiver: List? = null, wantsToMarkAsSensitive: Boolean, zapRaiserAmount: Long? = null, - relayList: List? = null, + relayList: List? = null, geohash: String? = null, nip94attachments: List? = 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? { + fun activeRelays(): Array? { 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 { - return localRelays.map { Relay(RelayUrlFormatter.normalize(it.url), it.read, it.write, it.feedTypes) }.toTypedArray() + fun convertLocalRelays(): Array { + return localRelays.map { RelaySetupInfo(RelayUrlFormatter.normalize(it.url), it.read, it.write, it.feedTypes) }.toTypedArray() } fun activeGlobalRelays(): Array { return connectToRelays.value - .filter { it.activeTypes.contains(FeedType.GLOBAL) } + .filter { it.feedTypes.contains(FeedType.GLOBAL) } .map { it.url } .toTypedArray() } - fun activeWriteRelays(): List { + fun activeWriteRelays(): List { return connectToRelays.value.filter { it.write } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Client.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Client.kt index 4fef51755..22d449c4c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Client.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Client.kt @@ -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?, + relays: Array?, 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?): Boolean { + fun isSameRelaySetConfig(newRelayConfig: Array?): 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? = null, - relayList: List? = null, + relayList: List? = null, onDone: (() -> Unit)? = null, ) { checkNotInMainThread() diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt index 5e5f10638..240b62436 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt @@ -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 { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/RelayPool.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/RelayPool.kt index 4f07771d0..b7470feca 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/RelayPool.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/RelayPool.kt @@ -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, + list: List, signedEvent: EventInterface, ) { list.forEach { relay -> relays.filter { it.url == relay.url }.forEach { it.sendOverride(signedEvent) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt index f4e0d6b67..abfc5d22f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt @@ -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? = null) { + fun sendPost(relayList: List? = null) { viewModelScope.launch(Dispatchers.IO) { innerSendPost(relayList) } } - suspend fun innerSendPost(relayList: List? = null) { + suspend fun innerSendPost(relayList: List? = null) { if (accountViewModel == null) { cancel() return diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt index 5f89c0d01..16d9a0258 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt @@ -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? = null, + relayList: List? = null, ) { isUploadingImage = true @@ -188,7 +188,7 @@ open class NewMediaModel : ViewModel() { localContentType: String?, alt: String, sensitiveContent: Boolean, - relayList: List? = null, + relayList: List? = null, context: Context, ) { uploadingPercentage.value = 0.40f @@ -272,7 +272,7 @@ open class NewMediaModel : ViewModel() { mimeType: String?, alt: String, sensitiveContent: Boolean, - relayList: List? = null, + relayList: List? = null, context: Context, ) { if (bytes.size > 80000) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt index 04ff2eaf0..3640858b1 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt @@ -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? = null) { + fun sendPost(relayList: List? = null) { viewModelScope.launch(Dispatchers.IO) { innerSendPost(relayList, null) accountViewModel?.deleteDraft(draftTag) @@ -446,18 +446,18 @@ open class NewPostViewModel() : ViewModel() { } } - fun sendDraft(relayList: List? = null) { + fun sendDraft(relayList: List? = null) { viewModelScope.launch { sendDraftSync(relayList) } } - suspend fun sendDraftSync(relayList: List? = null) { + suspend fun sendDraftSync(relayList: List? = null) { innerSendPost(relayList, draftTag) } private suspend fun innerSendPost( - relayList: List? = null, + relayList: List? = null, localDraft: String?, ) = withContext(Dispatchers.IO) { if (accountViewModel == null) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt index 73f142040..9addb828c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt @@ -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, + preSelectedList: ImmutableList, onClose: () -> Unit, - onPost: (list: ImmutableList) -> Unit, + onPost: (list: ImmutableList) -> Unit, accountViewModel: AccountViewModel, nav: (String) -> Unit, ) {