New Greeting Tab + New File Structure
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
use easy_nostr::EasyNostr;
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use tauri::State;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
async fn get_client(state: &State<'_, NostrState>) -> Result<Arc<EasyNostr>, String> {
|
||||
let mut guard = state.0.lock().await;
|
||||
if guard.is_none() {
|
||||
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.primal.net",
|
||||
"wss://nostr.wine",
|
||||
])
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
*guard = Some(Arc::new(easy));
|
||||
|
||||
// Short sleep to allow initial connection
|
||||
sleep(Duration::from_millis(1500)).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?;
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn send_nostr_message(
|
||||
content: String,
|
||||
receiver_npub: String,
|
||||
state: State<'_, NostrState>,
|
||||
) -> 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)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(event_id.to_string())
|
||||
}
|
||||
@@ -1,21 +1,14 @@
|
||||
mod chat;
|
||||
mod home;
|
||||
mod news;
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
// Registriert den Nostr-State
|
||||
.manage(chat::NostrState(Mutex::new(None)))
|
||||
// HINZUGEFÜGT: Registriert den News-State für den API-Key
|
||||
.manage(news::NewsState::default())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
home::fetch_nostr_posts,
|
||||
chat::send_nostr_message,
|
||||
chat::get_nostr_messages,
|
||||
news::save_openrouter_key,
|
||||
news::fetch_ai_news,
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user