fix(nostr): debugging nostr after version update(in progress)

This commit is contained in:
Ryan Mwangi 2024-12-12 23:24:23 +03:00
parent 2414eec8ba
commit 86680d9c21
4 changed files with 13 additions and 25 deletions

2
Cargo.lock generated
View File

@ -728,8 +728,6 @@ dependencies = [
"leptos_router", "leptos_router",
"nostr-sdk", "nostr-sdk",
"serde", "serde",
"serde_json",
"tokio",
"uuid", "uuid",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",

View File

@ -20,10 +20,6 @@ serde = { version = "1.0", features = ["derive"] }
uuid = { version = "1.0", features = ["v4"] } uuid = { version = "1.0", features = ["v4"] }
web-sys = { version = "0.3", features = ["Event"] } web-sys = { version = "0.3", features = ["Event"] }
nostr-sdk = "0.37" nostr-sdk = "0.37"
tokio = { version = "1.41", features = ["full"] }
serde_json = "1.0"
[features] [features]
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"] csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]

View File

@ -5,9 +5,8 @@ use leptos_meta::*;
use crate::components::{item_form::ItemForm, items_list::ItemsList}; use crate::components::{item_form::ItemForm, items_list::ItemsList};
use crate::models::item::Item; use crate::models::item::Item;
use crate::nostr::NostrClient; use crate::nostr::NostrClient;
use tokio::sync::mpsc; use std::sync::mpsc;
use uuid::Uuid; use uuid::Uuid;
use serde_json;
use leptos::spawn_local; use leptos::spawn_local;
#[component] #[component]
@ -15,8 +14,7 @@ pub fn App() -> impl IntoView {
provide_meta_context(); provide_meta_context();
// Signal to store and update the list of items. // Signal to store and update the list of items.
let (items_signal, set_items) = create_signal(Vec::<Item>::new()); let (items_signal, set_items) = create_signal(Vec::<Item>::new());
let (tx, mut rx) = mpsc::channel::<String>(100); let (tx, mut rx) = mpsc::channel::<String>();
spawn_local(async move { spawn_local(async move {
//initialize nostr client //initialize nostr client
@ -24,7 +22,7 @@ pub fn App() -> impl IntoView {
nostr_client.subscribe_to_items(tx.clone()).await.unwrap(); nostr_client.subscribe_to_items(tx.clone()).await.unwrap();
// Handle incoming events // Handle incoming events
while let Some(content) = rx.recv().await { while let Ok(content) = rx.recv(){
if let Ok(item) = serde_json::from_str::<Item>(&content) { if let Ok(item) = serde_json::from_str::<Item>(&content) {
set_items.update(|items| items.push(item)); set_items.update(|items| items.push(item));
} }
@ -61,4 +59,4 @@ pub fn App() -> impl IntoView {
</div> </div>
</> </>
} }
} }

View File

@ -1,6 +1,5 @@
use nostr_sdk::client::Error; use nostr_sdk::client::Error;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use tokio::sync::mpsc;
use nostr_sdk::RelayPoolNotification; use nostr_sdk::RelayPoolNotification;
pub struct NostrClient { pub struct NostrClient {
@ -9,9 +8,9 @@ pub struct NostrClient {
impl NostrClient { impl NostrClient {
pub async fn new(relay_url: &str) -> Result<Self, Error> { pub async fn new(relay_url: &str) -> Result<Self, Error> {
let keys = Keys::generate_from_os_random(); let keys = Keys::new(SecretKey::generate());
let client = Client::new(&keys); let client = Client::new(keys);
client.add_relay(relay_url, None).await?; client.add_relay(relay_url).await?;
client.connect().await; client.connect().await;
Ok(Self { client }) Ok(Self { client })
@ -23,23 +22,20 @@ impl NostrClient {
description: String, description: String,
tags: Vec<(String, String)> tags: Vec<(String, String)>
) -> Result<(), Error> { ) -> Result<(), Error> {
let content = serde_json::json!({ let content = format!("{{\"name\":\"{}\",\"description\":\"{}\",\"tags\":[{}]}}", name, description, tags.iter().map(|(k, v)| format!("(\"{}\",\"{}\")", k, v)).collect::<Vec<_>>().join(","));
"name": name, let event = EventBuilder::new(Kind::TextNote, content).build()?;
"description": description, self.client.send_event_builder(event).await?;
"tags": tags
});
self.client.publish_text_note(content.to_string(), &[]).await?;
Ok(()) Ok(())
} }
pub async fn subscribe_to_items( pub async fn subscribe_to_items(
&self, &self,
tx: mpsc::Sender<String> tx: std::sync::mpsc::Sender<String>
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut notifications = self.client.notifications(); let mut notifications = self.client.notifications();
tokio::spawn(async move { std::thread::spawn(move || {
while let Ok(notification) = notifications.recv().await { 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(); let content = event.content.clone();
tx.send(content).await.unwrap(); tx.send(content).await.unwrap();
} }