Compare commits
No commits in common. "9a2f3f8794c7944204dda00eb15186e919cd99ca" and "026915f870783e073e16c31c24f616b8c0a51179" have entirely different histories.
9a2f3f8794
...
026915f870
4 changed files with 69 additions and 106 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1 @@
|
||||||
/target
|
/target
|
||||||
|
|
||||||
*.html
|
|
||||||
/src/bin
|
|
||||||
|
|
77
src/main.rs
77
src/main.rs
|
@ -62,8 +62,38 @@ async fn main() {
|
||||||
|
|
||||||
CLIENT.connect().await;
|
CLIENT.connect().await;
|
||||||
|
|
||||||
|
let timeout = Duration::from_secs(3);
|
||||||
|
|
||||||
|
let filter = Filter::new();
|
||||||
|
let sub_id: SubscriptionId = CLIENT.subscribe(vec![filter.clone()], None).await;
|
||||||
|
|
||||||
repl().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 {
|
||||||
|
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 {
|
||||||
|
@ -75,38 +105,12 @@ 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");
|
|
||||||
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 {
|
|
||||||
print_event(&event);
|
|
||||||
tasks.add_task(event);
|
|
||||||
}
|
|
||||||
for event in props {
|
|
||||||
print_event(&event);
|
|
||||||
tasks.add_prop(&event);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
|
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
tasks.print_current_tasks();
|
tasks.print_current_tasks();
|
||||||
|
|
||||||
|
@ -192,31 +196,16 @@ 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 });
|
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
println!("Submitting events");
|
println!("Submitting created events");
|
||||||
// TODO send via message passing
|
|
||||||
let _ = CLIENT
|
let _ = CLIENT
|
||||||
.batch_event(
|
.batch_event(
|
||||||
tasks
|
tasks
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::make_event;
|
|
||||||
use nostr_sdk::{Event, EventId, Kind, Tag, Timestamp};
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use nostr_sdk::{Event, EventId, Kind, Tag, Timestamp};
|
||||||
|
use crate::make_event;
|
||||||
|
|
||||||
pub(crate) struct Task {
|
pub(crate) struct Task {
|
||||||
pub(crate) event: Event,
|
pub(crate) event: Event,
|
||||||
|
|
59
src/tasks.rs
59
src/tasks.rs
|
@ -1,9 +1,7 @@
|
||||||
use std::collections::HashMap;
|
use crate::{make_event, make_task};
|
||||||
|
use crate::task::{Task, State};
|
||||||
use nostr_sdk::{Event, EventId, Tag};
|
use nostr_sdk::{Event, EventId, Tag};
|
||||||
|
use std::collections::HashMap;
|
||||||
use crate::make_task;
|
|
||||||
use crate::task::{State, Task};
|
|
||||||
|
|
||||||
type TaskMap = HashMap<EventId, Task>;
|
type TaskMap = HashMap<EventId, Task>;
|
||||||
pub(crate) struct Tasks {
|
pub(crate) struct Tasks {
|
||||||
|
@ -40,11 +38,7 @@ impl Tasks {
|
||||||
/// Total time this task and its subtasks have been active
|
/// Total time this task and its subtasks have been active
|
||||||
fn total_time_tracked(&self, task: &EventId) -> u64 {
|
fn total_time_tracked(&self, task: &EventId) -> u64 {
|
||||||
self.tasks.get(task).map_or(0, |t| {
|
self.tasks.get(task).map_or(0, |t| {
|
||||||
t.time_tracked()
|
t.time_tracked() + t.children.iter().map(|e| self.total_time_tracked(e)).sum::<u64>()
|
||||||
+ t.children
|
|
||||||
.iter()
|
|
||||||
.map(|e| self.total_time_tracked(e))
|
|
||||||
.sum::<u64>()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,11 +54,10 @@ impl Tasks {
|
||||||
self.position.map_or_else(
|
self.position.map_or_else(
|
||||||
|| self.tasks.values().collect(),
|
|| self.tasks.values().collect(),
|
||||||
|p| {
|
|p| {
|
||||||
self.tasks
|
self.tasks.get(&p).map_or(Vec::new(), |t| {
|
||||||
.get(&p)
|
self.collect_tasks(&t.children)
|
||||||
.map_or(Vec::new(), |t| self.collect_tasks(&t.children))
|
})
|
||||||
},
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn print_current_tasks(&self) {
|
pub(crate) fn print_current_tasks(&self) {
|
||||||
|
@ -104,29 +97,18 @@ impl Tasks {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn referenced_tasks<F: Fn(&mut Task)>(&mut self, event: &Event, f: F) {
|
|
||||||
for tag in event.tags.iter() {
|
|
||||||
if let Tag::Event { event_id, .. } = tag {
|
|
||||||
self.tasks.get_mut(event_id).map(|t| f(t));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
for tag in event.tags.iter() {
|
||||||
self.tasks.insert(event.id, Task::new(event));
|
match tag {
|
||||||
|
Tag::Event { event_id, .. } => {
|
||||||
|
self.tasks
|
||||||
|
.get_mut(event_id)
|
||||||
|
.map(|t| t.children.push(event.id));
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
pub(crate) fn add_prop(&mut self, event: &Event) {
|
}
|
||||||
self.referenced_tasks(&event, |t| t.props.push(event.clone()));
|
}
|
||||||
|
self.tasks.insert(event.id, Task::new(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn move_up(&mut self) {
|
pub(crate) fn move_up(&mut self) {
|
||||||
|
@ -174,7 +156,6 @@ impl Tasks {
|
||||||
ParentIterator {
|
ParentIterator {
|
||||||
tasks: &self.tasks,
|
tasks: &self.tasks,
|
||||||
current: id,
|
current: id,
|
||||||
prev: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,16 +183,12 @@ impl Tasks {
|
||||||
struct ParentIterator<'a> {
|
struct ParentIterator<'a> {
|
||||||
tasks: &'a TaskMap,
|
tasks: &'a TaskMap,
|
||||||
current: Option<EventId>,
|
current: Option<EventId>,
|
||||||
/// Inexpensive helper to assert correctness
|
|
||||||
prev: Option<EventId>,
|
|
||||||
}
|
}
|
||||||
impl<'a> Iterator for ParentIterator<'a> {
|
impl<'a> Iterator for ParentIterator<'a> {
|
||||||
type Item = &'a Task;
|
type Item = &'a Task;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.current.and_then(|id| self.tasks.get(&id)).map(|t| {
|
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();
|
self.current = t.parent_id();
|
||||||
t
|
t
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue