From 23763f87ee0942b45c79fc9a2a4c245494f20687 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Sat, 13 Jul 2024 16:00:42 +0300 Subject: [PATCH] Implement global variables, tasks with tags and a repl --- Cargo.lock | 1 + Cargo.toml | 1 + README.md | 8 +++++- src/main.rs | 73 ++++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 65 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 503e3c3..d24fe21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -766,6 +766,7 @@ name = "mostr" version = "0.1.0" dependencies = [ "nostr-sdk", + "once_cell", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index e970724..aa4e6b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] nostr-sdk = "0.30" tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] } +once_cell = "1.19.0" diff --git a/README.md b/README.md index bde7fbc..6bd2255 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,10 @@ https://github.com/coracle-social/bucket ```sh cargo run # Listen to events nostril --envelope --content "realtime message" --kind 90002 | websocat ws://localhost:4736 # Send a test event -``` \ No newline at end of file +``` + +## Plans + +- TUI +- Send messages asynchronously +- How to clear terminal? \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index da9b770..ab6cdf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,32 @@ +use std::borrow::Borrow; use std::env::args; +use std::io::{stdin, stdout, Write}; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; +use std::ops::Deref; use std::time::Duration; +use once_cell::sync::Lazy; use nostr_sdk::async_utility::futures_util::TryFutureExt; use nostr_sdk::prelude::*; + +static TASK_KIND: Lazy = Lazy::new(||Kind::from(90002)); + +static MY_KEYS: Lazy = Lazy::new(||Keys::generate()); +static CLIENT: Lazy = Lazy::new(||Client::new(MY_KEYS.borrow().deref())); + #[tokio::main] async fn main() { - let my_keys: Keys = Keys::generate(); - let client = Client::new(&my_keys); - let proxy = Some(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9050))); - client.add_relay("ws://localhost:4736").await; - //client.add_relay("wss://relay.damus.io").await; - //client + CLIENT.add_relay("ws://localhost:4736").await; + //CLIENT.add_relay("wss://relay.damus.io").await; + //CLIENT // .add_relay_with_opts( // "wss://relay.nostr.info", // RelayOptions::new().proxy(proxy).flags(RelayServiceFlags::default().remove(RelayServiceFlags::WRITE)), // ) // .await?; - //client + //CLIENT // .add_relay_with_opts( // "ws://jgqaglhautb4k6e6i2g34jakxiemqp6z4wynlirltuukgkft2xuglmqd.onion", // RelayOptions::new().proxy(proxy), @@ -36,29 +43,28 @@ async fn main() { // .lud16("yuki@getalby.com") // .custom_field("custom_field", "my value"); - //client.set_metadata(&metadata).await?; + //CLIENT.set_metadata(&metadata).await?; - client.connect().await; + CLIENT.connect().await; let timeout = Duration::from_secs(3); - let task_kind = Kind::from(90002); - let filter = Filter::new().kind(task_kind); - let sub_id: SubscriptionId = client.subscribe(vec![filter.clone()], None).await; + let filter = Filter::new().kind(*TASK_KIND); + let sub_id: SubscriptionId = CLIENT.subscribe(vec![filter.clone()], None).await; for argument in args().skip(1) { - println!("Sending {}", argument); - let event = EventBuilder::new(task_kind, argument, []).to_event(&my_keys).unwrap(); - let _ = client.send_event(event).await; + let _ = send(argument, &[]).await; } + repl().await; + println!("Finding existing events"); - let res = client.get_events_of(vec![filter], Option::from(timeout)).map_ok(|res| + 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; - let mut notifications = client.notifications(); + let mut notifications = CLIENT.notifications(); println!("Listening for events..."); while let Ok(notification) = notifications.recv().await { if let RelayPoolNotification::Event { subscription_id, event, .. } = notification { @@ -69,3 +75,36 @@ async fn main() { } } } + +async fn send(text: String, tags: &[Tag]) -> Result { + println!("Sending {}", text); + let event = EventBuilder::new(*TASK_KIND, text, tags.to_vec()).to_event(&MY_KEYS).unwrap(); + return CLIENT.send_event(event).await; +} + +async fn repl() { + loop { + print!("> "); + stdout().flush().unwrap(); + match stdin().lines().next() { + Some(Ok(input)) => { + if input.trim() == "exit" { + break; + } + if input.trim().is_empty() { + continue; + } + let fut = match input.split_once(": ") { + None => { + send(input, &[Tag::Name("default".to_string())]).await; + } + Some(s) => { + let tags: Vec = s.1.split(" ").map(|t|Tag::Hashtag(t.to_string())).collect(); + send(s.0.to_string(), &tags).await; + } + }; + } + _ => {} + } + } +}