From ec919f5d9f2886717df8cab4f5546f2d095d636d Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Tue, 7 Jul 2026 02:40:48 +0200 Subject: [PATCH] Bug fix on broadcast --- .../dao/BroadcastNostrEventRequestDao.kt | 3 +- .../repository/DatabaseNostrRepository.kt | 3 +- .../compose/repository/NostrRepository.kt | 4 +- .../ui/view/model/SynchronizationViewModel.kt | 52 +++++++++---------- 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/BroadcastNostrEventRequestDao.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/BroadcastNostrEventRequestDao.kt index e46e97c2..ef4c0db0 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/BroadcastNostrEventRequestDao.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/BroadcastNostrEventRequestDao.kt @@ -4,6 +4,7 @@ import androidx.room3.Dao import androidx.room3.Insert import androidx.room3.Query import androidx.room3.Upsert +import at.torch.compose.database.model.intermdiate.LocalBroadcastNostrEventRequest import kotlinx.coroutines.flow.Flow import kotlin.time.Clock import kotlin.time.Instant @@ -14,7 +15,7 @@ interface BroadcastNostrEventRequestDao { fun getAllBroadcastNostrEventRequests(): List @Query("SELECT * FROM BroadcastNostrEventRequest WHERE status = :status AND createdAt > :createdAt") - fun observeBroadcastNostrEventRequestsByStatus(status: String, createdAt: Instant = Clock.System.now()): Flow> + fun observeBroadcastNostrEventRequestsByStatus(status: String, createdAt: Instant = Clock.System.now()): Flow @Query("SELECT * FROM BroadcastNostrEventRequest WHERE nostrEventId = :nostrEventId") fun observeBroadcastNostrEventRequestByNostrEventId(nostrEventId: String): Flow diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/database/repository/DatabaseNostrRepository.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/database/repository/DatabaseNostrRepository.kt index 4d94f038..21e4ceab 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/database/repository/DatabaseNostrRepository.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/database/repository/DatabaseNostrRepository.kt @@ -5,6 +5,7 @@ import at.torch.compose.database.model.BroadcastNostrEventRequest import at.torch.compose.database.model.NostrEvent import at.torch.compose.database.model.UnsignedNostrEvent import at.torch.compose.database.model.intermdiate.LocalAccount +import at.torch.compose.database.model.intermdiate.LocalBroadcastNostrEventRequest import at.torch.compose.database.model.types.SynchronizationFilter import at.torch.compose.nostr.Relays import co.touchlab.kermit.Logger @@ -84,7 +85,7 @@ class DatabaseNostrRepository( return database.unsignedNostrEventDao().observeUnsignedNostrEvents(publicKey) } - override suspend fun observePendingBroadcastNostrEventRequests(): Flow> { + override suspend fun observePendingBroadcastNostrEventRequests(): Flow { return database.broadcastNostrEventRequestDao().observeBroadcastNostrEventRequestsByStatus("pending") } diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/repository/NostrRepository.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/repository/NostrRepository.kt index 64ce8dad..af4b14a4 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/repository/NostrRepository.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/repository/NostrRepository.kt @@ -32,7 +32,7 @@ interface NostrRepository { suspend fun observeUnsignedNostrEvents(publicKey: HexKey): Flow - suspend fun observePendingBroadcastNostrEventRequests(): Flow> + suspend fun observePendingBroadcastNostrEventRequests(): Flow suspend fun observePendingSynchronizeNostrEventRequests(): Flow @@ -161,7 +161,7 @@ interface NostrRepository { TODO("Not yet implemented") } - override suspend fun observePendingBroadcastNostrEventRequests(): Flow> { + override suspend fun observePendingBroadcastNostrEventRequests(): Flow { TODO("Not yet implemented") } diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/SynchronizationViewModel.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/SynchronizationViewModel.kt index 4dcd30e4..21334ae1 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/SynchronizationViewModel.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/SynchronizationViewModel.kt @@ -387,36 +387,37 @@ class SynchronizationViewModel( logger.i { "observePendingBroadcastNostrEventRequests" } scope.launch(Dispatchers.IO) { - nostrRepository.observePendingBroadcastNostrEventRequests().distinctUntilChanged().collect { localBroadcastNostrEventRequests -> - localBroadcastNostrEventRequests.forEach { localBroadcastNostrEventRequest -> + nostrRepository.observePendingBroadcastNostrEventRequests().distinctUntilChanged().collect { localBroadcastNostrEventRequest -> + logger.d("localBroadcastNostrEventRequest: $localBroadcastNostrEventRequest") + if (localBroadcastNostrEventRequest != null) { nostrRepository.broadcastProcessed( localBroadcastNostrEventRequest.broadcastNostrEventRequest ) - if (localBroadcastNostrEventRequest.nostrEvent.broadcastedAt == null) { - scope.launch(Dispatchers.IO) { - relaysSocketManager.publishEvent( - localBroadcastNostrEventRequest.nostrEvent - ).timeout(RelayPool.PUBLISH_TIMEOUT.milliseconds).catch { - // Timeout... - }.collect { nostrPublishResult -> - if (nostrPublishResult.error != null) { - logger.e("Error publishing note: $nostrPublishResult") - nostrRepository.broadcastProcessed( - localBroadcastNostrEventRequest.broadcastNostrEventRequest, - "failed" - ) - } else { - logger.d("Nostr Publish Result: $nostrPublishResult") - nostrRepository.broadcastProcessed( - localBroadcastNostrEventRequest.broadcastNostrEventRequest, - "published" // TODO: Might want to pass the nostrPublishResult.result - ) - } - } - } - } +// if (localBroadcastNostrEventRequest.nostrEvent.broadcastedAt == null) { +// scope.launch(Dispatchers.IO) { +// relaysSocketManager.publishEvent( +// localBroadcastNostrEventRequest.nostrEvent +// ).timeout(RelayPool.PUBLISH_TIMEOUT.milliseconds).catch { +// // Timeout... +// }.collect { nostrPublishResult -> +// if (nostrPublishResult.error != null) { +// logger.e("Error publishing note: $nostrPublishResult") +// nostrRepository.broadcastProcessed( +// localBroadcastNostrEventRequest.broadcastNostrEventRequest, +// "failed" +// ) +// } else { +// logger.d("Nostr Publish Result: $nostrPublishResult") +// nostrRepository.broadcastProcessed( +// localBroadcastNostrEventRequest.broadcastNostrEventRequest, +// "published" // TODO: Might want to pass the nostrPublishResult.result +// ) +// } +// } +// } +// } scope.launch(Dispatchers.IO) { // Broadcast to the intended relay... @@ -443,7 +444,6 @@ class SynchronizationViewModel( } } } - } } }