Bug fix for inner events.
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
@@ -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...
|
||||
|
||||
@@ -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 = {}
|
||||
)
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<MantraDialect> = emptyList(),
|
||||
): AddTranslationUIState
|
||||
|
||||
Reference in New Issue
Block a user