From 77da80b261abc115a80eae8bfd89c948efda7b7e Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Sun, 15 Jan 2023 09:39:56 +1300 Subject: [PATCH] Events::get_highest_local_parent() --- src/events.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/events.rs b/src/events.rs index c247c496..3ef6d06e 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,5 +1,6 @@ use crate::error::Error; use crate::globals::GLOBALS; +use async_recursion::async_recursion; use dashmap::{DashMap, DashSet}; use nostr_types::{Event, Id}; use tokio::task; @@ -22,6 +23,7 @@ impl Events { let _ = self.events.insert(event.id, event); } + #[allow(dead_code)] pub fn contains_key(&self, id: &Id) -> bool { self.events.contains_key(id) } @@ -59,6 +61,23 @@ impl Events { } } + #[allow(dead_code)] + #[async_recursion] + pub async fn get_highest_local_parent(&self, id: &Id) -> Result, Error> { + if let Some(event) = self.get_local(*id).await? { + if let Some((parent_id, _opturl)) = event.replies_to() { + match self.get_highest_local_parent(&parent_id).await? { + Some(top_id) => Ok(Some(top_id)), // went higher + None => Ok(Some(*id)), // couldn't go higher, stay here + } + } else { + Ok(Some(*id)) // is a root + } + } else { + Ok(None) // not present locally + } + } + pub fn is_new(&self, id: &Id) -> bool { self.new_events.contains(id) }