feat: fetch new events in session

This commit is contained in:
xeruf 2024-07-19 21:04:21 +03:00
parent 3a444c3ac2
commit 1ea34898d1
2 changed files with 36 additions and 22 deletions

View File

@ -62,25 +62,8 @@ async fn main() {
CLIENT.connect().await; CLIENT.connect().await;
let sub_id: SubscriptionId = CLIENT.subscribe(vec![Filter::new()], Some(SubscribeAutoCloseOptions::default())).await;
repl().await; repl().await;
let mut notifications = CLIENT.notifications();
println!("Listening for events...");
while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Event {
subscription_id,
event,
..
} = notification
{
let kind = event.kind;
let content = &event.content;
println!("{kind}: {content}");
//break; // Exit
}
}
} }
fn make_task(text: &str, tags: &[Tag]) -> Event { fn make_task(text: &str, tags: &[Tag]) -> Event {
@ -92,12 +75,19 @@ fn make_event(kind: Kind, text: &str, tags: &[Tag]) -> Event {
.unwrap() .unwrap()
} }
fn print_event(event: &Event) {
println!("At {} found {} kind {} '{}' {:?}", event.created_at, event.id, event.kind, event.content, event.tags);
}
async fn repl() { async fn repl() {
let mut tasks: Tasks = Default::default(); let mut tasks: Tasks = Default::default();
for argument in args().skip(1) { for argument in args().skip(1) {
tasks.add_task(make_task(&argument, &[Tag::Hashtag("arg".to_string())])); tasks.add_task(make_task(&argument, &[Tag::Hashtag("arg".to_string())]));
} }
let sub_id: SubscriptionId = CLIENT.subscribe(vec![Filter::new()], None).await;
let mut notifications = CLIENT.notifications();
println!("Finding existing events"); println!("Finding existing events");
let res = CLIENT let res = CLIENT
.get_events_of(vec![Filter::new()], None) .get_events_of(vec![Filter::new()], None)
@ -106,12 +96,12 @@ async fn repl() {
let (mut task_events, props): (Vec<Event>, Vec<Event>) = res.into_iter().partition(|e| e.kind.as_u32() == 1621); let (mut task_events, props): (Vec<Event>, Vec<Event>) = res.into_iter().partition(|e| e.kind.as_u32() == 1621);
task_events.sort_unstable(); task_events.sort_unstable();
for event in task_events { for event in task_events {
println!("{} found {} '{}' {:?}", event.created_at, event.kind, event.content, event.tags); print_event(&event);
tasks.add_task(event); tasks.add_task(event);
} }
for event in props { for event in props {
println!("{} found {} '{}' {:?}", event.created_at, event.kind, event.content, event.tags); print_event(&event);
tasks.referenced_tasks(&event, |t| t.props.push(event.clone())); tasks.add_prop(&event);
} }
}) })
.await; .await;
@ -202,12 +192,24 @@ async fn repl() {
tasks.add_task(tasks.make_task(&input)); tasks.add_task(tasks.make_task(&input));
} }
} }
tasks.print_current_tasks();
} }
Some(Err(e)) => eprintln!("{}", e), Some(Err(e)) => eprintln!("{}", e),
None => break, None => break,
} }
while let Ok(notification) = notifications.try_recv() {
if let RelayPoolNotification::Event {
subscription_id,
event,
..
} = notification
{
print_event(&event);
tasks.add(*event);
}
}
tasks.print_current_tasks();
} }
tasks.update_state("", |t| if t.pure_state() == State::Active { Some(State::Open) } else { None }); tasks.update_state("", |t| if t.pure_state() == State::Active { Some(State::Open) } else { None });

View File

@ -105,11 +105,23 @@ impl Tasks {
} }
} }
pub(crate) fn add(&mut self, event: Event) {
if event.kind.as_u64() == 1621 {
self.add_task(event)
} else {
self.add_prop(&event)
}
}
pub(crate) fn add_task(&mut self, event: Event) { pub(crate) fn add_task(&mut self, event: Event) {
self.referenced_tasks(&event, |t| t.children.push(event.id)); self.referenced_tasks(&event, |t| t.children.push(event.id));
self.tasks.insert(event.id, Task::new(event)); self.tasks.insert(event.id, Task::new(event));
} }
pub(crate) fn add_prop(&mut self, event: &Event) {
self.referenced_tasks(&event, |t| t.props.push(event.clone()));
}
pub(crate) fn move_up(&mut self) { pub(crate) fn move_up(&mut self) {
self.move_to( self.move_to(
self.position self.position