Skip to content

Commit 849b817

Browse files
refactor(db): move relay invite store ownership
Signed-off-by: tornquist <[email protected]>
1 parent 75a5146 commit 849b817

3 files changed

Lines changed: 76 additions & 49 deletions

File tree

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

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,54 +1335,6 @@ impl Db {
13351335
Ok(result.rows_affected())
13361336
}
13371337

1338-
/// Mints a v2 use-limited relay invite. The plaintext code is returned
1339-
/// exactly once; only its SHA-256 hash is persisted.
1340-
///
1341-
/// `max_uses` is `None` for unlimited or `Some(1..=10000)`.
1342-
/// `ttl_secs` must be in the shared invite lifetime range.
1343-
#[datastore_span(name = "mint_relay_invite", system = "postgresql")]
1344-
pub async fn mint_relay_invite(
1345-
&self,
1346-
community: CommunityId,
1347-
created_by: &str,
1348-
ttl_secs: u64,
1349-
max_uses: Option<i32>,
1350-
) -> Result<relay_invite::MintedInvite> {
1351-
relay_invite::mint_relay_invite(&self.pool, community, created_by, ttl_secs, max_uses).await
1352-
}
1353-
1354-
/// Delete one bounded batch of invites expired before `cutoff`.
1355-
#[datastore_span(name = "reap_expired_relay_invites", system = "postgresql")]
1356-
pub async fn reap_expired_relay_invites(
1357-
&self,
1358-
cutoff: chrono::DateTime<chrono::Utc>,
1359-
) -> Result<u64> {
1360-
relay_invite::reap_expired_relay_invites(&self.pool, cutoff).await
1361-
}
1362-
1363-
/// Atomically claims a v2 relay invite. The full redemption (membership
1364-
/// insert, policy evidence, use_count increment) runs in one PostgreSQL
1365-
/// transaction with `FOR UPDATE` on the invite row.
1366-
///
1367-
/// `token_hash` is the SHA-256 of the presented v2 code (32 bytes).
1368-
#[datastore_span(name = "claim_relay_invite", system = "postgresql")]
1369-
pub async fn claim_relay_invite(
1370-
&self,
1371-
community: CommunityId,
1372-
token_hash: &[u8; 32],
1373-
claimer_pubkey: &str,
1374-
policy_version: Option<&str>,
1375-
) -> Result<relay_invite::ClaimOutcome> {
1376-
relay_invite::claim_relay_invite(
1377-
&self.pool,
1378-
community,
1379-
token_hash,
1380-
claimer_pubkey,
1381-
policy_version,
1382-
)
1383-
.await
1384-
}
1385-
13861338
/// Sidecar an accepted product-feedback event, idempotent by event id.
13871339
#[datastore_span(name = "insert_product_feedback", system = "postgresql")]
13881340
pub async fn insert_product_feedback(

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ use buzz_core::invite::{
2121
encode_v2_code, hash_v2_code, MAX_INVITE_TTL_SECS, MAX_INVITE_USES, MIN_INVITE_TTL_SECS,
2222
V2_SECRET_LEN,
2323
};
24+
use buzz_datastore_tracing::datastore_span;
2425
use chrono::{DateTime, Utc};
2526
use sqlx::{PgPool, Row as _};
2627

2728
use crate::error::Result;
28-
use crate::CommunityId;
29+
use crate::{CommunityId, Db};
2930

3031
/// Outcome of a v2 invite claim. Expected invalid/expired/exhausted states are
3132
/// typed variants so the relay layer can map them to distinct HTTP responses
@@ -380,6 +381,53 @@ pub async fn claim_relay_invite(
380381
})
381382
}
382383

384+
impl Db {
385+
/// Mints a v2 use-limited relay invite. The plaintext code is returned
386+
/// exactly once; only its SHA-256 hash is persisted.
387+
///
388+
/// `max_uses` is `None` for unlimited or `Some(1..=10000)`.
389+
/// `ttl_secs` must be in the shared invite lifetime range.
390+
#[datastore_span(name = "mint_relay_invite", system = "postgresql")]
391+
pub async fn mint_relay_invite(
392+
&self,
393+
community: CommunityId,
394+
created_by: &str,
395+
ttl_secs: u64,
396+
max_uses: Option<i32>,
397+
) -> Result<MintedInvite> {
398+
mint_relay_invite(&self.pool, community, created_by, ttl_secs, max_uses).await
399+
}
400+
401+
/// Delete one bounded batch of invites expired before `cutoff`.
402+
#[datastore_span(name = "reap_expired_relay_invites", system = "postgresql")]
403+
pub async fn reap_expired_relay_invites(&self, cutoff: DateTime<Utc>) -> Result<u64> {
404+
reap_expired_relay_invites(&self.pool, cutoff).await
405+
}
406+
407+
/// Atomically claims a v2 relay invite. The full redemption (membership
408+
/// insert, policy evidence, use_count increment) runs in one PostgreSQL
409+
/// transaction with `FOR UPDATE` on the invite row.
410+
///
411+
/// `token_hash` is the SHA-256 of the presented v2 code (32 bytes).
412+
#[datastore_span(name = "claim_relay_invite", system = "postgresql")]
413+
pub async fn claim_relay_invite(
414+
&self,
415+
community: CommunityId,
416+
token_hash: &[u8; 32],
417+
claimer_pubkey: &str,
418+
policy_version: Option<&str>,
419+
) -> Result<ClaimOutcome> {
420+
claim_relay_invite(
421+
&self.pool,
422+
community,
423+
token_hash,
424+
claimer_pubkey,
425+
policy_version,
426+
)
427+
.await
428+
}
429+
}
430+
383431
#[cfg(test)]
384432
mod tests {
385433
use super::*;

‎crates/buzz-db/tests/store_ownership.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ fn relay_members_store_has_single_ownership() {
6666
);
6767
}
6868

69+
#[test]
70+
fn relay_invite_store_has_single_ownership() {
71+
let root = include_str!("../src/lib.rs");
72+
let store = include_str!("../src/relay_invite.rs");
73+
74+
for method in [
75+
"mint_relay_invite",
76+
"reap_expired_relay_invites",
77+
"claim_relay_invite",
78+
] {
79+
let signature = format!("pub async fn {method}(");
80+
assert_eq!(count(root, &signature), 0, "{method} remains in lib.rs");
81+
assert_eq!(
82+
count(store, &signature),
83+
2,
84+
"{method} must have one Db method and one SQL function in relay_invite.rs"
85+
);
86+
let span = format!("name = \"{method}\"");
87+
assert_eq!(count(store, &span), 1, "{method} span is not unique");
88+
}
89+
90+
for ty in ["pub enum ClaimOutcome", "pub struct MintedInvite"] {
91+
assert_eq!(count(root, ty), 0, "{ty} remains in lib.rs");
92+
assert_eq!(count(store, ty), 1, "{ty} is not singly owned");
93+
}
94+
}
95+
6996
#[test]
7097
fn channel_and_membership_stores_have_single_ownership() {
7198
let root = include_str!("../src/lib.rs");

0 commit comments

Comments
 (0)