@@ -2265,6 +2265,48 @@ impl Db {
22652265 Ok ( result)
22662266 }
22672267
2268+ /// Atomically inserts a DM message, its optional thread metadata, and the
2269+ /// recipient visibility mutations that make the accepted message visible.
2270+ ///
2271+ /// Duplicate events do not repeat the visibility mutation. The returned
2272+ /// viewers are the recipients whose canonical hidden state changed and
2273+ /// whose relay-authored visibility snapshots should be published after
2274+ /// this transaction commits.
2275+ #[ datastore_span( name = "insert_dm_event_with_thread_metadata" , system = "postgresql" ) ]
2276+ pub async fn insert_dm_event_with_thread_metadata (
2277+ & self ,
2278+ community_id : CommunityId ,
2279+ event : & nostr:: Event ,
2280+ channel_id : Uuid ,
2281+ thread_meta : Option < event:: ThreadMetadataParams < ' _ > > ,
2282+ sender_pubkey : & [ u8 ] ,
2283+ ) -> Result < ( StoredEvent , bool , Vec < Vec < u8 > > ) > {
2284+ let mut tx = self . pool . begin ( ) . await ?;
2285+ let ( stored_event, was_inserted) = event:: insert_event_with_thread_metadata_tx (
2286+ & mut tx,
2287+ community_id,
2288+ event,
2289+ Some ( channel_id) ,
2290+ thread_meta,
2291+ )
2292+ . await ?;
2293+ let resurfaced_viewers = if was_inserted {
2294+ dm:: unhide_dm_recipients_tx ( & mut tx, community_id, channel_id, sender_pubkey) . await ?
2295+ } else {
2296+ Vec :: new ( )
2297+ } ;
2298+ tx. commit ( ) . await ?;
2299+
2300+ if was_inserted {
2301+ if let Err ( e) = insert_mentions ( & self . pool , community_id, event, Some ( channel_id) ) . await
2302+ {
2303+ tracing:: warn!( event_id = %event. id, "Failed to insert mentions: {e}" ) ;
2304+ }
2305+ }
2306+
2307+ Ok ( ( stored_event, was_inserted, resurfaced_viewers) )
2308+ }
2309+
22682310 /// Atomically insert a kind:7 reaction event and its reaction row.
22692311 #[ allow( clippy:: too_many_arguments) ]
22702312 #[ datastore_span(
@@ -7500,6 +7542,156 @@ mod tests {
75007542 ) ;
75017543 }
75027544
7545+ #[ tokio:: test]
7546+ #[ ignore = "requires Postgres" ]
7547+ async fn dm_event_insert_and_recipient_resurface_roll_back_together ( ) {
7548+ use nostr:: { EventBuilder , Keys , Kind , Tag } ;
7549+
7550+ let admin = PgPool :: connect ( & admin_url ( ) . await )
7551+ . await
7552+ . expect ( "connect admin" ) ;
7553+ let ( pool, scratch_name) = create_scratch_db ( & admin, "dm_insert_resurface" ) . await ;
7554+ let db = Db :: from_pool ( pool. clone ( ) ) ;
7555+ let community_uuid = Uuid :: new_v4 ( ) ;
7556+ let community = CommunityId :: from_uuid ( community_uuid) ;
7557+ let dm_channel = Uuid :: new_v4 ( ) ;
7558+ let unrelated_dm = Uuid :: new_v4 ( ) ;
7559+ let sender = Keys :: generate ( ) ;
7560+ let recipient = Keys :: generate ( ) ;
7561+ let sender_bytes = sender. public_key ( ) . to_bytes ( ) ;
7562+ let recipient_bytes = recipient. public_key ( ) . to_bytes ( ) ;
7563+
7564+ sqlx:: query ( "INSERT INTO communities (id, host) VALUES ($1, $2)" )
7565+ . bind ( community_uuid)
7566+ . bind ( format ! ( "dm-insert-{}.example" , community_uuid. simple( ) ) )
7567+ . execute ( & pool)
7568+ . await
7569+ . expect ( "insert community" ) ;
7570+ for channel_id in [ dm_channel, unrelated_dm] {
7571+ sqlx:: query (
7572+ "INSERT INTO channels \
7573+ (id, community_id, name, channel_type, visibility, created_by) \
7574+ VALUES ($1, $2, 'atomic DM', 'dm', 'private', $3)",
7575+ )
7576+ . bind ( channel_id)
7577+ . bind ( community_uuid)
7578+ . bind ( sender_bytes. as_slice ( ) )
7579+ . execute ( & pool)
7580+ . await
7581+ . expect ( "insert DM" ) ;
7582+ }
7583+ for ( channel_id, pubkey) in [
7584+ ( dm_channel, sender_bytes. as_slice ( ) ) ,
7585+ ( dm_channel, recipient_bytes. as_slice ( ) ) ,
7586+ ( unrelated_dm, recipient_bytes. as_slice ( ) ) ,
7587+ ] {
7588+ sqlx:: query (
7589+ "INSERT INTO channel_members \
7590+ (community_id, channel_id, pubkey, hidden_at) \
7591+ VALUES ($1, $2, $3, NOW())",
7592+ )
7593+ . bind ( community_uuid)
7594+ . bind ( channel_id)
7595+ . bind ( pubkey)
7596+ . execute ( & pool)
7597+ . await
7598+ . expect ( "insert hidden DM member" ) ;
7599+ }
7600+
7601+ sqlx:: raw_sql (
7602+ "CREATE FUNCTION fail_dm_visibility_enqueue() RETURNS trigger \
7603+ LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected enqueue failure'; END $$; \
7604+ CREATE TRIGGER fail_dm_visibility_enqueue \
7605+ BEFORE INSERT OR UPDATE ON dm_visibility_dirty_viewers \
7606+ FOR EACH ROW EXECUTE FUNCTION fail_dm_visibility_enqueue();",
7607+ )
7608+ . execute ( & pool)
7609+ . await
7610+ . expect ( "install visibility failure injection" ) ;
7611+
7612+ let event = EventBuilder :: new ( Kind :: Custom ( 9 ) , "accepted DM" )
7613+ . tags ( vec ! [
7614+ Tag :: parse( [ "h" , & dm_channel. to_string( ) ] ) . expect( "h tag" )
7615+ ] )
7616+ . sign_with_keys ( & sender)
7617+ . expect ( "sign DM event" ) ;
7618+ db. insert_dm_event_with_thread_metadata (
7619+ community,
7620+ & event,
7621+ dm_channel,
7622+ None ,
7623+ sender_bytes. as_slice ( ) ,
7624+ )
7625+ . await
7626+ . expect_err ( "injected enqueue failure must abort accepted-message transaction" ) ;
7627+
7628+ let persisted_after_failure: i64 =
7629+ sqlx:: query_scalar ( "SELECT COUNT(*) FROM events WHERE community_id = $1 AND id = $2" )
7630+ . bind ( community_uuid)
7631+ . bind ( event. id . as_bytes ( ) . as_slice ( ) )
7632+ . fetch_one ( & pool)
7633+ . await
7634+ . expect ( "count rolled-back event" ) ;
7635+ assert_eq ! ( persisted_after_failure, 0 ) ;
7636+ let hidden_after_failure: bool = sqlx:: query_scalar (
7637+ "SELECT hidden_at IS NOT NULL FROM channel_members \
7638+ WHERE community_id = $1 AND channel_id = $2 AND pubkey = $3",
7639+ )
7640+ . bind ( community_uuid)
7641+ . bind ( dm_channel)
7642+ . bind ( recipient_bytes. as_slice ( ) )
7643+ . fetch_one ( & pool)
7644+ . await
7645+ . expect ( "read recipient after rollback" ) ;
7646+ assert ! ( hidden_after_failure) ;
7647+
7648+ sqlx:: raw_sql (
7649+ "DROP TRIGGER fail_dm_visibility_enqueue ON dm_visibility_dirty_viewers; \
7650+ DROP FUNCTION fail_dm_visibility_enqueue();",
7651+ )
7652+ . execute ( & pool)
7653+ . await
7654+ . expect ( "remove visibility failure injection" ) ;
7655+ let ( _stored, inserted, resurfaced) = db
7656+ . insert_dm_event_with_thread_metadata (
7657+ community,
7658+ & event,
7659+ dm_channel,
7660+ None ,
7661+ sender_bytes. as_slice ( ) ,
7662+ )
7663+ . await
7664+ . expect ( "retry accepted DM transaction" ) ;
7665+ assert ! ( inserted) ;
7666+ assert_eq ! ( resurfaced, vec![ recipient_bytes. to_vec( ) ] ) ;
7667+
7668+ let hidden_states: Vec < ( Uuid , Vec < u8 > , bool ) > = sqlx:: query_as (
7669+ "SELECT channel_id, pubkey, hidden_at IS NOT NULL \
7670+ FROM channel_members WHERE community_id = $1 ORDER BY channel_id, pubkey",
7671+ )
7672+ . bind ( community_uuid)
7673+ . fetch_all ( & pool)
7674+ . await
7675+ . expect ( "read final hidden states" ) ;
7676+ assert ! ( hidden_states
7677+ . iter( )
7678+ . any( |( channel, pubkey, hidden) | * channel == dm_channel
7679+ && pubkey == sender_bytes. as_slice( )
7680+ && * hidden) ) ;
7681+ assert ! ( hidden_states
7682+ . iter( )
7683+ . any( |( channel, pubkey, hidden) | * channel == dm_channel
7684+ && pubkey == recipient_bytes. as_slice( )
7685+ && !* hidden) ) ;
7686+ assert ! ( hidden_states
7687+ . iter( )
7688+ . any( |( channel, pubkey, hidden) | * channel == unrelated_dm
7689+ && pubkey == recipient_bytes. as_slice( )
7690+ && * hidden) ) ;
7691+
7692+ drop_scratch_db ( & admin, pool, & scratch_name) . await ;
7693+ }
7694+
75037695 #[ tokio:: test]
75047696 #[ ignore = "requires Postgres" ]
75057697 async fn dm_visibility_fresh_claims_are_not_starved_by_poison_retries ( ) {
@@ -7547,8 +7739,8 @@ mod tests {
75477739 ) ;
75487740 assert_eq ! (
75497741 claims. len( ) ,
7550- 26 ,
7551- "the batch reserves half for fresh work and a quarter for retries "
7742+ 100 ,
7743+ "reserved fairness capacity must not leave the rest of the batch idle "
75527744 ) ;
75537745
75547746 let retry_claim = claims
@@ -7563,7 +7755,7 @@ mod tests {
75637755 . await
75647756 . expect ( "release retry claim" ) ;
75657757 let retry_delay_seconds: f64 = sqlx:: query_scalar (
7566- "SELECT EXTRACT(EPOCH FROM (next_attempt_at - NOW())) \
7758+ "SELECT EXTRACT(EPOCH FROM (next_attempt_at - NOW()))::DOUBLE PRECISION \
75677759 FROM dm_visibility_dirty_viewers \
75687760 WHERE community_id = $1 AND viewer = $2",
75697761 )
0 commit comments