@@ -38,6 +38,20 @@ pub struct DmParticipant {
3838 pub role : String ,
3939}
4040
41+ /// One bounded retry-queue claim for a relay-authored DM visibility snapshot.
42+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
43+ pub struct ClaimedDmVisibilityViewer {
44+ /// Community containing the viewer's DM memberships.
45+ pub community_id : CommunityId ,
46+ /// Canonical host used to construct the tenant context for fan-out.
47+ pub community_host : String ,
48+ /// Viewer whose kind:30622 projection needs rebuilding.
49+ pub viewer : Vec < u8 > ,
50+ /// Claim fence used to release a failed publication without touching a
51+ /// newer mutation that reset the queue row.
52+ pub claim_id : Uuid ,
53+ }
54+
4155// -- Pure helpers -------------------------------------------------------------
4256
4357/// Compute a stable SHA-256 fingerprint for a set of participant pubkeys.
@@ -402,9 +416,25 @@ pub async fn hide_dm(
402416) -> Result < ( ) > {
403417 let result = sqlx:: query (
404418 r#"
405- UPDATE channel_members
406- SET hidden_at = NOW()
407- WHERE community_id = $1 AND channel_id = $2 AND pubkey = $3 AND removed_at IS NULL
419+ WITH changed AS (
420+ UPDATE channel_members
421+ SET hidden_at = NOW()
422+ WHERE community_id = $1
423+ AND channel_id = $2
424+ AND pubkey = $3
425+ AND removed_at IS NULL
426+ RETURNING pubkey
427+ )
428+ INSERT INTO dm_visibility_dirty_viewers (community_id, viewer)
429+ SELECT $1, pubkey FROM changed
430+ ON CONFLICT (community_id, viewer) DO UPDATE SET
431+ generation = dm_visibility_dirty_viewers.generation + 1,
432+ state = 'pending',
433+ dirty_at = NOW(),
434+ next_attempt_at = NOW(),
435+ claim_id = NULL,
436+ lease_until = NULL,
437+ attempts = 0
408438 "# ,
409439 )
410440 . bind ( community_id. as_uuid ( ) )
@@ -434,9 +464,25 @@ pub async fn unhide_dm(
434464) -> Result < ( ) > {
435465 sqlx:: query (
436466 r#"
437- UPDATE channel_members
438- SET hidden_at = NULL
439- WHERE community_id = $1 AND channel_id = $2 AND pubkey = $3 AND removed_at IS NULL
467+ WITH changed AS (
468+ UPDATE channel_members
469+ SET hidden_at = NULL
470+ WHERE community_id = $1
471+ AND channel_id = $2
472+ AND pubkey = $3
473+ AND removed_at IS NULL
474+ RETURNING pubkey
475+ )
476+ INSERT INTO dm_visibility_dirty_viewers (community_id, viewer)
477+ SELECT $1, pubkey FROM changed
478+ ON CONFLICT (community_id, viewer) DO UPDATE SET
479+ generation = dm_visibility_dirty_viewers.generation + 1,
480+ state = 'pending',
481+ dirty_at = NOW(),
482+ next_attempt_at = NOW(),
483+ claim_id = NULL,
484+ lease_until = NULL,
485+ attempts = 0
440486 "# ,
441487 )
442488 . bind ( community_id. as_uuid ( ) )
@@ -462,19 +508,32 @@ pub async fn unhide_dm_recipients(
462508) -> Result < Vec < Vec < u8 > > > {
463509 let rows = sqlx:: query (
464510 r#"
465- UPDATE channel_members cm
466- SET hidden_at = NULL
467- FROM channels c
468- WHERE cm.community_id = $1
469- AND cm.channel_id = $2
470- AND cm.pubkey != $3
471- AND cm.removed_at IS NULL
472- AND cm.hidden_at IS NOT NULL
473- AND c.community_id = cm.community_id
474- AND c.id = cm.channel_id
475- AND c.channel_type = 'dm'
476- AND c.deleted_at IS NULL
477- RETURNING cm.pubkey
511+ WITH changed AS (
512+ UPDATE channel_members cm
513+ SET hidden_at = NULL
514+ FROM channels c
515+ WHERE cm.community_id = $1
516+ AND cm.channel_id = $2
517+ AND cm.pubkey != $3
518+ AND cm.removed_at IS NULL
519+ AND cm.hidden_at IS NOT NULL
520+ AND c.community_id = cm.community_id
521+ AND c.id = cm.channel_id
522+ AND c.channel_type = 'dm'
523+ AND c.deleted_at IS NULL
524+ RETURNING cm.pubkey
525+ )
526+ INSERT INTO dm_visibility_dirty_viewers (community_id, viewer)
527+ SELECT $1, pubkey FROM changed
528+ ON CONFLICT (community_id, viewer) DO UPDATE SET
529+ generation = dm_visibility_dirty_viewers.generation + 1,
530+ state = 'pending',
531+ dirty_at = NOW(),
532+ next_attempt_at = NOW(),
533+ claim_id = NULL,
534+ lease_until = NULL,
535+ attempts = 0
536+ RETURNING viewer
478537 "# ,
479538 )
480539 . bind ( community_id. as_uuid ( ) )
@@ -484,10 +543,95 @@ pub async fn unhide_dm_recipients(
484543 . await ?;
485544
486545 rows. into_iter ( )
487- . map ( |row| row. try_get :: < Vec < u8 > , _ > ( "pubkey " ) . map_err ( Into :: into) )
546+ . map ( |row| row. try_get :: < Vec < u8 > , _ > ( "viewer " ) . map_err ( Into :: into) )
488547 . collect ( )
489548}
490549
550+ /// Claim at most `limit` indexed dirty-viewer rows across active communities.
551+ /// Expired claims are recovered by the same bounded query.
552+ pub async fn claim_dm_visibility_dirty_viewers (
553+ pool : & PgPool ,
554+ limit : i64 ,
555+ lease_seconds : i64 ,
556+ ) -> Result < Vec < ClaimedDmVisibilityViewer > > {
557+ let claim_id = Uuid :: new_v4 ( ) ;
558+ let rows = sqlx:: query (
559+ r#"
560+ WITH candidates AS (
561+ SELECT q.community_id, q.viewer
562+ FROM dm_visibility_dirty_viewers q
563+ JOIN communities c ON c.id = q.community_id
564+ WHERE c.archived_at IS NULL
565+ AND c.deleted_at IS NULL
566+ AND c.deletion_state = 'active'
567+ AND community_write_allowed(q.community_id)
568+ AND (
569+ (q.state = 'pending' AND q.next_attempt_at <= NOW())
570+ OR (q.state = 'publishing' AND q.lease_until <= NOW())
571+ )
572+ ORDER BY COALESCE(q.lease_until, q.next_attempt_at), q.dirty_at
573+ FOR UPDATE OF q SKIP LOCKED
574+ LIMIT $1
575+ )
576+ UPDATE dm_visibility_dirty_viewers q
577+ SET state = 'publishing',
578+ claim_id = $2,
579+ lease_until = NOW() + make_interval(secs => $3::int),
580+ attempts = q.attempts + 1
581+ FROM candidates candidate, communities c
582+ WHERE q.community_id = candidate.community_id
583+ AND q.viewer = candidate.viewer
584+ AND c.id = q.community_id
585+ RETURNING q.community_id, c.host, q.viewer
586+ "# ,
587+ )
588+ . bind ( limit. clamp ( 1 , 1_000 ) )
589+ . bind ( claim_id)
590+ . bind ( lease_seconds. clamp ( 1 , 3_600 ) as i32 )
591+ . fetch_all ( pool)
592+ . await ?;
593+
594+ rows. into_iter ( )
595+ . map ( |row| {
596+ Ok ( ClaimedDmVisibilityViewer {
597+ community_id : CommunityId :: from_uuid ( row. try_get ( "community_id" ) ?) ,
598+ community_host : row. try_get ( "host" ) ?,
599+ viewer : row. try_get ( "viewer" ) ?,
600+ claim_id,
601+ } )
602+ } )
603+ . collect ( )
604+ }
605+
606+ /// Release a failed queue claim for a bounded retry. A mutation that occurred
607+ /// after the claim resets `claim_id`, so this cannot overwrite newer work.
608+ pub async fn retry_dm_visibility_dirty_viewer (
609+ pool : & PgPool ,
610+ community_id : CommunityId ,
611+ viewer : & [ u8 ] ,
612+ claim_id : Uuid ,
613+ ) -> Result < ( ) > {
614+ sqlx:: query (
615+ r#"
616+ UPDATE dm_visibility_dirty_viewers
617+ SET state = 'pending',
618+ next_attempt_at = NOW() + INTERVAL '5 seconds',
619+ claim_id = NULL,
620+ lease_until = NULL
621+ WHERE community_id = $1
622+ AND viewer = $2
623+ AND state = 'publishing'
624+ AND claim_id = $3
625+ "# ,
626+ )
627+ . bind ( community_id. as_uuid ( ) )
628+ . bind ( viewer)
629+ . bind ( claim_id)
630+ . execute ( pool)
631+ . await ?;
632+ Ok ( ( ) )
633+ }
634+
491635/// Return the channel IDs of all DMs the given user currently has hidden
492636/// (`hidden_at IS NOT NULL`) while still being an active member. Used to build
493637/// the relay-signed NIP-DV visibility snapshot.
0 commit comments