diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-16 21:12:25 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-16 21:12:25 +0000 |
| commit | 61ffbf2008c0aaaee3d19ac027d63bca823dc8c9 (patch) | |
| tree | 4ace7a39faf9d6acca95d190e0399561b295d22f | |
| parent | 454c955d60e6f313bf8da6822337cace9bb6fc16 (diff) | |
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
| -rw-r--r-- | src/sub_commands/list.rs | 67 |
1 files 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<String> { | |||
| 143 | } | 143 | } |
| 144 | } | 144 | } |
| 145 | 145 | ||
| 146 | fn get_event_parent_id(event: &nostr::Event) -> Result<String> { | ||
| 147 | Ok(if let Some(reply_tag) = event | ||
| 148 | .tags | ||
| 149 | .iter() | ||
| 150 | .find(|t| t.as_vec().len().gt(&3) && t.as_vec()[3].eq("reply")) | ||
| 151 | { | ||
| 152 | reply_tag | ||
| 153 | } else { | ||
| 154 | event | ||
| 155 | .tags | ||
| 156 | .iter() | ||
| 157 | .find(|t| t.as_vec().len().gt(&3) && t.as_vec()[3].eq("root")) | ||
| 158 | .context("no reply or root e tag present".to_string())? | ||
| 159 | } | ||
| 160 | .as_vec()[1] | ||
| 161 | .clone()) | ||
| 162 | } | ||
| 163 | |||
| 146 | pub fn get_most_recent_patch_with_ancestors( | 164 | pub fn get_most_recent_patch_with_ancestors( |
| 147 | mut patches: Vec<nostr::Event>, | 165 | mut patches: Vec<nostr::Event>, |
| 148 | ) -> Result<Vec<nostr::Event>> { | 166 | ) -> Result<Vec<nostr::Event>> { |
| @@ -155,41 +173,30 @@ pub fn get_most_recent_patch_with_ancestors( | |||
| 155 | .filter(|p| p.created_at.eq(&first_patch.created_at)) | 173 | .filter(|p| p.created_at.eq(&first_patch.created_at)) |
| 156 | .collect(); | 174 | .collect(); |
| 157 | 175 | ||
| 158 | let latest_commit_id = get_commit_id_from_patch( | 176 | let mut res = vec![]; |
| 159 | // get the first patch which isn't a parent of a patch event created at the same | 177 | |
| 160 | // time | 178 | let mut event_id_to_search = patches_with_youngest_created_at |
| 161 | patches_with_youngest_created_at | 179 | .clone() |
| 162 | .clone() | 180 | .iter() |
| 163 | .iter() | 181 | .find(|p| { |
| 164 | .find(|p| { | 182 | !patches_with_youngest_created_at.iter().any(|p2| { |
| 165 | if let Ok(commit) = get_commit_id_from_patch(p) { | 183 | if let Ok(reply_to) = get_event_parent_id(p2) { |
| 166 | !patches_with_youngest_created_at.iter().any(|p2| { | 184 | reply_to.eq(&p.id.to_string()) |
| 167 | if let Ok(parent) = tag_value(p2, "parent-commit") { | ||
| 168 | commit.eq(&parent) | ||
| 169 | } else { | ||
| 170 | false // skip | ||
| 171 | } | ||
| 172 | }) | ||
| 173 | } else { | 185 | } else { |
| 174 | false // skip | 186 | false |
| 175 | } | 187 | } |
| 176 | }) | 188 | }) |
| 177 | .context("cannot find patches_with_youngest_created_at")?, | 189 | }) |
| 178 | )?; | 190 | .context("cannot find patches_with_youngest_created_at")? |
| 179 | 191 | .id | |
| 180 | let mut res = vec![]; | 192 | .to_string(); |
| 181 | |||
| 182 | let mut commit_id_to_search = latest_commit_id; | ||
| 183 | 193 | ||
| 184 | while let Some(event) = patches.iter().find(|e| { | 194 | while let Some(event) = patches |
| 185 | if let Ok(commit) = get_commit_id_from_patch(e) { | 195 | .iter() |
| 186 | commit.eq(&commit_id_to_search) | 196 | .find(|e| e.id.to_string().eq(&event_id_to_search)) |
| 187 | } else { | 197 | { |
| 188 | false // skip | ||
| 189 | } | ||
| 190 | }) { | ||
| 191 | res.push(event.clone()); | 198 | res.push(event.clone()); |
| 192 | commit_id_to_search = tag_value(event, "parent-commit")?; | 199 | event_id_to_search = get_event_parent_id(event).unwrap_or_default(); |
| 193 | } | 200 | } |
| 194 | Ok(res) | 201 | Ok(res) |
| 195 | } | 202 | } |