@@ -11,6 +11,7 @@ use uuid::Uuid;
1111
1212use buzz_core:: kind:: {
1313 event_kind_i32, is_ephemeral, is_parameterized_replaceable, KIND_AUTH , KIND_EVENT_REMINDER ,
14+ KIND_HUDDLE_STARTED ,
1415} ;
1516use buzz_core:: { CommunityId , StoredEvent } ;
1617
@@ -105,6 +106,16 @@ impl EventQuery {
105106/// anything beyond this is either a bug or abuse.
106107pub const D_TAG_MAX_LEN : usize = 1024 ;
107108
109+ /// Maximum huddle-start content bytes considered by the parent-link lookup.
110+ ///
111+ /// The canonical content is a small JSON object containing one UUID. Rejecting
112+ /// oversized candidates keeps a malformed lifecycle event from making audio
113+ /// admission pull large text rows into memory.
114+ const HUDDLE_LINK_CONTENT_MAX_BYTES : i64 = 512 ;
115+ /// Maximum candidate rows inspected after SQL prefiltering by parent, creator,
116+ /// kind, and UUID substring.
117+ const HUDDLE_LINK_CANDIDATE_LIMIT : i64 = 32 ;
118+
108119/// Extract the `d_tag` value for storage.
109120///
110121/// For NIP-33 parameterized replaceable events (kind 30000–39999): returns the first
@@ -150,6 +161,62 @@ pub fn extract_not_before(event: &Event) -> Option<i64> {
150161 } )
151162}
152163
164+ fn huddle_started_content_links ( content : & str , ephemeral_channel_id : Uuid ) -> bool {
165+ serde_json:: from_str :: < serde_json:: Value > ( content)
166+ . ok ( )
167+ . and_then ( |value| {
168+ value
169+ . get ( "ephemeral_channel_id" )
170+ . and_then ( serde_json:: Value :: as_str)
171+ . and_then ( |id| Uuid :: parse_str ( id) . ok ( ) )
172+ } )
173+ . is_some_and ( |id| id == ephemeral_channel_id)
174+ }
175+
176+ /// Return whether `parent_channel_id` has a creator-signed huddle-start event
177+ /// that links to `ephemeral_channel_id`.
178+ ///
179+ /// The creator constraint matters: a member of some unrelated channel can post
180+ /// their own kind:48100 event there, but they cannot sign as the creator of the
181+ /// target ephemeral channel.
182+ pub async fn huddle_started_link_exists (
183+ pool : & PgPool ,
184+ community_id : CommunityId ,
185+ parent_channel_id : Uuid ,
186+ ephemeral_channel_id : Uuid ,
187+ creator_pubkey : & [ u8 ] ,
188+ ) -> Result < bool > {
189+ let uuid_needle = format ! ( "%{}%" , ephemeral_channel_id) ;
190+ let candidates: Vec < String > = sqlx:: query_scalar (
191+ r#"
192+ SELECT content
193+ FROM events
194+ WHERE deleted_at IS NULL
195+ AND community_id = $1
196+ AND channel_id = $2
197+ AND kind = $3
198+ AND pubkey = $4
199+ AND octet_length(content) <= $5
200+ AND content ILIKE $6
201+ ORDER BY created_at DESC, id ASC
202+ LIMIT $7
203+ "# ,
204+ )
205+ . bind ( community_id. as_uuid ( ) )
206+ . bind ( parent_channel_id)
207+ . bind ( KIND_HUDDLE_STARTED as i32 )
208+ . bind ( creator_pubkey)
209+ . bind ( HUDDLE_LINK_CONTENT_MAX_BYTES )
210+ . bind ( uuid_needle)
211+ . bind ( HUDDLE_LINK_CANDIDATE_LIMIT )
212+ . fetch_all ( pool)
213+ . await ?;
214+
215+ Ok ( candidates
216+ . iter ( )
217+ . any ( |content| huddle_started_content_links ( content, ephemeral_channel_id) ) )
218+ }
219+
153220/// Insert a Nostr event. Rejects AUTH and ephemeral kinds.
154221///
155222/// Returns `(StoredEvent, was_inserted)` — `was_inserted` is `false` on duplicate.
@@ -1709,4 +1776,21 @@ mod tests {
17091776 "A/X must be reclaimable after its own release"
17101777 ) ;
17111778 }
1779+
1780+ #[ test]
1781+ fn huddle_started_content_requires_matching_ephemeral_field ( ) {
1782+ let channel_id = Uuid :: new_v4 ( ) ;
1783+ let matching = serde_json:: json!( {
1784+ "ephemeral_channel_id" : channel_id. to_string( ) ,
1785+ } )
1786+ . to_string ( ) ;
1787+ assert ! ( huddle_started_content_links( & matching, channel_id) ) ;
1788+
1789+ let wrong_field = serde_json:: json!( {
1790+ "other" : channel_id. to_string( ) ,
1791+ } )
1792+ . to_string ( ) ;
1793+ assert ! ( !huddle_started_content_links( & wrong_field, channel_id) ) ;
1794+ assert ! ( !huddle_started_content_links( "not-json" , channel_id) ) ;
1795+ }
17121796}
0 commit comments