wip: multi accounts

This commit is contained in:
2024-02-19 09:58:27 +07:00
parent 0f06522c28
commit bfe35ad885
13 changed files with 537 additions and 465 deletions

View File

@@ -1,35 +1,52 @@
use crate::Nostr;
use crate::NostrClient;
use nostr_sdk::prelude::*;
use std::{str::FromStr, time::Duration};
use tauri::State;
#[tauri::command]
pub async fn get_profile(id: &str, nostr: State<'_, Nostr>) -> Result<Metadata, ()> {
let client = &nostr.client;
let public_key: PublicKey = match Nip19::from_bech32(id) {
pub async fn get_profile(id: &str, state: State<'_, NostrClient>) -> Result<Metadata, String> {
let client = state.0.lock().await;
let public_key: Option<PublicKey> = match Nip19::from_bech32(id) {
Ok(val) => match val {
Nip19::Pubkey(pubkey) => pubkey,
Nip19::Profile(profile) => profile.public_key,
_ => panic!("not nip19"),
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,
},
Err(_) => PublicKey::from_str(id).unwrap(),
};
let filter = Filter::new()
.author(public_key)
.kind(Kind::Metadata)
.limit(1);
if let Some(author) = public_key {
let filter = Filter::new().author(author).kind(Kind::Metadata).limit(1);
let events = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
.expect("Get metadata failed");
let events = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
.expect("Get metadata failed");
if let Some(event) = events.first() {
let metadata: Metadata = Metadata::from_json(&event.content).unwrap();
Ok(metadata)
if let Some(event) = events.first() {
let metadata: Metadata = Metadata::from_json(&event.content).unwrap();
Ok(metadata)
} else {
let rand_metadata = Metadata::new();
Ok(rand_metadata)
}
} else {
Err(())
Err("Public Key is not valid".into())
}
}
#[tauri::command]
pub async fn get_contact_list(state: State<'_, NostrClient>) -> Result<Vec<String>, String> {
let client = state.0.lock().await;
let contact_list = client.get_contact_list(Some(Duration::from_secs(10))).await;
if let Ok(list) = contact_list {
let v = list.into_iter().map(|f| f.public_key.to_hex()).collect();
Ok(v)
} else {
Err("Contact list not found".into())
}
}
@@ -43,10 +60,9 @@ pub async fn create_profile(
nip05: &str,
lud16: &str,
website: &str,
nostr: State<'_, Nostr>,
state: State<'_, NostrClient>,
) -> Result<EventId, ()> {
let client = &nostr.client;
let client = state.0.lock().await;
let metadata = Metadata::new()
.name(name)
.display_name(display_name)