From 61ffbf2008c0aaaee3d19ac027d63bca823dc8c9 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 16 Feb 2024 21:12:25 +0000 Subject: refactor: use event_id in get_most_recent_accestor instead of commit ids as part of nip34 compliance and to enable applying proposals to tip of master in the future --- src/sub_commands/list.rs | 67 ++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs index be5b97f..4764adc 100644 --- a/src/sub_commands/list.rs +++ b/src/sub_commands/list.rs @@ -143,6 +143,24 @@ pub fn get_commit_id_from_patch(event: &nostr::Event) -> Result { } } +fn get_event_parent_id(event: &nostr::Event) -> Result { + Ok(if let Some(reply_tag) = event + .tags + .iter() + .find(|t| t.as_vec().len().gt(&3) && t.as_vec()[3].eq("reply")) + { + reply_tag + } else { + event + .tags + .iter() + .find(|t| t.as_vec().len().gt(&3) && t.as_vec()[3].eq("root")) + .context("no reply or root e tag present".to_string())? + } + .as_vec()[1] + .clone()) +} + pub fn get_most_recent_patch_with_ancestors( mut patches: Vec, ) -> Result> { @@ -155,41 +173,30 @@ pub fn get_most_recent_patch_with_ancestors( .filter(|p| p.created_at.eq(&first_patch.created_at)) .collect(); - let latest_commit_id = get_commit_id_from_patch( - // get the first patch which isn't a parent of a patch event created at the same - // time - patches_with_youngest_created_at - .clone() - .iter() - .find(|p| { - if let Ok(commit) = get_commit_id_from_patch(p) { - !patches_with_youngest_created_at.iter().any(|p2| { - if let Ok(parent) = tag_value(p2, "parent-commit") { - commit.eq(&parent) - } else { - false // skip - } - }) + let mut res = vec![]; + + let mut event_id_to_search = patches_with_youngest_created_at + .clone() + .iter() + .find(|p| { + !patches_with_youngest_created_at.iter().any(|p2| { + if let Ok(reply_to) = get_event_parent_id(p2) { + reply_to.eq(&p.id.to_string()) } else { - false // skip + false } }) - .context("cannot find patches_with_youngest_created_at")?, - )?; - - let mut res = vec![]; - - let mut commit_id_to_search = latest_commit_id; + }) + .context("cannot find patches_with_youngest_created_at")? + .id + .to_string(); - while let Some(event) = patches.iter().find(|e| { - if let Ok(commit) = get_commit_id_from_patch(e) { - commit.eq(&commit_id_to_search) - } else { - false // skip - } - }) { + while let Some(event) = patches + .iter() + .find(|e| e.id.to_string().eq(&event_id_to_search)) + { res.push(event.clone()); - commit_id_to_search = tag_value(event, "parent-commit")?; + event_id_to_search = get_event_parent_id(event).unwrap_or_default(); } Ok(res) } -- cgit v1.2.3