Compare commits

...

2 Commits

Author SHA1 Message Date
xeruf 23763f87ee Implement global variables, tasks with tags and a repl 2024-07-13 16:12:39 +03:00
xeruf 32b7ff94ec feat: implement sending events from args and finding events 2024-07-11 10:53:58 +03:00
4 changed files with 79 additions and 13 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

@ -11,3 +11,9 @@ https://github.com/coracle-social/bucket
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,22 +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),
@ -33,14 +43,29 @@ 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 filter = Filter::new().kind(Kind::from(90002));
let sub_id: SubscriptionId = client.subscribe(vec![filter], None).await;
let timeout = Duration::from_secs(3);
let mut notifications = client.notifications();
let filter = Filter::new().kind(*TASK_KIND);
let sub_id: SubscriptionId = CLIENT.subscribe(vec![filter.clone()], None).await;
for argument in args().skip(1) {
let _ = send(argument, &[]).await;
}
repl().await;
println!("Finding existing events");
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();
println!("Listening for events...");
while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Event { subscription_id, event, .. } = notification {
let kind = event.kind;
@ -50,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;
}
};
}
_ => {}
}
}
}