@@ -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 ) ]
119180pub 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
0 commit comments