2024-07-13 13:00:42 +00:00
|
|
|
use std::borrow::Borrow;
|
2024-07-11 07:53:58 +00:00
|
|
|
use std::env::args;
|
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 22:23:25 +00:00
|
|
|
use nostr_sdk::async_utility::futures_util::TryFutureExt;
|
|
|
|
use nostr_sdk::prelude::*;
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
use crate::task::State;
|
|
|
|
use crate::tasks::Tasks;
|
|
|
|
|
|
|
|
mod task;
|
|
|
|
mod tasks;
|
|
|
|
|
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 {
|
2024-07-18 19:19:08 +00:00
|
|
|
println!("Found {} '{}' {:?}", event.kind, event.content, event.tags)
|
2024-07-18 09:09:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.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-13 13:00:42 +00:00
|
|
|
async fn repl() {
|
2024-07-18 22:15:11 +00:00
|
|
|
let mut tasks: Tasks = Default::default();
|
2024-07-18 10:20:25 +00:00
|
|
|
for argument in args().skip(1) {
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.add_task(make_task(&argument, &[Tag::Hashtag("arg".to_string())]));
|
2024-07-18 10:20:25 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 15:24:40 +00:00
|
|
|
println!();
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.print_current_tasks();
|
2024-07-18 17:48:51 +00:00
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
loop {
|
2024-07-18 22:15:11 +00:00
|
|
|
print!(" {}> ", tasks.taskpath(tasks.get_position()));
|
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) => {
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.properties.insert(index, input[2..].to_string());
|
2024-07-18 16:10:52 +00:00
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
let prop = &input[1..];
|
2024-07-18 22:15:11 +00:00
|
|
|
let pos = tasks.properties.iter().position(|s| s == &prop);
|
2024-07-18 16:10:52 +00:00
|
|
|
match pos {
|
|
|
|
None => {
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.properties.push(prop.to_string());
|
2024-07-18 16:10:52 +00:00
|
|
|
}
|
|
|
|
Some(i) => {
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.properties.remove(i);
|
2024-07-18 16:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-07-18 17:48:51 +00:00
|
|
|
|
|
|
|
Some('>') | Some('<') => {
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.update_state(&input[1..], |_| {
|
2024-07-18 22:23:25 +00:00
|
|
|
Some(if op.unwrap() == '<' {
|
|
|
|
State::Closed
|
|
|
|
} else {
|
|
|
|
State::Done
|
|
|
|
})
|
2024-07-18 17:48:51 +00:00
|
|
|
});
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.move_up()
|
2024-07-18 17:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 16:10:52 +00:00
|
|
|
Some('.') => {
|
|
|
|
let mut dots = 1;
|
2024-07-18 22:15:11 +00:00
|
|
|
let mut pos = tasks.get_position();
|
2024-07-18 16:10:52 +00:00
|
|
|
for _ in iter.take_while(|c| c == &'.') {
|
|
|
|
dots += 1;
|
2024-07-18 22:15:11 +00:00
|
|
|
pos = tasks.parent(pos);
|
2024-07-18 16:10:52 +00:00
|
|
|
}
|
2024-07-18 18:43:35 +00:00
|
|
|
let slice = &input[dots..];
|
|
|
|
if !slice.is_empty() {
|
2024-07-18 22:15:11 +00:00
|
|
|
pos = EventId::parse(slice).ok().or_else(|| {
|
|
|
|
tasks.move_to(pos);
|
2024-07-19 06:35:03 +00:00
|
|
|
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() {
|
|
|
|
0 => {
|
|
|
|
// No match, new task
|
|
|
|
let task = tasks.make_task(slice);
|
|
|
|
let ret = Some(task.id);
|
|
|
|
tasks.add_task(task);
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
// One match, select
|
|
|
|
Some(filtered.first().unwrap().clone())
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Multiple match, filter
|
|
|
|
tasks.set_filter(filtered);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2024-07-18 22:15:11 +00:00
|
|
|
});
|
2024-07-19 06:35:03 +00:00
|
|
|
if pos != None {
|
|
|
|
tasks.move_to(pos);
|
|
|
|
}
|
|
|
|
} else {
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.move_to(pos);
|
2024-07-18 18:43:35 +00:00
|
|
|
}
|
2024-07-18 16:10:52 +00:00
|
|
|
}
|
2024-07-18 17:48:51 +00:00
|
|
|
|
2024-07-18 16:10:52 +00:00
|
|
|
_ => {
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks.add_task(tasks.make_task(&input));
|
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 22:15:11 +00:00
|
|
|
tasks.print_current_tasks();
|
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 19:19:08 +00:00
|
|
|
println!("Submitting created events");
|
2024-07-18 12:19:12 +00:00
|
|
|
let _ = CLIENT
|
2024-07-18 09:09:18 +00:00
|
|
|
.batch_event(
|
2024-07-18 22:15:11 +00:00
|
|
|
tasks
|
|
|
|
.tasks
|
|
|
|
.into_values()
|
|
|
|
.flat_map(|t| {
|
|
|
|
let mut ev = t.props;
|
|
|
|
ev.push(t.event);
|
|
|
|
ev
|
|
|
|
})
|
|
|
|
.collect(),
|
2024-07-18 09:09:18 +00:00
|
|
|
RelaySendOptions::new().skip_send_confirmation(true),
|
|
|
|
)
|
2024-07-18 12:19:12 +00:00
|
|
|
.await;
|
2024-07-13 13:00:42 +00:00
|
|
|
}
|