Send message function
This commit is contained in:
64
src-tauri/src/chat.rs
Normal file
64
src-tauri/src/chat.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use easy_nostr::EasyNostr;
|
||||
use tauri::State;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
// State-Struktur, die in lib.rs mit .manage() registriert wird
|
||||
pub struct NostrState(pub Mutex<Option<EasyNostr>>);
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn send_nostr_message(
|
||||
content: String,
|
||||
receiver_npub: String,
|
||||
state: State<'_, NostrState>,
|
||||
) -> Result<String, String> {
|
||||
let mut guard = state.0.lock().await;
|
||||
|
||||
// Lazy Initialization: Erstelle den Client nur, wenn er noch nicht existiert
|
||||
if guard.is_none() {
|
||||
println!("[NOSTR] Initialisiere neuen Client...");
|
||||
let easy =
|
||||
EasyNostr::new("nsec1rz87pcjnhcl9yfkyq2pn3mlptluvxdgdgw6fyhhg3zmzw2zpwn0sm2q82f")
|
||||
.await
|
||||
.map_err(|e| {
|
||||
println!("[NOSTR] Fehler bei Key-Initialisierung: {}", e);
|
||||
e.to_string()
|
||||
})?;
|
||||
|
||||
println!("[NOSTR] Verbinde zu Relays...");
|
||||
easy.add_relays(vec![
|
||||
//"wss://relay.damus.io",
|
||||
//"wss://nos.lol",
|
||||
//"wss://relay.snort.social",
|
||||
"wss://relay.malxte.de",
|
||||
])
|
||||
.await
|
||||
.map_err(|e| {
|
||||
println!("[NOSTR] Relay-Fehler: {}", e);
|
||||
e.to_string()
|
||||
})?;
|
||||
|
||||
*guard = Some(easy);
|
||||
println!("[NOSTR] Client bereit.");
|
||||
|
||||
// Kleine Pause für den Verbindungsaufbau der Relays
|
||||
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
|
||||
}
|
||||
|
||||
if let Some(easy) = guard.as_ref() {
|
||||
println!("[NOSTR] Sende Private Message an: {}", receiver_npub);
|
||||
|
||||
match easy.send_private_message(&receiver_npub, &content).await {
|
||||
Ok(event_id) => {
|
||||
let id_str = event_id.to_string();
|
||||
println!("[NOSTR] ERFOLG! ID: {}", id_str);
|
||||
Ok(id_str)
|
||||
}
|
||||
Err(e) => {
|
||||
println!("[NOSTR] VERSAND FEHLGESCHLAGEN: {:?}", e);
|
||||
Err(format!("Versand-Fehler: {}", e))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Err("Client konnte nicht initialisiert werden".to_string())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user