@@ -622,14 +622,42 @@ async fn find_managed_agent_channel_message_by_marker(
622622
623623fn marker_author_for_scope < ' a > (
624624 marker_scope : Option < & str > ,
625- agent_pubkey : & ' a str ,
626- ) -> Option < & ' a str > {
625+ agent_pubkey : Option < & ' a str > ,
626+ ) -> Result < Option < & ' a str > , String > {
627627 match marker_scope {
628- Some ( "channel" ) => None ,
629- _ => Some ( agent_pubkey) ,
628+ Some ( "channel" ) => Ok ( None ) ,
629+ Some ( "agent" ) | None => agent_pubkey
630+ . map ( Some )
631+ . ok_or_else ( || "agent pubkey is required for agent-scoped markers" . to_string ( ) ) ,
632+ Some ( scope) => Err ( format ! ( "unsupported marker scope: {scope}" ) ) ,
630633 }
631634}
632635
636+ #[ tauri:: command]
637+ pub async fn has_managed_agent_channel_message_marker (
638+ channel_id : String ,
639+ marker : String ,
640+ agent_pubkey : Option < String > ,
641+ marker_scope : Option < String > ,
642+ state : State < ' _ , AppState > ,
643+ ) -> Result < bool , String > {
644+ uuid:: Uuid :: parse_str ( & channel_id)
645+ . map_err ( |_| format ! ( "invalid channel UUID: {channel_id}" ) ) ?;
646+ let marker = marker. trim ( ) ;
647+ if marker. is_empty ( ) {
648+ return Err ( "message marker is required" . into ( ) ) ;
649+ }
650+ let agent_pubkey = agent_pubkey
651+ . as_deref ( )
652+ . map ( str:: trim)
653+ . filter ( |value| !value. is_empty ( ) ) ;
654+
655+ let marker_author = marker_author_for_scope ( marker_scope. as_deref ( ) , agent_pubkey) ?;
656+ find_managed_agent_channel_message_by_marker ( & state, marker_author, & channel_id, marker)
657+ . await
658+ . map ( |event| event. is_some ( ) )
659+ }
660+
633661fn stored_managed_agent_auth_tag ( auth_tag : Option < & str > ) -> Option < String > {
634662 auth_tag
635663 . map ( str:: trim)
@@ -663,13 +691,37 @@ fn managed_agent_submission_auth_tag(
663691 legacy_managed_agent_auth_tag ( & owner_keys, agent_pubkey)
664692}
665693
694+ fn build_managed_agent_channel_message (
695+ channel_id : uuid:: Uuid ,
696+ content : & str ,
697+ thread_ref : Option < & events:: ThreadRef > ,
698+ mention_pubkeys : & [ String ] ,
699+ client_tags : & [ Vec < String > ] ,
700+ ) -> Result < nostr:: EventBuilder , String > {
701+ let mention_refs: Vec < & str > = mention_pubkeys. iter ( ) . map ( String :: as_str) . collect ( ) ;
702+ events:: build_message_with_client_tags (
703+ channel_id,
704+ content,
705+ thread_ref,
706+ & mention_refs,
707+ & [ ] ,
708+ & [ ] ,
709+ & [ ] ,
710+ client_tags,
711+ )
712+ }
713+
714+ #[ allow( clippy:: too_many_arguments) ]
666715#[ tauri:: command]
667716pub async fn send_managed_agent_channel_message (
668717 agent_pubkey : String ,
669718 channel_id : String ,
670719 content : String ,
671720 marker : Option < String > ,
672721 marker_scope : Option < String > ,
722+ mention_pubkeys : Option < Vec < String > > ,
723+ parent_event_id : Option < String > ,
724+ additional_markers : Option < Vec < String > > ,
673725 app : AppHandle ,
674726 state : State < ' _ , AppState > ,
675727) -> Result < SendChannelMessageResponse , String > {
@@ -706,196 +758,64 @@ pub async fn send_managed_agent_channel_message(
706758 }
707759 let submission_auth_tag =
708760 managed_agent_submission_auth_tag ( & record, & state, & keys. public_key ( ) ) ?;
761+ let thread_ref = match parent_event_id. as_deref ( ) {
762+ Some ( parent_id) => Some ( resolve_thread_ref ( parent_id, & state) . await ?) ,
763+ None => None ,
764+ } ;
709765
710766 if let Some ( marker) = marker. as_deref ( ) {
711767 if let Some ( existing) = find_managed_agent_channel_message_by_marker (
712768 & state,
713- marker_author_for_scope ( marker_scope. as_deref ( ) , & record. pubkey ) ,
769+ marker_author_for_scope ( marker_scope. as_deref ( ) , Some ( & record. pubkey ) ) ? ,
714770 & channel_id,
715771 marker,
716772 )
717773 . await ?
718774 {
719775 return Ok ( SendChannelMessageResponse {
720776 event_id : existing. id . to_hex ( ) ,
721- parent_event_id : None ,
722- root_event_id : None ,
723- depth : 0 ,
777+ parent_event_id : thread_ref
778+ . as_ref ( )
779+ . map ( |reference| reference. parent_event_id . to_hex ( ) ) ,
780+ root_event_id : thread_ref
781+ . as_ref ( )
782+ . map ( |reference| reference. root_event_id . to_hex ( ) ) ,
783+ depth : if thread_ref. is_some ( ) { 1 } else { 0 } ,
724784 created_at : existing. created_at . as_secs ( ) as i64 ,
725785 } ) ;
726786 }
727787 }
728788
729- let client_tags = marker
789+ let mut client_tags = marker
730790 . as_deref ( )
731791 . map ( |marker| vec ! [ vec![ "client" . to_string( ) , marker. to_string( ) ] ] )
732792 . unwrap_or_default ( ) ;
733- let builder = events:: build_message_with_client_tags (
793+ for marker in additional_markers. unwrap_or_default ( ) {
794+ let marker = marker. trim ( ) ;
795+ if !marker. is_empty ( ) {
796+ client_tags. push ( vec ! [ "client" . to_string( ) , marker. to_string( ) ] ) ;
797+ }
798+ }
799+ let mentions = mention_pubkeys. unwrap_or_default ( ) ;
800+ let builder = build_managed_agent_channel_message (
734801 channel_uuid,
735802 trimmed,
736- None ,
737- & [ ] ,
738- & [ ] ,
739- & [ ] ,
740- & [ ] ,
803+ thread_ref. as_ref ( ) ,
804+ & mentions,
741805 & client_tags,
742806 ) ?;
743807 let result =
744808 submit_event_with_keys ( builder, & state, & keys, submission_auth_tag. as_deref ( ) ) . await ?;
745809
746810 Ok ( SendChannelMessageResponse {
747811 event_id : result. event_id ,
748- parent_event_id : None ,
749- root_event_id : None ,
750- depth : 0 ,
812+ parent_event_id : parent_event_id . clone ( ) ,
813+ root_event_id : thread_ref . map ( |reference| reference . root_event_id . to_hex ( ) ) ,
814+ depth : if parent_event_id . is_some ( ) { 1 } else { 0 } ,
751815 created_at : chrono:: Utc :: now ( ) . timestamp ( ) ,
752816 } )
753817}
754818
755- #[ cfg( test) ]
756- mod tests {
757- use super :: * ;
758-
759- #[ test]
760- fn marker_author_scope_defaults_to_agent ( ) {
761- assert_eq ! (
762- marker_author_for_scope( None , "agent-pubkey" ) ,
763- Some ( "agent-pubkey" )
764- ) ;
765- assert_eq ! (
766- marker_author_for_scope( Some ( "agent" ) , "agent-pubkey" ) ,
767- Some ( "agent-pubkey" )
768- ) ;
769- assert_eq ! (
770- marker_author_for_scope( Some ( "unknown" ) , "agent-pubkey" ) ,
771- Some ( "agent-pubkey" )
772- ) ;
773- }
774-
775- #[ test]
776- fn marker_author_scope_can_dedupe_across_channel ( ) {
777- assert_eq ! (
778- marker_author_for_scope( Some ( "channel" ) , "agent-pubkey" ) ,
779- None
780- ) ;
781- }
782-
783- #[ test]
784- fn search_messages_filter_requests_prefix_mode_for_topbar_typeahead ( ) {
785- let filter = build_search_messages_filter ( " pro " , 12 , Some ( "channel-1" ) ) ;
786-
787- assert_eq ! ( filter[ "search" ] , serde_json:: json!( "pro" ) ) ;
788- assert_eq ! ( filter[ "search_mode" ] , serde_json:: json!( "prefix" ) ) ;
789- assert_eq ! ( filter[ "limit" ] , serde_json:: json!( 12 ) ) ;
790- assert_eq ! ( filter[ "#h" ] , serde_json:: json!( [ "channel-1" ] ) ) ;
791- }
792-
793- #[ test]
794- fn channel_messages_before_filter_sends_before_id_the_relay_reads ( ) {
795- // The relay bridge's `extract_before_id` reads the composite tiebreak
796- // from `before_id`. If this filter sent the id under any other key (an
797- // earlier cut used `n`), the relay would silently drop the tiebreak and
798- // the dense-second keyset would degrade to a bare inclusive `until` —
799- // re-returning the same page forever. Pin the field name here so the
800- // client/relay contract can't drift without a red test (the Playwright
801- // mock reimplements the keyset in JS and cannot catch this).
802- let filter =
803- build_channel_messages_before_filter ( "channel-1" , 1_700_000_000 , Some ( "ab" ) , 200 ) ;
804-
805- assert_eq ! ( filter[ "until" ] , serde_json:: json!( 1_700_000_000 ) ) ;
806- assert_eq ! ( filter[ "before_id" ] , serde_json:: json!( "ab" ) ) ;
807- assert_eq ! ( filter[ "limit" ] , serde_json:: json!( 200 ) ) ;
808- assert_eq ! ( filter[ "#h" ] , serde_json:: json!( [ "channel-1" ] ) ) ;
809- assert ! (
810- !filter. contains_key( "n" ) ,
811- "tiebreak must be `before_id`, not the `n` alias the relay ignores"
812- ) ;
813- }
814-
815- #[ test]
816- fn thread_replies_filter_carries_non_p_gated_kinds_to_clear_the_gate ( ) {
817- // The relay bridge p-gates EVERY filter before routing
818- // (`p_gated_filters_authorized`): a kindless filter "could match" a
819- // p-gated kind, so it demands a `#p` tag we don't send -> HTTP 403,
820- // before the thread-subtree query runs. The headline Lane-1 fix
821- // (`useThreadReplies` closing the descendant gap) then fails on every
822- // call against a real relay. So the thread filter MUST carry `kinds`,
823- // and every kind MUST be non-p-gated (else the gate still fires). The
824- // Playwright mock does not model p-gating, so this unit test is the
825- // only guard against the client/relay auth contract drifting.
826- let filter = build_thread_replies_filter ( "root-hex" , Some ( "channel-1" ) , 64 , 200 , None ) ;
827-
828- let kinds = filter
829- . get ( "kinds" )
830- . and_then ( |v| v. as_array ( ) )
831- . expect ( "thread filter must carry `kinds` so the p-gate passes" ) ;
832- assert ! ( !kinds. is_empty( ) , "kinds must be non-empty" ) ;
833- for kind in kinds {
834- let k = kind. as_u64 ( ) . expect ( "kind is a number" ) as u32 ;
835- assert ! (
836- !buzz_core_pkg:: kind:: P_GATED_KINDS . contains( & k) ,
837- "kind {k} is p-gated; a p-gated kind in the filter re-triggers the \
838- 403 that this fix exists to prevent"
839- ) ;
840- }
841- assert_eq ! ( filter[ "#e" ] , serde_json:: json!( [ "root-hex" ] ) ) ;
842- assert_eq ! ( filter[ "depth_limit" ] , serde_json:: json!( 64 ) ) ;
843- assert_eq ! ( filter[ "#h" ] , serde_json:: json!( [ "channel-1" ] ) ) ;
844- }
845-
846- #[ test]
847- fn thread_replies_filter_pages_with_composite_cursor ( ) {
848- // When a cursor is supplied, both the timestamp and the event-id
849- // tiebreak must be emitted (`thread_cursor` + `thread_cursor_id`), else
850- // paging degrades to timestamp-only and drops same-second replies.
851- let cursor = crate :: models:: ThreadCursor {
852- created_at : 1_700_000_000 ,
853- event_id : "abcd" . to_string ( ) ,
854- } ;
855- let filter = build_thread_replies_filter ( "root-hex" , None , 64 , 200 , Some ( & cursor) ) ;
856- assert_eq ! ( filter[ "thread_cursor" ] , serde_json:: json!( 1_700_000_000 ) ) ;
857- assert_eq ! ( filter[ "thread_cursor_id" ] , serde_json:: json!( "abcd" ) ) ;
858- assert ! (
859- !filter. contains_key( "#h" ) ,
860- "no channel_id -> no #h scope in the filter"
861- ) ;
862- }
863-
864- #[ test]
865- fn stored_managed_agent_auth_tag_trims_blank_values ( ) {
866- assert_eq ! (
867- stored_managed_agent_auth_tag( Some ( " [\" auth\" ,\" owner\" ,\" \" ,\" sig\" ] " ) ) ,
868- Some ( "[\" auth\" ,\" owner\" ,\" \" ,\" sig\" ]" . to_string( ) )
869- ) ;
870- assert_eq ! ( stored_managed_agent_auth_tag( Some ( " " ) ) , None ) ;
871- assert_eq ! ( stored_managed_agent_auth_tag( None ) , None ) ;
872- }
873-
874- #[ test]
875- fn legacy_managed_agent_auth_tag_verifies_for_agent_pubkey ( ) {
876- let owner_keys = Keys :: generate ( ) ;
877- let agent_keys = Keys :: generate ( ) ;
878-
879- let tag = legacy_managed_agent_auth_tag ( & owner_keys, & agent_keys. public_key ( ) )
880- . expect ( "legacy auth tag should compute" )
881- . expect ( "legacy auth tag should be present" ) ;
882-
883- let owner = buzz_sdk_pkg:: nip_oa:: verify_auth_tag ( & tag, & agent_keys. public_key ( ) )
884- . expect ( "legacy auth tag should verify" ) ;
885- assert_eq ! ( owner, owner_keys. public_key( ) ) ;
886- }
887-
888- #[ test]
889- fn legacy_managed_agent_auth_tag_skips_self_attestation ( ) {
890- let owner_keys = Keys :: generate ( ) ;
891-
892- let tag = legacy_managed_agent_auth_tag ( & owner_keys, & owner_keys. public_key ( ) )
893- . expect ( "self-attestation should be skipped" ) ;
894-
895- assert_eq ! ( tag, None ) ;
896- }
897- }
898-
899819#[ tauri:: command]
900820pub async fn add_reaction (
901821 event_id : String ,
@@ -1021,3 +941,6 @@ fn feed_item_from_event(ev: &nostr::Event, category: &str) -> FeedItemInfo {
1021941 category : category. to_string ( ) ,
1022942 }
1023943}
944+ #[ cfg( test) ]
945+ #[ path = "messages_tests.rs" ]
946+ mod tests;
0 commit comments