Skip to content

Commit 5da797c

Browse files
committed
Merge remote-tracking branch 'origin/main' into kennylopez-community-labels-20261029
Signed-off-by: kenny lopez <[email protected]>
2 parents e7fc30a + 7585411 commit 5da797c

60 files changed

Lines changed: 3963 additions & 381 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Justfile‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,10 @@ test-unit:
545545
# All four modules are infra-free; config parses env into a struct and
546546
# never connects.
547547
# `^config::` is anchored so it does not also select nip_fi_config.
548+
# subscription::tests covers the in-memory registry and its fan-out
549+
# indexes; it is infra-free.
548550
cargo nextest run -p buzz-relay --lib --bin buzz-relay \
549-
-E 'test(/^api::admin::/) + test(/^push_runtime::/) + test(/^operator_listener::tests::/) + test(/^handlers::channel_authz::/) + test(/^handlers::moderation_authz::/) + test(/^handlers::side_effects::tests::/) + test(/^handlers::coalesced_refresh::tests::/) + test(/^handlers::system_channels::tests::/) + test(/^nip11::tests::/) + test(/^storage_sweep::tests::/) + test(/^nip_fi_core::tests::/) + test(/^nip_fi_http::tests::/) + test(/^nip_fi_config::tests::/) + test(/^nip_fi_resolver::tests::/) + test(/^readiness::tests::/) + test(/^router::tests::/) + test(/^api::parse_query_tests::/) + test(/^api::profiles::/) + test(/^api::git::transport::off_mode_precedence_tests::/) + test(/^audio::join::tests::/) + test(/^audio::handler::tests::/) + test(/^nip_fi_gate::tests::/) + test(/^nip_fi_session::tests::/) + test(/^nip_fi_shadow::tests::/) + test(/^nip_fi_shadow_session::tests::/) + test(/^startup_steps::tests::/) + test(/^mesh_boot::tests::/) + test(/^telemetry::tests::/) + test(/^config::tests::/) + test(=state::tests::accessible_channel_invalidation_is_scoped_to_community) + test(=state::tests::accessible_channels_join_member_and_open_halves) + test(=state::tests::channel_deleted_invalidation_is_scoped_to_community) + test(=state::tests::neither_a_confirmed_inactive_community_nor_a_failed_lookup_admits_the_socket) + test(=state::tests::periodic_revalidation_disconnects_inside_the_fenced_callback) + test(=state::tests::bare_community_disconnect_fails_closed_when_the_fence_is_unavailable) + test(=connection::tests::send_loop_sends_policy_close_when_community_is_archived) + test(=state::tests::community_disconnect_then_nip_fi_keeps_community_deleted_reason) + test(=state::tests::disconnect_community_wins_reason_losing_nip_fi_does_not_enqueue_frame) + test(=handlers::req::tests::timed_out_historical_read_deregisters_before_closed) + test(=handlers::req::tests::superseded_timeout_leaves_replacement_intact) + test(=handlers::req::tests::search_claim_retires_live_and_yields_to_replacement) + test(=handlers::req::tests::failed_search_closes_without_eose_or_trying_later_filters) + test(=handlers::req::tests::concurrent_claims_and_stale_teardowns_keep_the_last_owner) + test(=handlers::req::tests::timeout_closed_is_emitted_before_a_replacement_can_claim) + test(=handlers::req::tests::revoke_then_replacement_keeps_replacement_whole) + test(=handlers::req::tests::claims_after_connection_cleanup_are_refused) + test(=handlers::req::tests::dropped_terminal_frame_cancels_connection) + test(=handlers::req::tests::revoke_dropped_terminal_frame_cancels_connection) + test(=handlers::req::tests::count_fallback_covers_filters_that_can_match_private_deletions) + test(=handlers::req::tests::group_state_t_and_creator_tags_reach_sql_only_for_group_state_kinds) + test(=api::bridge::postgres_tests::bridge_search_mode_extension_accepts_prefix_snake_or_camel_case) + test(=api::bridge::postgres_tests::bridge_search_sort_extension_selects_newest_only_when_named) + test(=api::invites::postgres_tests::restriction_tests::postgres_tests::destructive_fixture_rejects_shared_database_urls) + (kind(bin) & (test(/^tests::/) + test(/^composition_tests::/) + test(/^env_filter_tests::/)) - test(/^tests::postgres_tests::/))'
551+
-E 'test(/^subscription::tests::/) + test(/^api::admin::/) + test(/^push_runtime::/) + test(/^operator_listener::tests::/) + test(/^handlers::channel_authz::/) + test(/^handlers::moderation_authz::/) + test(/^handlers::side_effects::tests::/) + test(/^handlers::coalesced_refresh::tests::/) + test(/^handlers::system_channels::tests::/) + test(/^nip11::tests::/) + test(/^storage_sweep::tests::/) + test(/^nip_fi_core::tests::/) + test(/^nip_fi_http::tests::/) + test(/^nip_fi_config::tests::/) + test(/^nip_fi_resolver::tests::/) + test(/^readiness::tests::/) + test(/^router::tests::/) + test(/^api::parse_query_tests::/) + test(/^api::profiles::/) + test(/^api::git::transport::off_mode_precedence_tests::/) + test(/^audio::join::tests::/) + test(/^audio::handler::tests::/) + test(/^nip_fi_gate::tests::/) + test(/^nip_fi_session::tests::/) + test(/^nip_fi_shadow::tests::/) + test(/^nip_fi_shadow_session::tests::/) + test(/^startup_steps::tests::/) + test(/^mesh_boot::tests::/) + test(/^telemetry::tests::/) + test(/^config::tests::/) + test(=state::tests::accessible_channel_invalidation_is_scoped_to_community) + test(=state::tests::accessible_channels_join_member_and_open_halves) + test(=state::tests::channel_deleted_invalidation_is_scoped_to_community) + test(=state::tests::neither_a_confirmed_inactive_community_nor_a_failed_lookup_admits_the_socket) + test(=state::tests::periodic_revalidation_disconnects_inside_the_fenced_callback) + test(=state::tests::bare_community_disconnect_fails_closed_when_the_fence_is_unavailable) + test(=connection::tests::send_loop_sends_policy_close_when_community_is_archived) + test(=state::tests::community_disconnect_then_nip_fi_keeps_community_deleted_reason) + test(=state::tests::disconnect_community_wins_reason_losing_nip_fi_does_not_enqueue_frame) + test(=handlers::req::tests::timed_out_historical_read_deregisters_before_closed) + test(=handlers::req::tests::superseded_timeout_leaves_replacement_intact) + test(=handlers::req::tests::search_claim_retires_live_and_yields_to_replacement) + test(=handlers::req::tests::failed_search_closes_without_eose_or_trying_later_filters) + test(=handlers::req::tests::concurrent_claims_and_stale_teardowns_keep_the_last_owner) + test(=handlers::req::tests::timeout_closed_is_emitted_before_a_replacement_can_claim) + test(=handlers::req::tests::revoke_then_replacement_keeps_replacement_whole) + test(=handlers::req::tests::claims_after_connection_cleanup_are_refused) + test(=handlers::req::tests::dropped_terminal_frame_cancels_connection) + test(=handlers::req::tests::revoke_dropped_terminal_frame_cancels_connection) + test(=handlers::req::tests::count_fallback_covers_filters_that_can_match_private_deletions) + test(=handlers::req::tests::group_state_t_and_creator_tags_reach_sql_only_for_group_state_kinds) + test(=api::bridge::postgres_tests::bridge_search_mode_extension_accepts_prefix_snake_or_camel_case) + test(=api::bridge::postgres_tests::bridge_search_sort_extension_selects_newest_only_when_named) + test(=api::invites::postgres_tests::restriction_tests::postgres_tests::destructive_fixture_rejects_shared_database_urls) + (kind(bin) & (test(/^tests::/) + test(/^composition_tests::/) + test(/^env_filter_tests::/)) - test(/^tests::postgres_tests::/))'
550552
# Note on audio::join::tests scope: the full suite is infra-free (no DB,
551553
# no Redis). The infra-free audio/FI regression witnesses — bootstrap
552554
# ordering barrier, CommitConfirmed arm, pending-close invisibility,

