Lock it up
This commit is contained in:
1 parent
eadb6d3e0a
commit
f188548ea2
9 files changed
+188
-141
No files matched your search
@@ -6,6 +6,7 @@ import ac.cord.auxiliary.compose.database.model.traits.NostrEventEntity
|
|||||||
import ac.cord.auxiliary.compose.database.model.traits.SoftDeletableEntity
|
import ac.cord.auxiliary.compose.database.model.traits.SoftDeletableEntity
|
||||||
import ac.cord.auxiliary.compose.database.model.traits.TimestampedEntity
|
import ac.cord.auxiliary.compose.database.model.traits.TimestampedEntity
|
||||||
import ac.cord.auxiliary.compose.database.model.traits.UserViewableEntity
|
import ac.cord.auxiliary.compose.database.model.traits.UserViewableEntity
|
||||||
|
import ac.cord.auxiliary.compose.repository.NostrRepository
|
||||||
import ac.cord.auxiliary.compose.ui.composable.navigation.routes.NostrEventDetailRoute
|
import ac.cord.auxiliary.compose.ui.composable.navigation.routes.NostrEventDetailRoute
|
||||||
import ac.cord.auxiliary.compose.ui.composable.navigation.routes.Route
|
import ac.cord.auxiliary.compose.ui.composable.navigation.routes.Route
|
||||||
import ac.cord.auxiliary.compose.ui.composable.widgets.profile.ProfileAvatar
|
import ac.cord.auxiliary.compose.ui.composable.widgets.profile.ProfileAvatar
|
||||||
@@ -31,6 +32,8 @@ import androidx.room3.Entity
|
|||||||
import androidx.room3.ForeignKey
|
import androidx.room3.ForeignKey
|
||||||
import androidx.room3.Index
|
import androidx.room3.Index
|
||||||
import androidx.room3.PrimaryKey
|
import androidx.room3.PrimaryKey
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||||
|
import com.vitorpamplona.quartz.nip51Lists.relayLists.RelayFeedsListEvent
|
||||||
import kotlin.time.Clock
|
import kotlin.time.Clock
|
||||||
import kotlin.time.Instant
|
import kotlin.time.Instant
|
||||||
|
|
||||||
@@ -100,6 +103,32 @@ data class Profile(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun profilePublicRelays(
|
||||||
|
nostrRepository: NostrRepository
|
||||||
|
): List<NormalizedRelayUrl>? {
|
||||||
|
val relays = nostrRepository.getNostrEvent(
|
||||||
|
publicKey,
|
||||||
|
kind = RelayFeedsListEvent.KIND
|
||||||
|
)?.let { relayFeedNostrEvent ->
|
||||||
|
val relayFeedEvent = RelayFeedsListEvent(
|
||||||
|
id = relayFeedNostrEvent.id,
|
||||||
|
pubKey = relayFeedNostrEvent.pubKey,
|
||||||
|
content = relayFeedNostrEvent.content,
|
||||||
|
tags = relayFeedNostrEvent.tags,
|
||||||
|
sig = relayFeedNostrEvent.sig,
|
||||||
|
createdAt = relayFeedNostrEvent.createdAt.epochSeconds
|
||||||
|
)
|
||||||
|
|
||||||
|
relayFeedEvent.publicRelays()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (relays?.isNotEmpty() == true) {
|
||||||
|
return relays
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun RenderAsListItem(
|
fun RenderAsListItem(
|
||||||
onNavigateToEvent: (Route) -> Unit
|
onNavigateToEvent: (Route) -> Unit
|
||||||
|
|||||||
-3
@@ -390,17 +390,14 @@ class DatabaseNostrRepository(
|
|||||||
broadcastNostrEventRequest: BroadcastNostrEventRequest,
|
broadcastNostrEventRequest: BroadcastNostrEventRequest,
|
||||||
status: String
|
status: String
|
||||||
) {
|
) {
|
||||||
logger.d("Update local reference: $broadcastNostrEventRequest")
|
|
||||||
database.broadcastNostrEventRequestDao().upsert(
|
database.broadcastNostrEventRequestDao().upsert(
|
||||||
broadcastNostrEventRequest.copy(
|
broadcastNostrEventRequest.copy(
|
||||||
status = status
|
status = status
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
logger.d("$status: $broadcastNostrEventRequest")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun synchronizeNostrEventRequestProcessed(synchronizeNostrEventRequest: SynchronizeNostrEventRequest) {
|
override suspend fun synchronizeNostrEventRequestProcessed(synchronizeNostrEventRequest: SynchronizeNostrEventRequest) {
|
||||||
logger.i("Update local reference: $synchronizeNostrEventRequest")
|
|
||||||
database.synchronizeNostrEventRequestDao().upsert(
|
database.synchronizeNostrEventRequestDao().upsert(
|
||||||
synchronizeNostrEventRequest.copy(
|
synchronizeNostrEventRequest.copy(
|
||||||
status = "sent",
|
status = "sent",
|
||||||
|
|||||||
@@ -124,15 +124,11 @@ class RelayPool(
|
|||||||
|
|
||||||
suspend fun addRelaysIfMissing(relays: Set<RelayDTO>) {
|
suspend fun addRelaysIfMissing(relays: Set<RelayDTO>) {
|
||||||
relayMutex.withLock {
|
relayMutex.withLock {
|
||||||
logger.d("addRelaysIfMissing: ${relays.map { it.url }}")
|
|
||||||
val existingRelayUrls = socketClients.map { NormalizedRelayUrl(it.socketUrl).displayUrl() }
|
val existingRelayUrls = socketClients.map { NormalizedRelayUrl(it.socketUrl).displayUrl() }
|
||||||
logger.d("Existing Relays: $existingRelayUrls")
|
|
||||||
val newRelayUrls = relays.map { it.url }
|
val newRelayUrls = relays.map { it.url }
|
||||||
|
|
||||||
val toAddRelayUrls = newRelayUrls.filter { NormalizedRelayUrl(it).displayUrl() !in existingRelayUrls }.toSet()
|
val toAddRelayUrls = newRelayUrls.filter { NormalizedRelayUrl(it).displayUrl() !in existingRelayUrls }.toSet()
|
||||||
logger.d("toAddRelayUrls: $toAddRelayUrls")
|
|
||||||
val toAddSocketClients = relays.filter { it.url in toAddRelayUrls }.mapAsNostrSocketClient()
|
val toAddSocketClients = relays.filter { it.url in toAddRelayUrls }.mapAsNostrSocketClient()
|
||||||
logger.d("toAddSocketClients: ${toAddSocketClients.map { it.socketUrl }}" )
|
|
||||||
if (toAddSocketClients.isNotEmpty()) {
|
if (toAddSocketClients.isNotEmpty()) {
|
||||||
val newSocketClients = socketClients.toMutableSet().apply {
|
val newSocketClients = socketClients.toMutableSet().apply {
|
||||||
addAll(toAddSocketClients)
|
addAll(toAddSocketClients)
|
||||||
@@ -242,7 +238,6 @@ class RelayPool(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.d("socketClients: ${socketClients.map { it.socketUrl }}")
|
|
||||||
val nostrSocketClient = socketClients.find { NormalizedRelayUrl(it.socketUrl).displayUrl() == NormalizedRelayUrl(relayUrl).displayUrl() }
|
val nostrSocketClient = socketClients.find { NormalizedRelayUrl(it.socketUrl).displayUrl() == NormalizedRelayUrl(relayUrl).displayUrl() }
|
||||||
|
|
||||||
val closeSubscription = OptimizedJsonMapper.toJson(closeCmd)
|
val closeSubscription = OptimizedJsonMapper.toJson(closeCmd)
|
||||||
|
|||||||
+2
-1
@@ -79,7 +79,8 @@ fun TextNoteEventDetail(
|
|||||||
val inReplyToNostrEventFeedListViewModel: InReplyToViewModel = viewModel(
|
val inReplyToNostrEventFeedListViewModel: InReplyToViewModel = viewModel(
|
||||||
key = "replies-${localNostrEvent.nostrEvent.id}",
|
key = "replies-${localNostrEvent.nostrEvent.id}",
|
||||||
factory = InReplyToViewModel.factory(
|
factory = InReplyToViewModel.factory(
|
||||||
nostrEventId = localNostrEvent.nostrEvent.id,
|
nostrEvent = localNostrEvent.nostrEvent,
|
||||||
|
profile = localNostrEvent.profile,
|
||||||
initialFeedListUIState = FeedListUIState.Loading,
|
initialFeedListUIState = FeedListUIState.Loading,
|
||||||
nostrRepository = nostrRepository,
|
nostrRepository = nostrRepository,
|
||||||
createdAt = localNostrEvent.nostrEvent.createdAt
|
createdAt = localNostrEvent.nostrEvent.createdAt
|
||||||
|
|||||||
+1
-1
@@ -53,7 +53,7 @@ class FeedListViewModel(
|
|||||||
|
|
||||||
val isSynchronizationPending: MutableState<Boolean> = mutableStateOf(false)
|
val isSynchronizationPending: MutableState<Boolean> = mutableStateOf(false)
|
||||||
|
|
||||||
val negentropySynchronizeRequests = Relays.negentropicRelaySet.shuffled().take(1).map { normalizedRelayUrl ->
|
val negentropySynchronizeRequests = Relays.negentropicRelaySet.shuffled().map { normalizedRelayUrl ->
|
||||||
NegentropySynchronizeRequest(
|
NegentropySynchronizeRequest(
|
||||||
id = NegentropySynchronizeRequest.computeId(
|
id = NegentropySynchronizeRequest.computeId(
|
||||||
normalizedRelayUrl.url,
|
normalizedRelayUrl.url,
|
||||||
|
|||||||
+1
-1
@@ -76,7 +76,7 @@ class FollowersListViewModel(
|
|||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
// Sync Notifications... might want to also run this in the background
|
// Sync Notifications... might want to also run this in the background
|
||||||
nostrRepository.queueNegentropySynchronizeRequest(
|
nostrRepository.queueNegentropySynchronizeRequest(
|
||||||
Relays.eventPublishRelaySet.take(1).map { normalizedRelayUrl ->
|
Relays.negentropicRelaySet.shuffled().map { normalizedRelayUrl ->
|
||||||
NegentropySynchronizeRequest(
|
NegentropySynchronizeRequest(
|
||||||
id = NegentropySynchronizeRequest.computeId(
|
id = NegentropySynchronizeRequest.computeId(
|
||||||
relayURL = normalizedRelayUrl.url,
|
relayURL = normalizedRelayUrl.url,
|
||||||
|
|||||||
+1
-1
@@ -75,7 +75,7 @@ class FollowingListViewModel(
|
|||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
// Sync Notifications... might want to also run this in the background
|
// Sync Notifications... might want to also run this in the background
|
||||||
nostrRepository.queueNegentropySynchronizeRequest(
|
nostrRepository.queueNegentropySynchronizeRequest(
|
||||||
Relays.eventPublishRelaySet.take(1).map { normalizedRelayUrl ->
|
Relays.negentropicRelaySet.shuffled().map { normalizedRelayUrl ->
|
||||||
NegentropySynchronizeRequest(
|
NegentropySynchronizeRequest(
|
||||||
id = NegentropySynchronizeRequest.computeId(
|
id = NegentropySynchronizeRequest.computeId(
|
||||||
relayURL = normalizedRelayUrl.url,
|
relayURL = normalizedRelayUrl.url,
|
||||||
|
|||||||
+16
-5
@@ -1,6 +1,9 @@
|
|||||||
package ac.cord.auxiliary.compose.ui.view.model
|
package ac.cord.auxiliary.compose.ui.view.model
|
||||||
|
|
||||||
import ac.cord.auxiliary.compose.database.model.NegentropySynchronizeRequest
|
import ac.cord.auxiliary.compose.database.model.NegentropySynchronizeRequest
|
||||||
|
import ac.cord.auxiliary.compose.database.model.NostrEvent
|
||||||
|
import ac.cord.auxiliary.compose.database.model.NostrEventRelay
|
||||||
|
import ac.cord.auxiliary.compose.database.model.Profile
|
||||||
import ac.cord.auxiliary.compose.database.model.SynchronizeNostrEventRequest
|
import ac.cord.auxiliary.compose.database.model.SynchronizeNostrEventRequest
|
||||||
import ac.cord.auxiliary.compose.database.model.types.SynchronizationFilter
|
import ac.cord.auxiliary.compose.database.model.types.SynchronizationFilter
|
||||||
import ac.cord.auxiliary.compose.nostr.Relays
|
import ac.cord.auxiliary.compose.nostr.Relays
|
||||||
@@ -37,6 +40,7 @@ import co.touchlab.kermit.Logger
|
|||||||
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
|
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
|
||||||
import com.vitorpamplona.quartz.nip18Reposts.RepostEvent
|
import com.vitorpamplona.quartz.nip18Reposts.RepostEvent
|
||||||
import com.vitorpamplona.quartz.nip25Reactions.ReactionEvent
|
import com.vitorpamplona.quartz.nip25Reactions.ReactionEvent
|
||||||
|
import com.vitorpamplona.quartz.nip51Lists.relayLists.RelayFeedsListEvent
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.IO
|
import kotlinx.coroutines.IO
|
||||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
@@ -44,7 +48,8 @@ import kotlinx.coroutines.launch
|
|||||||
import kotlin.time.Instant
|
import kotlin.time.Instant
|
||||||
|
|
||||||
class InReplyToViewModel(
|
class InReplyToViewModel(
|
||||||
nostrEventId: String,
|
val nostrEvent: NostrEvent,
|
||||||
|
val profile: Profile?,
|
||||||
initialFeedListUIState: FeedListUIState,
|
initialFeedListUIState: FeedListUIState,
|
||||||
val synchronizationFilter: SynchronizationFilter,
|
val synchronizationFilter: SynchronizationFilter,
|
||||||
val nostrRepository: NostrRepository
|
val nostrRepository: NostrRepository
|
||||||
@@ -79,8 +84,12 @@ class InReplyToViewModel(
|
|||||||
logger.d("scheduleSynchronization")
|
logger.d("scheduleSynchronization")
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
// Sync Notifications... might want to also run this in the background
|
// Sync Notifications... might want to also run this in the background
|
||||||
|
val publicRelays = profile?.profilePublicRelays(
|
||||||
|
nostrRepository
|
||||||
|
) ?: Relays.negentropicRelaySet
|
||||||
|
|
||||||
nostrRepository.queueNegentropySynchronizeRequest(
|
nostrRepository.queueNegentropySynchronizeRequest(
|
||||||
Relays.eventPublishRelaySet.take(1).map { normalizedRelayUrl ->
|
publicRelays.take(3).map { normalizedRelayUrl ->
|
||||||
NegentropySynchronizeRequest(
|
NegentropySynchronizeRequest(
|
||||||
id = NegentropySynchronizeRequest.computeId(
|
id = NegentropySynchronizeRequest.computeId(
|
||||||
relayURL = normalizedRelayUrl.url,
|
relayURL = normalizedRelayUrl.url,
|
||||||
@@ -193,7 +202,8 @@ class InReplyToViewModel(
|
|||||||
const val TAG = "InReplyToViewModel"
|
const val TAG = "InReplyToViewModel"
|
||||||
|
|
||||||
fun factory(
|
fun factory(
|
||||||
nostrEventId: String,
|
nostrEvent: NostrEvent,
|
||||||
|
profile: Profile?,
|
||||||
createdAt: Instant,
|
createdAt: Instant,
|
||||||
initialFeedListUIState: FeedListUIState = FeedListUIState.Loading,
|
initialFeedListUIState: FeedListUIState = FeedListUIState.Loading,
|
||||||
synchronizationFilter: SynchronizationFilter = SynchronizationFilter(
|
synchronizationFilter: SynchronizationFilter = SynchronizationFilter(
|
||||||
@@ -204,7 +214,7 @@ class InReplyToViewModel(
|
|||||||
),
|
),
|
||||||
tags = mapOf(
|
tags = mapOf(
|
||||||
"e" to listOf(
|
"e" to listOf(
|
||||||
nostrEventId
|
nostrEvent.id
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
since = createdAt
|
since = createdAt
|
||||||
@@ -214,7 +224,8 @@ class InReplyToViewModel(
|
|||||||
initializer {
|
initializer {
|
||||||
InReplyToViewModel(
|
InReplyToViewModel(
|
||||||
initialFeedListUIState = initialFeedListUIState,
|
initialFeedListUIState = initialFeedListUIState,
|
||||||
nostrEventId = nostrEventId,
|
nostrEvent = nostrEvent,
|
||||||
|
profile = profile,
|
||||||
synchronizationFilter = synchronizationFilter,
|
synchronizationFilter = synchronizationFilter,
|
||||||
nostrRepository = nostrRepository
|
nostrRepository = nostrRepository
|
||||||
)
|
)
|
||||||
|
|||||||
+138
-124
@@ -43,6 +43,8 @@ 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 kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlin.time.Duration.Companion.milliseconds
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
import kotlin.time.Instant
|
import kotlin.time.Instant
|
||||||
|
|
||||||
@@ -62,6 +64,8 @@ class NavigationViewModel(
|
|||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "NavigationViewModel"
|
private const val TAG = "NavigationViewModel"
|
||||||
|
|
||||||
|
private val mutex = Mutex()
|
||||||
|
|
||||||
fun factory(
|
fun factory(
|
||||||
initialNavigationUIState: NavigationUIState,
|
initialNavigationUIState: NavigationUIState,
|
||||||
nostrRepository: NostrRepository,
|
nostrRepository: NostrRepository,
|
||||||
@@ -221,168 +225,178 @@ class NavigationViewModel(
|
|||||||
scope.launch(Dispatchers.IO) {
|
scope.launch(Dispatchers.IO) {
|
||||||
nostrRepository.observePendingNegentropySynchronizeRequests().distinctUntilChanged().collect { negentropySynchronizeRequestOrNull ->
|
nostrRepository.observePendingNegentropySynchronizeRequests().distinctUntilChanged().collect { negentropySynchronizeRequestOrNull ->
|
||||||
negentropySynchronizeRequestOrNull?.let { negentropySynchronizeRequest ->
|
negentropySynchronizeRequestOrNull?.let { negentropySynchronizeRequest ->
|
||||||
logger.i("negentropySynchronizeRequest: $negentropySynchronizeRequest")
|
mutex.withLock {
|
||||||
|
logger.i("negentropySynchronizeRequest: $negentropySynchronizeRequest")
|
||||||
|
|
||||||
val events = nostrRepository.getNostrFeedIds(
|
val events = nostrRepository.getNostrFeedIds(
|
||||||
arrayOf(
|
arrayOf(
|
||||||
negentropySynchronizeRequest.synchronizationFilter
|
negentropySynchronizeRequest.synchronizationFilter
|
||||||
),
|
),
|
||||||
applyLimits = false
|
applyLimits = false
|
||||||
)
|
)
|
||||||
logger.d("Events: ${events.size}")
|
logger.d("Events: ${events.size}")
|
||||||
val storage = StorageVector().apply {
|
val storage = StorageVector().apply {
|
||||||
events.forEach { event ->
|
events.forEach { event ->
|
||||||
insert(
|
insert(
|
||||||
event.createdAt.toEpochMilliseconds(),
|
event.createdAt.toEpochMilliseconds(),
|
||||||
event.id
|
event.id
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
seal()
|
||||||
}
|
}
|
||||||
|
val negentropy = Negentropy(
|
||||||
|
storage,
|
||||||
|
)
|
||||||
|
|
||||||
seal()
|
val negOpenCmd = NegOpenCmd(
|
||||||
}
|
subId = negentropySynchronizeRequest.uuid,
|
||||||
val negentropy = Negentropy(
|
filter = Filter(
|
||||||
storage,
|
ids = negentropySynchronizeRequest.synchronizationFilter.ids?.toList(),
|
||||||
)
|
authors = negentropySynchronizeRequest.synchronizationFilter.authors?.toList(),
|
||||||
|
kinds = negentropySynchronizeRequest.synchronizationFilter.kinds?.toList(),
|
||||||
|
tags = negentropySynchronizeRequest.synchronizationFilter.tags,
|
||||||
|
tagsAll = negentropySynchronizeRequest.synchronizationFilter.tagsAll,
|
||||||
|
since = negentropySynchronizeRequest.synchronizationFilter.since?.epochSeconds,
|
||||||
|
until = negentropySynchronizeRequest.synchronizationFilter.until?.epochSeconds,
|
||||||
|
limit = null, // We don't do limits when performing negentropy...
|
||||||
|
search = negentropySynchronizeRequest.synchronizationFilter.search
|
||||||
|
),
|
||||||
|
initialMessage = negentropy.initiate().toHexString()
|
||||||
|
)
|
||||||
|
logger.d("negOpenCmd: ${negOpenCmd.initialMessage}")
|
||||||
|
|
||||||
val negOpenCmd = NegOpenCmd(
|
nostrRepository.negentropySynchronizeRequestProcessed(negentropySynchronizeRequest)
|
||||||
subId = negentropySynchronizeRequest.uuid,
|
|
||||||
filter = Filter(
|
|
||||||
ids = negentropySynchronizeRequest.synchronizationFilter.ids?.toList(),
|
|
||||||
authors = negentropySynchronizeRequest.synchronizationFilter.authors?.toList(),
|
|
||||||
kinds = negentropySynchronizeRequest.synchronizationFilter.kinds?.toList(),
|
|
||||||
tags = negentropySynchronizeRequest.synchronizationFilter.tags,
|
|
||||||
tagsAll = negentropySynchronizeRequest.synchronizationFilter.tagsAll,
|
|
||||||
since = negentropySynchronizeRequest.synchronizationFilter.since?.epochSeconds,
|
|
||||||
until = negentropySynchronizeRequest.synchronizationFilter.until?.epochSeconds,
|
|
||||||
limit = null, // We don't do limits when performing negentropy...
|
|
||||||
search = negentropySynchronizeRequest.synchronizationFilter.search
|
|
||||||
),
|
|
||||||
initialMessage = negentropy.initiate().toHexString()
|
|
||||||
)
|
|
||||||
logger.d("negOpenCmd: ${negOpenCmd.initialMessage}")
|
|
||||||
|
|
||||||
nostrRepository.negentropySynchronizeRequestProcessed(negentropySynchronizeRequest)
|
scope.launch(Dispatchers.IO) {
|
||||||
|
try {
|
||||||
|
val negCloseCmd = NegCloseCmd(
|
||||||
|
subId = negentropySynchronizeRequest.uuid,
|
||||||
|
)
|
||||||
|
|
||||||
scope.launch(Dispatchers.IO) {
|
relaysSocketManager.negentropySync(
|
||||||
try {
|
negOpenCmd,
|
||||||
val negCloseCmd = NegCloseCmd(
|
negentropySynchronizeRequest.relayURL
|
||||||
subId = negentropySynchronizeRequest.uuid,
|
).collect { nostrIncomingMessage ->
|
||||||
)
|
when (nostrIncomingMessage) {
|
||||||
|
is NostrIncomingMessage.EventMessage -> {
|
||||||
|
scope.launch(Dispatchers.IO) {
|
||||||
|
logger.d("Import message: $nostrIncomingMessage")
|
||||||
|
nostrIncomingMessage.nostrEvent?.let {
|
||||||
|
nostrRepository.saveNostrEvent(
|
||||||
|
nostrEvent = it,
|
||||||
|
negentropySynchronizeRequest,
|
||||||
|
synchronizationRelayURLs = listOf(negentropySynchronizeRequest.relayURL) // TODO: + Relays.eventPublishRelaySet.map { normalizedRelayUrl -> normalizedRelayUrl.url }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is NostrIncomingMessage.EventsMessage -> {
|
||||||
|
logger.d("Import messages: $nostrIncomingMessage")
|
||||||
|
|
||||||
relaysSocketManager.negentropySync(
|
nostrIncomingMessage.nostrEvents.forEach { nostrEvent ->
|
||||||
negOpenCmd,
|
|
||||||
negentropySynchronizeRequest.relayURL
|
|
||||||
).collect { nostrIncomingMessage ->
|
|
||||||
when (nostrIncomingMessage) {
|
|
||||||
is NostrIncomingMessage.EventMessage -> {
|
|
||||||
scope.launch(Dispatchers.IO) {
|
|
||||||
logger.d("Import message: $nostrIncomingMessage")
|
|
||||||
nostrIncomingMessage.nostrEvent?.let {
|
|
||||||
nostrRepository.saveNostrEvent(
|
nostrRepository.saveNostrEvent(
|
||||||
nostrEvent = it,
|
nostrEvent = nostrEvent,
|
||||||
negentropySynchronizeRequest,
|
negentropySynchronizeRequest,
|
||||||
synchronizationRelayURLs = listOf(negentropySynchronizeRequest.relayURL) // TODO: + Relays.eventPublishRelaySet.map { normalizedRelayUrl -> normalizedRelayUrl.url }
|
synchronizationRelayURLs = listOf(negentropySynchronizeRequest.relayURL) // TODO: + Relays.eventPublishRelaySet.map { normalizedRelayUrl -> normalizedRelayUrl.url }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
is NostrIncomingMessage.EoseMessage -> {
|
||||||
is NostrIncomingMessage.EventsMessage -> {
|
logger.d("Sync request has been successfully processed (${negentropySynchronizeRequest.relayURL}): $nostrIncomingMessage")
|
||||||
logger.d("Import messages: $nostrIncomingMessage")
|
|
||||||
|
|
||||||
nostrIncomingMessage.nostrEvents.forEach { nostrEvent ->
|
relaysSocketManager.closeNegentropySync(
|
||||||
nostrRepository.saveNostrEvent(
|
negCloseCmd,
|
||||||
nostrEvent = nostrEvent,
|
negentropySynchronizeRequest.relayURL
|
||||||
negentropySynchronizeRequest,
|
|
||||||
synchronizationRelayURLs = listOf(negentropySynchronizeRequest.relayURL) // TODO: + Relays.eventPublishRelaySet.map { normalizedRelayUrl -> normalizedRelayUrl.url }
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return@collect
|
||||||
}
|
}
|
||||||
}
|
is NostrIncomingMessage.NegentropyError -> {
|
||||||
is NostrIncomingMessage.EoseMessage -> {
|
logger.d("Negentropy Error (need to synchronize like normal): ${nostrIncomingMessage.negentropyReason}")
|
||||||
logger.d("Sync request has been successfully processed (${negentropySynchronizeRequest.relayURL}): $nostrIncomingMessage")
|
|
||||||
|
|
||||||
relaysSocketManager.closeNegentropySync(
|
// TODO: Don't schedule a sync when on mobile internet.
|
||||||
negCloseCmd,
|
val since = negentropySynchronizeRequest.synchronizationFilter.since // TODO: Update since -> until based on what we have in the local db...
|
||||||
negentropySynchronizeRequest.relayURL
|
val until = negentropySynchronizeRequest.synchronizationFilter.until
|
||||||
)
|
|
||||||
}
|
|
||||||
is NostrIncomingMessage.NegentropyError -> {
|
|
||||||
logger.d("Negentropy Error (need to synchronize like normal): ${nostrIncomingMessage.negentropyReason}")
|
|
||||||
|
|
||||||
// TODO: Don't schedule a sync when on mobile internet.
|
val synchronizationFilter = negentropySynchronizeRequest.synchronizationFilter.copy(
|
||||||
val since = negentropySynchronizeRequest.synchronizationFilter.since // TODO: Update since -> until based on what we have in the local db...
|
since = since,
|
||||||
val until = negentropySynchronizeRequest.synchronizationFilter.until
|
until = until
|
||||||
|
|
||||||
val synchronizationFilter = negentropySynchronizeRequest.synchronizationFilter.copy(
|
|
||||||
since = since,
|
|
||||||
until = until
|
|
||||||
)
|
|
||||||
|
|
||||||
nostrRepository.queueSynchronizeNostrEvent(
|
|
||||||
listOf(
|
|
||||||
SynchronizeNostrEventRequest(
|
|
||||||
purpose = negentropySynchronizeRequest.purpose,
|
|
||||||
synchronizationFilters = arrayOf(
|
|
||||||
synchronizationFilter
|
|
||||||
),
|
|
||||||
relayURL = negentropySynchronizeRequest.relayURL,
|
|
||||||
level = negentropySynchronizeRequest.level,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
is NostrIncomingMessage.NegentropyMessage -> {
|
|
||||||
logger.d("NegentropyMessage: ${nostrIncomingMessage.negentropyMessage}")
|
|
||||||
|
|
||||||
val result = negentropy.reconcile(
|
|
||||||
nostrIncomingMessage.negentropyMessage.hexToByteArray()
|
|
||||||
)
|
|
||||||
logger.d("NeedIds: ${result.needIds.map { it.toHexString() }}")
|
|
||||||
logger.d("SendIds: ${result.sendIds.map { it.toHexString() }}")
|
|
||||||
logger.d("EventsIds: ${events.map { it.id }}")
|
|
||||||
logger.d("Timestamp: ${events.map { it.createdAt.epochSeconds }}")
|
|
||||||
|
|
||||||
if (result.needIds.isNotEmpty()) {
|
|
||||||
// Schedule a sync from this relay...
|
|
||||||
nostrRepository.queueSynchronizeNostrEvent(
|
nostrRepository.queueSynchronizeNostrEvent(
|
||||||
listOf(
|
listOf(
|
||||||
SynchronizeNostrEventRequest(
|
SynchronizeNostrEventRequest(
|
||||||
purpose = negentropySynchronizeRequest.purpose,
|
purpose = negentropySynchronizeRequest.purpose,
|
||||||
synchronizationFilters = arrayOf(
|
synchronizationFilters = arrayOf(
|
||||||
SynchronizationFilter(
|
synchronizationFilter
|
||||||
ids = result.needIds.map { it.toHexString() }.toTypedArray()
|
|
||||||
)
|
|
||||||
),
|
),
|
||||||
relayURL = negentropySynchronizeRequest.relayURL,
|
relayURL = negentropySynchronizeRequest.relayURL,
|
||||||
level = negentropySynchronizeRequest.level,
|
level = negentropySynchronizeRequest.level,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
return@collect
|
||||||
}
|
}
|
||||||
val broadcastNostrEventRequests = result.sendIds.map { nostrEventId ->
|
is NostrIncomingMessage.NegentropyMessage -> {
|
||||||
BroadcastNostrEventRequest(
|
logger.d("NegentropyMessage: ${nostrIncomingMessage.negentropyMessage}")
|
||||||
nostrEventId = nostrEventId.toHexString(),
|
|
||||||
relayURL = negentropySynchronizeRequest.relayURL
|
|
||||||
)
|
|
||||||
}
|
|
||||||
nostrRepository.scheduleBroadcastNostrEventRequests(
|
|
||||||
broadcastNostrEventRequests
|
|
||||||
)
|
|
||||||
|
|
||||||
relaysSocketManager.closeNegentropySync(
|
val result = negentropy.reconcile(
|
||||||
negCloseCmd,
|
nostrIncomingMessage.negentropyMessage.hexToByteArray()
|
||||||
negentropySynchronizeRequest.relayURL
|
)
|
||||||
)
|
logger.d("NeedIds: ${result.needIds.map { it.toHexString() }}")
|
||||||
}
|
logger.d("SendIds: ${result.sendIds.map { it.toHexString() }}")
|
||||||
else -> {
|
logger.d("EventsIds: ${events.map { it.id }}")
|
||||||
logger.d("Unhandled message ${negentropySynchronizeRequest.relayURL}: $nostrIncomingMessage")
|
logger.d("Timestamp: ${events.map { it.createdAt.epochSeconds }}")
|
||||||
|
|
||||||
|
if (result.needIds.isNotEmpty()) {
|
||||||
|
// Schedule a sync from this relay...
|
||||||
|
nostrRepository.queueSynchronizeNostrEvent(
|
||||||
|
listOf(
|
||||||
|
SynchronizeNostrEventRequest(
|
||||||
|
purpose = negentropySynchronizeRequest.purpose,
|
||||||
|
synchronizationFilters = arrayOf(
|
||||||
|
SynchronizationFilter(
|
||||||
|
ids = result.needIds.map { it.toHexString() }.toTypedArray()
|
||||||
|
)
|
||||||
|
),
|
||||||
|
relayURL = negentropySynchronizeRequest.relayURL,
|
||||||
|
level = negentropySynchronizeRequest.level,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val broadcastNostrEventRequests = result.sendIds.map { nostrEventId ->
|
||||||
|
BroadcastNostrEventRequest(
|
||||||
|
nostrEventId = nostrEventId.toHexString(),
|
||||||
|
relayURL = negentropySynchronizeRequest.relayURL
|
||||||
|
)
|
||||||
|
}
|
||||||
|
nostrRepository.scheduleBroadcastNostrEventRequests(
|
||||||
|
broadcastNostrEventRequests
|
||||||
|
)
|
||||||
|
|
||||||
|
relaysSocketManager.closeNegentropySync(
|
||||||
|
negCloseCmd,
|
||||||
|
negentropySynchronizeRequest.relayURL
|
||||||
|
)
|
||||||
|
|
||||||
|
return@collect
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
logger.d("Unhandled message ${negentropySynchronizeRequest.relayURL}: $nostrIncomingMessage")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.d("Queried Sync")
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
logger.e("Failed to sync", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
logger.e("Failed to sync", e)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.i("After launch")
|
||||||
}
|
}
|
||||||
|
logger.i("Lock released")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user