From c8463e6924cca277313983c5d1f3f0e2966d0203 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Fri, 19 Jul 2024 16:49:23 +0300 Subject: [PATCH] feat: properly load tasks from relay --- src/main.rs | 38 ++++++++++++++++++++++---------------- src/tasks.rs | 20 ++++++++++++-------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5a18e06..7bff79b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,23 +62,10 @@ async fn main() { CLIENT.connect().await; - let timeout = Duration::from_secs(3); - - let filter = Filter::new(); - let sub_id: SubscriptionId = CLIENT.subscribe(vec![filter.clone()], None).await; + let sub_id: SubscriptionId = CLIENT.subscribe(vec![Filter::new()], Some(SubscribeAutoCloseOptions::default())).await; repl().await; - println!("Finding existing events"); - let res = CLIENT - .get_events_of(vec![filter], Option::from(timeout)) - .map_ok(|res| { - for event in res { - println!("Found {} '{}' {:?}", event.kind, event.content, event.tags) - } - }) - .await; - let mut notifications = CLIENT.notifications(); println!("Listening for events..."); while let Ok(notification) = notifications.recv().await { @@ -111,6 +98,25 @@ async fn repl() { tasks.add_task(make_task(&argument, &[Tag::Hashtag("arg".to_string())])); } + println!("Finding existing events"); + let res = CLIENT + .get_events_of(vec![Filter::new()], None) + .map_ok(|res| { + println!("Found {} events", res.len()); + let (mut task_events, props): (Vec, Vec) = res.into_iter().partition(|e| e.kind.as_u32() == 1621); + task_events.sort_unstable(); + for event in task_events { + println!("{} found {} '{}' {:?}", event.created_at, event.kind, event.content, event.tags); + tasks.add_task(event); + } + for event in props { + println!("{} found {} '{}' {:?}", event.created_at, event.kind, event.content, event.tags); + tasks.referenced_tasks(&event, |t| t.props.push(event.clone())); + } + }) + .await; + + println!(); tasks.print_current_tasks(); @@ -165,7 +171,7 @@ async fn repl() { pos = EventId::parse(slice).ok().or_else(|| { tasks.move_to(pos); let filtered: Vec = tasks.current_tasks().iter().filter(|t| t.event.content.starts_with(slice)).map(|t| t.event.id).collect(); - match filtered.len() { + match filtered.len() { 0 => { // No match, new task let task = tasks.make_task(slice); @@ -182,7 +188,7 @@ async fn repl() { tasks.set_filter(filtered); None } - } + } }); if pos != None { tasks.move_to(pos); diff --git a/src/tasks.rs b/src/tasks.rs index b1b46db..de29a7b 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -97,17 +97,16 @@ impl Tasks { }; } - pub(crate) fn add_task(&mut self, event: Event) { + pub(crate) fn referenced_tasks(&mut self, event: &Event, f: F) { for tag in event.tags.iter() { - match tag { - Tag::Event { event_id, .. } => { - self.tasks - .get_mut(event_id) - .map(|t| t.children.push(event.id)); - } - _ => {} + if let Tag::Event { event_id, .. } = tag { + self.tasks.get_mut(event_id).map(|t| f(t)); } } + } + + pub(crate) fn add_task(&mut self, event: Event) { + self.referenced_tasks(&event, |t| t.children.push(event.id)); self.tasks.insert(event.id, Task::new(event)); } @@ -156,6 +155,7 @@ impl Tasks { ParentIterator { tasks: &self.tasks, current: id, + prev: None, } } @@ -183,12 +183,16 @@ impl Tasks { struct ParentIterator<'a> { tasks: &'a TaskMap, current: Option, + /// Inexpensive helper to assert correctness + prev: Option, } impl<'a> Iterator for ParentIterator<'a> { type Item = &'a Task; fn next(&mut self) -> Option { self.current.and_then(|id| self.tasks.get(&id)).map(|t| { + self.prev.map(|id| assert!(t.children.contains(&id))); + self.prev = self.current; self.current = t.parent_id(); t })