11//! NIP-IA identity archive request handler (kinds 9035–9036).
22//!
3- //! These events are processed before storage: the request mutates the
4- //! `archived_identities` table and may emit relay-signed NIP-IA deltas and a
5- //! snapshot, then the ingest pipeline stores the request itself for audit.
3+ //! Validate consent before storage. Persist the request, archive mutation and
4+ //! relay delta atomically; publish only after commit.
65
76use std:: sync:: Arc ;
87
98use nostr:: { Event , PublicKey } ;
10- use tracing:: { info, warn} ;
119
12- use buzz_core:: kind:: { KIND_IA_ARCHIVE_REQUEST , KIND_IA_UNARCHIVE_REQUEST , KIND_PROFILE } ;
10+ use buzz_core:: kind:: {
11+ KIND_IA_ARCHIVED , KIND_IA_ARCHIVE_REQUEST , KIND_IA_UNARCHIVED , KIND_IA_UNARCHIVE_REQUEST ,
12+ KIND_PROFILE ,
13+ } ;
1314use buzz_core:: tenant:: TenantContext ;
14- use buzz_core:: CommunityId ;
15+ use buzz_core:: { CommunityId , StoredEvent } ;
1516use buzz_db:: EventQuery ;
1617
17- use crate :: handlers:: side_effects:: {
18- publish_nipia_archival_list, publish_nipia_archived, publish_nipia_unarchived,
19- } ;
18+ use crate :: handlers:: side_effects:: { build_nipia_delta, publish_nipia_archival_list} ;
2019use crate :: state:: AppState ;
2120
2221#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
@@ -36,12 +35,20 @@ impl ConsentPath {
3635 }
3736}
3837
39- /// Validate and execute a NIP-IA archive/unarchive request.
40- pub async fn handle_identity_archive_event (
38+ /// Validated archive parameters; no persistence occurs during validation.
39+ pub ( super ) struct IdentityArchivePlan {
40+ target : String ,
41+ consent : ConsentPath ,
42+ reason : Option < String > ,
43+ replaced_by : Option < String > ,
44+ }
45+
46+ /// Validate a NIP-IA archive/unarchive request without changing state.
47+ pub ( super ) async fn validate_identity_archive_event (
4148 tenant : & TenantContext ,
4249 state : & Arc < AppState > ,
4350 event : & Event ,
44- ) -> Result < ( ) , String > {
51+ ) -> Result < IdentityArchivePlan , String > {
4552 let kind = event. kind . as_u16 ( ) as u32 ;
4653 let actor_hex = event. pubkey . to_hex ( ) ;
4754
@@ -64,77 +71,83 @@ pub async fn handle_identity_archive_event(
6471 let reason = extract_tag_value ( event, "reason" ) ;
6572 let consent_path =
6673 determine_consent_path ( tenant. community ( ) , state, event, & target_hex, & actor_hex) . await ?;
67- let request_event_id = event. id . to_hex ( ) ;
74+ Ok ( IdentityArchivePlan {
75+ target : target_hex,
76+ consent : consent_path,
77+ reason,
78+ replaced_by,
79+ } )
80+ }
6881
69- let changed = if kind == KIND_IA_ARCHIVE_REQUEST {
70- state
71- . db
72- . archive (
73- tenant. community ( ) ,
74- & target_hex,
75- consent_path. as_str ( ) ,
76- & actor_hex,
77- reason. as_deref ( ) ,
78- replaced_by. as_deref ( ) ,
79- & request_event_id,
80- )
81- . await
82- . map_err ( |e| format ! ( "database error: {e}" ) ) ?
82+ /// Commit archive state and its audit events before publishing notifications.
83+ pub ( super ) async fn persist_identity_archive_event (
84+ tenant : & TenantContext ,
85+ state : & Arc < AppState > ,
86+ event : & Event ,
87+ plan : IdentityArchivePlan ,
88+ ) -> anyhow:: Result < ( StoredEvent , bool ) > {
89+ let archive = u32:: from ( event. kind . as_u16 ( ) ) == KIND_IA_ARCHIVE_REQUEST ;
90+ let delta_kind = if archive {
91+ KIND_IA_ARCHIVED
8392 } else {
84- state
85- . db
86- . unarchive ( tenant. community ( ) , & target_hex)
87- . await
88- . map_err ( |e| format ! ( "database error: {e}" ) ) ?
93+ KIND_IA_UNARCHIVED
8994 } ;
90-
91- info ! (
92- actor = %actor_hex,
93- target = %target_hex,
94- consent = consent_path. as_str( ) ,
95- changed,
96- kind,
97- "identity archive request processed"
98- ) ;
99-
100- if !changed {
101- return Ok ( ( ) ) ;
102- }
103-
104- let publish_delta = if kind == KIND_IA_ARCHIVE_REQUEST {
105- publish_nipia_archived (
106- tenant,
107- state,
108- & target_hex,
109- consent_path. as_str ( ) ,
110- & actor_hex,
111- & request_event_id,
112- & event. content ,
113- reason. as_deref ( ) ,
114- replaced_by. as_deref ( ) ,
95+ // Use the request timestamp for its corresponding relay notification.
96+ let delta = build_nipia_delta (
97+ state,
98+ delta_kind,
99+ & plan. target ,
100+ plan. consent . as_str ( ) ,
101+ & event. pubkey . to_hex ( ) ,
102+ & event. id . to_hex ( ) ,
103+ & event. content ,
104+ event. created_at ,
105+ plan. reason . as_deref ( ) ,
106+ plan. replaced_by . as_deref ( ) ,
107+ ) ?;
108+ let ( stored, inserted, notification) = state
109+ . db
110+ . apply_identity_archive_event (
111+ tenant. community ( ) ,
112+ event,
113+ buzz_db:: archived_identities:: IdentityArchiveChange {
114+ target : & plan. target ,
115+ archive,
116+ consent_path : plan. consent . as_str ( ) ,
117+ reason : plan. reason . as_deref ( ) ,
118+ replaced_by : plan. replaced_by . as_deref ( ) ,
119+ } ,
120+ & delta,
115121 )
116- . await
117- } else {
118- publish_nipia_unarchived (
122+ . await ? ;
123+ if let Some ( notification ) = notification {
124+ super :: event :: dispatch_persistent_event (
119125 tenant,
120126 state,
121- & target_hex,
122- consent_path. as_str ( ) ,
123- & actor_hex,
124- & request_event_id,
125- & event. content ,
126- reason. as_deref ( ) ,
127+ & notification,
128+ delta_kind,
129+ & state. relay_keypair . public_key ( ) . to_hex ( ) ,
130+ None ,
127131 )
128- . await
129- } ;
130-
131- if let Err ( e) = publish_delta {
132- warn ! ( error = %e, "failed to publish NIP-IA delta" ) ;
133- }
134- if let Err ( e) = publish_nipia_archival_list ( tenant, state) . await {
135- warn ! ( error = %e, "failed to publish NIP-IA archival list" ) ;
132+ . await ;
136133 }
134+ // Also refresh on duplicate retries: a previous post-commit refresh may
135+ // have failed. Propagate failures so callers can retry without reapplying
136+ // the archive mutation. The stored delta remains available for replay.
137+ publish_nipia_archival_list ( tenant, state) . await ?;
138+ Ok ( ( stored, inserted) )
139+ }
137140
141+ #[ cfg( test) ]
142+ async fn handle_identity_archive_event (
143+ tenant : & TenantContext ,
144+ state : & Arc < AppState > ,
145+ event : & Event ,
146+ ) -> Result < ( ) , String > {
147+ let plan = validate_identity_archive_event ( tenant, state, event) . await ?;
148+ persist_identity_archive_event ( tenant, state, event, plan)
149+ . await
150+ . map_err ( |e| e. to_string ( ) ) ?;
138151 Ok ( ( ) )
139152}
140153
0 commit comments