‎crates/buzz-db/src/error.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ pub enum DbError {
7777
#[error("deletion safety error: {0}")]
7878
DeletionSafety(String),
7979

80+
/// An artifact sync page exceeds its resource limit.
81+
#[error("artifact sync limit exceeded")]
82+
ArtifactSyncLimit,
83+
8084
/// A complete read-state snapshot exceeds its bounded resource budget.
8185
#[error("read-state snapshot exceeds event or byte limit")]
8286
ReadStateSnapshotTooLarge,

‎crates/buzz-db/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(crate) use runtime::{
6565
sweep_admitted_communities, ReadSessionInner, RouteDecision, RoutePredicate,
6666
};
6767
pub use store::{
68-
admin_moderation, allowlist, api_token, archived_identities, artifact, channel,
68+
admin_moderation, allowlist, api_token, archived_identities, artifact, artifact_sync, channel,
6969
channel_members, community, deletion, dm, entity_deletion, event, feed, git_repo, moderation,
7070
nip_fi_binding, operator_listener, partition, personal_read, product_feedback, push, reaction,
7171
read_state, relay_admin_actions, relay_invite, relay_members, relay_operators, reminder,

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ mod postgres_tests {
776776
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
777777
migrations.sort_by_key(|migration| migration.version);
778778

779-
assert_eq!(migrations.len(), 75);
779+
assert_eq!(migrations.len(), 78);
780780
for (index, migration) in migrations.iter().enumerate() {
781781
assert_eq!(migration.version, (index + 1) as i64);
782782
}
@@ -2918,6 +2918,10 @@ mod postgres_tests {
29182918
"relay_operator_audit",
29192919
"entity_deletion_approvals",
29202920
"entity_deletion_requests",
2921+
"artifact_heads",
2922+
"artifact_revisions",
2923+
"channel_members",
2924+
"communities",
29212925
"entity_deletion_stage_history",
29222926
"user_deletion_fences",
29232927
] {

‎crates/buzz-db/src/store/artifact.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,15 @@ impl Db {
185185
}
186186
}
187187
sqlx::query(
188-
"INSERT INTO artifact_revisions (community_id,event_id,artifact_id) VALUES ($1,$2,$3)",
188+
"INSERT INTO artifact_revisions (community_id,event_id,artifact_id,source_channel_id,modified_at) VALUES ($1,$2,$3,$4,clock_timestamp())",
189189
)
190190
.bind(community.as_uuid())
191191
.bind(event.id.as_bytes().as_slice())
192192
.bind(env.id)
193+
.bind(if env.op == ArtifactOp::Move { source } else { None })
193194
.execute(tx.conn())
194195
.await?;
195-
sqlx::query("INSERT INTO artifact_heads (community_id,artifact_id,event_id,channel_id,artifact_type,root,deleted) VALUES ($1,$2,$3,$4,$5,$6,$7) ON CONFLICT (community_id,artifact_id) DO UPDATE SET event_id=EXCLUDED.event_id,channel_id=EXCLUDED.channel_id,root=EXCLUDED.root,deleted=EXCLUDED.deleted")
196+
sqlx::query("INSERT INTO artifact_heads (community_id,artifact_id,event_id,channel_id,artifact_type,root,deleted,modified_at) VALUES ($1,$2,$3,$4,$5,$6,$7,clock_timestamp()) ON CONFLICT (community_id,artifact_id) DO UPDATE SET event_id=EXCLUDED.event_id,channel_id=EXCLUDED.channel_id,root=EXCLUDED.root,deleted=EXCLUDED.deleted,modified_at=clock_timestamp()")
196197
.bind(community.as_uuid()).bind(env.id).bind(event.id.as_bytes().as_slice()).bind(env.home).bind(&env.artifact_type).bind(&env.root).bind(env.op == ArtifactOp::Delete).execute(tx.conn()).await?;
197198
let (stored, _) =
198199
crate::event::insert_event_in_transaction(&mut tx, event, Some(env.home)).await?;

‎crates/buzz-db/src/store/artifact_query.rs‎

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,7 @@ impl Db {
3333
} else {
3434
"SELECT e.id,e.pubkey,e.created_at,e.kind,e.tags,e.content,e.sig,e.received_at,e.channel_id FROM events e "
3535
});
36-
if query.view == ArtifactView::Current {
37-
qb.push("JOIN artifact_heads a ON a.community_id=e.community_id AND a.event_id=e.id AND NOT a.deleted ");
38-
}
39-
// A system channel, or an unlisted open channel the reader is not a
40-
// member of, is read only by a query whose `#h` names it (NIP-CL);
41-
// malformed `#h` values name no channel.
42-
let named_channels: Vec<uuid::Uuid> = query
43-
.tags
44-
.iter()
45-
.filter(|(name, _)| name == "h")
46-
.flat_map(|(_, values)| values.iter().filter_map(|value| value.parse().ok()))
47-
.collect();
48-
// The explicit kind lets the community/kind index exclude chat before
49-
// the join, even for an unfiltered current-state page.
50-
qb.push("WHERE e.community_id=").push_bind(community.as_uuid()).push(" AND e.kind=45010 AND e.deleted_at IS NULL AND EXISTS (SELECT 1 FROM channels c WHERE c.community_id=e.community_id AND c.id=e.channel_id AND c.deleted_at IS NULL AND (")
51-
.push(crate::store::channel::legacy_channel_type_sql("c.channel_type"))
52-
.push(" OR c.id=ANY(")
53-
.push_bind(named_channels.clone())
54-
.push(")) AND ((c.visibility='open' AND (NOT c.unlisted OR c.id=ANY(")
55-
.push_bind(named_channels)
56-
.push("))) OR EXISTS (SELECT 1 FROM channel_members m WHERE m.community_id=c.community_id AND m.channel_id=c.id AND m.pubkey=")
57-
.push_bind(reader.to_vec()).push(" AND m.removed_at IS NULL)))");
58-
for (name, values) in &query.tags {
59-
// GIN-usable necessary prefilter; retain the positional recheck
60-
// because JSON array containment alone also matches swapped values.
61-
qb.push(" AND (");
62-
for (index, value) in values.iter().enumerate() {
63-
if index != 0 {
64-
qb.push(" OR ");
65-
}
66-
qb.push("e.tags @> ")
67-
.push_bind(serde_json::json!([[name, value]]));
68-
}
69-
qb.push(")");
70-
qb.push(" AND EXISTS (SELECT 1 FROM jsonb_array_elements(e.tags) tag WHERE tag->>0=")
71-
.push_bind(name)
72-
.push(" AND tag->>1=ANY(")
73-
.push_bind(values)
74-
.push("))");
75-
}
36+
predicates(&mut qb, community, reader, query);
7637
if count {
7738
let n: i64 = qb.build_query_scalar().fetch_one(&mut *tx).await?;
7839
tx.commit().await?;
@@ -93,3 +54,68 @@ impl Db {
9354
Ok((events, 0))
9455
}
9556
}
57+
58+
/// Own the visibility and tag rules for artifact queries and current-ID reads.
59+
pub(super) fn predicates(
60+
qb: &mut QueryBuilder<sqlx::Postgres>,
61+
community: CommunityId,
62+
reader: &[u8],
63+
query: &ArtifactQuery,
64+
) {
65+
if query.view == ArtifactView::Current {
66+
qb.push("JOIN artifact_heads a ON a.community_id=e.community_id AND a.event_id=e.id AND NOT a.deleted ");
67+
}
68+
// System channels and unlisted open channels require an explicit `#h`
69+
// unless the reader is a member of the unlisted channel (NIP-CL).
70+
let named_channels: Vec<uuid::Uuid> = query
71+
.tags
72+
.iter()
73+
.filter(|(name, _)| name == "h")
74+
.flat_map(|(_, values)| values.iter().filter_map(|value| value.parse().ok()))
75+
.collect();
76+
// The explicit kind lets the community/kind index exclude chat before
77+
// the join, even for an unfiltered current-state page.
78+
qb.push("WHERE e.community_id=").push_bind(community.as_uuid()).push(" AND e.kind=45010 AND e.deleted_at IS NULL AND EXISTS (SELECT 1 FROM channels c WHERE c.community_id=e.community_id AND c.id=e.channel_id AND c.deleted_at IS NULL AND (")
79+
.push(crate::store::channel::legacy_channel_type_sql("c.channel_type"))
80+
.push(" OR c.id=ANY(")
81+
.push_bind(named_channels.clone())
82+
.push(")) AND ((c.visibility='open' AND (NOT c.unlisted OR c.id=ANY(")
83+
.push_bind(named_channels)
84+
.push("))) OR EXISTS (SELECT 1 FROM channel_members m WHERE m.community_id=c.community_id AND m.channel_id=c.id AND m.pubkey=")
85+
.push_bind(reader.to_vec()).push(" AND m.removed_at IS NULL)))");
86+
tag_predicates(qb, query);
87+
if query.view == ArtifactView::Current {
88+
for (_, values) in query.tags.iter().filter(|(name, _)| name == "d") {
89+
qb.push(" AND a.artifact_id=ANY(")
90+
.push_bind(
91+
values
92+
.iter()
93+
.filter_map(|id| id.parse::<uuid::Uuid>().ok())
94+
.collect::<Vec<_>>(),
95+
)
96+
.push(")");
97+
}
98+
}
99+
}
100+
101+
/// Apply exact artifact tag predicates after access is resolved.
102+
pub(super) fn tag_predicates(qb: &mut QueryBuilder<sqlx::Postgres>, query: &ArtifactQuery) {
103+
for (name, values) in &query.tags {
104+
// GIN-usable necessary prefilter; retain the positional recheck
105+
// because JSON array containment alone also matches swapped values.
106+
qb.push(" AND (");
107+
for (index, value) in values.iter().enumerate() {
108+
if index != 0 {
109+
qb.push(" OR ");
110+
}
111+
qb.push("e.tags @> ")
112+
.push_bind(serde_json::json!([[name, value]]));
113+
}
114+
qb.push(")");
115+
qb.push(" AND EXISTS (SELECT 1 FROM jsonb_array_elements(e.tags) tag WHERE tag->>0=")
116+
.push_bind(name)
117+
.push(" AND tag->>1=ANY(")
118+
.push_bind(values)
119+
.push("))");
120+
}
121+
}

0 commit comments

Comments
 (0)