From 616255b2de88edc069efc62981585e4735893cfc Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Mon, 9 Jan 2023 18:36:16 +1300 Subject: [PATCH] FIX replies (include p tags as required by NIP-10) --- src/overlord/mod.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/overlord/mod.rs b/src/overlord/mod.rs index ff1fd45c..961e09c1 100644 --- a/src/overlord/mod.rs +++ b/src/overlord/mod.rs @@ -673,6 +673,8 @@ impl Overlord { } async fn post_reply(&mut self, content: String, reply_to: Id) -> Result<(), Error> { + let mut tags: Vec = Vec::new(); + let event = { let public_key = match GLOBALS.signer.read().await.public_key() { Some(pk) => pk, @@ -682,15 +684,47 @@ impl Overlord { } }; + // Get the event we are replying to + let event = match GLOBALS.events.get(&reply_to) { + Some(e) => e, + None => { + return Err(Error::General( + "Cannot find event we are replying to.".to_owned(), + )) + } + }; + + // Add an 'e' tag for the note we are replying to + tags.push(Tag::Event { + id: reply_to, + recommended_relay_url: DbRelay::recommended_relay_for_reply(reply_to).await?, + marker: Some("reply".to_string()), + }); + + // Add a 'p' tag for the author we are replying to + tags.push(Tag::Pubkey { + pubkey: event.pubkey, + recommended_relay_url: None, // FIXME + petname: None, + }); + + // Add all the 'p' tags from the note we are replying to + let parent_p_tags: Vec = event + .tags + .iter() + .filter(|t| match t { + Tag::Pubkey { pubkey, .. } => *pubkey != event.pubkey, + _ => false, + }) + .map(|t| t.to_owned()) + .collect(); + tags.extend(parent_p_tags); + let pre_event = PreEvent { pubkey: public_key, created_at: Unixtime::now().unwrap(), kind: EventKind::TextNote, - tags: vec![Tag::Event { - id: reply_to, - recommended_relay_url: DbRelay::recommended_relay_for_reply(reply_to).await?, - marker: Some("reply".to_string()), - }], + tags, content, ots: None, };