Skip to content

Commit a810c50

Browse files
tlongwell-blocknpub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757klopez4212npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d
authored
Apply optional relay join policy across join flows (#1894)
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: klopez4212 <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co>
1 parent e95382d commit a810c50

31 files changed

Lines changed: 2063 additions & 106 deletions

‎.env.example‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,10 @@ RUST_LOG=buzz_relay=debug,buzz_db=debug,buzz_auth=debug,buzz_pubsub=debug,tower_
202202
# These are accepted for backward compatibility but the canonical names above
203203
# are preferred:
204204
# BUZZ_ACP_PRIVATE_KEY → BUZZ_PRIVATE_KEY
205+
206+
# Optional relay join policy. Markdown is served by the relay so every join
207+
# surface can present the same documents. Each document and the independent age
208+
# attestation are optional; configuring any one enables policy acceptance.
209+
# BUZZ_TERMS_OF_SERVICE_MARKDOWN="# Terms of Service\n\nFull terms here."
210+
# BUZZ_PRIVACY_POLICY_MARKDOWN="# Privacy Policy\n\nFull policy here."
211+
# BUZZ_AGE_ATTESTATION_REQUIRED=true

‎Cargo.lock‎

Lines changed: 32 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,30 @@ impl Db {
26892689
relay_members::add_relay_member(&self.pool, community, pubkey, role, added_by).await
26902690
}
26912691

2692+
/// Claims relay membership via an invite and atomically persists the
2693+
/// accepted policy version when a policy is configured.
2694+
pub async fn claim_relay_membership(
2695+
&self,
2696+
community: CommunityId,
2697+
pubkey: &str,
2698+
role: &str,
2699+
policy_version: Option<&str>,
2700+
) -> Result<bool> {
2701+
relay_members::claim_relay_membership(&self.pool, community, pubkey, role, policy_version)
2702+
.await
2703+
}
2704+
2705+
/// Returns whether a member has persisted acceptance evidence for a policy version.
2706+
pub async fn has_join_policy_acceptance(
2707+
&self,
2708+
community: CommunityId,
2709+
pubkey: &str,
2710+
policy_version: &str,
2711+
) -> Result<bool> {
2712+
relay_members::has_join_policy_acceptance(&self.pool, community, pubkey, policy_version)
2713+
.await
2714+
}
2715+
26922716
/// Removes a relay member from `community` atomically, refusing to delete the owner.
26932717
pub async fn remove_relay_member(
26942718
&self,

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ mod tests {
549549
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
550550
migrations.sort_by_key(|migration| migration.version);
551551

552-
assert_eq!(migrations.len(), 19);
552+
assert_eq!(migrations.len(), 20);
553553
assert_eq!(migrations[0].version, 1);
554554
assert_eq!(&*migrations[0].description, "initial schema");
555555
assert!(migrations[0]
@@ -812,6 +812,18 @@ mod tests {
812812
.sql
813813
.as_str()
814814
.contains("purge_soft_deleted_buzz_mesh_status"));
815+
816+
// Join policy acceptances landed concurrently with mesh status retention;
817+
// keep both additive migrations in a single, unambiguous sequence.
818+
assert_eq!(migrations[19].version, 20);
819+
assert!(migrations[19]
820+
.sql
821+
.as_str()
822+
.contains("CREATE TABLE join_policy_acceptances"));
823+
assert!(!migrations[0]
824+
.sql
825+
.as_str()
826+
.contains("join_policy_acceptances"));
815827
}
816828

817829
#[test]
@@ -1054,7 +1066,7 @@ mod tests {
10541066
run_migrations(&pool)
10551067
.await
10561068
.expect("retry succeeds after operator repair");
1057-
assert_eq!(applied_versions(&pool).await.last().copied(), Some(19));
1069+
assert_eq!(applied_versions(&pool).await.last().copied(), Some(20));
10581070
}
10591071

10601072
#[tokio::test]

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,67 @@ pub async fn add_relay_member(
114114
Ok(result.rows_affected() > 0)
115115
}
116116

117+
/// Claims relay membership via an invite and atomically persists policy evidence.
118+
///
119+
/// Returns `true` when membership was inserted, or `false` when the pubkey was
120+
/// already a member. A configured `policy_version` is recorded in the same
121+
/// transaction, so membership cannot be granted without its acceptance record.
122+
pub async fn claim_relay_membership(
123+
pool: &PgPool,
124+
community: CommunityId,
125+
pubkey: &str,
126+
role: &str,
127+
policy_version: Option<&str>,
128+
) -> Result<bool> {
129+
let mut tx = pool.begin().await?;
130+
let inserted = sqlx::query(
131+
"INSERT INTO relay_members (community_id, pubkey, role, added_by) \
132+
VALUES ($1, $2, $3, 'invite') \
133+
ON CONFLICT (community_id, pubkey) DO NOTHING",
134+
)
135+
.bind(community.as_uuid())
136+
.bind(pubkey)
137+
.bind(role)
138+
.execute(&mut *tx)
139+
.await?
140+
.rows_affected()
141+
> 0;
142+
143+
if let Some(version) = policy_version {
144+
sqlx::query(
145+
"INSERT INTO join_policy_acceptances (community_id, pubkey, policy_version) \
146+
VALUES ($1, $2, $3) ON CONFLICT DO NOTHING",
147+
)
148+
.bind(community.as_uuid())
149+
.bind(pubkey)
150+
.bind(version)
151+
.execute(&mut *tx)
152+
.await?;
153+
}
154+
155+
tx.commit().await?;
156+
Ok(inserted)
157+
}
158+
159+
/// Returns whether a member has persisted acceptance evidence for a policy version.
160+
pub async fn has_join_policy_acceptance(
161+
pool: &PgPool,
162+
community: CommunityId,
163+
pubkey: &str,
164+
policy_version: &str,
165+
) -> Result<bool> {
166+
let row = sqlx::query(
167+
"SELECT 1 FROM join_policy_acceptances \
168+
WHERE community_id = $1 AND pubkey = $2 AND policy_version = $3",
169+
)
170+
.bind(community.as_uuid())
171+
.bind(pubkey)
172+
.bind(policy_version)
173+
.fetch_optional(pool)
174+
.await?;
175+
Ok(row.is_some())
176+
}
177+
117178
/// The result of a relay member removal attempt.
118179
#[derive(Debug, PartialEq)]
119180
pub enum RemoveResult {
@@ -544,6 +605,38 @@ mod tests {
544605
(community, owner)
545606
}
546607

608+
#[tokio::test]
609+
#[ignore = "requires Postgres"]
610+
async fn invite_claim_persists_policy_version_and_legacy_claim_does_not() {
611+
let pool = setup_pool().await;
612+
let community = make_test_community(&pool).await;
613+
let policy_member = test_pubkey();
614+
let legacy_member = test_pubkey();
615+
let version = "a".repeat(64);
616+
617+
assert!(
618+
claim_relay_membership(&pool, community, &policy_member, "member", Some(&version),)
619+
.await
620+
.expect("claim membership with policy")
621+
);
622+
assert!(
623+
has_join_policy_acceptance(&pool, community, &policy_member, &version)
624+
.await
625+
.expect("policy acceptance lookup")
626+
);
627+
628+
assert!(
629+
claim_relay_membership(&pool, community, &legacy_member, "member", None)
630+
.await
631+
.expect("legacy claim membership")
632+
);
633+
assert!(
634+
!has_join_policy_acceptance(&pool, community, &legacy_member, &version)
635+
.await
636+
.expect("legacy acceptance lookup")
637+
);
638+
}
639+
547640
/// NIP-43 admission confinement: a pubkey admitted to community A is *not*
548641
/// admitted to community B. This is the exact mutation #1285 targets — a
549642
/// `WHERE pubkey = $1` membership check (no community predicate) would let an

‎crates/buzz-relay/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ moka = { workspace = true }
7777
metrics = { workspace = true }
7878
metrics-exporter-prometheus = { workspace = true }
7979
metrics-util = { workspace = true }
80+
pulldown-cmark = { version = "0.13.4", default-features = false, features = ["html"] }
8081

8182
[features]
8283
dev = ["buzz-auth/dev"]

0 commit comments

Comments
 (0)