From 4e7da4108bd02242cb63a19cd84026b5725980b1 Mon Sep 17 00:00:00 2001 From: XIAO YU Date: Wed, 5 Jun 2024 15:24:32 +0900 Subject: [PATCH] feat: Add get user following function (#202) * feat: Add get user following function * refactor: Refactor get_following function to use state and string public key * feat: Update get_following function to use timeout duration * feat: Fix connect_remote_account function to return remote_npub without conversion * feat: Refactor get_following function to handle public key parsing errors * Refactor get_followers function to handle public key parsing errors and use timeout duration --- src-tauri/src/nostr/keys.rs | 2 +- src-tauri/src/nostr/metadata.rs | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/nostr/keys.rs b/src-tauri/src/nostr/keys.rs index 3f74af2c..bee6e163 100644 --- a/src-tauri/src/nostr/keys.rs +++ b/src-tauri/src/nostr/keys.rs @@ -239,7 +239,7 @@ pub async fn connect_remote_account(uri: &str, state: State<'_, Nostr>) -> Resul // Update signer let _ = client.set_signer(Some(signer.into())).await; - Ok(remote_npub.into()) + Ok(remote_npub) } Err(err) => Err(err.to_string()), } diff --git a/src-tauri/src/nostr/metadata.rs b/src-tauri/src/nostr/metadata.rs index 8cc5deed..dc8891b4 100644 --- a/src-tauri/src/nostr/metadata.rs +++ b/src-tauri/src/nostr/metadata.rs @@ -465,3 +465,67 @@ pub async fn zap_event( Err("Parse public key failed".into()) } } + +#[tauri::command] +#[specta::specta] +pub async fn get_following( + state: State<'_, Nostr>, + public_key: &str, + timeout: Option, +) -> Result, String> { + let client = &state.client; + let public_key = match PublicKey::from_str(public_key) { + Ok(val) => val, + Err(err) => return Err(err.to_string()), + }; + let duration = timeout.map(Duration::from_secs); + let filter = Filter::new().kind(Kind::ContactList).author(public_key); + let events = match client.get_events_of(vec![filter], duration).await { + Ok(events) => events, + Err(err) => return Err(err.to_string()), + }; + let mut ret: Vec = vec![]; + if let Some(latest_event) = events.iter().max_by_key(|event| event.created_at()) { + ret.extend(latest_event.tags().iter().filter_map(|tag| { + if let Some(TagStandard::PublicKey { + uppercase: false, .. + }) = ::clone(tag).to_standardized() + { + tag.content().map(String::from) + } else { + None + } + })); + } + Ok(ret) +} + +#[tauri::command] +#[specta::specta] +pub async fn get_followers( + state: State<'_, Nostr>, + public_key: &str, + timeout: Option, +) -> Result, String> { + let client = &state.client; + let public_key = match PublicKey::from_str(public_key) { + Ok(val) => val, + Err(err) => return Err(err.to_string()), + }; + let duration = timeout.map(Duration::from_secs); + + let filter = Filter::new().kind(Kind::ContactList).custom_tag( + SingleLetterTag::lowercase(Alphabet::P), + vec![public_key.to_hex()], + ); + let events = match client.get_events_of(vec![filter], duration).await { + Ok(events) => events, + Err(err) => return Err(err.to_string()), + }; + let ret: Vec = events + .into_iter() + .map(|event| event.author().to_hex()) + .collect(); + Ok(ret) + //todo: get more than 500 events +}