feat(desktop): add DM broadcast banner and fix empty room loading
- Fix ListChangeFeedViewModel stuck on Loading for empty chatrooms by adding invalidateData() call in init block - Add DmBroadcastStatus sealed class and DmBroadcastBanner composable in commons for relay send progress display - Add DmSendTracker using sendAndWaitForResponse for confirmed delivery - Wire send tracking through DesktopIAccount into ChatPane banner - Fix audit risks: unsubscribe DMs on account change, wait for relay connect before subscribing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -96,12 +96,14 @@ import com.vitorpamplona.amethyst.desktop.ui.ThreadScreen
|
||||
import com.vitorpamplona.amethyst.desktop.ui.UserProfileScreen
|
||||
import com.vitorpamplona.amethyst.desktop.ui.ZapFeedback
|
||||
import com.vitorpamplona.amethyst.desktop.ui.chats.DesktopMessagesScreen
|
||||
import com.vitorpamplona.amethyst.desktop.ui.chats.DmSendTracker
|
||||
import com.vitorpamplona.amethyst.desktop.ui.profile.ProfileInfoCard
|
||||
import com.vitorpamplona.amethyst.desktop.ui.relay.RelayStatusCard
|
||||
import com.vitorpamplona.quartz.nip47WalletConnect.Nip47WalletConnect
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
private val isMacOS = System.getProperty("os.name").lowercase().contains("mac")
|
||||
@@ -284,10 +286,16 @@ fun App(
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to DMs when user logs in
|
||||
// Subscribe to DMs when user logs in; unsubscribe on account change
|
||||
LaunchedEffect(accountState) {
|
||||
// Clean up previous DM subscriptions on any account change (logout, switch)
|
||||
subscriptionsCoordinator.unsubscribeFromDms()
|
||||
|
||||
val currentAccount = accountState
|
||||
if (currentAccount is AccountState.LoggedIn) {
|
||||
// Wait for at least one relay to connect before subscribing
|
||||
relayManager.connectedRelays.first { it.isNotEmpty() }
|
||||
|
||||
val dmRelayState =
|
||||
DesktopDmRelayState(
|
||||
dmRelayList = kotlinx.coroutines.flow.MutableStateFlow(emptySet()),
|
||||
@@ -535,9 +543,13 @@ fun MainContent(
|
||||
}
|
||||
|
||||
DesktopScreen.Messages -> {
|
||||
val dmSendTracker =
|
||||
remember(relayManager) {
|
||||
DmSendTracker(relayManager.client)
|
||||
}
|
||||
val iAccount =
|
||||
remember(account, localCache, relayManager) {
|
||||
DesktopIAccount(account, localCache, relayManager)
|
||||
remember(account, localCache, relayManager, dmSendTracker) {
|
||||
DesktopIAccount(account, localCache, relayManager, dmSendTracker, scope)
|
||||
}
|
||||
DesktopMessagesScreen(
|
||||
account = iAccount,
|
||||
|
||||
+9
-4
@@ -28,6 +28,7 @@ import com.vitorpamplona.amethyst.commons.model.privateChats.ChatroomList
|
||||
import com.vitorpamplona.amethyst.desktop.account.AccountState
|
||||
import com.vitorpamplona.amethyst.desktop.cache.DesktopLocalCache
|
||||
import com.vitorpamplona.amethyst.desktop.network.RelayConnectionManager
|
||||
import com.vitorpamplona.amethyst.desktop.ui.chats.DmSendTracker
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner
|
||||
import com.vitorpamplona.quartz.nip04Dm.messages.PrivateDmEvent
|
||||
@@ -41,6 +42,8 @@ import com.vitorpamplona.quartz.nip57Zaps.IPrivateZapsDecryptionCache
|
||||
import com.vitorpamplona.quartz.nip57Zaps.LnZapRequestEvent
|
||||
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
|
||||
import com.vitorpamplona.quartz.utils.DualCase
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Desktop implementation of IAccount.
|
||||
@@ -55,6 +58,8 @@ class DesktopIAccount(
|
||||
private val accountState: AccountState.LoggedIn,
|
||||
private val localCache: DesktopLocalCache,
|
||||
private val relayManager: RelayConnectionManager,
|
||||
val dmSendTracker: DmSendTracker,
|
||||
private val scope: CoroutineScope,
|
||||
) : IAccount {
|
||||
override val signer: NostrSigner = accountState.signer
|
||||
|
||||
@@ -114,7 +119,7 @@ class DesktopIAccount(
|
||||
}
|
||||
}
|
||||
|
||||
relayManager.send(signedEvent, targetRelays)
|
||||
scope.launch { dmSendTracker.sendAndTrack(signedEvent, targetRelays) }
|
||||
}
|
||||
|
||||
override suspend fun sendNip17PrivateMessage(template: EventTemplate<ChatMessageEvent>) {
|
||||
@@ -122,7 +127,7 @@ class DesktopIAccount(
|
||||
|
||||
val result = NIP17Factory().createMessageNIP17(template, signer)
|
||||
|
||||
// Broadcast each gift wrap to the recipient's inbox relays
|
||||
// Broadcast each gift wrap to the recipient's inbox relays with tracking
|
||||
result.wraps.forEach { wrap ->
|
||||
val recipientKey = wrap.recipientPubKey()
|
||||
val targetRelays =
|
||||
@@ -138,7 +143,7 @@ class DesktopIAccount(
|
||||
relayManager.connectedRelays.value
|
||||
}
|
||||
|
||||
relayManager.send(wrap, targetRelays)
|
||||
scope.launch { dmSendTracker.sendAndTrack(wrap, targetRelays) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +163,7 @@ class DesktopIAccount(
|
||||
relayManager.connectedRelays.value
|
||||
}
|
||||
|
||||
relayManager.send(wrap, targetRelays)
|
||||
scope.launch { dmSendTracker.sendAndTrack(wrap, targetRelays) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,8 @@ import com.vitorpamplona.amethyst.commons.model.User
|
||||
import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider
|
||||
import com.vitorpamplona.amethyst.commons.ui.chat.ChatMessageCompose
|
||||
import com.vitorpamplona.amethyst.commons.ui.chat.ChatroomHeader
|
||||
import com.vitorpamplona.amethyst.commons.ui.chat.DmBroadcastBanner
|
||||
import com.vitorpamplona.amethyst.commons.ui.chat.DmBroadcastStatus
|
||||
import com.vitorpamplona.amethyst.commons.ui.chat.GroupChatroomHeader
|
||||
import com.vitorpamplona.amethyst.commons.ui.components.LoadingState
|
||||
import com.vitorpamplona.amethyst.commons.ui.feeds.FeedState
|
||||
@@ -114,6 +116,7 @@ fun ChatPane(
|
||||
cacheProvider: ICacheProvider,
|
||||
feedViewModel: ChatroomFeedViewModel,
|
||||
messageState: ChatNewMessageState,
|
||||
dmBroadcastStatus: DmBroadcastStatus = DmBroadcastStatus.Idle,
|
||||
onNavigateToProfile: (String) -> Unit = {},
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
@@ -157,6 +160,9 @@ fun ChatPane(
|
||||
|
||||
HorizontalDivider()
|
||||
|
||||
// Broadcast status banner
|
||||
DmBroadcastBanner(status = dmBroadcastStatus)
|
||||
|
||||
// Message list
|
||||
Box(modifier = Modifier.weight(1f).fillMaxWidth()) {
|
||||
when (feedState) {
|
||||
|
||||
+12
@@ -54,8 +54,10 @@ import androidx.compose.ui.input.key.type
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.vitorpamplona.amethyst.commons.model.IAccount
|
||||
import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider
|
||||
import com.vitorpamplona.amethyst.commons.ui.chat.DmBroadcastStatus
|
||||
import com.vitorpamplona.amethyst.commons.viewmodels.ChatNewMessageState
|
||||
import com.vitorpamplona.amethyst.commons.viewmodels.ChatroomFeedViewModel
|
||||
import com.vitorpamplona.amethyst.desktop.model.DesktopIAccount
|
||||
|
||||
private val isMacOS = System.getProperty("os.name").lowercase().contains("mac")
|
||||
|
||||
@@ -138,12 +140,22 @@ fun DesktopMessagesScreen(
|
||||
ChatNewMessageState(account, cacheProvider, scope)
|
||||
}
|
||||
|
||||
val broadcastStatus =
|
||||
if (account is DesktopIAccount) {
|
||||
account.dmSendTracker.status
|
||||
.collectAsState()
|
||||
.value
|
||||
} else {
|
||||
DmBroadcastStatus.Idle
|
||||
}
|
||||
|
||||
ChatPane(
|
||||
roomKey = currentRoom,
|
||||
account = account,
|
||||
cacheProvider = cacheProvider,
|
||||
feedViewModel = feedViewModel,
|
||||
messageState = messageState,
|
||||
dmBroadcastStatus = broadcastStatus,
|
||||
onNavigateToProfile = onNavigateToProfile,
|
||||
)
|
||||
} else {
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.desktop.ui.chats
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.ui.chat.DmBroadcastStatus
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.sendAndWaitForResponse
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
class DmSendTracker(
|
||||
private val client: INostrClient,
|
||||
) {
|
||||
private val _status = MutableStateFlow<DmBroadcastStatus>(DmBroadcastStatus.Idle)
|
||||
val status: StateFlow<DmBroadcastStatus> = _status.asStateFlow()
|
||||
|
||||
suspend fun sendAndTrack(
|
||||
event: Event,
|
||||
relays: Set<NormalizedRelayUrl>,
|
||||
) {
|
||||
if (relays.isEmpty()) {
|
||||
_status.value = DmBroadcastStatus.Failed("No relays available")
|
||||
delay(3000)
|
||||
_status.value = DmBroadcastStatus.Idle
|
||||
return
|
||||
}
|
||||
|
||||
_status.value = DmBroadcastStatus.Sending(0, relays.size)
|
||||
|
||||
val success = client.sendAndWaitForResponse(event, relays, 10)
|
||||
|
||||
_status.value =
|
||||
if (success) {
|
||||
DmBroadcastStatus.Sent(relays.size)
|
||||
} else {
|
||||
DmBroadcastStatus.Failed("No relay accepted the message")
|
||||
}
|
||||
|
||||
delay(3000)
|
||||
_status.value = DmBroadcastStatus.Idle
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user