Skip to content

Commit 0950d39

Browse files
kalvinnchaunpub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7
andauthored
fix(relay): publish membership snapshot on provisioning (#1761)
Signed-off-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@sprout-oss.stage.blox.sqprod.co>
1 parent bea507d commit 0950d39

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎crates/buzz-relay/src/api/operator.rs‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ mod tests {
260260
use tower::ServiceExt;
261261
use uuid::Uuid;
262262

263+
use buzz_core::kind::KIND_NIP43_MEMBERSHIP_LIST;
264+
use buzz_db::event::EventQuery;
265+
263266
use crate::router::build_router;
264267
use crate::state::AppState;
265268

@@ -324,6 +327,7 @@ mod tests {
324327
.iter()
325328
.map(|keys| keys.public_key().to_hex())
326329
.collect();
330+
config.require_relay_membership = true;
327331

328332
let pool = sqlx::PgPool::connect(TEST_DB_URL).await.ok()?;
329333
let db = buzz_db::Db::from_pool(pool.clone());
@@ -512,6 +516,7 @@ mod tests {
512516
let body = serde_json::json!({
513517
"host": host,
514518
"initial_owner_pubkey": owner.public_key().to_hex(),
519+
"create_only": true,
515520
})
516521
.to_string();
517522
let url = format!("http://{INGRESS_HOST}/operator/communities");
@@ -544,12 +549,37 @@ mod tests {
544549
.await
545550
.expect("lookup community")
546551
.expect("community exists");
552+
let owner_hex = owner.public_key().to_hex();
547553
let member = state
548554
.db
549-
.get_relay_member(community.id, &owner.public_key().to_hex())
555+
.get_relay_member(community.id, &owner_hex)
550556
.await
551557
.expect("lookup owner role")
552558
.expect("owner member exists");
553559
assert_eq!(member.role, "owner");
560+
561+
let membership_events = state
562+
.db
563+
.query_events(&EventQuery {
564+
kinds: Some(vec![KIND_NIP43_MEMBERSHIP_LIST as i32]),
565+
global_only: true,
566+
limit: Some(1),
567+
..EventQuery::for_community(community.id)
568+
})
569+
.await
570+
.expect("query membership snapshot");
571+
let snapshot = membership_events
572+
.first()
573+
.expect("membership snapshot published during provisioning");
574+
assert!(snapshot.event.tags.iter().any(|tag| {
575+
tag.as_slice()
576+
.first()
577+
.is_some_and(|value| value == "member")
578+
&& tag
579+
.as_slice()
580+
.get(1)
581+
.is_some_and(|value| value == &owner_hex)
582+
&& tag.as_slice().get(2).is_some_and(|value| value == "owner")
583+
}));
554584
}
555585
}

‎crates/buzz-relay/src/handlers/community_provisioning.rs‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
use std::sync::Arc;
2929

3030
use serde::{Deserialize, Serialize};
31-
use tracing::info;
31+
use tracing::{info, warn};
3232

33-
use buzz_core::tenant::normalize_host;
33+
use buzz_core::tenant::{normalize_host, TenantContext};
3434
use url::{Host, Url};
3535

3636
use crate::state::AppState;
@@ -201,6 +201,34 @@ pub(crate) fn normalize_candidate_host(host: &str) -> Result<String, String> {
201201
Ok(normalized)
202202
}
203203

204+
/// Publish the event-backed membership view after owner persistence.
205+
///
206+
/// Provisioning is already committed when this runs. Publication therefore
207+
/// remains best-effort, matching every other membership mutation path: turning
208+
/// a stored success into an HTTP failure would make create-only retries report
209+
/// a misleading conflict while leaving clients without a repair path.
210+
async fn publish_membership_snapshot_if_required(
211+
state: &Arc<AppState>,
212+
community: buzz_core::CommunityId,
213+
host: &str,
214+
) {
215+
if !state.config.require_relay_membership {
216+
return;
217+
}
218+
219+
let tenant = TenantContext::resolved(community, host);
220+
if let Err(error) =
221+
crate::handlers::side_effects::publish_nip43_membership_list(&tenant, state).await
222+
{
223+
warn!(
224+
community = %community,
225+
host,
226+
error = %error,
227+
"community provisioned but NIP-43 membership snapshot publication failed"
228+
);
229+
}
230+
}
231+
204232
/// Validate and execute a relay-operator community provisioning request.
205233
///
206234
/// The caller is an HTTP operator endpoint, not the Nostr event ingest path.
@@ -267,6 +295,7 @@ pub async fn provision_community(
267295
owner = %owner_hex,
268296
"community created via operator endpoint"
269297
);
298+
publish_membership_snapshot_if_required(state, record.id, &record.host).await;
270299
return Ok(ProvisionCommunityResponse {
271300
community_id: record.id.to_string(),
272301
host: record.host,
@@ -290,6 +319,7 @@ pub async fn provision_community(
290319
.bootstrap_owner(record.id, owner_hex)
291320
.await
292321
.map_err(|e| format!("community provisioned but owner bootstrap failed: {e}"))?;
322+
publish_membership_snapshot_if_required(state, record.id, &record.host).await;
293323
}
294324

295325
info!(

0 commit comments

Comments
 (0)