chore: use latest nostr sdk

This commit is contained in:
reya 2024-10-28 16:19:50 +07:00
parent cfb017f70b
commit d87371aec4
6 changed files with 65 additions and 86 deletions

20
src-tauri/Cargo.lock generated
View File

@ -3081,7 +3081,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
dependencies = [
"cfg-if",
"windows-targets 0.52.6",
"windows-targets 0.48.5",
]
[[package]]
@ -3480,7 +3480,7 @@ checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8"
[[package]]
name = "nostr"
version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#677da267a80838d66b2ea627d2d08bca837495c9"
source = "git+https://github.com/rust-nostr/nostr#282fbc39373674b7394806d47311a8e483da0ef0"
dependencies = [
"aes",
"base64 0.22.1",
@ -3510,7 +3510,7 @@ dependencies = [
[[package]]
name = "nostr-database"
version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#677da267a80838d66b2ea627d2d08bca837495c9"
source = "git+https://github.com/rust-nostr/nostr#282fbc39373674b7394806d47311a8e483da0ef0"
dependencies = [
"async-trait",
"flatbuffers",
@ -3524,7 +3524,7 @@ dependencies = [
[[package]]
name = "nostr-lmdb"
version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#677da267a80838d66b2ea627d2d08bca837495c9"
source = "git+https://github.com/rust-nostr/nostr#282fbc39373674b7394806d47311a8e483da0ef0"
dependencies = [
"heed",
"nostr",
@ -3537,7 +3537,7 @@ dependencies = [
[[package]]
name = "nostr-relay-pool"
version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#677da267a80838d66b2ea627d2d08bca837495c9"
source = "git+https://github.com/rust-nostr/nostr#282fbc39373674b7394806d47311a8e483da0ef0"
dependencies = [
"async-utility",
"async-wsocket",
@ -3555,7 +3555,7 @@ dependencies = [
[[package]]
name = "nostr-sdk"
version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#677da267a80838d66b2ea627d2d08bca837495c9"
source = "git+https://github.com/rust-nostr/nostr#282fbc39373674b7394806d47311a8e483da0ef0"
dependencies = [
"async-utility",
"atomic-destructor",
@ -3575,7 +3575,7 @@ dependencies = [
[[package]]
name = "nostr-signer"
version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#677da267a80838d66b2ea627d2d08bca837495c9"
source = "git+https://github.com/rust-nostr/nostr#282fbc39373674b7394806d47311a8e483da0ef0"
dependencies = [
"async-utility",
"nostr",
@ -3588,7 +3588,7 @@ dependencies = [
[[package]]
name = "nostr-zapper"
version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#677da267a80838d66b2ea627d2d08bca837495c9"
source = "git+https://github.com/rust-nostr/nostr#282fbc39373674b7394806d47311a8e483da0ef0"
dependencies = [
"async-trait",
"nostr",
@ -3732,7 +3732,7 @@ dependencies = [
[[package]]
name = "nwc"
version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#677da267a80838d66b2ea627d2d08bca837495c9"
source = "git+https://github.com/rust-nostr/nostr#282fbc39373674b7394806d47311a8e483da0ef0"
dependencies = [
"async-utility",
"nostr",
@ -7223,7 +7223,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.59.0",
"windows-sys 0.48.0",
]
[[package]]

View File

@ -30,10 +30,6 @@ pub async fn watch_account(id: String, state: State<'_, Nostr>) -> Result<String
// Set empty password
keyring.set_password("").map_err(|e| e.to_string())?;
// Update state
let mut accounts = state.accounts.lock().unwrap().clone();
accounts.push(npub.clone());
// Get user's profile
let _ = client
.fetch_metadata(public_key, Some(Duration::from_secs(4)))
@ -80,10 +76,6 @@ pub async fn import_account(
// Update signer
client.set_signer(Some(signer)).await;
// Update state
let mut accounts = state.accounts.lock().unwrap().clone();
accounts.push(npub.clone());
// Get user's profile
let _ = client
.fetch_metadata(public_key, Some(Duration::from_secs(4)))
@ -131,10 +123,6 @@ pub async fn connect_account(uri: String, state: State<'_, Nostr>) -> Result<Str
// Update signer
let _ = client.set_signer(Some(signer.into())).await;
// Update state
let mut accounts = state.accounts.lock().unwrap().clone();
accounts.push(remote_npub.clone());
// Get user's profile
let _ = client
.fetch_metadata(remote_user, Some(Duration::from_secs(4)))

View File

@ -19,42 +19,44 @@ pub async fn get_event(id: String, state: State<'_, Nostr>) -> Result<RichEvent,
let client = &state.client;
let event_id = EventId::from_str(&id).map_err(|err| err.to_string())?;
match client.database().event_by_id(&event_id).await {
Ok(events) => {
if let Some(event) = events {
let raw = event.as_json();
let parsed = if event.kind == Kind::TextNote {
Some(parse_event(&event.content).await)
} else {
None
};
let events = client
.database()
.event_by_id(&event_id)
.await
.map_err(|err| err.to_string())?;
Ok(RichEvent { raw, parsed })
if let Some(event) = events {
let raw = event.as_json();
let parsed = if event.kind == Kind::TextNote {
Some(parse_event(&event.content).await)
} else {
None
};
Ok(RichEvent { raw, parsed })
} else {
let filter = Filter::new().id(event_id);
let mut rich_event = RichEvent {
raw: "".to_string(),
parsed: None,
};
let mut rx = client
.stream_events(vec![filter], Some(Duration::from_secs(5)))
.await
.map_err(|e| e.to_string())?;
while let Some(event) = rx.next().await {
let raw = event.as_json();
let parsed = if event.kind == Kind::TextNote {
Some(parse_event(&event.content).await)
} else {
let filter = Filter::new().id(event_id);
match client
.fetch_events(vec![filter], Some(Duration::from_secs(3)))
.await
{
Ok(events) => {
if let Some(event) = events.iter().next() {
let raw = event.as_json();
let parsed = if event.kind == Kind::TextNote {
Some(parse_event(&event.content).await)
} else {
None
};
Ok(RichEvent { raw, parsed })
} else {
Err(format!("Cannot found the event with ID {}", id))
}
}
Err(err) => Err(err.to_string()),
}
}
None
};
rich_event = RichEvent { raw, parsed }
}
Err(err) => Err(err.to_string()),
Ok(rich_event)
}
}
@ -332,25 +334,15 @@ pub async fn repost(raw: String, state: State<'_, Nostr>) -> Result<String, Stri
#[specta::specta]
pub async fn is_reposted(id: String, state: State<'_, Nostr>) -> Result<bool, String> {
let client = &state.client;
let accounts = state.accounts.lock().unwrap().clone();
let signer = client.signer().await.map_err(|err| err.to_string())?;
let public_key = signer.public_key().await.map_err(|err| err.to_string())?;
let event_id = EventId::parse(&id).map_err(|err| err.to_string())?;
let authors: Vec<PublicKey> = accounts
.iter()
.filter_map(|acc| {
if let Ok(pk) = PublicKey::from_str(acc) {
Some(pk)
} else {
None
}
})
.collect();
let filter = Filter::new()
.event(event_id)
.kind(Kind::Repost)
.authors(authors);
.author(public_key);
match client.database().query(vec![filter]).await {
Ok(events) => Ok(!events.is_empty()),

View File

@ -50,11 +50,22 @@ pub async fn get_profile(
return Ok(profile.metadata().as_json());
};
let metadata = client
.fetch_metadata(public_key, Some(Duration::from_secs(3)))
let filter = Filter::new()
.author(public_key)
.kind(Kind::Metadata)
.limit(1);
let mut metadata = Metadata::new();
let mut rx = client
.stream_events(vec![filter], Some(Duration::from_secs(5)))
.await
.map_err(|e| e.to_string())?;
while let Some(event) = rx.next().await {
metadata = Metadata::from_json(&event.content).map_err(|e| e.to_string())?;
}
Ok(metadata.as_json())
}

View File

@ -25,8 +25,6 @@ pub async fn sync_account(
app_handle: tauri::AppHandle,
) -> Result<(), String> {
let client = &state.client;
let bootstrap_relays = state.bootstrap_relays.lock().unwrap().clone();
let public_key = PublicKey::from_bech32(&id).map_err(|e| e.to_string())?;
let filter = Filter::new().author(public_key).kinds(vec![
@ -60,7 +58,7 @@ pub async fn sync_account(
}
});
if let Ok(output) = client.sync_with(&bootstrap_relays, filter, &opts).await {
if let Ok(output) = client.sync(filter, &opts).await {
println!("Success: {:?}", output.success);
println!("Failed: {:?}", output.failed);
@ -101,7 +99,7 @@ pub async fn sync_account(
])
.limit(10000);
if let Ok(output) = client.sync_with(&bootstrap_relays, filter, &opts).await {
if let Ok(output) = client.sync(filter, &opts).await {
println!("Success: {:?}", output.success);
println!("Failed: {:?}", output.failed);
}

View File

@ -29,8 +29,6 @@ pub mod common;
pub struct Nostr {
client: Client,
settings: Mutex<Settings>,
accounts: Mutex<Vec<String>>,
bootstrap_relays: Mutex<Vec<Url>>,
}
#[derive(Clone, Serialize, Deserialize, Type)]
@ -169,7 +167,7 @@ fn main() {
#[cfg(target_os = "macos")]
main_window.set_traffic_lights_inset(7.0, 10.0).unwrap();
let (client, bootstrap_relays) = tauri::async_runtime::block_on(async move {
let client = tauri::async_runtime::block_on(async move {
// Setup database
let database = NostrLMDB::open(config_dir.join("nostr"))
.expect("Error: cannot create database.");
@ -225,21 +223,13 @@ fn main() {
// Connect
client.connect_with_timeout(Duration::from_secs(10)).await;
// Get all bootstrap relays
let bootstrap_relays: Vec<Url> =
client.pool().all_relays().await.into_keys().collect();
(client, bootstrap_relays)
client
});
let accounts = get_all_accounts();
// Create global state
app.manage(Nostr {
client,
accounts: Mutex::new(accounts),
settings: Mutex::new(Settings::default()),
bootstrap_relays: Mutex::new(bootstrap_relays),
});
// Run notification thread