Fixed the message loading for allready sendet messages (bug for loading self sended messages)
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use easy_nostr::EasyNostr;
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use tauri::State;
|
||||
use tokio::sync::Mutex;
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
pub struct NostrState(pub Mutex<Option<Arc<EasyNostr>>>);
|
||||
@@ -10,6 +10,7 @@ pub struct NostrState(pub Mutex<Option<Arc<EasyNostr>>>);
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ChatMessage {
|
||||
pub id: String,
|
||||
pub content: String,
|
||||
pub is_incoming: bool,
|
||||
pub created_at: u64,
|
||||
@@ -18,26 +19,27 @@ pub struct ChatMessage {
|
||||
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
|
||||
println!("[BACKEND] Initializing EasyNostr...");
|
||||
// Use your private key
|
||||
let easy =
|
||||
EasyNostr::new("nsec1rz87pcjnhcl9yfkyq2pn3mlptluvxdgdgw6fyhhg3zmzw2zpwn0sm2q82f")
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
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())?;
|
||||
"wss://relay.primal.net",
|
||||
"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;
|
||||
|
||||
// Short sleep to allow initial connection
|
||||
sleep(Duration::from_millis(1500)).await;
|
||||
}
|
||||
Ok(guard.as_ref().unwrap().clone())
|
||||
}
|
||||
@@ -48,26 +50,30 @@ pub async fn get_nostr_messages(
|
||||
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();
|
||||
if clean_npub.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
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();
|
||||
let client = get_client(&state).await?;
|
||||
|
||||
chat_msgs.sort_by_key(|m| m.created_at);
|
||||
println!("[BACKEND] {} Nachrichten für {} geladen.", chat_msgs.len(), clean_npub);
|
||||
// easy-nostr now handles the 10s timeout and heavy filtering internally
|
||||
let messages = client
|
||||
.get_private_messages(&clean_npub)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
// Map to frontend structure
|
||||
let chat_msgs: Vec<ChatMessage> = messages
|
||||
.into_iter()
|
||||
.map(|m| ChatMessage {
|
||||
id: m.id.to_string(),
|
||||
content: m.content,
|
||||
is_incoming: m.is_incoming,
|
||||
created_at: m.created_at.as_secs(), // Convert Timestamp to u64
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Sorting is already done in easy-nostr, but we ensure it here too
|
||||
Ok(chat_msgs)
|
||||
}
|
||||
|
||||
@@ -79,8 +85,11 @@ pub async fn send_nostr_message(
|
||||
) -> Result<String, String> {
|
||||
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)
|
||||
|
||||
let event_id = client
|
||||
.send_private_message(&clean_npub, &content)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(event_id.to_string())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user