diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 98f99956..df7c3051 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -100,6 +100,10 @@ fn main() { nostr::metadata::create_profile, nostr::metadata::follow, nostr::metadata::unfollow, + nostr::metadata::set_interest, + nostr::metadata::get_interest, + nostr::metadata::set_settings, + nostr::metadata::get_settings, nostr::event::get_event, nostr::event::get_local_events, nostr::event::get_global_events, diff --git a/src-tauri/src/nostr/metadata.rs b/src-tauri/src/nostr/metadata.rs index ce29547a..34c02511 100644 --- a/src-tauri/src/nostr/metadata.rs +++ b/src-tauri/src/nostr/metadata.rs @@ -20,17 +20,20 @@ pub async fn get_profile(id: &str, state: State<'_, Nostr>) -> Result) -> Result) -> Result { + let client = state.client.lock().await; + let tag = Tag::Identifier("lume_user_interest".into()); + let builder = EventBuilder::new(Kind::ApplicationSpecificData, content, vec![tag]); + + if let Ok(event_id) = client.send_event_builder(builder).await { + Ok(event_id) + } else { + Err("Set interest failed".into()) + } +} + +#[tauri::command] +pub async fn get_interest(id: &str, state: State<'_, Nostr>) -> Result { + let client = state.client.lock().await; + let public_key: Option = match Nip19::from_bech32(id) { + Ok(val) => match val { + Nip19::Pubkey(pubkey) => Some(pubkey), + Nip19::Profile(profile) => Some(profile.public_key), + _ => None, + }, + Err(_) => match PublicKey::from_str(id) { + Ok(val) => Some(val), + Err(_) => None, + }, + }; + + if let Some(author) = public_key { + let filter = Filter::new() + .author(author) + .kind(Kind::ApplicationSpecificData) + .identifier("lume_user_interest") + .limit(1); + + let query = client + .get_events_of(vec![filter], Some(Duration::from_secs(10))) + .await; + + if let Ok(events) = query { + if let Some(event) = events.first() { + Ok(event.content.to_string()) + } else { + Err("User interest not found".into()) + } + } else { + Err("User interest not found".into()) + } + } else { + Err("Get interest failed".into()) + } +} + +#[tauri::command] +pub async fn set_settings(content: &str, state: State<'_, Nostr>) -> Result { + let client = state.client.lock().await; + let tag = Tag::Identifier("lume_user_settings".into()); + let builder = EventBuilder::new(Kind::ApplicationSpecificData, content, vec![tag]); + + if let Ok(event_id) = client.send_event_builder(builder).await { + Ok(event_id) + } else { + Err("Set interest failed".into()) + } +} + +#[tauri::command] +pub async fn get_settings(id: &str, state: State<'_, Nostr>) -> Result { + let client = state.client.lock().await; + let public_key: Option = match Nip19::from_bech32(id) { + Ok(val) => match val { + Nip19::Pubkey(pubkey) => Some(pubkey), + Nip19::Profile(profile) => Some(profile.public_key), + _ => None, + }, + Err(_) => match PublicKey::from_str(id) { + Ok(val) => Some(val), + Err(_) => None, + }, + }; + + if let Some(author) = public_key { + let filter = Filter::new() + .author(author) + .kind(Kind::ApplicationSpecificData) + .identifier("lume_user_settings") + .limit(1); + + let query = client + .get_events_of(vec![filter], Some(Duration::from_secs(10))) + .await; + + if let Ok(events) = query { + if let Some(event) = events.first() { + Ok(event.content.to_string()) + } else { + Err("User settings not found".into()) + } + } else { + Err("User settings not found".into()) + } + } else { + Err("Get settings failed".into()) + } +}