forked from janek/mostr
1
0
Fork 0

Implement global variables, tasks with tags and a repl

This commit is contained in:
xeruf 2024-07-13 16:00:42 +03:00
parent 32b7ff94ec
commit 23763f87ee
4 changed files with 65 additions and 18 deletions

1
Cargo.lock generated
View File

@ -766,6 +766,7 @@ name = "mostr"
version = "0.1.0"
dependencies = [
"nostr-sdk",
"once_cell",
"tokio",
]

View File

@ -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"

View File

@ -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
```
```
## Plans
- TUI
- Send messages asynchronously
- How to clear terminal?

View File

@ -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<Kind> = Lazy::new(||Kind::from(90002));
static MY_KEYS: Lazy<Keys> = Lazy::new(||Keys::generate());
static CLIENT: Lazy<Client> = 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<EventId, Error> {
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<Tag> = s.1.split(" ").map(|t|Tag::Hashtag(t.to_string())).collect();
send(s.0.to_string(), &tags).await;
}
};
}
_ => {}
}
}
}