forked from janek/mostr
1
0
Fork 0

feat: properly load tasks from relay

This commit is contained in:
xeruf 2024-07-19 16:49:23 +03:00
parent 026915f870
commit c8463e6924
2 changed files with 34 additions and 24 deletions

View File

@ -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();
@ -165,7 +171,7 @@ async fn repl() {
pos = EventId::parse(slice).ok().or_else(|| {
tasks.move_to(pos);
let filtered: Vec<EventId> = 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);

View File

@ -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
})