forked from janek/mostr
feat: properly load tasks from relay
This commit is contained in:
parent
026915f870
commit
c8463e6924
34
src/main.rs
34
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<Event>, Vec<Event>) = 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();
|
||||
|
||||
|
|
20
src/tasks.rs
20
src/tasks.rs
|
@ -97,17 +97,16 @@ impl Tasks {
|
|||
};
|
||||
}
|
||||
|
||||
pub(crate) fn add_task(&mut self, event: Event) {
|
||||
pub(crate) fn referenced_tasks<F: Fn(&mut Task)>(&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<EventId>,
|
||||
/// Inexpensive helper to assert correctness
|
||||
prev: Option<EventId>,
|
||||
}
|
||||
impl<'a> Iterator for ParentIterator<'a> {
|
||||
type Item = &'a Task;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
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
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue