better Chat, but many bugs
This commit is contained in:
@@ -1,9 +1,75 @@
|
||||
use easy_nostr::EasyNostr;
|
||||
use serde::Serialize;
|
||||
use tauri::State;
|
||||
use tokio::sync::Mutex;
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
// State-Struktur, die in lib.rs mit .manage() registriert wird
|
||||
pub struct NostrState(pub Mutex<Option<EasyNostr>>);
|
||||
pub struct NostrState(pub Mutex<Option<Arc<EasyNostr>>>);
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ChatMessage {
|
||||
pub content: String,
|
||||
pub is_incoming: bool,
|
||||
pub created_at: u64,
|
||||
}
|
||||
|
||||
async fn get_client(state: &State<'_, NostrState>) -> Result<Arc<EasyNostr>, String> {
|
||||
let mut guard = state.0.lock().await;
|
||||
if guard.is_none() {
|
||||
println!("[BACKEND] Verbinde zu Nostr Relays...");
|
||||
let easy = EasyNostr::new("nsec1rz87pcjnhcl9yfkyq2pn3mlptluvxdgdgw6fyhhg3zmzw2zpwn0sm2q82f")
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
// Nutze eine breite Auswahl an stabilen Relays
|
||||
easy.add_relays(vec![
|
||||
"wss://relay.damus.io",
|
||||
"wss://nos.lol",
|
||||
"wss://relay.snort.social",
|
||||
"wss://relay.malxte.de",
|
||||
"wss://relay.0xchat.com",
|
||||
"wss://nostr.wine"
|
||||
]).await.map_err(|e| e.to_string())?;
|
||||
|
||||
*guard = Some(Arc::new(easy));
|
||||
|
||||
// WICHTIG: Erhöhte Wartezeit beim Kaltstart, damit Relays DMs senden können
|
||||
println!("[BACKEND] Warte auf Relay-Handshake...");
|
||||
sleep(Duration::from_millis(2000)).await;
|
||||
}
|
||||
Ok(guard.as_ref().unwrap().clone())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_nostr_messages(
|
||||
receiver_npub: String,
|
||||
state: State<'_, NostrState>,
|
||||
) -> Result<Vec<ChatMessage>, String> {
|
||||
let clean_npub = receiver_npub.trim().trim_matches('#').to_string();
|
||||
if clean_npub.is_empty() { return Ok(vec![]); }
|
||||
|
||||
let client = get_client(&state).await?;
|
||||
|
||||
// Wir versuchen es bis zu 2 mal, falls das erste Mal 0 zurückkommt (Nostr-Latenz)
|
||||
let mut messages = client.get_private_messages(&clean_npub).await.unwrap_or_default();
|
||||
|
||||
if messages.is_empty() {
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
messages = client.get_private_messages(&clean_npub).await.unwrap_or_default();
|
||||
}
|
||||
|
||||
let mut chat_msgs: Vec<ChatMessage> = messages.into_iter().map(|m| ChatMessage {
|
||||
content: m.content,
|
||||
is_incoming: m.is_incoming,
|
||||
created_at: m.created_at.as_secs(),
|
||||
}).collect();
|
||||
|
||||
chat_msgs.sort_by_key(|m| m.created_at);
|
||||
println!("[BACKEND] {} Nachrichten für {} geladen.", chat_msgs.len(), clean_npub);
|
||||
Ok(chat_msgs)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn send_nostr_message(
|
||||
@@ -11,54 +77,10 @@ pub async fn send_nostr_message(
|
||||
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",
|
||||
])
|
||||
let clean_npub = receiver_npub.trim().trim_matches('#').to_string();
|
||||
let client = get_client(&state).await?;
|
||||
let event_id = client.send_private_message(&clean_npub, &content)
|
||||
.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())
|
||||
}
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(event_id.to_string())
|
||||
}
|
||||
|
||||
@@ -11,7 +11,8 @@ pub fn run() {
|
||||
.manage(chat::NostrState(Mutex::new(None)))
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
home::fetch_nostr_posts,
|
||||
chat::send_nostr_message
|
||||
chat::send_nostr_message,
|
||||
chat::get_nostr_messages
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user