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",
"nostr-sdk",
"serde",
"serde_json",
"tokio",
"uuid",
"wasm-bindgen",
"web-sys",

View File

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

View File

@ -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::<Item>::new());
let (tx, mut rx) = mpsc::channel::<String>(100);
let (tx, mut rx) = mpsc::channel::<String>();
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::<Item>(&content) {
set_items.update(|items| items.push(item));
}
@ -61,4 +59,4 @@ pub fn App() -> impl IntoView {
</div>
</>
}
}
}

View File

@ -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<Self, Error> {
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::<Vec<_>>().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<String>
tx: std::sync::mpsc::Sender<String>
) -> 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();
}