2024-07-18 16:10:52 +00:00
|
|
|
use crate::State::*;
|
2024-07-18 09:03:32 +00:00
|
|
|
use nostr_sdk::async_utility::futures_util::TryFutureExt;
|
|
|
|
use nostr_sdk::prelude::*;
|
|
|
|
use once_cell::sync::Lazy;
|
2024-07-13 13:00:42 +00:00
|
|
|
use std::borrow::Borrow;
|
2024-07-17 19:55:25 +00:00
|
|
|
use std::collections::HashMap;
|
2024-07-11 07:53:58 +00:00
|
|
|
use std::env::args;
|
2024-07-18 09:03:32 +00:00
|
|
|
use std::fmt;
|
2024-07-18 18:11:00 +00:00
|
|
|
use std::fmt::{Display, Formatter};
|
2024-07-13 13:00:42 +00:00
|
|
|
use std::io::{stdin, stdout, Write};
|
2024-07-09 14:56:08 +00:00
|
|
|
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
|
2024-07-18 09:03:32 +00:00
|
|
|
use std::ops::Deref;
|
2024-07-11 07:53:58 +00:00
|
|
|
use std::time::Duration;
|
2024-07-09 14:56:08 +00:00
|
|
|
|
2024-07-18 09:03:32 +00:00
|
|
|
/*
|
|
|
|
1: Task Description
|
|
|
|
Issue Tracking: https://github.com/nostr-protocol/nips/blob/master/34.md
|
|
|
|
1621: MD Issue
|
|
|
|
1622: MD Reply
|
|
|
|
1630-1633: Status (Time-tracking, Kanban)
|
|
|
|
Calendar: https://github.com/nostr-protocol/nips/blob/master/52.md
|
|
|
|
31922 (GANTT, only Date)
|
|
|
|
31923 (Calendar, with Time)
|
|
|
|
*/
|
2024-07-13 13:00:42 +00:00
|
|
|
|
2024-07-18 09:03:32 +00:00
|
|
|
static MY_KEYS: Lazy<Keys> = Lazy::new(|| Keys::generate());
|
|
|
|
static CLIENT: Lazy<Client> = Lazy::new(|| Client::new(MY_KEYS.borrow().deref()));
|
2024-07-13 13:00:42 +00:00
|
|
|
|
2024-07-09 14:56:08 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let proxy = Some(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9050)));
|
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
CLIENT.add_relay("ws://localhost:4736").await;
|
|
|
|
//CLIENT.add_relay("wss://relay.damus.io").await;
|
|
|
|
//CLIENT
|
2024-07-09 14:56:08 +00:00
|
|
|
// .add_relay_with_opts(
|
|
|
|
// "wss://relay.nostr.info",
|
|
|
|
// RelayOptions::new().proxy(proxy).flags(RelayServiceFlags::default().remove(RelayServiceFlags::WRITE)),
|
|
|
|
// )
|
|
|
|
// .await?;
|
2024-07-13 13:00:42 +00:00
|
|
|
//CLIENT
|
2024-07-09 14:56:08 +00:00
|
|
|
// .add_relay_with_opts(
|
|
|
|
// "ws://jgqaglhautb4k6e6i2g34jakxiemqp6z4wynlirltuukgkft2xuglmqd.onion",
|
|
|
|
// RelayOptions::new().proxy(proxy),
|
|
|
|
// )
|
|
|
|
// .await?;
|
|
|
|
|
|
|
|
//let metadata = Metadata::new()
|
|
|
|
// .name("username")
|
|
|
|
// .display_name("My Username")
|
|
|
|
// .about("Description")
|
|
|
|
// .picture(Url::parse("https://example.com/avatar.png")?)
|
|
|
|
// .banner(Url::parse("https://example.com/banner.png")?)
|
|
|
|
// .nip05("username@example.com")
|
|
|
|
// .lud16("yuki@getalby.com")
|
|
|
|
// .custom_field("custom_field", "my value");
|
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
//CLIENT.set_metadata(&metadata).await?;
|
2024-07-09 14:56:08 +00:00
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
CLIENT.connect().await;
|
2024-07-09 14:56:08 +00:00
|
|
|
|
2024-07-11 07:53:58 +00:00
|
|
|
let timeout = Duration::from_secs(3);
|
|
|
|
|
2024-07-18 10:20:25 +00:00
|
|
|
let filter = Filter::new();
|
2024-07-13 13:00:42 +00:00
|
|
|
let sub_id: SubscriptionId = CLIENT.subscribe(vec![filter.clone()], None).await;
|
2024-07-11 07:53:58 +00:00
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
repl().await;
|
|
|
|
|
2024-07-11 07:53:58 +00:00
|
|
|
println!("Finding existing events");
|
2024-07-18 09:09:18 +00:00
|
|
|
let res = CLIENT
|
|
|
|
.get_events_of(vec![filter], Option::from(timeout))
|
|
|
|
.map_ok(|res| {
|
|
|
|
for event in res {
|
|
|
|
println!("Found {} {:?}", event.content, event.tags)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
2024-07-09 14:56:08 +00:00
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
let mut notifications = CLIENT.notifications();
|
2024-07-11 07:53:58 +00:00
|
|
|
println!("Listening for events...");
|
2024-07-09 14:56:08 +00:00
|
|
|
while let Ok(notification) = notifications.recv().await {
|
2024-07-18 09:09:18 +00:00
|
|
|
if let RelayPoolNotification::Event {
|
|
|
|
subscription_id,
|
|
|
|
event,
|
|
|
|
..
|
|
|
|
} = notification
|
|
|
|
{
|
2024-07-09 14:56:08 +00:00
|
|
|
let kind = event.kind;
|
|
|
|
let content = &event.content;
|
|
|
|
println!("{kind}: {content}");
|
|
|
|
//break; // Exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-13 13:00:42 +00:00
|
|
|
|
2024-07-18 17:48:51 +00:00
|
|
|
fn make_task(text: &str, tags: &[Tag]) -> Event {
|
|
|
|
make_event(Kind::from(1621), text, tags)
|
|
|
|
}
|
|
|
|
fn make_event(kind: Kind, text: &str, tags: &[Tag]) -> Event {
|
|
|
|
EventBuilder::new(kind, text, tags.to_vec())
|
2024-07-18 09:09:18 +00:00
|
|
|
.to_event(&MY_KEYS)
|
|
|
|
.unwrap()
|
2024-07-17 19:55:25 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 12:19:12 +00:00
|
|
|
type TaskMap = HashMap<EventId, Task>;
|
2024-07-18 17:48:51 +00:00
|
|
|
fn add_task(tasks: &mut TaskMap, event: Event) -> Option<Task> {
|
|
|
|
tasks.insert(event.id, Task::new(event))
|
2024-07-18 15:25:09 +00:00
|
|
|
}
|
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
async fn repl() {
|
2024-07-18 12:19:12 +00:00
|
|
|
let mut tasks: TaskMap = HashMap::new();
|
2024-07-18 10:20:25 +00:00
|
|
|
for argument in args().skip(1) {
|
|
|
|
add_task(
|
|
|
|
&mut tasks,
|
2024-07-18 17:48:51 +00:00
|
|
|
make_task(&argument, &[Tag::Hashtag("arg".to_string())]),
|
2024-07-18 10:20:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-18 09:08:44 +00:00
|
|
|
let mut properties: Vec<String> = vec!["id".into(), "name".into(), "state".into()];
|
2024-07-18 12:19:12 +00:00
|
|
|
let mut position: Option<EventId> = None;
|
2024-07-18 15:24:40 +00:00
|
|
|
let print_tasks = |tasks: Vec<&Task>, properties: &Vec<String>| {
|
|
|
|
println!("{}", properties.join(" "));
|
|
|
|
for task in tasks {
|
|
|
|
println!("{}", properties.iter().map(|p| task.get(p).unwrap_or(String::new())).collect::<Vec<String>>().join(" "));
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
};
|
2024-07-18 17:48:51 +00:00
|
|
|
|
2024-07-18 15:24:40 +00:00
|
|
|
println!();
|
|
|
|
print_tasks(tasks.values().collect(), &properties);
|
2024-07-18 17:48:51 +00:00
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
loop {
|
2024-07-18 09:08:44 +00:00
|
|
|
let mut prompt = String::with_capacity(64);
|
2024-07-17 21:38:27 +00:00
|
|
|
let mut pos = position;
|
|
|
|
while pos.is_some() {
|
|
|
|
let id = pos.unwrap();
|
|
|
|
let task = tasks.get(&id);
|
|
|
|
prompt = task.map_or(id.to_string(), |t| t.event.content.clone()) + " " + &prompt;
|
|
|
|
pos = task.and_then(|t| t.parent_id());
|
|
|
|
}
|
|
|
|
print!(" {}> ", prompt);
|
2024-07-13 13:00:42 +00:00
|
|
|
stdout().flush().unwrap();
|
|
|
|
match stdin().lines().next() {
|
|
|
|
Some(Ok(input)) => {
|
2024-07-18 16:10:52 +00:00
|
|
|
let mut iter = input.chars();
|
2024-07-18 17:48:51 +00:00
|
|
|
let op = iter.next();
|
|
|
|
match op {
|
2024-07-18 16:10:52 +00:00
|
|
|
None => {}
|
2024-07-18 17:48:51 +00:00
|
|
|
|
2024-07-18 16:10:52 +00:00
|
|
|
Some(':') => match input[1..2].parse::<usize>() {
|
|
|
|
Ok(index) => {
|
|
|
|
properties.insert(index, input[2..].to_string());
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
let prop = &input[1..];
|
|
|
|
let pos = properties.iter().position(|s| s == &prop);
|
|
|
|
match pos {
|
|
|
|
None => {
|
|
|
|
properties.push(prop.to_string());
|
|
|
|
}
|
|
|
|
Some(i) => {
|
|
|
|
properties.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-07-18 17:48:51 +00:00
|
|
|
|
|
|
|
Some('>') | Some('<') => {
|
|
|
|
position.inspect(|e| {
|
|
|
|
let pos = tasks.get(e)
|
2024-07-18 18:11:00 +00:00
|
|
|
.and_then(|t| t.state())
|
|
|
|
.and_then(|state| STATES.iter().position(|s| s == &state.state))
|
2024-07-18 17:48:51 +00:00
|
|
|
.unwrap_or(1);
|
|
|
|
tasks.get_mut(e).map(|t| t.props.push(make_event(STATES[if op.unwrap() == '<' { pos - 1 } else { pos + 1 }].kind(), &input[1..], &[Tag::event(e.clone())])));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-18 16:10:52 +00:00
|
|
|
Some('.') => {
|
|
|
|
let mut dots = 1;
|
|
|
|
for _ in iter.take_while(|c| c == &'.') {
|
|
|
|
dots += 1;
|
|
|
|
position = position
|
|
|
|
.and_then(|id| tasks.get(&id))
|
|
|
|
.and_then(|t| t.parent_id());
|
|
|
|
}
|
|
|
|
let _ = EventId::parse(&input[dots..]).map(|p| position = Some(p));
|
|
|
|
}
|
2024-07-18 17:48:51 +00:00
|
|
|
|
2024-07-18 16:10:52 +00:00
|
|
|
_ => {
|
2024-07-18 12:19:12 +00:00
|
|
|
let mut tags: Vec<Tag> = Vec::new();
|
|
|
|
position.inspect(|p| tags.push(Tag::event(*p)));
|
|
|
|
let event = match input.split_once(": ") {
|
2024-07-18 17:48:51 +00:00
|
|
|
None => make_task(&input, &tags),
|
2024-07-18 12:19:12 +00:00
|
|
|
Some(s) => {
|
|
|
|
tags.append(
|
|
|
|
&mut s.1.split(" ")
|
|
|
|
.map(|t| Tag::Hashtag(t.to_string()))
|
|
|
|
.collect());
|
2024-07-18 17:48:51 +00:00
|
|
|
make_task(s.0, &tags)
|
2024-07-18 12:19:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
for tag in event.tags.iter() {
|
|
|
|
match tag {
|
|
|
|
Tag::Event { event_id, .. } => {
|
|
|
|
tasks
|
|
|
|
.get_mut(event_id)
|
2024-07-18 15:24:40 +00:00
|
|
|
.map(|t| t.children.push(event.id));
|
2024-07-18 12:19:12 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-07-18 15:24:40 +00:00
|
|
|
let _ = add_task(&mut tasks, event);
|
2024-07-18 12:19:12 +00:00
|
|
|
}
|
2024-07-17 19:55:25 +00:00
|
|
|
}
|
2024-07-18 17:48:51 +00:00
|
|
|
|
2024-07-18 15:24:40 +00:00
|
|
|
let tasks: Vec<&Task> =
|
|
|
|
position.map_or(tasks.values().collect(),
|
|
|
|
|p| {
|
|
|
|
tasks.get(&p)
|
|
|
|
.map_or(Vec::new(), |t| t.children.iter().filter_map(|id| tasks.get(id)).collect())
|
|
|
|
});
|
|
|
|
print_tasks(tasks, &properties);
|
2024-07-13 13:00:42 +00:00
|
|
|
}
|
2024-07-18 15:25:09 +00:00
|
|
|
Some(Err(e)) => eprintln!("{}", e),
|
|
|
|
None => break,
|
2024-07-13 13:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-18 17:48:51 +00:00
|
|
|
|
2024-07-18 15:25:09 +00:00
|
|
|
println!();
|
2024-07-18 12:19:12 +00:00
|
|
|
let _ = CLIENT
|
2024-07-18 09:09:18 +00:00
|
|
|
.batch_event(
|
|
|
|
tasks.into_values().map(|t| t.event).collect(),
|
|
|
|
RelaySendOptions::new().skip_send_confirmation(true),
|
|
|
|
)
|
2024-07-18 12:19:12 +00:00
|
|
|
.await;
|
2024-07-13 13:00:42 +00:00
|
|
|
}
|
2024-07-17 19:55:25 +00:00
|
|
|
|
|
|
|
struct Task {
|
|
|
|
event: Event,
|
2024-07-18 15:24:40 +00:00
|
|
|
children: Vec<EventId>,
|
|
|
|
props: Vec<Event>
|
2024-07-17 19:55:25 +00:00
|
|
|
}
|
|
|
|
impl Task {
|
|
|
|
fn new(event: Event) -> Task {
|
|
|
|
Task {
|
|
|
|
event,
|
|
|
|
children: Vec::new(),
|
2024-07-18 15:24:40 +00:00
|
|
|
props: Vec::new(),
|
2024-07-17 19:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-18 09:08:44 +00:00
|
|
|
|
|
|
|
fn parent_id(&self) -> Option<EventId> {
|
|
|
|
for tag in self.event.tags.iter() {
|
|
|
|
match tag {
|
|
|
|
Tag::Event { event_id, .. } => return Some(*event_id),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-07-18 18:11:00 +00:00
|
|
|
fn descriptions(&self) -> impl Iterator<Item = String> + '_ {
|
|
|
|
self.props.iter().filter_map(|event| {
|
|
|
|
if event.kind == Kind::TextNote {
|
|
|
|
Some(event.content.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-18 09:08:44 +00:00
|
|
|
fn states(&self) -> impl Iterator<Item = TaskState> + '_ {
|
2024-07-18 15:24:40 +00:00
|
|
|
self.props.iter().filter_map(|event| {
|
2024-07-18 09:08:44 +00:00
|
|
|
match event.kind.as_u32() {
|
|
|
|
1630 => Some(Open),
|
|
|
|
1631 => Some(Done),
|
|
|
|
1632 => Some(Closed),
|
|
|
|
1633 => Some(Active),
|
|
|
|
_ => None,
|
2024-07-18 17:48:51 +00:00
|
|
|
}.map(|s| TaskState {
|
2024-07-18 18:11:00 +00:00
|
|
|
name: if event.content.is_empty() { None } else { Some(event.content.clone()) },
|
2024-07-18 09:08:44 +00:00
|
|
|
state: s,
|
2024-07-18 17:48:51 +00:00
|
|
|
time: event.created_at.clone(),
|
2024-07-18 09:08:44 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-18 18:11:00 +00:00
|
|
|
fn state(&self) -> Option<TaskState> {
|
|
|
|
self.states().max_by_key(|t| t.time)
|
2024-07-18 09:08:44 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 18:11:00 +00:00
|
|
|
fn default_state(&self) -> TaskState {
|
|
|
|
TaskState {
|
|
|
|
name: None,
|
2024-07-18 17:48:51 +00:00
|
|
|
state: Open,
|
|
|
|
time: self.event.created_at,
|
2024-07-18 18:11:00 +00:00
|
|
|
}
|
2024-07-18 09:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&self, property: &str) -> Option<String> {
|
|
|
|
match property {
|
|
|
|
"id" => Some(self.event.id.to_string()),
|
|
|
|
"parentid" => self.parent_id().map(|i| i.to_string()),
|
2024-07-18 18:11:00 +00:00
|
|
|
"state" => self.state().map(|s| s.to_string()),
|
2024-07-18 09:08:44 +00:00
|
|
|
"name" => Some(self.event.content.clone()),
|
|
|
|
"desc" | "description" => self.descriptions().fold(None, |total, s| {
|
|
|
|
Some(match total {
|
|
|
|
None => s,
|
|
|
|
Some(i) => i + " " + &s,
|
|
|
|
})
|
|
|
|
}),
|
2024-07-18 15:25:09 +00:00
|
|
|
_ => {
|
|
|
|
eprintln!("Unknown column {}", property);
|
|
|
|
None
|
|
|
|
},
|
2024-07-18 09:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-18 09:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TaskState {
|
2024-07-18 18:11:00 +00:00
|
|
|
name: Option<String>,
|
2024-07-18 09:03:32 +00:00
|
|
|
state: State,
|
|
|
|
time: Timestamp,
|
|
|
|
}
|
2024-07-18 18:11:00 +00:00
|
|
|
impl Display for TaskState {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}{}", self.state, self.name.as_ref().map_or(String::new(), |s| format!(": {}", s)))
|
|
|
|
}
|
|
|
|
}
|
2024-07-18 17:48:51 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
2024-07-18 09:03:32 +00:00
|
|
|
enum State {
|
2024-07-18 17:48:51 +00:00
|
|
|
Closed,
|
2024-07-18 09:03:32 +00:00
|
|
|
Open,
|
|
|
|
Active,
|
|
|
|
Done,
|
|
|
|
}
|
2024-07-18 17:48:51 +00:00
|
|
|
impl State {
|
|
|
|
fn kind(&self) -> Kind {
|
|
|
|
match self {
|
|
|
|
Closed => Kind::from(1632),
|
|
|
|
Open => Kind::from(1630),
|
|
|
|
Active => Kind::from(1633),
|
|
|
|
Done => Kind::from(1631),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static STATES: [State; 4] = [Closed, Open, Active, Done];
|
2024-07-18 09:03:32 +00:00
|
|
|
impl fmt::Display for State {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|