From fe141d1ee356c357c03d19582176a3636a905ab2 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 21 Aug 2025 14:29:34 -0400 Subject: [PATCH] Fixes crash when typing a new post without permissions to do a draft post. --- .../com/vitorpamplona/amethyst/model/Account.kt | 4 ++++ .../ui/note/nip22Comments/CommentPostViewModel.kt | 2 +- .../ui/screen/loggedIn/AccountViewModel.kt | 4 ---- .../privateDM/send/ChatNewMessageViewModel.kt | 6 ++++-- .../send/ChannelNewMessageViewModel.kt | 8 +++++--- .../nip99Classifieds/NewProductViewModel.kt | 15 +++++++++------ .../loggedIn/home/ShortNotePostViewModel.kt | 6 ++++-- .../publicMessages/NewPublicMessageViewModel.kt | 6 ++++-- 8 files changed, 31 insertions(+), 20 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index b77838951..e95538198 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -1145,6 +1145,8 @@ class Account( template: EventTemplate, broadcast: Set = emptySet(), ) { + if (!isWriteable()) return + val extraRelays = cache.getAddressableNoteIfExists(DraftWrapEvent.createAddressTag(signer.pubKey, draftTag))?.relays ?: emptyList() val rumor = RumorAssembler.assembleRumor(signer.pubKey, template) @@ -1163,6 +1165,8 @@ class Account( } suspend fun deleteDraft(draftTag: String) { + if (!isWriteable()) return + val extraRelays = cache.getAddressableNoteIfExists(DraftWrapEvent.createAddressTag(signer.pubKey, draftTag))?.relays ?: emptyList() val deletedDraft = DraftWrapEvent.createDeletedEvent(draftTag, signer) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/CommentPostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/CommentPostViewModel.kt index 7bfefdeca..d9016fb11 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/CommentPostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/nip22Comments/CommentPostViewModel.kt @@ -317,7 +317,7 @@ open class CommentPostViewModel : cancel() accountViewModel.account.signAndComputeBroadcast(template, extraNotesToBroadcast) - accountViewModel.deleteDraft(version) + accountViewModel.account.deleteDraft(version) } suspend fun sendDraftSync() { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index fa3fa2a02..272ae9f47 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -1506,10 +1506,6 @@ class AccountViewModel( fun dataSources() = app.sources - suspend fun deleteDraft(draftTag: String) { - account.deleteDraft(draftTag) - } - suspend fun createTempDraftNote(noteEvent: DraftWrapEvent): Note? = draftNoteCache.update(noteEvent) fun createTempDraftNote( diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/ChatNewMessageViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/ChatNewMessageViewModel.kt index ec6dd1737..dcb020516 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/ChatNewMessageViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/send/ChatNewMessageViewModel.kt @@ -112,7 +112,9 @@ class ChatNewMessageViewModel : draftTag.versions.collectLatest { // don't save the first if (it > 0) { - sendDraftSync() + accountViewModel.runIOCatching { + sendDraftSync() + } } } } @@ -349,7 +351,7 @@ class ChatNewMessageViewModel : val version = draftTag.current innerSendPost(null) cancel() - accountViewModel.deleteDraft(version) + accountViewModel.account.deleteDraft(version) } suspend fun sendDraftSync() { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt index 61fb907dd..71499c5fa 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt @@ -106,7 +106,9 @@ open class ChannelNewMessageViewModel : draftTag.versions.collectLatest { // don't save the first if (it > 0) { - sendDraftSync() + accountViewModel.runIOCatching { + sendDraftSync() + } } } } @@ -260,7 +262,7 @@ open class ChannelNewMessageViewModel : } fun sendPost(onDone: suspend () -> Unit) { - viewModelScope.launch(Dispatchers.IO) { + accountViewModel.runIOCatching { sendPostSync() onDone() } @@ -274,7 +276,7 @@ open class ChannelNewMessageViewModel : cancel() accountViewModel.account.signAndSendPrivately(template, channelRelays) - accountViewModel.deleteDraft(version) + accountViewModel.account.deleteDraft(version) } suspend fun sendDraftSync() { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductViewModel.kt index 8cb8ed970..d55c8fe79 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/discover/nip99Classifieds/NewProductViewModel.kt @@ -104,20 +104,22 @@ open class NewProductViewModel : IZapRaiser { val draftTag = DraftTagState() + var accountViewModel: AccountViewModel? = null + var account: Account? = null + init { viewModelScope.launch(Dispatchers.IO) { draftTag.versions.collectLatest { // don't save the first if (it > 0) { - sendDraftSync() + accountViewModel?.runIOCatching { + sendDraftSync() + } } } } } - var accountViewModel: AccountViewModel? = null - var account: Account? = null - var productImages by mutableStateOf>(emptyList()) val iMetaDescription = IMetaAttachments() @@ -288,14 +290,15 @@ open class NewProductViewModel : } suspend fun sendPostSync() { + val accountViewModel = accountViewModel ?: return val template = createTemplate() ?: return - accountViewModel?.account?.signAndSendPrivatelyOrBroadcast( + accountViewModel.account.signAndSendPrivatelyOrBroadcast( template, relayList = { relayList }, ) - accountViewModel?.deleteDraft(draftTag.current) + accountViewModel.account.deleteDraft(draftTag.current) cancel() } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt index 4d38c1aa5..387bf4351 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt @@ -146,7 +146,9 @@ open class ShortNotePostViewModel : draftTag.versions.collectLatest { // don't save the first if (it > 0) { - sendDraftSync() + accountViewModel.runIOCatching { + sendDraftSync() + } } } } @@ -481,7 +483,7 @@ open class ShortNotePostViewModel : cancel() accountViewModel.account.signAndComputeBroadcast(template, extraNotesToBroadcast) - accountViewModel.deleteDraft(version) + accountViewModel.account.deleteDraft(version) } suspend fun sendDraftSync() { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageViewModel.kt index f3b8ff4fe..c0a1cd99f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/publicMessages/NewPublicMessageViewModel.kt @@ -127,7 +127,9 @@ class NewPublicMessageViewModel : draftTag.versions.collectLatest { // don't save the first if (it > 0) { - sendDraftSync() + accountViewModel.runIOCatching { + sendDraftSync() + } } } } @@ -319,7 +321,7 @@ class NewPublicMessageViewModel : cancel() accountViewModel.account.signAndComputeBroadcast(template, extraNotesToBroadcast) - accountViewModel.deleteDraft(version) + accountViewModel.account.deleteDraft(version) } suspend fun sendDraftSync() {