Compare commits

..

No commits in common. "23763f87ee0942b45c79fc9a2a4c245494f20687" and "199273d21b7ec87c1d2ab56377ead223c1c7fd2f" have entirely different histories.

4 changed files with 13 additions and 79 deletions

1
Cargo.lock generated
View File

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

View File

@ -8,4 +8,3 @@ 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,10 +10,4 @@ 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,32 +1,22 @@
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),
@ -43,29 +33,14 @@ 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 filter = Filter::new().kind(Kind::from(90002));
let sub_id: SubscriptionId = client.subscribe(vec![filter], 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) {
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...");
let mut notifications = client.notifications();
while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Event { subscription_id, event, .. } = notification {
let kind = event.kind;
@ -75,36 +50,3 @@ 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;
}
};
}
_ => {}
}
}
}