Files
buzz/crates/buzz-search/src/query.rs
Will Pfleger a406191694 Revert "feat(relay): support community-scoped NIP-AR artifacts" (#9689)
Reverts #9650 (squash commit `eebf62b6`), except for migration `0097`
and the channel artifact sync fences it added.

Community scope adds a relay access gate based on community admission
rather than channel membership, a new relay-admin command kind (`9034`)
and an operator-run key transition. Its design and NIP-AR text need
relay maintainer review before they ship. Two integration gaps also
remain:

- Media download authorization looks up references only in `events`, so
an attachment referenced only by a community artifact is unreadable when
media-read enforcement is on.
- User erasure retires a community head in the database without
signaling live syncs, so members who are already syncing keep the erased
copy.

The feature can return as a new PR once those are resolved.

## Kept from #9650

- **Migration `0097`, byte for byte.** Published images already include
it, and databases that auto-migrated have recorded it. Dropping it would
stop those relays from starting. It only adds columns and indexes that
the channel code ignores. The declared schema matches that expanded
shape and keeps the `(community_id, artifact_id)` primary key, so the
operator key transition no longer exists.
- **Channel artifact sync fences.** Snapshot and live sends check for a
pending resync and for connection cancellation under the queue lock. A
reader who loses access, or whose content is redacted, while a frame is
in flight therefore receives no further payload. Handler-backed tests
cover a resync recorded while a snapshot payload waits for queue room
and one recorded after a live head read.

## Compatibility

This revert supports databases that applied only migration `0097`, which
is all that auto-migration runs. A database that ran #9650's operator
contract script, or was bootstrapped from #9650's declared schema, has
no `(community_id, artifact_id)` key, so channel artifact writes fail
there. Before deploying to such a database, check that
`artifact_heads_pkey` is on `(community_id, artifact_id)` and that
`artifact_heads` has no rows with `scope = 'community'`; if either check
fails, it needs a separate recovery step.

---------

Signed-off-by: Will Pfleger <wpfleger@block.xyz>
Co-authored-by: Paul <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@buzz.block.builderlab.xyz>
2026-11-19 02:38:17 +00:00

725 lines
30 KiB
Rust

//! NIP-50 search query against Postgres FTS, community-scoped.
//!
//! The relay never trusts a hit by itself: this layer returns canonical event
//! ids ordered by relevance, the relay refetches `StoredEvent`s through
//! buzz-db's `(community_id, event_id)` scoped fetcher, and runs the access
//! predicate (`search_hit_accepted` in `bridge.rs`) per hit. Search is never
//! the access boundary — it cannot widen visibility.
//!
//! See conformance row 50.
use sqlx::{PgPool, QueryBuilder, Row};
use uuid::Uuid;
use buzz_core::CommunityId;
use buzz_datastore_tracing::datastore_span;
use crate::error::SearchError;
/// Channel-scope filter for a community-scoped FTS query.
///
/// Four variants, 1-to-1 with the legacy `(accessible_channels: &[Uuid],
/// include_global: bool)` matrix from the Typesense relay:
///
/// | accessible | include_global | `ChannelScope` |
/// |---|---|---|
/// | non-empty | true | `ChannelsOrChannelLess(accessible)` |
/// | non-empty | false | `Channels(accessible)` |
/// | empty | true | `ChannelLessOnly` |
/// | empty | false | (don't call — caller short-circuits to EOSE) |
///
/// `ChannelLessOnly` is the variant that the old `Option<Vec<Uuid>>` +
/// `bool` 2x2 could not express unambiguously: with empty accessible
/// channels and `include_global=true`, both `Some(vec![]) + true` and
/// `None + true` would broaden to all community channels rather than
/// restrict to channel-less events. The enum closes that hole at the
/// type level.
///
/// Empty-vec edge cases are intentionally not special-cased:
/// `Channels(vec![])` emits `channel_id = ANY('{}')` which Postgres
/// evaluates as false-for-all-rows (zero hits), and
/// `ChannelsOrChannelLess(vec![])` emits `(channel_id = ANY('{}') OR
/// channel_id IS NULL)` which is equivalent to `ChannelLessOnly`.
#[derive(Debug, Clone)]
pub enum ChannelScope {
/// No channel constraint. Matches every event in the community.
Any,
/// Restrict to `channel_id IS NULL` events only — what the legacy
/// Typesense `channel_id:=__global__` sentinel meant.
ChannelLessOnly,
/// Restrict to events whose `channel_id` is in this list.
Channels(Vec<Uuid>),
/// Restrict to events whose `channel_id` is in this list, OR are
/// channel-less (`channel_id IS NULL`).
ChannelsOrChannelLess(Vec<Uuid>),
}
/// Search matching semantics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchMode {
/// Standard NIP-50-ish word/lexeme search using `websearch_to_tsquery`.
FullText,
/// Prefix-match the trailing normalized query token (`pro` matches `project`).
///
/// Intended for bounded typeahead surfaces such as the desktop topbar. The
/// relay still refetches and re-authorizes every hit; this mode changes the
/// candidate tsquery and, for relevance outside profile search, ranks only
/// the newest 1000 matches. It does not change the access boundary. A
/// trailing token that yields a lexeme shorter than three characters
/// returns no hits unless the query is kind:0-only (see `PREFIX_MIN_CHARS`).
Prefix,
}
/// Result ordering for a search.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchSort {
/// `ts_rank_cd` relevance, newest first among equal ranks. In
/// [`SearchMode::Prefix`] outside profile search, only the newest 1000
/// matches are ranked, so pages end with them.
Relevance,
/// Newest first, ignoring relevance; ties ordered by event id.
Newest,
}
/// A community-scoped FTS query.
///
/// The community is REQUIRED at the type level — there is no construction path
/// that omits it. This is the search-side expression of conformance row zero:
/// every search call carries the server-resolved tenant, never client input.
#[derive(Debug, Clone)]
pub struct SearchQuery {
/// Server-resolved community. Required.
pub community: CommunityId,
/// NIP-50 search text. Empty string is rejected by `search()` early
/// (no hits, no SQL roundtrip).
pub q: String,
/// How to scope hits by channel. See [`ChannelScope`] — the four variants
/// are 1-to-1 with the legacy `(accessible_channels, include_global)`
/// matrix, and `ChannelLessOnly` closes the gap where "empty accessible
/// channels + include global" used to silently broaden to all channels.
pub channel_scope: ChannelScope,
/// NIP-01 kinds filter. None = no kind constraint.
pub kinds: Option<Vec<i32>>,
/// NIP-01 authors filter (32-byte pubkeys). None = no author constraint.
pub authors: Option<Vec<Vec<u8>>>,
/// NIP-01 since (Unix seconds). Inclusive lower bound on created_at.
pub since: Option<i64>,
/// NIP-01 until (Unix seconds). Inclusive upper bound on created_at.
pub until: Option<i64>,
/// 1-indexed page number.
pub page: u32,
/// Page size. Clamped at 500 internally.
pub per_page: u32,
/// Matching semantics for the search text.
pub mode: SearchMode,
/// Result ordering. Paging is over this order.
pub sort: SearchSort,
}
/// A single FTS hit. The relay refetches the canonical `StoredEvent` and
/// re-authorizes; this struct is just enough to drive that fetch and preserve
/// relevance ordering.
#[derive(Debug, Clone)]
pub struct SearchHit {
/// 32-byte event id.
pub event_id: [u8; 32],
/// Nostr kind.
pub kind: i32,
/// 32-byte pubkey of author.
pub pubkey: [u8; 32],
/// Optional channel UUID. `None` = channel-less event.
pub channel_id: Option<Uuid>,
/// Unix seconds.
pub created_at: i64,
/// `ts_rank_cd` relevance score (higher = better).
pub rank: f32,
}
/// Result of a search.
#[derive(Debug, Clone)]
pub struct SearchResult {
/// Hits on this page, in the query's `sort` order.
pub hits: Vec<SearchHit>,
/// 1-indexed page returned.
pub page: u32,
}
const PER_PAGE_MAX: u32 = 500;
const PER_PAGE_DEFAULT: u32 = 100;
/// Hard cap on search text handed to Postgres text-search parsers. This keeps a
/// single request from spending unbounded parser CPU/memory while still allowing
/// far longer queries than the desktop UI normally emits.
const SEARCH_TEXT_MAX_CHARS: usize = 4096;
/// Search pages are currently server-generated (WS uses 1..=MAX_SEARCH_PAGES,
/// bridge uses page 1), but clamp here too so a future caller cannot accidentally
/// wire untrusted input into a multi-trillion-row OFFSET.
const PAGE_MAX: u32 = 1000;
/// How many of the newest matches a prefix-mode relevance search ranks.
/// The GIN index returns matches in no useful order, so ranking every match
/// before `LIMIT` costs time proportional to the match count, and a short
/// typeahead prefix matches most of a community. Reading candidates newest
/// first lets Postgres stop at this many instead. Every page ranks the same
/// window, so pages stay consistent and end with it.
const PREFIX_RANK_WINDOW: i64 = 1000;
/// Shortest lexeme [`SearchMode::Prefix`] gives `:*` for anything but a
/// kind:0-only (profile) query. If the trailing token yields a shorter one,
/// the tsquery is NULL and the search returns no hits, whatever completed
/// words precede it. The check runs on Postgres' own `simple` parser output,
/// so punctuation such as `c++`, `@al` or `don't` cannot slip a short `:*`
/// past it.
/// The GIN index cannot stop early or return rows in rank order, so a one- or
/// two-character `:*` prefix makes Postgres fetch, recheck and rank a large
/// share of every indexed message before it can apply `LIMIT`, and typeahead
/// sends one on every typing pause. Profiles are few enough to stay
/// one-character typeahead.
const PREFIX_MIN_CHARS: i32 = 3;
/// `min_prefix_chars` applies to [`SearchMode::Prefix`] only; 0 disables it.
fn push_tsquery(
qb: &mut QueryBuilder<sqlx::Postgres>,
mode: SearchMode,
search_text: &str,
min_prefix_chars: i32,
) {
match mode {
SearchMode::FullText => {
qb.push("websearch_to_tsquery('simple', ");
qb.push_bind(search_text);
qb.push(")");
}
SearchMode::Prefix => {
// Prefix mode is for typeahead: completed whitespace-delimited tokens
// are exact, and only the trailing token is suffixed with `:*`.
// Each raw token still goes through Postgres' `simple` parser before
// tsquery construction so query-side normalization matches the
// `search_tsv` generated column. `quote_literal` prevents tsquery
// syntax injection from punctuation/operators in the raw topbar input.
// A trailing lexeme shorter than `min_prefix_chars` makes the whole
// tsquery NULL, which matches nothing (see `PREFIX_MIN_CHARS`).
qb.push(
"(SELECT CASE WHEN bool_or(token_ord = max_token_ord AND char_length(lexeme) < ",
);
qb.push_bind(min_prefix_chars);
qb.push(
") THEN NULL::tsquery ELSE COALESCE(\
string_agg(\
quote_literal(lexeme) || CASE WHEN token_ord = max_token_ord THEN ':*' ELSE '' END, \
' & ' ORDER BY token_ord, lex_ord\
), \
''\
)::tsquery END \
FROM (\
SELECT raw_token.token_ord, normalized.lexeme, normalized.lex_ord, raw_token.max_token_ord \
FROM (\
SELECT token, token_ord, max(token_ord) OVER () AS max_token_ord \
FROM regexp_split_to_table(",
);
qb.push_bind(search_text);
qb.push(
", '\\s+') WITH ORDINALITY AS split(token, token_ord)\
) AS raw_token \
CROSS JOIN LATERAL unnest(tsvector_to_array(to_tsvector('simple', raw_token.token))) \
WITH ORDINALITY AS normalized(lexeme, lex_ord)\
) AS prefix_terms)",
);
}
}
}
fn normalized_search_text(q: &str) -> Option<String> {
let trimmed = q.trim();
if trimmed.is_empty() {
return None;
}
let mut cleaned = String::with_capacity(trimmed.len().min(SEARCH_TEXT_MAX_CHARS));
for ch in trimmed.chars().take(SEARCH_TEXT_MAX_CHARS) {
cleaned.push(if ch == '\0' { ' ' } else { ch });
}
let cleaned = cleaned.trim();
if cleaned.is_empty() {
None
} else {
Some(cleaned.to_string())
}
}
/// Execute a community-scoped FTS query.
///
/// The mode-specific tsquery is resolved first and bound as a value, so the
/// planner estimates its selectivity instead of assuming a rare match. The
/// search itself is a one-shot statement, planned for that value every time.
///
/// SQL shape:
/// ```sql
/// SELECT id, kind, pubkey, channel_id, EXTRACT(EPOCH FROM created_at)::bigint AS created_at_s,
/// ts_rank_cd(search_tsv, $q) AS rank
/// FROM events -- prefix relevance: the newest PREFIX_RANK_WINDOW matches
/// WHERE community_id = $ctx
/// AND deleted_at IS NULL
/// AND search_tsv @@ $q
/// [+ channel scope, kinds, authors, since, until]
/// ORDER BY rank DESC, created_at DESC, id -- SearchSort::Newest: created_at DESC, id
/// LIMIT $per_page OFFSET (($page - 1) * $per_page)
/// ```
///
/// `community_id = $ctx` is the first predicate and is non-negotiable. There
/// is no code path through this function that omits it.
#[datastore_span(name = "search", system = "postgresql")]
pub async fn search(pool: &PgPool, query: &SearchQuery) -> Result<SearchResult, SearchError> {
let profile_only = query.kinds.as_deref() == Some(&[0][..]);
let Some(search_text) = normalized_search_text(&query.q) else {
return Ok(SearchResult {
hits: Vec::new(),
page: query.page.clamp(1, PAGE_MAX),
});
};
let per_page = query.per_page.clamp(1, PER_PAGE_MAX);
let per_page_actual = if query.per_page == 0 {
PER_PAGE_DEFAULT
} else {
per_page
};
let page = query.page.clamp(1, PAGE_MAX);
let offset = ((page - 1) as i64) * (per_page_actual as i64);
// Profile typeahead uses broad prefix matching. For one- and two-character
// queries, a busy community can have enough newer prefix matches to push a
// short exact display name off the bounded first page. Keep the same result
// set and pagination contract, but put rows containing the whole lexeme
// first for this one narrow caller shape.
let prioritize_exact_profile_lexeme = query.sort == SearchSort::Relevance
&& query.mode == SearchMode::Prefix
&& profile_only
&& search_text.chars().count() <= 2;
// Profiles are few, so profile typeahead keeps ranking every match.
let rank_window =
(query.sort == SearchSort::Relevance && query.mode == SearchMode::Prefix && !profile_only)
.then_some(PREFIX_RANK_WINDOW);
let min_prefix_chars = if profile_only { 0 } else { PREFIX_MIN_CHARS };
// One checkout for both statements, so a busy pool queues a search once.
let mut conn = pool.acquire().await?;
let mut resolve: QueryBuilder<sqlx::Postgres> = QueryBuilder::new("SELECT (");
push_tsquery(&mut resolve, query.mode, &search_text, min_prefix_chars);
resolve.push(")::text");
let tsquery: Option<String> = resolve.build_query_scalar().fetch_one(&mut *conn).await?;
let Some(tsquery) = tsquery.filter(|tsquery| !tsquery.is_empty()) else {
// A too-short trailing prefix (NULL) or nothing searchable after
// normalization (empty) matches nothing.
return Ok(SearchResult {
hits: Vec::new(),
page,
});
};
let mut qb: QueryBuilder<sqlx::Postgres> = QueryBuilder::new(
"SELECT id, kind, pubkey, channel_id, \
EXTRACT(EPOCH FROM created_at)::bigint AS created_at_s, \
ts_rank_cd(search_tsv, ",
);
qb.push_bind(&tsquery);
qb.push("::tsquery) AS rank FROM ");
if rank_window.is_some() {
qb.push("(SELECT id, kind, pubkey, channel_id, created_at, search_tsv FROM ");
}
qb.push("events WHERE community_id = ");
qb.push_bind(*query.community.as_uuid());
qb.push(" AND deleted_at IS NULL AND search_tsv @@ ");
qb.push_bind(&tsquery);
qb.push("::tsquery");
// Search is a current-state surface; old artifact snapshots cannot match.
qb.push(" AND (events.kind <> 45010 OR EXISTS (SELECT 1 FROM artifact_heads ah WHERE ah.community_id=events.community_id AND ah.event_id=events.id AND NOT ah.deleted))");
// Channel scope — see `ChannelScope` doc for the four-case mapping. The
// emitted SQL fragments are identical to the legacy 2x2 tuple for the
// three carry-over cases; `ChannelLessOnly` is the new fence that the
// old shape could not express.
match &query.channel_scope {
ChannelScope::Any => {
// No channel constraint.
}
ChannelScope::ChannelLessOnly => {
qb.push(" AND channel_id IS NULL");
}
ChannelScope::Channels(ids) => {
qb.push(" AND channel_id = ANY(");
qb.push_bind(ids.clone());
qb.push(")");
}
ChannelScope::ChannelsOrChannelLess(ids) => {
qb.push(" AND (channel_id = ANY(");
qb.push_bind(ids.clone());
qb.push(") OR channel_id IS NULL)");
}
}
if let Some(ref kinds) = query.kinds {
if !kinds.is_empty() {
qb.push(" AND kind = ANY(");
qb.push_bind(kinds.clone());
qb.push(")");
}
}
if let Some(ref authors) = query.authors {
if !authors.is_empty() {
qb.push(" AND pubkey = ANY(");
qb.push_bind(authors.clone());
qb.push(")");
}
}
if let Some(since) = query.since {
qb.push(" AND created_at >= to_timestamp(");
qb.push_bind(since);
qb.push(")");
}
if let Some(until) = query.until {
qb.push(" AND created_at <= to_timestamp(");
qb.push_bind(until);
qb.push(")");
}
if let Some(window) = rank_window {
// Matches the backward scan of each partition's primary key, so the
// newest candidates stream in order and the scan stops at `window`.
qb.push(" ORDER BY created_at DESC, id DESC LIMIT ");
qb.push_bind(window);
qb.push(") AS candidates");
}
if query.sort == SearchSort::Newest {
qb.push(" ORDER BY created_at DESC, id LIMIT ");
} else if prioritize_exact_profile_lexeme {
qb.push(" ORDER BY search_tsv @@ websearch_to_tsquery('simple', ");
qb.push_bind(&search_text);
qb.push(") DESC, rank DESC, created_at DESC, id LIMIT ");
} else {
qb.push(" ORDER BY rank DESC, created_at DESC, id LIMIT ");
}
qb.push_bind(per_page_actual as i64);
qb.push(" OFFSET ");
qb.push_bind(offset);
// Not a cached prepared statement: after five runs Postgres may switch a
// cached statement to a generic plan, which cannot see the tsquery.
let rows = crate::cancel::fetch_all(pool, conn, qb.build().persistent(false)).await?;
let mut hits = Vec::with_capacity(rows.len());
for row in rows {
let id_bytes: Vec<u8> = row.try_get("id")?;
let pk_bytes: Vec<u8> = row.try_get("pubkey")?;
let id: [u8; 32] = id_bytes.try_into().map_err(|v: Vec<u8>| {
sqlx::Error::Decode(format!("event id column is {} bytes, expected 32", v.len()).into())
})?;
let pubkey: [u8; 32] = pk_bytes.try_into().map_err(|v: Vec<u8>| {
sqlx::Error::Decode(format!("pubkey column is {} bytes, expected 32", v.len()).into())
})?;
hits.push(SearchHit {
event_id: id,
kind: row.try_get("kind")?,
pubkey,
channel_id: row.try_get("channel_id")?,
created_at: row.try_get("created_at_s")?,
rank: row.try_get("rank")?,
});
}
Ok(SearchResult { hits, page })
}
/// The SQL that normalizes a name, and the query, for result tiers, around
/// its argument: lower-cased, whitespace runs collapsed to one space, trimmed.
const TIER_TEXT: [&str; 2] = ["btrim(regexp_replace(lower(", "), '\\s+', ' ', 'g'))"];
fn tier_name(column: &str) -> String {
format!("{}{column}{}", TIER_TEXT[0], TIER_TEXT[1])
}
/// Largest page size [`search_profile_names`] accepts.
pub const PROFILE_NAMES_PER_PAGE_MAX: u32 = 100;
/// Find people whose current kind:0 `name` or `display_name` matches `q`,
/// or, with `verified_by`, whose current verified profile does.
///
/// Matching is the NIP-50 [`SearchMode::Prefix`] rule (every completed query
/// word must equal a word, the last may be a prefix, in any order), applied to
/// name fields only. For kind:0 these are `name` and `display_name`; other
/// profile fields, such as `picture` data URLs, `about` and `nip05`, cannot
/// satisfy a word, and the existing `search_tsv` index stays the access path.
/// With `verified_by`, a current kind:30382 event signed by that key matches
/// on its `display_name`, `username` and `full_name` tags; its `d` tag names
/// the person.
///
/// Order is per person, by their best match: a name equal to the query, then
/// a name that starts with it, then a name with a word that starts with it,
/// then any other match. The query and names are compared lower-cased with
/// whitespace collapsed, both normalized by the same SQL. Ties go by
/// relevance of the query to the names (`ts_rank_cd` over the names alone,
/// not the whole event), then newest first, then pubkey, so pages are stable.
/// Pages are 1-based, like [`SearchQuery::page`], and count people.
///
/// Each person yields their current kind:0, when they have one, then, with
/// `verified_by`, their current verified profile, when they have one,
/// whichever of the two matched.
#[datastore_span(name = "search_profile_names", system = "postgresql")]
pub async fn search_profile_names(
pool: &PgPool,
community: CommunityId,
q: &str,
verified_by: Option<[u8; 32]>,
page: u32,
per_page: u32,
) -> Result<SearchResult, SearchError> {
let page = page.clamp(1, PAGE_MAX);
let Some(search_text) = normalized_search_text(q) else {
return Ok(SearchResult {
hits: Vec::new(),
page,
});
};
let per_page = per_page.clamp(1, PROFILE_NAMES_PER_PAGE_MAX);
let offset = ((page - 1) as i64) * (per_page as i64);
let mut qb: QueryBuilder<sqlx::Postgres> = QueryBuilder::new("WITH search_query AS (SELECT ");
push_tsquery(&mut qb, SearchMode::Prefix, &search_text, 0);
qb.push(" AS query, ");
// Every verified match is ranked before `LIMIT`, so verified names need
// the NIP-50 minimum prefix; kind:0 keeps one-character typeahead.
push_tsquery(&mut qb, SearchMode::Prefix, &search_text, PREFIX_MIN_CHARS);
qb.push(" AS verified_query, ");
qb.push(TIER_TEXT[0]);
qb.push_bind(search_text.clone());
qb.push(TIER_TEXT[1]);
qb.push(" AS typed), matches AS (SELECT e.pubkey AS person, ");
push_name_match(&mut qb, &["name", "display_name"], "e.created_at");
qb.push(" FROM events e CROSS JOIN search_query");
qb.push(PROFILE_NAME_FIELDS);
push_name_columns(&mut qb, &["name", "display_name"]);
qb.push(" WHERE e.community_id = ");
qb.push_bind(*community.as_uuid());
qb.push(
" AND e.kind = 0 AND e.channel_id IS NULL AND e.deleted_at IS NULL \
AND e.search_tsv @@ search_query.query \
AND names.tsv @@ search_query.query",
);
if let Some(author) = verified_by {
qb.push(" UNION ALL SELECT decode(pubkey, 'hex'), ");
push_name_match(
&mut qb,
&VERIFIED_NAME_TAGS,
"to_timestamp((event ->> 'created_at')::bigint)",
);
qb.push(" FROM nip_fi_profile_heads CROSS JOIN search_query CROSS JOIN LATERAL (SELECT ");
for (i, tag) in VERIFIED_NAME_TAGS.iter().enumerate() {
if i > 0 {
qb.push(", ");
}
qb.push(format!("{} AS {tag}", verified_name(tag)));
}
qb.push(") AS fields");
push_name_columns(&mut qb, &VERIFIED_NAME_TAGS);
qb.push(" WHERE community_id = ");
qb.push_bind(*community.as_uuid());
qb.push(format!(
" AND pubkey ~ '^[0-9a-f]{{64}}$' AND {} @@ search_query.verified_query",
verified_names_tsv()
));
// The head counts only while its event is live and signed by `author`.
qb.push(" AND EXISTS (SELECT 1 FROM events e WHERE e.community_id = ");
qb.push_bind(*community.as_uuid());
// `created_at` lets Postgres prune `events` to the head's partition.
qb.push(
" AND e.id = decode(event ->> 'id', 'hex') \
AND e.created_at = to_timestamp((event ->> 'created_at')::bigint) \
AND e.kind = 30382 AND e.pubkey = ",
);
qb.push_bind(author.to_vec());
qb.push(" AND e.channel_id IS NULL AND e.deleted_at IS NULL)");
}
qb.push(
// Each person ranks by their single best match, not by mixing sources.
"), people AS (SELECT * FROM (SELECT DISTINCT ON (person) person, tier, \
rank, created_at FROM matches \
ORDER BY person, tier, rank DESC, created_at DESC) AS best \
ORDER BY tier, rank DESC, created_at DESC, person LIMIT ",
);
qb.push_bind(per_page as i64);
qb.push(" OFFSET ");
qb.push_bind(offset);
qb.push(
") SELECT ev.id, ev.kind, ev.pubkey, ev.channel_id, \
EXTRACT(EPOCH FROM ev.created_at)::bigint AS created_at_s, people.rank \
FROM people CROSS JOIN LATERAL (\
(SELECT id, kind, pubkey, channel_id, created_at FROM events \
WHERE community_id = ",
);
qb.push_bind(*community.as_uuid());
qb.push(
" AND pubkey = people.person AND kind = 0 \
AND channel_id IS NULL AND deleted_at IS NULL \
ORDER BY created_at DESC, id LIMIT 1)",
);
if let Some(author) = verified_by {
qb.push(
" UNION ALL (SELECT e.id, e.kind, e.pubkey, e.channel_id, e.created_at \
FROM nip_fi_profile_heads h JOIN events e ON e.community_id = h.community_id \
AND e.id = decode(h.event ->> 'id', 'hex') \
AND e.created_at = to_timestamp((h.event ->> 'created_at')::bigint) \
WHERE h.community_id = ",
);
qb.push_bind(*community.as_uuid());
qb.push(" AND h.pubkey = encode(people.person, 'hex') AND e.kind = 30382 AND e.pubkey = ");
qb.push_bind(author.to_vec());
qb.push(" AND e.channel_id IS NULL AND e.deleted_at IS NULL)");
}
qb.push(
") AS ev ORDER BY people.tier, people.rank DESC, people.created_at DESC, \
people.person, ev.kind",
);
let rows = crate::cancel::fetch_all(pool, pool.acquire().await?, qb.build()).await?;
let mut hits = Vec::with_capacity(rows.len());
for row in rows {
let id_bytes: Vec<u8> = row.try_get("id")?;
let pk_bytes: Vec<u8> = row.try_get("pubkey")?;
let id: [u8; 32] = id_bytes.try_into().map_err(|v: Vec<u8>| {
sqlx::Error::Decode(format!("event id column is {} bytes, expected 32", v.len()).into())
})?;
let pubkey: [u8; 32] = pk_bytes.try_into().map_err(|v: Vec<u8>| {
sqlx::Error::Decode(format!("pubkey column is {} bytes, expected 32", v.len()).into())
})?;
hits.push(SearchHit {
event_id: id,
kind: row.try_get("kind")?,
pubkey,
channel_id: row.try_get("channel_id")?,
created_at: row.try_get("created_at_s")?,
rank: row.try_get("rank")?,
});
}
Ok(SearchResult { hits, page })
}
/// Joins `fields.name` and `fields.display_name` for the kind:0 `e.content`.
/// A field is NULL unless the content is a JSON object and the field is a
/// string.
///
/// Content is any text the signer chose, and some of it must have no names
/// rather than fail the whole search:
/// - Valid JSON that cannot become jsonb, such as `\u0000`, a lone surrogate
/// or a huge number. `pg_input_is_valid` rejects it.
/// - Nesting deep enough to exhaust PostgreSQL's recursive JSON parser (about
/// ten thousand levels at the default `max_stack_depth`). That raises an
/// error `pg_input_is_valid` does not catch, so content with more than 4096
/// `[` and `{` is never parsed. The count bounds the depth; ingest already
/// rejects kind:0 content nested deeper than 128 levels. The bound assumes
/// the default 2 MB `max_stack_depth`; well below that, content stored
/// outside ingest with fewer than 4096 levels can still fail the search.
///
/// `OFFSET 0` keeps the planner from copying the parse into each use of `doc`.
const PROFILE_NAME_FIELDS: &str = " CROSS JOIN LATERAL (SELECT CASE \
WHEN length(e.content) - length(translate(e.content, '[{', '')) > 4096 THEN NULL \
WHEN pg_input_is_valid(e.content, 'jsonb') THEN e.content::jsonb END AS doc \
OFFSET 0) AS profile \
CROSS JOIN LATERAL (SELECT \
CASE WHEN jsonb_typeof(profile.doc -> 'name') = 'string' \
THEN profile.doc ->> 'name' END AS name, \
CASE WHEN jsonb_typeof(profile.doc -> 'display_name') = 'string' \
THEN profile.doc ->> 'display_name' END AS display_name) AS fields";
/// Name tags of a relay-signed kind:30382 verified profile.
const VERIFIED_NAME_TAGS: [&str; 3] = ["display_name", "username", "full_name"];
/// The first value of `tag` in `nip_fi_profile_heads.event`. The relay
/// writes each name tag at most once. Strict mode keeps a tag's array
/// shape (lax mode unwraps it), and `silent` makes malformed tags no match.
fn verified_name(tag: &str) -> String {
format!(
"(jsonb_path_query_first(event, 'strict $.tags[*] ? (@[0] == \"{tag}\")[1]', '{{}}', true) \
#>> '{{}}')"
)
}
/// The expression `idx_nip_fi_profile_heads_names` indexes
/// (migrations/0081_nip_fi_profile_head_names.sql).
fn verified_names_tsv() -> String {
let names = VERIFIED_NAME_TAGS
.iter()
.map(|tag| format!("COALESCE({}, '')", verified_name(tag)))
.collect::<Vec<_>>()
.join(" || ' ' || ");
format!("to_tsvector('simple', {names})")
}
/// Pushes `rank, created_at, tier` for one match over the `names` columns
/// built by [`push_name_columns`] from the same `fields`.
fn push_name_match(qb: &mut QueryBuilder<sqlx::Postgres>, fields: &[&str], created_at: &str) {
qb.push(format!(
"ts_rank_cd(names.tsv, search_query.query) AS rank, {created_at} AS created_at, LEAST("
));
for (i, field) in fields.iter().enumerate() {
if i > 0 {
qb.push(", ");
}
let column = format!("names.{field}");
qb.push(format!(
"CASE WHEN {column} = search_query.typed THEN 0 \
WHEN starts_with({column}, search_query.typed) THEN 1 \
WHEN strpos({column}, ' ' || search_query.typed) > 0 THEN 2 ELSE 3 END"
));
}
qb.push(") AS tier");
}
/// Pushes the `names` lateral: the `simple` tsvector of `fields` and each
/// field normalized for tiers ([`TIER_TEXT`]).
fn push_name_columns(qb: &mut QueryBuilder<sqlx::Postgres>, fields: &[&str]) {
let joined = fields
.iter()
.map(|field| format!("fields.{field}"))
.collect::<Vec<_>>()
.join(", ");
qb.push(format!(
" CROSS JOIN LATERAL (SELECT to_tsvector('simple', concat_ws(' ', {joined})) AS tsv"
));
for field in fields {
qb.push(format!(
", {} AS {field}",
tier_name(&format!("fields.{field}"))
));
}
qb.push(") AS names");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalized_search_text_trims_and_rejects_empty() {
assert_eq!(
normalized_search_text(" hello ").as_deref(),
Some("hello")
);
assert!(normalized_search_text(" ").is_none());
}
#[test]
fn normalized_search_text_replaces_nul_bytes() {
assert_eq!(
normalized_search_text("foo\0bar").as_deref(),
Some("foo bar")
);
}
#[test]
fn normalized_search_text_caps_length() {
let long = "x".repeat(SEARCH_TEXT_MAX_CHARS + 10);
let cleaned = normalized_search_text(&long).expect("non-empty");
assert_eq!(cleaned.chars().count(), SEARCH_TEXT_MAX_CHARS);
}
}