feat(search): add SearchFilterFactory and SearchResultFilter

Phase 2: SearchFilterFactory converts SearchQuery → List<Filter> with
default 3-group kind splitting (Android parity), NIP-50 inline
extensions, OR query support. SearchResultFilter handles client-side
exclusions, reply/media pseudo-kind detection, dedup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
nrobi144
2026-03-10 11:43:48 +02:00
parent ae76b86ab6
commit c3a45822d5
2 changed files with 237 additions and 0 deletions
@@ -0,0 +1,74 @@
/*
* 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.commons.search
import com.vitorpamplona.quartz.nip01Core.core.Event
object SearchResultFilter {
fun filter(
events: List<Event>,
query: SearchQuery,
): List<Event> {
var result = events
// Dedup by event ID
result = result.distinctBy { it.id }
// Exclusion terms (client-side)
if (query.excludeTerms.isNotEmpty()) {
result =
result.filter { event ->
query.excludeTerms.none { term ->
event.content.contains(term, ignoreCase = true)
}
}
}
// Pseudo-kind: reply (kind 1 with e tag)
if ("reply" in query.pseudoKinds) {
result = result.filter { event -> isReply(event) }
}
// Pseudo-kind: media (kind 1 with imeta tag or image URLs)
if ("media" in query.pseudoKinds) {
result = result.filter { event -> isMedia(event) }
}
// Sort by createdAt descending
return result.sortedByDescending { it.createdAt }
}
fun isReply(event: Event): Boolean = event.kind == 1 && event.tags.any { it.size >= 2 && it[0] == "e" }
fun isMedia(event: Event): Boolean {
if (event.kind != 1) return false
// Check for imeta tag
if (event.tags.any { it.size >= 2 && it[0] == "imeta" }) return true
// Check for image/video URLs in content
return IMAGE_URL_PATTERN.containsMatchIn(event.content)
}
private val IMAGE_URL_PATTERN =
Regex(
"""https?://\S+\.(jpg|jpeg|png|gif|webp|svg|mp4|webm|mov)""",
RegexOption.IGNORE_CASE,
)
}
@@ -0,0 +1,163 @@
/*
* 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.subscriptions
import com.vitorpamplona.amethyst.commons.search.SearchQuery
import com.vitorpamplona.quartz.experimental.audio.header.AudioHeaderEvent
import com.vitorpamplona.quartz.experimental.audio.track.AudioTrackEvent
import com.vitorpamplona.quartz.experimental.interactiveStories.InteractiveStoryPrologueEvent
import com.vitorpamplona.quartz.experimental.interactiveStories.InteractiveStorySceneEvent
import com.vitorpamplona.quartz.experimental.nipsOnNostr.NipTextEvent
import com.vitorpamplona.quartz.experimental.nns.NNSEvent
import com.vitorpamplona.quartz.experimental.publicMessages.PublicMessageEvent
import com.vitorpamplona.quartz.experimental.zapPolls.PollNoteEvent
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
import com.vitorpamplona.quartz.nip22Comments.CommentEvent
import com.vitorpamplona.quartz.nip23LongContent.LongTextNoteEvent
import com.vitorpamplona.quartz.nip28PublicChat.admin.ChannelCreateEvent
import com.vitorpamplona.quartz.nip28PublicChat.admin.ChannelMetadataEvent
import com.vitorpamplona.quartz.nip30CustomEmoji.pack.EmojiPackEvent
import com.vitorpamplona.quartz.nip51Lists.PinListEvent
import com.vitorpamplona.quartz.nip51Lists.bookmarkList.BookmarkListEvent
import com.vitorpamplona.quartz.nip51Lists.followList.FollowListEvent
import com.vitorpamplona.quartz.nip51Lists.peopleList.PeopleListEvent
import com.vitorpamplona.quartz.nip53LiveActivities.streaming.LiveActivitiesEvent
import com.vitorpamplona.quartz.nip54Wiki.WikiNoteEvent
import com.vitorpamplona.quartz.nip58Badges.BadgeDefinitionEvent
import com.vitorpamplona.quartz.nip72ModCommunities.definition.CommunityDefinitionEvent
import com.vitorpamplona.quartz.nip84Highlights.HighlightEvent
import com.vitorpamplona.quartz.nip88Polls.poll.PollEvent
import com.vitorpamplona.quartz.nip88Polls.response.PollResponseEvent
import com.vitorpamplona.quartz.nip99Classifieds.ClassifiedsEvent
object SearchFilterFactory {
// Default kind groups (ported from Android SearchPostsByText)
private val defaultKindGroup1 =
listOf(
TextNoteEvent.KIND,
LongTextNoteEvent.KIND,
BadgeDefinitionEvent.KIND,
PeopleListEvent.KIND,
BookmarkListEvent.KIND,
AudioHeaderEvent.KIND,
AudioTrackEvent.KIND,
PinListEvent.KIND,
PollNoteEvent.KIND,
ChannelCreateEvent.KIND,
)
private val defaultKindGroup2 =
listOf(
ChannelMetadataEvent.KIND,
ClassifiedsEvent.KIND,
CommunityDefinitionEvent.KIND,
EmojiPackEvent.KIND,
HighlightEvent.KIND,
LiveActivitiesEvent.KIND,
PublicMessageEvent.KIND,
NNSEvent.KIND,
WikiNoteEvent.KIND,
CommentEvent.KIND,
)
private val defaultKindGroup3 =
listOf(
InteractiveStoryPrologueEvent.KIND,
InteractiveStorySceneEvent.KIND,
FollowListEvent.KIND,
NipTextEvent.KIND,
PollEvent.KIND,
PollResponseEvent.KIND,
)
fun createFilters(
query: SearchQuery,
limit: Int = 100,
): List<Filter> {
if (query.isEmpty) return emptyList()
val searchString = buildSearchString(query)
val tags = buildTags(query)
val authors = query.authors.takeIf { it.isNotEmpty() }
if (query.kinds.isNotEmpty()) {
// User specified kinds — single filter (no group splitting needed)
return listOf(
Filter(
kinds = query.kinds.toList(),
search = searchString,
authors = authors,
tags = tags,
since = query.since,
until = query.until,
limit = limit,
),
)
}
// No kinds specified — use default 3-group search (Android parity)
return listOf(defaultKindGroup1, defaultKindGroup2, defaultKindGroup3).map { kindGroup ->
Filter(
kinds = kindGroup,
search = searchString,
authors = authors,
tags = tags,
since = query.since,
until = query.until,
limit = limit,
)
}
}
fun createOrFilters(
query: SearchQuery,
limit: Int = 100,
): List<List<Filter>> {
if (query.orTerms.isEmpty()) return listOf(createFilters(query, limit))
// Each OR term gets its own set of filters
return query.orTerms.take(3).map { term ->
val termQuery = query.copy(text = term, orTerms = kotlinx.collections.immutable.persistentListOf())
createFilters(termQuery, limit)
}
}
private fun buildSearchString(query: SearchQuery): String? {
val parts = mutableListOf<String>()
// Free text (exclude negation terms — those are client-side only)
if (query.text.isNotBlank()) {
parts.add(query.text)
}
// NIP-50 inline extensions
query.language?.let { parts.add("language:$it") }
query.domain?.let { parts.add("domain:$it") }
return parts.joinToString(" ").takeIf { it.isNotBlank() }
}
private fun buildTags(query: SearchQuery): Map<String, List<String>>? {
if (query.hashtags.isEmpty()) return null
return mapOf("t" to query.hashtags.toList())
}
}