refactor(search): extract thread-safe EventDeduplicator, unify dedup

Replace raw MutableSet with synchronized EventDeduplicator class.
Remove dual dedup in addNoteResults — trackRelayEvent now gates all
event processing. SearchScreen callbacks skip dupes early via return
value.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
nrobi144
2026-03-11 11:36:39 +02:00
parent 979d07679c
commit 3c6c368582
7 changed files with 390 additions and 78 deletions
@@ -45,5 +45,6 @@ object DefaultRelays {
"wss://nos.lol",
"wss://relay.snort.social",
"wss://nostr.wine",
"wss://relay.noswhere.com",
)
}
@@ -145,6 +145,7 @@ fun createSearchPeopleSubscription(
limit: Int = 50,
onEvent: (Event, Boolean, NormalizedRelayUrl, List<Filter>?) -> Unit,
onEose: (NormalizedRelayUrl, List<Filter>?) -> Unit = { _, _ -> },
onClosed: (NormalizedRelayUrl, String, List<Filter>?) -> Unit = { _, _, _ -> },
): SubscriptionConfig? {
if (searchQuery.isBlank()) return null
@@ -154,6 +155,7 @@ fun createSearchPeopleSubscription(
relays = relays,
onEvent = onEvent,
onEose = onEose,
onClosed = onClosed,
)
}
@@ -46,6 +46,7 @@ data class SubscriptionConfig(
val relays: Set<NormalizedRelayUrl>,
val onEvent: (Event, Boolean, NormalizedRelayUrl, List<Filter>?) -> Unit,
val onEose: (NormalizedRelayUrl, List<Filter>?) -> Unit = { _, _ -> },
val onClosed: (NormalizedRelayUrl, String, List<Filter>?) -> Unit = { _, _, _ -> },
)
/**
@@ -95,6 +96,14 @@ fun rememberSubscription(
) {
cfg.onEose(relay, forFilters)
}
override fun onClosed(
message: String,
relay: NormalizedRelayUrl,
forFilters: List<Filter>?,
) {
cfg.onClosed(relay, message, forFilters)
}
},
)
}
@@ -27,7 +27,6 @@ import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
@@ -70,7 +69,6 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.input.key.Key
@@ -82,6 +80,7 @@ import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import com.vitorpamplona.amethyst.commons.chess.RelaySyncStatus
import com.vitorpamplona.amethyst.commons.model.User
import com.vitorpamplona.amethyst.commons.search.AdvancedSearchBarState
import com.vitorpamplona.amethyst.commons.search.QuerySerializer
@@ -102,6 +101,7 @@ import com.vitorpamplona.amethyst.desktop.subscriptions.generateSubId
import com.vitorpamplona.amethyst.desktop.subscriptions.rememberSubscription
import com.vitorpamplona.amethyst.desktop.ui.search.AdvancedSearchPanel
import com.vitorpamplona.amethyst.desktop.ui.search.SearchResultsList
import com.vitorpamplona.amethyst.desktop.ui.search.SearchSyncBanner
import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent
import com.vitorpamplona.quartz.nip19Bech32.decodePublicKeyAsHexOrNull
@@ -137,6 +137,7 @@ fun SearchScreen(
val isSearching by state.isSearching.collectAsState()
val peopleResults by state.peopleResults.collectAsState()
val noteResults by state.noteResults.collectAsState()
val relayStates by state.relayStates.collectAsState()
// Bech32 parsing (immediate, no debounce)
val bech32Results = remember(displayText) { parseSearchInput(displayText) }
@@ -145,8 +146,12 @@ fun SearchScreen(
LaunchedEffect(debouncedQuery) {
if (!debouncedQuery.isEmpty && bech32Results.isEmpty()) {
state.clearResults()
state.startSearching()
state.startSearching()
state.initRelayStates(allRelayUrls)
state.startSearching("people-search")
state.startSearching("adv-search")
// Timeout relays that silently ignore NIP-50 (e.g. strfry)
kotlinx.coroutines.delay(10_000L)
state.timeoutWaitingRelays()
}
}
@@ -164,18 +169,25 @@ fun SearchScreen(
QuerySerializer.serialize(debouncedQuery)
},
limit = 20,
onEvent = { event, _, _, _ ->
if (event is MetadataEvent) {
localCache.consumeMetadata(event)
@Suppress("UNCHECKED_CAST")
val user = localCache.getUserIfExists(event.pubKey) as? User
if (user != null) {
state.addPeopleResult(user)
onEvent = { event, _, relay, _ ->
if (state.trackRelayEvent(relay.url, event.id)) {
if (event is MetadataEvent) {
localCache.consumeMetadata(event)
@Suppress("UNCHECKED_CAST")
val user = localCache.getUserIfExists(event.pubKey) as? User
if (user != null) {
state.addPeopleResult(user)
}
}
}
},
onEose = { _, _ ->
state.stopSearching()
onEose = { relay, _ ->
state.updateRelayState(relay.url, RelaySyncStatus.EOSE_RECEIVED)
state.stopSearching("people-search")
},
onClosed = { relay, _, _ ->
state.updateRelayState(relay.url, RelaySyncStatus.FAILED)
state.stopSearching("people-search")
},
)
}
@@ -194,15 +206,22 @@ fun SearchScreen(
subId = generateSubId("adv-search"),
filters = filters,
relays = allRelayUrls,
onEvent = { event, _, _, _ ->
onEvent = { event, _, relay, _ ->
if (event.kind == MetadataEvent.KIND) return@SubscriptionConfig
val filtered = SearchResultFilter.filter(listOf(event), debouncedQuery)
if (filtered.isNotEmpty()) {
state.addNoteResults(filtered)
if (state.trackRelayEvent(relay.url, event.id)) {
val filtered = SearchResultFilter.filter(listOf(event), debouncedQuery)
if (filtered.isNotEmpty()) {
state.addNoteResults(filtered)
}
}
},
onEose = { _, _ ->
state.stopSearching()
onEose = { relay, _ ->
state.updateRelayState(relay.url, RelaySyncStatus.EOSE_RECEIVED)
state.stopSearching("adv-search")
},
onClosed = { relay, _, _ ->
state.updateRelayState(relay.url, RelaySyncStatus.FAILED)
state.stopSearching("adv-search")
},
)
}
@@ -269,6 +288,26 @@ fun SearchScreen(
}
},
) {
// Progress bar at very top
AnimatedVisibility(
visible = isSearching,
enter = expandVertically(expandFrom = Alignment.Top) + fadeIn(),
exit = shrinkVertically(shrinkTowards = Alignment.Top) + fadeOut(),
) {
LinearProgressIndicator(
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.primary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
}
// Relay status banner
SearchSyncBanner(
relayStates = relayStates,
isSearching = isSearching,
)
// Title row
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
@@ -294,53 +333,36 @@ fun SearchScreen(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Box(modifier = Modifier.weight(1f)) {
OutlinedTextField(
value =
TextFieldValue(
text = displayText,
selection = TextRange(displayText.length),
),
onValueChange = { state.updateFromText(it.text) },
modifier = Modifier.fillMaxWidth().focusRequester(focusRequester),
placeholder = { Text("Search notes, people, tags... or use operators") },
leadingIcon = {
Icon(
Icons.Default.Search,
contentDescription = "Search",
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
},
trailingIcon = {
if (displayText.isNotEmpty()) {
IconButton(onClick = { state.clearSearch() }) {
Icon(
Icons.Default.Clear,
contentDescription = "Clear",
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
},
singleLine = true,
shape = RoundedCornerShape(12.dp),
)
androidx.compose.animation.AnimatedVisibility(
visible = isSearching,
modifier =
Modifier
.align(Alignment.BottomCenter)
.fillMaxWidth()
.padding(horizontal = 1.dp)
.clip(RoundedCornerShape(bottomStart = 12.dp, bottomEnd = 12.dp)),
) {
LinearProgressIndicator(
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.primary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
OutlinedTextField(
value =
TextFieldValue(
text = displayText,
selection = TextRange(displayText.length),
),
onValueChange = { state.updateFromText(it.text) },
modifier = Modifier.weight(1f).focusRequester(focusRequester),
placeholder = { Text("Search notes, people, tags... or use operators") },
leadingIcon = {
Icon(
Icons.Default.Search,
contentDescription = "Search",
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
},
trailingIcon = {
if (displayText.isNotEmpty()) {
IconButton(onClick = { state.clearSearch() }) {
Icon(
Icons.Default.Clear,
contentDescription = "Clear",
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
},
singleLine = true,
shape = RoundedCornerShape(12.dp),
)
IconButton(onClick = { state.togglePanel() }) {
Icon(
Icons.Default.Tune,
@@ -0,0 +1,179 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.desktop.ui.search
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.CloudDownload
import androidx.compose.material.icons.filled.Error
import androidx.compose.material.icons.filled.ExpandLess
import androidx.compose.material.icons.filled.ExpandMore
import androidx.compose.material.icons.filled.HourglassEmpty
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.vitorpamplona.amethyst.commons.chess.RelaySyncState
import com.vitorpamplona.amethyst.commons.chess.RelaySyncStatus
import kotlinx.collections.immutable.ImmutableList
@Composable
fun SearchSyncBanner(
relayStates: ImmutableList<RelaySyncState>,
isSearching: Boolean,
modifier: Modifier = Modifier,
) {
val isVisible = isSearching || relayStates.isNotEmpty()
var isExpanded by remember { mutableStateOf(false) }
AnimatedVisibility(
visible = isVisible,
enter = expandVertically(expandFrom = Alignment.Top) + fadeIn(),
exit = shrinkVertically(shrinkTowards = Alignment.Top) + fadeOut(),
modifier = modifier,
) {
Column {
// Collapsed summary row
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier =
Modifier
.fillMaxWidth()
.clickable { isExpanded = !isExpanded }
.padding(horizontal = 4.dp, vertical = 6.dp),
) {
val responded = relayStates.count { it.status == RelaySyncStatus.EOSE_RECEIVED }
val total = relayStates.size
val totalEvents = relayStates.sumOf { it.eventsReceived }
Text(
text =
if (total > 0) {
"$responded/$total relays responded \u00B7 $totalEvents events"
} else {
"Connecting to relays..."
},
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
Icon(
imageVector = if (isExpanded) Icons.Default.ExpandLess else Icons.Default.ExpandMore,
contentDescription = if (isExpanded) "Collapse" else "Expand",
tint = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.size(16.dp),
)
}
// Expanded per-relay details
AnimatedVisibility(
visible = isExpanded && relayStates.isNotEmpty(),
enter = expandVertically() + fadeIn(),
exit = shrinkVertically() + fadeOut(),
) {
Column {
HorizontalDivider(
color = MaterialTheme.colorScheme.outlineVariant,
thickness = 0.5.dp,
)
Column(
verticalArrangement = Arrangement.spacedBy(4.dp),
modifier = Modifier.padding(horizontal = 4.dp, vertical = 6.dp),
) {
relayStates.forEach { relay ->
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.fillMaxWidth(),
) {
Icon(
imageVector = relayStatusIcon(relay.status),
contentDescription = null,
tint = relayStatusColor(relay.status),
modifier = Modifier.size(14.dp),
)
Text(
text = relay.displayName,
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
Text(
text = "${relay.eventsReceived} events",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f),
)
}
}
}
}
}
}
}
}
private fun relayStatusIcon(status: RelaySyncStatus): ImageVector =
when (status) {
RelaySyncStatus.CONNECTING -> Icons.Default.HourglassEmpty
RelaySyncStatus.WAITING -> Icons.Default.HourglassEmpty
RelaySyncStatus.RECEIVING -> Icons.Default.CloudDownload
RelaySyncStatus.EOSE_RECEIVED -> Icons.Default.CheckCircle
RelaySyncStatus.FAILED -> Icons.Default.Error
}
@Composable
private fun relayStatusColor(status: RelaySyncStatus): Color =
when (status) {
RelaySyncStatus.CONNECTING -> MaterialTheme.colorScheme.secondary
RelaySyncStatus.WAITING -> MaterialTheme.colorScheme.secondary
RelaySyncStatus.RECEIVING -> MaterialTheme.colorScheme.primary
RelaySyncStatus.EOSE_RECEIVED -> MaterialTheme.colorScheme.primary
RelaySyncStatus.FAILED -> MaterialTheme.colorScheme.error
}