@@ -24,8 +24,12 @@ pub async fn cmd_add_reaction(
2424 . map_err ( |e| CliError :: Other ( format ! ( "build_reaction failed: {e}" ) ) ) ?
2525 } ;
2626 // NIP-25: lets the target's author find reactions with a `#p` filter.
27- let target_author = fetch_target_author ( client, event_id) . await ?;
28- let builder = builder. tag ( Tag :: public_key ( target_author) ) ;
27+ // An unscoped read can miss a target the sender may react to (system
28+ // channels), so the relay, not this lookup, decides whether it exists.
29+ let builder = match fetch_target_author ( client, event_id) . await ? {
30+ Some ( target_author) => builder. tag ( Tag :: public_key ( target_author) ) ,
31+ None => builder,
32+ } ;
2933
3034 let event = client. sign_event ( builder) . await ?;
3135
@@ -34,19 +38,25 @@ pub async fn cmd_add_reaction(
3438 Ok ( ( ) )
3539}
3640
37- async fn fetch_target_author ( client : & BuzzClient , event_id : & str ) -> Result < PublicKey , CliError > {
41+ async fn fetch_target_author (
42+ client : & BuzzClient ,
43+ event_id : & str ,
44+ ) -> Result < Option < PublicKey > , CliError > {
3845 // Strong: a target just written may not have reached the read replica yet.
3946 let filter = serde_json:: json!( { "ids" : [ event_id] , "limit" : 1 , "consistency" : "strong" } ) ;
4047 let raw = client. query ( & filter) . await ?;
4148 let events: serde_json:: Value = serde_json:: from_str ( & raw)
4249 . map_err ( |e| CliError :: Other ( format ! ( "failed to parse query response: {e}" ) ) ) ?;
43- let author = events
50+ let Some ( author) = events
4451 . as_array ( )
4552 . and_then ( |events| events. first ( ) )
4653 . and_then ( |event| event. get ( "pubkey" ) )
4754 . and_then ( |pubkey| pubkey. as_str ( ) )
48- . ok_or_else ( || CliError :: NotFound ( format ! ( "event {event_id} not found" ) ) ) ?;
55+ else {
56+ return Ok ( None ) ;
57+ } ;
4958 PublicKey :: from_hex ( author)
59+ . map ( Some )
5060 . map_err ( |e| CliError :: Other ( format ! ( "event {event_id} has an invalid pubkey: {e}" ) ) )
5161}
5262
@@ -171,37 +181,54 @@ mod tests {
171181 const TARGET_AUTHOR : & str = "35c18ae273fccfaf80d629e20e7f8721b90499379addff533054acc2504c12b4" ;
172182
173183 type Submitted = Arc < Mutex < Option < serde_json:: Value > > > ;
184+ type Reply = ( StatusCode , String ) ;
185+
186+ const ACCEPTED : & str = r#"{"event_id":"fake","accepted":true}"# ;
187+
188+ #[ derive( Clone ) ]
189+ struct FakeRelay {
190+ query : Reply ,
191+ events : Reply ,
192+ submitted : Submitted ,
193+ queried : Submitted ,
194+ }
174195
175196 async fn fake_relay ( query_body : String ) -> ( String , Submitted , Submitted ) {
176- let submitted: Submitted = Arc :: new ( Mutex :: new ( None ) ) ;
177- let queried: Submitted = Arc :: new ( Mutex :: new ( None ) ) ;
178- let state = ( query_body, submitted. clone ( ) , queried. clone ( ) ) ;
197+ fake_relay_replying (
198+ ( StatusCode :: OK , query_body) ,
199+ ( StatusCode :: OK , ACCEPTED . into ( ) ) ,
200+ )
201+ . await
202+ }
203+
204+ async fn fake_relay_replying ( query : Reply , events : Reply ) -> ( String , Submitted , Submitted ) {
205+ let relay = FakeRelay {
206+ query,
207+ events,
208+ submitted : Arc :: new ( Mutex :: new ( None ) ) ,
209+ queried : Arc :: new ( Mutex :: new ( None ) ) ,
210+ } ;
211+ let ( submitted, queried) = ( relay. submitted . clone ( ) , relay. queried . clone ( ) ) ;
179212 let app = Router :: new ( )
180213 . route (
181214 "/query" ,
182215 post (
183- |State ( ( body , _ , queried ) ) : State < ( String , Submitted , Submitted ) > ,
184- request : Bytes | async move {
185- * queried . lock ( ) . unwrap ( ) = serde_json :: from_slice ( & request ) . ok ( ) ;
186- ( StatusCode :: OK , [ ( "content-type" , "application/json" ) ] , body)
216+ |State ( relay ) : State < FakeRelay > , request : Bytes | async move {
217+ * relay . queried . lock ( ) . unwrap ( ) = serde_json :: from_slice ( & request ) . ok ( ) ;
218+ let ( status , body ) = relay . query ;
219+ ( status , [ ( "content-type" , "application/json" ) ] , body)
187220 } ,
188221 ) ,
189222 )
190223 . route (
191224 "/events" ,
192- post (
193- |State ( ( _, submitted, _) ) : State < ( String , Submitted , Submitted ) > ,
194- body : Bytes | async move {
195- * submitted. lock ( ) . unwrap ( ) = serde_json:: from_slice ( & body) . ok ( ) ;
196- (
197- StatusCode :: OK ,
198- [ ( "content-type" , "application/json" ) ] ,
199- r#"{"event_id":"fake","accepted":true}"# ,
200- )
201- } ,
202- ) ,
225+ post ( |State ( relay) : State < FakeRelay > , body : Bytes | async move {
226+ * relay. submitted . lock ( ) . unwrap ( ) = serde_json:: from_slice ( & body) . ok ( ) ;
227+ let ( status, body) = relay. events ;
228+ ( status, [ ( "content-type" , "application/json" ) ] , body)
229+ } ) ,
203230 )
204- . with_state ( state ) ;
231+ . with_state ( relay ) ;
205232 let listener = TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
206233 let address = listener. local_addr ( ) . unwrap ( ) ;
207234 tokio:: spawn ( async move { axum:: serve ( listener, app) . await . unwrap ( ) } ) ;
@@ -257,13 +284,61 @@ mod tests {
257284 }
258285
259286 #[ tokio:: test]
260- async fn add_reaction_to_unknown_event_fails_without_submitting ( ) {
287+ async fn add_reaction_to_unreadable_target_submits_without_p_tag ( ) {
261288 let ( url, submitted, _) = fake_relay ( "[]" . to_string ( ) ) . await ;
262289 let client = BuzzClient :: new ( url, Credentials :: from_keys ( Keys :: generate ( ) ) ) . unwrap ( ) ;
263290
291+ cmd_add_reaction ( & client, TARGET_ID , "👍" , None )
292+ . await
293+ . unwrap ( ) ;
294+
295+ let tags = submitted_tags ( & submitted) ;
296+ assert ! ( tags. contains( & vec![ "e" . to_string( ) , TARGET_ID . to_string( ) ] ) ) ;
297+ assert ! (
298+ !tags
299+ . iter( )
300+ . any( |tag| tag. first( ) . map( String :: as_str) == Some ( "p" ) ) ,
301+ "an unreadable target has no known author to tag, got {tags:?}"
302+ ) ;
303+ }
304+
305+ #[ tokio:: test]
306+ async fn add_reaction_shows_the_relay_rejection_verbatim ( ) {
307+ let rejection = r#"{"error":"invalid: reaction target event not found"}"# ;
308+ let ( url, _, _) = fake_relay_replying (
309+ ( StatusCode :: OK , "[]" . into ( ) ) ,
310+ ( StatusCode :: BAD_REQUEST , rejection. into ( ) ) ,
311+ )
312+ . await ;
313+ let client = BuzzClient :: new ( url, Credentials :: from_keys ( Keys :: generate ( ) ) ) . unwrap ( ) ;
314+
315+ let error = cmd_add_reaction ( & client, TARGET_ID , "👍" , None )
316+ . await
317+ . unwrap_err ( ) ;
318+
319+ assert ! (
320+ error
321+ . to_string( )
322+ . contains( "invalid: reaction target event not found" ) ,
323+ "the relay's rejection must reach the user as written, got {error}"
324+ ) ;
325+ }
326+
327+ #[ tokio:: test]
328+ async fn add_reaction_fails_without_submitting_when_target_lookup_errors ( ) {
329+ let ( url, submitted, _) = fake_relay_replying (
330+ (
331+ StatusCode :: INTERNAL_SERVER_ERROR ,
332+ r#"{"error":"boom"}"# . into ( ) ,
333+ ) ,
334+ ( StatusCode :: OK , ACCEPTED . into ( ) ) ,
335+ )
336+ . await ;
337+ let client = BuzzClient :: new ( url, Credentials :: from_keys ( Keys :: generate ( ) ) ) . unwrap ( ) ;
338+
264339 let result = cmd_add_reaction ( & client, TARGET_ID , "👍" , None ) . await ;
265340
266- assert ! ( matches!( result, Err ( CliError :: NotFound ( _ ) ) ) ) ;
341+ assert ! ( matches!( result, Err ( CliError :: Relay { status : 500 , .. } ) ) ) ;
267342 assert ! ( submitted. lock( ) . unwrap( ) . is_none( ) ) ;
268343 }
269344
0 commit comments