diff --git a/Cargo.lock b/Cargo.lock index e4c9e15..39cbb5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -728,8 +728,6 @@ dependencies = [ "leptos_router", "nostr-sdk", "serde", - "serde_json", - "tokio", "uuid", "wasm-bindgen", "web-sys", diff --git a/Cargo.toml b/Cargo.toml index 2264a67..4248caf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,10 +20,6 @@ serde = { version = "1.0", features = ["derive"] } uuid = { version = "1.0", features = ["v4"] } web-sys = { version = "0.3", features = ["Event"] } nostr-sdk = "0.37" -tokio = { version = "1.41", features = ["full"] } -serde_json = "1.0" - - [features] csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"] diff --git a/src/app.rs b/src/app.rs index 2b5c203..6abd8f9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,9 +5,8 @@ use leptos_meta::*; use crate::components::{item_form::ItemForm, items_list::ItemsList}; use crate::models::item::Item; use crate::nostr::NostrClient; -use tokio::sync::mpsc; +use std::sync::mpsc; use uuid::Uuid; -use serde_json; use leptos::spawn_local; #[component] @@ -15,8 +14,7 @@ pub fn App() -> impl IntoView { provide_meta_context(); // Signal to store and update the list of items. let (items_signal, set_items) = create_signal(Vec::::new()); - let (tx, mut rx) = mpsc::channel::(100); - + let (tx, mut rx) = mpsc::channel::(); spawn_local(async move { //initialize nostr client @@ -24,7 +22,7 @@ pub fn App() -> impl IntoView { nostr_client.subscribe_to_items(tx.clone()).await.unwrap(); // Handle incoming events - while let Some(content) = rx.recv().await { + while let Ok(content) = rx.recv(){ if let Ok(item) = serde_json::from_str::(&content) { set_items.update(|items| items.push(item)); } @@ -61,4 +59,4 @@ pub fn App() -> impl IntoView { } -} +} \ No newline at end of file diff --git a/src/nostr.rs b/src/nostr.rs index 30f7768..0b9bef4 100644 --- a/src/nostr.rs +++ b/src/nostr.rs @@ -1,6 +1,5 @@ use nostr_sdk::client::Error; use nostr_sdk::prelude::*; -use tokio::sync::mpsc; use nostr_sdk::RelayPoolNotification; pub struct NostrClient { @@ -9,9 +8,9 @@ pub struct NostrClient { impl NostrClient { pub async fn new(relay_url: &str) -> Result { - let keys = Keys::generate_from_os_random(); - let client = Client::new(&keys); - client.add_relay(relay_url, None).await?; + let keys = Keys::new(SecretKey::generate()); + let client = Client::new(keys); + client.add_relay(relay_url).await?; client.connect().await; Ok(Self { client }) @@ -23,23 +22,20 @@ impl NostrClient { description: String, tags: Vec<(String, String)> ) -> Result<(), Error> { - let content = serde_json::json!({ - "name": name, - "description": description, - "tags": tags - }); - self.client.publish_text_note(content.to_string(), &[]).await?; + let content = format!("{{\"name\":\"{}\",\"description\":\"{}\",\"tags\":[{}]}}", name, description, tags.iter().map(|(k, v)| format!("(\"{}\",\"{}\")", k, v)).collect::>().join(",")); + let event = EventBuilder::new(Kind::TextNote, content).build()?; + self.client.send_event_builder(event).await?; Ok(()) } pub async fn subscribe_to_items( &self, - tx: mpsc::Sender + tx: std::sync::mpsc::Sender ) -> Result<(), Error> { let mut notifications = self.client.notifications(); - tokio::spawn(async move { + std::thread::spawn(move || { while let Ok(notification) = notifications.recv().await { - if let RelayPoolNotification::Event(_url, event) = notification { + if let RelayPoolNotification::Event { relay_url, subscription_id, event } = notification { let content = event.content.clone(); tx.send(content).await.unwrap(); }