Improve sync logic
This commit is contained in:
1 parent
1c8b280673
commit
ee8e544ff6
3 files changed
+54
-38
No files matched your search
+3
-1
@@ -432,7 +432,9 @@ class DatabaseNostrRepository(
|
|||||||
storeNostrEventMutex.withLock {
|
storeNostrEventMutex.withLock {
|
||||||
logger.d("saveNostrEvent: $nostrEvent")
|
logger.d("saveNostrEvent: $nostrEvent")
|
||||||
database.nostrDao().storeNostrEvent(
|
database.nostrDao().storeNostrEvent(
|
||||||
nostrEvent,
|
nostrEvent.copy(
|
||||||
|
broadcastedAt = nostrEvent.createdAt
|
||||||
|
),
|
||||||
synchronizationRelayURLs = synchronizationRelayURLs,
|
synchronizationRelayURLs = synchronizationRelayURLs,
|
||||||
level = synchronizeNostrEventRequest.level
|
level = synchronizeNostrEventRequest.level
|
||||||
)
|
)
|
||||||
|
|||||||
+3
@@ -75,6 +75,9 @@ interface NostrRepository {
|
|||||||
|
|
||||||
suspend fun saveBroadcastReceipt(broadcastNostrEventReceipt: BroadcastNostrEventReceipt)
|
suspend fun saveBroadcastReceipt(broadcastNostrEventReceipt: BroadcastNostrEventReceipt)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save nostrEvent from relays (and index for local viewing)...
|
||||||
|
*/
|
||||||
suspend fun saveNostrEvent(
|
suspend fun saveNostrEvent(
|
||||||
nostrEvent: NostrEvent,
|
nostrEvent: NostrEvent,
|
||||||
synchronizeNostrEventRequest: SynchronizeNostrEventRequest,
|
synchronizeNostrEventRequest: SynchronizeNostrEventRequest,
|
||||||
|
|||||||
+48
-37
@@ -2,6 +2,7 @@ package ac.cord.auxiliary.compose.ui.view.model
|
|||||||
|
|
||||||
import ac.cord.auxiliary.compose.database.model.BroadcastNostrEventRequest
|
import ac.cord.auxiliary.compose.database.model.BroadcastNostrEventRequest
|
||||||
import ac.cord.auxiliary.compose.database.model.NostrEvent
|
import ac.cord.auxiliary.compose.database.model.NostrEvent
|
||||||
|
import ac.cord.auxiliary.compose.database.model.SynchronizeNostrEventRequest
|
||||||
import ac.cord.auxiliary.compose.managers.SeedManager
|
import ac.cord.auxiliary.compose.managers.SeedManager
|
||||||
import ac.cord.auxiliary.compose.network.dto.RelayDTO
|
import ac.cord.auxiliary.compose.network.dto.RelayDTO
|
||||||
import ac.cord.auxiliary.compose.network.relays.RelayPool.Companion.PUBLISH_TIMEOUT
|
import ac.cord.auxiliary.compose.network.relays.RelayPool.Companion.PUBLISH_TIMEOUT
|
||||||
@@ -40,6 +41,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged
|
|||||||
import kotlinx.coroutines.flow.getAndUpdate
|
import kotlinx.coroutines.flow.getAndUpdate
|
||||||
import kotlinx.coroutines.flow.timeout
|
import kotlinx.coroutines.flow.timeout
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlin.time.Clock
|
||||||
import kotlin.time.Duration.Companion.milliseconds
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
import kotlin.time.Instant
|
import kotlin.time.Instant
|
||||||
|
|
||||||
@@ -303,19 +305,28 @@ class NavigationViewModel(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
is NostrIncomingMessage.NegentropyError -> {
|
is NostrIncomingMessage.NegentropyError -> {
|
||||||
// TODO: Handle negentropy error...
|
logger.d("Negentropy Erroy (need to synchronize like normal): ${nostrIncomingMessage.negentropyReason}")
|
||||||
// nostrRepository.queueSynchronizeNostrEvent(
|
|
||||||
// Relays.eventPublishRelaySet.take(1).map { normalizedRelay -> // TODO: Sync from all the publish relays...
|
val since = negentropySynchronizeRequest.synchronizationFilter.since // TODO: Update since -> until based on what we have in the local db...
|
||||||
// SynchronizeNostrEventRequest(
|
val until = negentropySynchronizeRequest.synchronizationFilter.until
|
||||||
// purpose = "feed",
|
|
||||||
// synchronizationFilters = arrayOf(
|
val synchronizationFilter = negentropySynchronizeRequest.synchronizationFilter.copy(
|
||||||
// synchronizationFilter
|
since = since,
|
||||||
// ),
|
until = until
|
||||||
// relayURL = normalizedRelay.url,
|
)
|
||||||
// level = 0,
|
|
||||||
// )
|
nostrRepository.queueSynchronizeNostrEvent(
|
||||||
// }
|
listOf(
|
||||||
// )
|
SynchronizeNostrEventRequest(
|
||||||
|
purpose = negentropySynchronizeRequest.purpose,
|
||||||
|
synchronizationFilters = arrayOf(
|
||||||
|
synchronizationFilter
|
||||||
|
),
|
||||||
|
relayURL = negentropySynchronizeRequest.relayURL,
|
||||||
|
level = negentropySynchronizeRequest.level,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
is NostrIncomingMessage.NegentropyMessage -> {
|
is NostrIncomingMessage.NegentropyMessage -> {
|
||||||
logger.d("NegentropyMessage: ${nostrIncomingMessage.negentropyMessage}")
|
logger.d("NegentropyMessage: ${nostrIncomingMessage.negentropyMessage}")
|
||||||
@@ -326,6 +337,7 @@ class NavigationViewModel(
|
|||||||
logger.d("NeedIds: ${result.needIds.map { it.toHexString() }}")
|
logger.d("NeedIds: ${result.needIds.map { it.toHexString() }}")
|
||||||
logger.d("SendIds: ${result.sendIds.map { it.toHexString() }}")
|
logger.d("SendIds: ${result.sendIds.map { it.toHexString() }}")
|
||||||
logger.d("EventsIds: ${events.map { it.id }}")
|
logger.d("EventsIds: ${events.map { it.id }}")
|
||||||
|
logger.d("Timestamp: ${events.map { it.createdAt.epochSeconds }}")
|
||||||
|
|
||||||
val broadcastNostrEventRequests = result.sendIds.map { nostrEventId ->
|
val broadcastNostrEventRequests = result.sendIds.map { nostrEventId ->
|
||||||
BroadcastNostrEventRequest(
|
BroadcastNostrEventRequest(
|
||||||
@@ -365,30 +377,29 @@ class NavigationViewModel(
|
|||||||
localBroadcastNostrEventRequest.broadcastNostrEventRequest
|
localBroadcastNostrEventRequest.broadcastNostrEventRequest
|
||||||
)
|
)
|
||||||
|
|
||||||
if (localBroadcastNostrEventRequest.nostrEvent.broadcastedAt == null) {
|
// if (localBroadcastNostrEventRequest.nostrEvent.broadcastedAt == null) {
|
||||||
scope.launch(Dispatchers.IO) {
|
// scope.launch(Dispatchers.IO) {
|
||||||
relaysSocketManager.publishEvent(
|
// relaysSocketManager.publishEvent(
|
||||||
localBroadcastNostrEventRequest.nostrEvent
|
// localBroadcastNostrEventRequest.nostrEvent
|
||||||
).timeout(PUBLISH_TIMEOUT.milliseconds).catch {
|
// ).timeout(PUBLISH_TIMEOUT.milliseconds).catch {
|
||||||
// Timeout...
|
// // Timeout...
|
||||||
}.collect { nostrPublishResult ->
|
// }.collect { nostrPublishResult ->
|
||||||
if (nostrPublishResult.error != null) {
|
// if (nostrPublishResult.error != null) {
|
||||||
logger.e("Error publishing note: $nostrPublishResult")
|
// logger.e("Error publishing note: $nostrPublishResult")
|
||||||
nostrRepository.broadcastProcessed(
|
// nostrRepository.broadcastProcessed(
|
||||||
localBroadcastNostrEventRequest.broadcastNostrEventRequest,
|
// localBroadcastNostrEventRequest.broadcastNostrEventRequest,
|
||||||
"failed"
|
// "failed"
|
||||||
)
|
// )
|
||||||
} else {
|
// } else {
|
||||||
logger.d("Nostr Publish Result: $nostrPublishResult")
|
// logger.d("Nostr Publish Result: $nostrPublishResult")
|
||||||
nostrRepository.broadcastProcessed(
|
// nostrRepository.broadcastProcessed(
|
||||||
localBroadcastNostrEventRequest.broadcastNostrEventRequest,
|
// localBroadcastNostrEventRequest.broadcastNostrEventRequest,
|
||||||
"published"
|
// "published"
|
||||||
)
|
// )
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
scope.launch(Dispatchers.IO) {
|
scope.launch(Dispatchers.IO) {
|
||||||
// Broadcast to the intended relay...
|
// Broadcast to the intended relay...
|
||||||
|
|||||||
Reference in new issue
Block a user