mostr/src/main.rs

274 lines
8.9 KiB
Rust
Raw Normal View History

use std::borrow::Borrow;
use std::env::args;
2024-07-24 13:03:34 +00:00
use std::fmt::Display;
use std::fs;
use std::io::{Read, stdin, stdout, Write};
2024-07-09 14:56:08 +00:00
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::ops::Deref;
2024-07-24 13:03:34 +00:00
use std::str::FromStr;
use std::sync::mpsc;
use std::sync::mpsc::Sender;
2024-07-09 14:56:08 +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;
/*
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-24 12:47:24 +00:00
static TASK_KIND: u64 = 1621;
2024-07-24 12:47:24 +00:00
static MY_KEYS: Lazy<Keys> = Lazy::new(|| {
match fs::read_to_string("keys") {
Ok(key) => {
Keys::from_str(&key).unwrap()
}
Err(e) => {
eprintln!("{}", e);
let keys = Keys::generate();
fs::write("keys", keys.secret_key().unwrap().to_string());
keys
},
}
});
struct EventSender {
tx: Sender<Event>,
keys: Keys,
}
impl EventSender {
fn submit(&self, event_builder: EventBuilder) -> Option<Event> {
or_print(event_builder.to_event(MY_KEYS.deref())).inspect(|event| {
or_print(self.tx.send(event.clone()));
})
}
}
fn or_print<T, U: Display>(result: Result<T, U>) -> Option<T> {
match result {
Ok(value) => Some(value),
Err(error) => {
eprintln!("{}", error);
None
}
}
}
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-24 12:47:24 +00:00
let client = Client::new(MY_KEYS.deref());
2024-07-23 10:27:36 +00:00
client.add_relay("ws://localhost:4736").await;
2024-07-24 12:47:24 +00:00
println!("My key: {}", MY_KEYS.public_key());
2024-07-23 10:27:36 +00:00
//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-23 10:27:36 +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-23 10:27:36 +00:00
//client.set_metadata(&metadata).await?;
2024-07-09 14:56:08 +00:00
2024-07-23 10:27:36 +00:00
client.connect().await;
2024-07-09 14:56:08 +00:00
2024-07-24 13:03:34 +00:00
let (tx, rx) = mpsc::channel::<Event>();
let mut tasks: Tasks = Tasks::from(EventSender {
keys: MY_KEYS.clone(),
tx,
});
2024-07-18 10:20:25 +00:00
2024-07-23 10:27:36 +00:00
let sub_id: SubscriptionId = client.subscribe(vec![Filter::new()], None).await;
2024-07-24 13:03:34 +00:00
eprintln!("Subscribed with {}", sub_id);
2024-07-23 10:27:36 +00:00
let mut notifications = client.notifications();
2024-07-19 18:04:21 +00:00
2024-07-24 13:03:34 +00:00
/*println!("Finding existing events");
let _ = client
.get_events_of(vec![Filter::new()], Some(Duration::from_secs(5)))
2024-07-19 13:49:23 +00:00
.map_ok(|res| {
println!("Found {} events", res.len());
2024-07-24 13:03:34 +00:00
let (mut task_events, props): (Vec<Event>, Vec<Event>) =
res.into_iter().partition(|e| e.kind.as_u32() == 1621);
2024-07-19 13:49:23 +00:00
task_events.sort_unstable();
for event in task_events {
2024-07-19 18:04:21 +00:00
print_event(&event);
2024-07-19 13:49:23 +00:00
tasks.add_task(event);
}
for event in props {
2024-07-19 18:04:21 +00:00
print_event(&event);
tasks.add_prop(&event);
2024-07-19 13:49:23 +00:00
}
})
2024-07-24 13:03:34 +00:00
.await;*/
2024-07-19 13:49:23 +00:00
2024-07-24 13:03:34 +00:00
let sender = tokio::spawn(async move {
while let Ok(e) = rx.recv() {
//eprintln!("Sending {}", e.id);
let _ = client.send_event(e).await;
}
println!("Stopping listeners...");
client.unsubscribe_all().await;
});
for argument in args().skip(1) {
tasks.make_task(&argument);
}
2024-07-19 13:49:23 +00:00
println!();
loop {
2024-07-24 13:03:34 +00:00
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();
2024-07-18 22:15:11 +00:00
print!(" {}> ", tasks.taskpath(tasks.get_position()));
stdout().flush().unwrap();
match stdin().lines().next() {
Some(Ok(input)) => {
let mut iter = input.chars();
2024-07-18 17:48:51 +00:00
let op = iter.next();
match op {
None => {}
2024-07-18 17:48:51 +00:00
Some(':') => match input[1..2].parse::<usize>() {
Ok(index) => {
2024-07-23 20:04:55 +00:00
let value = input[2..].to_string();
if tasks.properties.get(index) == Some(&value) {
tasks.properties.remove(index);
} else {
tasks.properties.insert(index, value);
}
}
Err(_) => {
let prop = &input[1..];
2024-07-18 22:15:11 +00:00
let pos = tasks.properties.iter().position(|s| s == &prop);
match pos {
None => {
2024-07-18 22:15:11 +00:00
tasks.properties.push(prop.to_string());
}
Some(i) => {
2024-07-18 22:15:11 +00:00
tasks.properties.remove(i);
}
}
}
},
2024-07-18 17:48:51 +00:00
Some('>') | Some('<') => {
2024-07-18 22:15:11 +00:00
tasks.update_state(&input[1..], |_| {
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
}
Some('.') => {
let mut dots = 1;
2024-07-18 22:15:11 +00:00
let mut pos = tasks.get_position();
for _ in iter.take_while(|c| c == &'.') {
dots += 1;
2024-07-18 22:15:11 +00:00
pos = tasks.parent(pos);
}
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-24 13:03:34 +00:00
let filtered: Vec<EventId> = tasks
.current_tasks()
.iter()
.filter(|t| t.event.content.starts_with(slice))
.map(|t| t.event.id)
.collect();
2024-07-19 13:49:23 +00:00
match filtered.len() {
0 => {
// No match, new task
2024-07-24 13:03:34 +00:00
tasks.make_task(slice)
}
1 => {
2024-07-24 13:03:34 +00:00
// One match, activate
Some(filtered.first().unwrap().clone())
}
_ => {
// Multiple match, filter
tasks.set_filter(filtered);
None
}
2024-07-19 13:49:23 +00:00
}
2024-07-18 22:15:11 +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 17:48:51 +00:00
_ => {
2024-07-24 13:03:34 +00:00
tasks.make_task(&input);
2024-07-18 12:19:12 +00:00
}
2024-07-17 19:55:25 +00:00
}
}
2024-07-18 15:25:09 +00:00
Some(Err(e)) => eprintln!("{}", e),
None => break,
}
}
2024-07-18 17:48:51 +00:00
2024-07-24 13:03:34 +00:00
tasks.update_state("", |t| {
if t.pure_state() == State::Active {
Some(State::Open)
} else {
None
}
});
drop(tasks);
2024-07-23 10:27:36 +00:00
2024-07-24 13:03:34 +00:00
eprintln!("Waiting for sync to relay...");
or_print(sender.await);
2024-07-23 10:27:36 +00:00
}
fn print_event(event: &Event) {
2024-07-24 13:03:34 +00:00
eprintln!(
"At {} found {} kind {} '{}' {:?}",
event.created_at, event.id, event.kind, event.content, event.tags
);
2024-07-23 10:27:36 +00:00
}