From 4292def206acd9c27b33444b4c650b581751d4dd Mon Sep 17 00:00:00 2001 From: reya Date: Tue, 13 Feb 2024 09:36:11 +0700 Subject: [PATCH] feat: add more nostr commands --- src-tauri/src/nostr.rs | 1 + src-tauri/src/nostr/metadata.rs | 31 +++++++++++++++++++++++++++++++ src-tauri/src/nostr/relay.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src-tauri/src/nostr/relay.rs diff --git a/src-tauri/src/nostr.rs b/src-tauri/src/nostr.rs index 35697966..7dc1f87f 100644 --- a/src-tauri/src/nostr.rs +++ b/src-tauri/src/nostr.rs @@ -1,3 +1,4 @@ pub mod event; pub mod keys; pub mod metadata; +pub mod relay; diff --git a/src-tauri/src/nostr/metadata.rs b/src-tauri/src/nostr/metadata.rs index 243aaca0..de6a077b 100644 --- a/src-tauri/src/nostr/metadata.rs +++ b/src-tauri/src/nostr/metadata.rs @@ -33,3 +33,34 @@ pub async fn get_profile(id: &str, nostr: State<'_, Nostr>) -> Result, +) -> Result { + let client = &nostr.client; + + let metadata = Metadata::new() + .name(name) + .display_name(display_name) + .about(about) + .nip05(nip05) + .lud16(lud16) + .picture(Url::parse(picture).unwrap()) + .banner(Url::parse(banner).unwrap()) + .website(Url::parse(website).unwrap()); + + if let Ok(event_id) = client.set_metadata(&metadata).await { + Ok(event_id) + } else { + Err(()) + } +} diff --git a/src-tauri/src/nostr/relay.rs b/src-tauri/src/nostr/relay.rs new file mode 100644 index 00000000..45d87fe1 --- /dev/null +++ b/src-tauri/src/nostr/relay.rs @@ -0,0 +1,32 @@ +use crate::Nostr; +use nostr_sdk::prelude::*; +use tauri::State; + +#[tauri::command(async)] +pub async fn list_connected_relays(nostr: State<'_, Nostr>) -> Result, ()> { + let client = &nostr.client; + let relays = client.relays().await; + let list: Vec = relays.into_keys().collect(); + + Ok(list) +} + +#[tauri::command(async)] +pub async fn connect_relay(relay: &str, nostr: State<'_, Nostr>) -> Result { + let client = &nostr.client; + if let Ok(_) = client.add_relay(relay).await { + Ok(true) + } else { + Ok(false) + } +} + +#[tauri::command(async)] +pub async fn remove_relay(relay: &str, nostr: State<'_, Nostr>) -> Result { + let client = &nostr.client; + if let Ok(_) = client.remove_relay(relay).await { + Ok(true) + } else { + Ok(false) + } +}