From eefcbaa157bad122fb5b71a21d0a263a05ca9269 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Tue, 28 Jul 2026 01:39:32 +0200 Subject: [PATCH] Bug fix for inner events. --- .../mantra/compose/database/dao/MantraDao.kt | 5 ++- .../compose/database/dao/MarmotOutboundDao.kt | 37 ++++++++++--------- .../ui/composable/AddTranslationScreen.kt | 17 +++++++++ .../ui/composable/navigation/MantraNavHost.kt | 1 + .../ui/view/model/AddTranslationViewModel.kt | 14 ++++++- .../ui/view/state/AddTranslationUIState.kt | 2 + 6 files changed, 55 insertions(+), 21 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/press/mantra/compose/database/dao/MantraDao.kt b/composeApp/src/commonMain/kotlin/press/mantra/compose/database/dao/MantraDao.kt index 0e574dc2..afdd5478 100644 --- a/composeApp/src/commonMain/kotlin/press/mantra/compose/database/dao/MantraDao.kt +++ b/composeApp/src/commonMain/kotlin/press/mantra/compose/database/dao/MantraDao.kt @@ -1,8 +1,10 @@ package press.mantra.compose.database.dao +import androidx.room3.Dao import androidx.room3.Transaction import co.touchlab.kermit.Logger import com.vitorpamplona.quartz.nip01Core.core.HexKey +import press.mantra.compose.database.MantraDatabase import press.mantra.compose.database.model.ChatMessage import press.mantra.compose.database.model.MantraArtifact import press.mantra.compose.database.model.MantraArtifactVersion @@ -16,8 +18,9 @@ import press.mantra.compose.nostr.nip30303.tags.ArtifactIdTag import press.mantra.compose.repository.MantraRepository.Companion.DEFAULT_LICENSE import press.mantra.compose.repository.MantraRepository.Companion.DEFAULT_VISIBILITY +@Dao abstract class MantraDao( - private val database: press.mantra.compose.database.MantraDatabase, + private val database: MantraDatabase, ) { val logger = Logger.withTag("NostrDao") diff --git a/composeApp/src/commonMain/kotlin/press/mantra/compose/database/dao/MarmotOutboundDao.kt b/composeApp/src/commonMain/kotlin/press/mantra/compose/database/dao/MarmotOutboundDao.kt index a4373d24..70aee19b 100644 --- a/composeApp/src/commonMain/kotlin/press/mantra/compose/database/dao/MarmotOutboundDao.kt +++ b/composeApp/src/commonMain/kotlin/press/mantra/compose/database/dao/MarmotOutboundDao.kt @@ -463,6 +463,25 @@ abstract class MarmotOutboundDao( ) ) + database.marmotGroupEventDao().upsert( + MarmotGroupEvent( + id = groupEvent.id, + createdAt = Instant.fromEpochSeconds(groupEvent.createdAt), + expiresAt = groupEvent.expiration()?.let { Instant.fromEpochSeconds(it) }, + encryptedContent = groupEvent.encryptedContent(), + chatRoomId = localChatRoom.chatRoom.id, + userPublicKey = groupEvent.pubKey, + publicKey = groupEvent.pubKey, + signature = groupEvent.sig, + ) + ) + + database.marmotInnerEventDao().upsert( + marmotInnerEvent.copy( + marmotGroupEventId = groupEvent.id + ) + ) + val chatMessageOrNull = database.chatMessageDao().getChatMessagesByMarmotInnerEventId(innerEvent.id) logger.d("chatMessageOrNull: $chatMessageOrNull") @@ -474,30 +493,12 @@ abstract class MarmotOutboundDao( ) ) - database.marmotGroupEventDao().upsert( - MarmotGroupEvent( - id = groupEvent.id, - createdAt = Instant.fromEpochSeconds(groupEvent.createdAt), - expiresAt = groupEvent.expiration()?.let { Instant.fromEpochSeconds(it) }, - encryptedContent = groupEvent.encryptedContent(), - chatRoomId = localChatRoom.chatRoom.id, - userPublicKey = groupEvent.pubKey, - publicKey = groupEvent.pubKey, - signature = groupEvent.sig, - ) - ) database.chatMessageDao().upsert( chatMessage.copy( marmotGroupEventId = groupEvent.id ) ) - database.marmotInnerEventDao().upsert( - marmotInnerEvent.copy( - marmotGroupEventId = groupEvent.id - ) - ) - // Sync broadcast to all the required relays... val broadcastNostrEventRequestIds = database.broadcastNostrEventRequestDao().insert( Relays.DefaultDMRelayList.map { // TODO: Get these from localChatRoom... diff --git a/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/composable/AddTranslationScreen.kt b/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/composable/AddTranslationScreen.kt index 18e2b45f..2463485c 100644 --- a/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/composable/AddTranslationScreen.kt +++ b/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/composable/AddTranslationScreen.kt @@ -41,7 +41,10 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.quartz.nip01Core.core.HexKey +import press.mantra.compose.database.model.ChatRoom import press.mantra.compose.database.model.MantraArtifact +import press.mantra.compose.database.model.intermdiate.LocalChatRoom +import press.mantra.compose.repository.ChatRepository import press.mantra.compose.repository.MantraRepository import press.mantra.compose.ui.composable.navigation.routes.ArtifactDetailRoute import press.mantra.compose.ui.composable.navigation.routes.Route @@ -59,6 +62,7 @@ fun AddTranslationScreen( relayHint: String?, initialAddTranslationUIState: AddTranslationUIState = AddTranslationUIState.Loading, mantraRepository: MantraRepository, + chatRepository: ChatRepository, onNavigateToRoute: (Route) -> Unit, onNavigateBack: () -> Unit, ) { @@ -70,6 +74,7 @@ fun AddTranslationScreen( relayHint = relayHint, initialAddTranslationUIState = initialAddTranslationUIState, mantraRepository = mantraRepository, + chatRepository = chatRepository ) ) @@ -114,6 +119,7 @@ fun AddTranslationScreen( ExtendedFloatingActionButton( onClick = { addTranslationViewModel.addTranslation( + localChatRoom = addTranslationUIState.localChatRoom, existingDialectId = selectedDialectId, dialectNameField = dialectNameFieldState, dialectCountryField = dialectCountryFieldState, @@ -258,6 +264,16 @@ private fun AddTranslationScreenPreview() { chatRoomId = "chatRoomId", relayHint = null, initialAddTranslationUIState = AddTranslationUIState.Loaded( + localChatRoom = LocalChatRoom( + chatRoom = ChatRoom( + id = "", + userPublicKey = "", + subject = "Message title", + description = "See something. Say somethin", + initialGiftWrapPayloadId = "sdfaer", + mlsGroupState = null + ), + ), artifact = MantraArtifact( id = "artifactId", publicKey = "author", @@ -271,6 +287,7 @@ private fun AddTranslationScreenPreview() { ) ), mantraRepository = MantraRepository.NO_OP_MANTRA_REPOSITORY, + chatRepository = ChatRepository.NO_OP_CHAT_REPOSITORY, onNavigateToRoute = {}, onNavigateBack = {} ) diff --git a/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/composable/navigation/MantraNavHost.kt b/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/composable/navigation/MantraNavHost.kt index a139f24d..9f9d602c 100644 --- a/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/composable/navigation/MantraNavHost.kt +++ b/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/composable/navigation/MantraNavHost.kt @@ -809,6 +809,7 @@ fun MantraNavHost( chatRoomId = route.chatRoomId, relayHint = route.relayHint, mantraRepository = databaseMantraRepository, + chatRepository = databaseChatRepository, onNavigateToRoute = { actionRoute -> // Replace this screen and the stale artifact detail beneath it // so we land on a freshly-loaded detail showing the new translation. diff --git a/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/view/model/AddTranslationViewModel.kt b/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/view/model/AddTranslationViewModel.kt index 61a9ad32..b0ed542e 100644 --- a/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/view/model/AddTranslationViewModel.kt +++ b/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/view/model/AddTranslationViewModel.kt @@ -16,7 +16,11 @@ import com.vitorpamplona.quartz.nip01Core.core.HexKey import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.IO import kotlinx.coroutines.launch +import press.mantra.compose.database.model.ChatRoom +import press.mantra.compose.database.model.intermdiate.LocalChatRoom +import press.mantra.compose.repository.ChatRepository import press.mantra.compose.repository.MantraRepository +import press.mantra.compose.repository.MarmotRepository import press.mantra.compose.ui.view.state.AddTranslationUIState class AddTranslationViewModel( @@ -26,6 +30,7 @@ class AddTranslationViewModel( val relayHint: String?, initialAddTranslationUIState: AddTranslationUIState, val mantraRepository: MantraRepository, + val chatRepository: ChatRepository, ): ViewModel() { var addTranslationUIState: AddTranslationUIState by mutableStateOf(initialAddTranslationUIState) @@ -38,11 +43,13 @@ class AddTranslationViewModel( fun initiateAddTranslation() { viewModelScope.launch(Dispatchers.IO) { val artifact = mantraRepository.getArtifact(artifactId) - addTranslationUIState = if (artifact == null) { + val localChatRoom = chatRepository.getChatRoomByIdentifier(chatRoomId) + addTranslationUIState = if (artifact == null || localChatRoom == null) { AddTranslationUIState.Error("Couldn't find the artifact") } else { AddTranslationUIState.Loaded( artifact = artifact, + localChatRoom = localChatRoom, dialects = mantraRepository.getDialects(chatRoomId), ) } @@ -50,6 +57,7 @@ class AddTranslationViewModel( } fun addTranslation( + localChatRoom: LocalChatRoom, existingDialectId: HexKey?, dialectNameField: TextFieldState, dialectCountryField: TextFieldState, @@ -81,7 +89,7 @@ class AddTranslationViewModel( // Reuse the selected dialect, or create a new one for the translation. val dialectId = if (creatingNewDialect) { mantraRepository.addDialect( - , + localChatRoom = localChatRoom, name = dialectName, country = dialectCountry, language = dialectLanguage, @@ -130,6 +138,7 @@ class AddTranslationViewModel( relayHint: String?, initialAddTranslationUIState: AddTranslationUIState = AddTranslationUIState.Loading, mantraRepository: MantraRepository, + chatRepository: ChatRepository ): ViewModelProvider.Factory = viewModelFactory { initializer { AddTranslationViewModel( @@ -139,6 +148,7 @@ class AddTranslationViewModel( relayHint = relayHint, initialAddTranslationUIState = initialAddTranslationUIState, mantraRepository = mantraRepository, + chatRepository = chatRepository ) } } diff --git a/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/view/state/AddTranslationUIState.kt b/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/view/state/AddTranslationUIState.kt index 88b2922a..960a95f5 100644 --- a/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/view/state/AddTranslationUIState.kt +++ b/composeApp/src/commonMain/kotlin/press/mantra/compose/ui/view/state/AddTranslationUIState.kt @@ -2,9 +2,11 @@ package press.mantra.compose.ui.view.state import press.mantra.compose.database.model.MantraArtifact import press.mantra.compose.database.model.MantraDialect +import press.mantra.compose.database.model.intermdiate.LocalChatRoom sealed interface AddTranslationUIState { data class Loaded( + val localChatRoom: LocalChatRoom, val artifact: MantraArtifact, val dialects: List = emptyList(), ): AddTranslationUIState