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 05cc1d394..b6e6793ae 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -155,8 +155,8 @@ class Account( return keyPair.privKey != null } - fun sendNewRelayList(relays: Map) { - if (!isWriteable()) return + fun sendNewRelayList(relays: Map, signEvent: Boolean): ContactListEvent? { + if (!isWriteable() && signEvent) return null val contactList = userProfile().latestContactList @@ -164,11 +164,17 @@ class Account( val event = ContactListEvent.updateRelayList( earlierVersion = contactList, relayUse = relays, - privateKey = keyPair.privKey!! + pubKey = keyPair.pubKey.toHexKey(), + privateKey = keyPair.privKey ) + if (!signEvent) { + return event + } + Client.send(event) LocalCache.consume(event) + return null } else { val event = ContactListEvent.createFromScratch( followUsers = listOf(), @@ -177,13 +183,19 @@ class Account( followCommunities = listOf(), followEvents = DefaultChannels.toList(), relayUse = relays, - privateKey = keyPair.privKey!! + privateKey = keyPair.privKey!!, + publicKey = if (!signEvent) keyPair.pubKey else null ) + if (!signEvent) { + return event + } + // Keep this local to avoid erasing a good contact list. // Client.send(event) LocalCache.consume(event) } + return null } fun sendNewUserMetadata(toString: String, identities: List, signEvent: Boolean = true): MetadataEvent? { @@ -1821,11 +1833,13 @@ class Account( ).toSet() } - fun saveRelayList(value: List) { - localRelays = value.toSet() - sendNewRelayList(value.associate { it.url to ContactListEvent.ReadWrite(it.read, it.write) }) - - saveable.invalidateData() + fun saveRelayList(value: List, signEvent: Boolean): ContactListEvent? { + try { + localRelays = value.toSet() + return sendNewRelayList(value.associate { it.url to ContactListEvent.ReadWrite(it.read, it.write) }, signEvent) + } finally { + saveable.invalidateData() + } } fun setHideDeleteRequestDialog() { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/model/ContactListEvent.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/model/ContactListEvent.kt index e6a1e6ff2..43c936dd0 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/model/ContactListEvent.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/model/ContactListEvent.kt @@ -319,13 +319,22 @@ class ContactListEvent( ) } - fun updateRelayList(earlierVersion: ContactListEvent, relayUse: Map?, privateKey: ByteArray, createdAt: Long = TimeUtils.now()): ContactListEvent { + fun updateRelayList(earlierVersion: ContactListEvent, relayUse: Map?, pubKey: HexKey, privateKey: ByteArray?, createdAt: Long = TimeUtils.now()): ContactListEvent { val content = if (relayUse != null) { gson.toJson(relayUse) } else { "" } + if (privateKey == null) { + return create( + content = content, + tags = earlierVersion.tags, + pubKey = pubKey, + createdAt = createdAt + ) + } + return create( content = content, tags = earlierVersion.tags, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListView.kt index d811a1e99..5a5ba5c5d 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListView.kt @@ -57,8 +57,12 @@ import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.RelayInformation import com.vitorpamplona.amethyst.model.RelaySetupInfo +import com.vitorpamplona.amethyst.service.PackageUtils +import com.vitorpamplona.amethyst.service.model.Event +import com.vitorpamplona.amethyst.service.relays.Client import com.vitorpamplona.amethyst.service.relays.Constants.defaultRelays import com.vitorpamplona.amethyst.service.relays.FeedType import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel @@ -68,6 +72,7 @@ import com.vitorpamplona.amethyst.ui.theme.Size35dp import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer import com.vitorpamplona.amethyst.ui.theme.placeholderText import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import java.lang.Math.round @@ -88,6 +93,25 @@ fun NewRelayListView(onClose: () -> Unit, accountViewModel: AccountViewModel, re } } + var event by remember { mutableStateOf(null) } + if (event != null) { + SignerDialog( + onClose = { + event = null + }, + onPost = { + scope.launch(Dispatchers.IO) { + Client.send(it) + LocalCache.verifyAndConsume(it, null) + event = null + postViewModel.clear() + onClose() + } + }, + event = event!! + ) + } + Dialog( onDismissRequest = { onClose() }, properties = DialogProperties( @@ -128,8 +152,12 @@ fun NewRelayListView(onClose: () -> Unit, accountViewModel: AccountViewModel, re PostButton( onPost = { - postViewModel.create() - onClose() + if (PackageUtils.isAmberInstalled(context)) { + event = postViewModel.create(false) + } else { + postViewModel.create(true) + onClose() + } }, true ) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListViewModel.kt index 75b633182..8296e3cb1 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListViewModel.kt @@ -26,14 +26,20 @@ class NewRelayListViewModel : ViewModel() { clear() } - fun create() { + fun create(signEvent: Boolean): ContactListEvent? { + if (!signEvent) { + relays.let { + return account.saveRelayList(it.value, false) + } + } relays.let { viewModelScope.launch(Dispatchers.IO) { - account.saveRelayList(it.value) + account.saveRelayList(it.value, true) } } clear() + return null } fun clear() {