From bacfaed48a541ea437b39feff2fe405db71db27a Mon Sep 17 00:00:00 2001 From: reya Date: Wed, 25 Sep 2024 09:59:54 +0700 Subject: [PATCH] wip: local relay --- src-tauri/Cargo.lock | 129 +++++++----------- src-tauri/Cargo.toml | 3 +- src-tauri/src/commands/account.rs | 2 +- src-tauri/src/common.rs | 24 +--- src-tauri/src/main.rs | 87 ++++++++---- .../$account/_settings/general.lazy.tsx | 5 +- 6 files changed, 123 insertions(+), 127 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 16237cfa..e2ac6878 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -19,6 +19,7 @@ dependencies = [ "keyring-search", "linkify", "monitor", + "nostr-relay-builder", "nostr-sdk", "objc", "rand 0.8.5", @@ -346,9 +347,9 @@ checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" [[package]] name = "async-trait" -version = "0.1.82" +version = "0.1.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a27b8a3a6e1a44fa4c8baf1f653e4172e81486d4941f2237e20dc2d0cf4ddff1" +checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", @@ -370,8 +371,7 @@ dependencies = [ [[package]] name = "async-wsocket" version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c0984bead67f20366bc8dd46018dfbe189b67eeefb0e5b86b9eade18d7c3c3b" +source = "git+https://github.com/shadowylab/async-wsocket?rev=4d6a5b1780e65dc657ac36e5990a97c10feef072#4d6a5b1780e65dc657ac36e5990a97c10feef072" dependencies = [ "async-utility", "futures", @@ -621,7 +621,7 @@ dependencies = [ [[package]] name = "border" version = "0.1.0" -source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#994bb36b860f91e8a6e9990f203031f4346a29b7" +source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#e2890ea09160354103406e1e2a908a3816a2a89c" dependencies = [ "cocoa 0.25.0", "color", @@ -929,7 +929,7 @@ dependencies = [ [[package]] name = "color" version = "0.1.0" -source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#994bb36b860f91e8a6e9990f203031f4346a29b7" +source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#e2890ea09160354103406e1e2a908a3816a2a89c" dependencies = [ "cocoa 0.25.0", "objc", @@ -2438,9 +2438,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da62f120a8a37763efb0cf8fdf264b884c7b8b9ac8660b900c8661030c00e6ba" +checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" dependencies = [ "bytes", "futures-channel", @@ -2451,7 +2451,6 @@ dependencies = [ "pin-project-lite", "socket2", "tokio", - "tower", "tower-service", "tracing", ] @@ -2819,9 +2818,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.158" +version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" [[package]] name = "libloading" @@ -3088,7 +3087,7 @@ dependencies = [ [[package]] name = "monitor" version = "0.1.0" -source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#994bb36b860f91e8a6e9990f203031f4346a29b7" +source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#e2890ea09160354103406e1e2a908a3816a2a89c" dependencies = [ "cocoa 0.25.0", "core-foundation 0.9.4", @@ -3226,7 +3225,7 @@ dependencies = [ [[package]] name = "nostr" version = "0.35.0" -source = "git+https://github.com/rust-nostr/nostr#8c1e2a85c5476f9aa0848a39676632833bc8446d" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" dependencies = [ "aes", "base64 0.22.1", @@ -3256,7 +3255,7 @@ dependencies = [ [[package]] name = "nostr-database" version = "0.35.0" -source = "git+https://github.com/rust-nostr/nostr#8c1e2a85c5476f9aa0848a39676632833bc8446d" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" dependencies = [ "async-trait", "flatbuffers", @@ -3270,7 +3269,7 @@ dependencies = [ [[package]] name = "nostr-lmdb" version = "0.35.0" -source = "git+https://github.com/rust-nostr/nostr#8c1e2a85c5476f9aa0848a39676632833bc8446d" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" dependencies = [ "heed", "nostr", @@ -3280,10 +3279,25 @@ dependencies = [ "tracing", ] +[[package]] +name = "nostr-relay-builder" +version = "0.35.0" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" +dependencies = [ + "async-utility", + "async-wsocket", + "atomic-destructor", + "nostr", + "nostr-database", + "thiserror", + "tokio", + "tracing", +] + [[package]] name = "nostr-relay-pool" version = "0.35.0" -source = "git+https://github.com/rust-nostr/nostr#8c1e2a85c5476f9aa0848a39676632833bc8446d" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" dependencies = [ "async-utility", "async-wsocket", @@ -3301,7 +3315,7 @@ dependencies = [ [[package]] name = "nostr-sdk" version = "0.35.0" -source = "git+https://github.com/rust-nostr/nostr#8c1e2a85c5476f9aa0848a39676632833bc8446d" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" dependencies = [ "async-utility", "atomic-destructor", @@ -3321,7 +3335,7 @@ dependencies = [ [[package]] name = "nostr-signer" version = "0.35.0" -source = "git+https://github.com/rust-nostr/nostr#8c1e2a85c5476f9aa0848a39676632833bc8446d" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" dependencies = [ "async-utility", "nostr", @@ -3334,7 +3348,7 @@ dependencies = [ [[package]] name = "nostr-zapper" version = "0.35.0" -source = "git+https://github.com/rust-nostr/nostr#8c1e2a85c5476f9aa0848a39676632833bc8446d" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" dependencies = [ "async-trait", "nostr", @@ -3467,7 +3481,7 @@ dependencies = [ [[package]] name = "nwc" version = "0.35.0" -source = "git+https://github.com/rust-nostr/nostr#8c1e2a85c5476f9aa0848a39676632833bc8446d" +source = "git+https://github.com/rust-nostr/nostr#e99e56e712ac4f3830390619e52ccd11a2b41f9e" dependencies = [ "async-utility", "nostr", @@ -3981,26 +3995,6 @@ dependencies = [ "siphasher", ] -[[package]] -name = "pin-project" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.77", -] - [[package]] name = "pin-project-lite" version = "0.2.14" @@ -4026,9 +4020,9 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "plist" @@ -4382,9 +4376,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.4" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" +checksum = "62871f2d65009c0256aed1b9cfeeb8ac272833c404e13d53d400cd0dad7a2ac0" dependencies = [ "bitflags 2.6.0", ] @@ -4765,9 +4759,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.11.1" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" +checksum = "ea4a292869320c0272d7bc55a5a6aafaff59b4f63404a003887b679a2e05b4b6" dependencies = [ "core-foundation-sys", "libc", @@ -5322,9 +5316,9 @@ dependencies = [ [[package]] name = "tao" -version = "0.30.1" +version = "0.30.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82e7ede56f9ef03a0bb384c7b2bed4f3985ee7f3f79ec887c50d8466eec21096" +checksum = "06e48d7c56b3f7425d061886e8ce3b6acfab1993682ed70bef50fd133d721ee6" dependencies = [ "bitflags 2.6.0", "cocoa 0.26.0", @@ -5372,9 +5366,9 @@ dependencies = [ [[package]] name = "tar" -version = "0.4.41" +version = "0.4.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb797dad5fb5b76fcf519e702f4a589483b5ef06567f160c392832c1f5e44909" +checksum = "4ff6c40d3aedb5e06b57c6f669ad17ab063dd1e63d977c6a88e7f4dfa4f04020" dependencies = [ "filetime", "libc", @@ -5554,8 +5548,8 @@ dependencies = [ [[package]] name = "tauri-plugin-decorum" -version = "1.1.0" -source = "git+https://github.com/reyamir/tauri-plugin-decorum?branch=fix/tauri-latest#f2c3a59eb76e0496c8c28245ed354f882e8f5838" +version = "1.1.1" +source = "git+https://github.com/clearlysid/tauri-plugin-decorum.git#278e34378116ea80f7b6176ae85cf6aa26c631eb" dependencies = [ "anyhow", "cocoa 0.25.0", @@ -5951,18 +5945,18 @@ checksum = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c" [[package]] name = "thiserror" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", @@ -6198,27 +6192,6 @@ dependencies = [ "winnow", ] -[[package]] -name = "tower" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" -dependencies = [ - "futures-core", - "futures-util", - "pin-project", - "pin-project-lite", - "tokio", - "tower-layer", - "tower-service", -] - -[[package]] -name = "tower-layer" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" - [[package]] name = "tower-service" version = "0.3.3" @@ -6761,7 +6734,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]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index e66b2cf3..96bb4db8 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -29,11 +29,12 @@ tauri-plugin-shell = "2.0.0-rc" tauri-plugin-updater = "2.0.0-rc" tauri-plugin-upload = "2.0.0-rc" tauri-plugin-store = "2.0.0-rc" -tauri-plugin-decorum = { git = "https://github.com/reyamir/tauri-plugin-decorum", branch = "fix/tauri-latest" } +tauri-plugin-decorum = { git = "https://github.com/clearlysid/tauri-plugin-decorum.git" } tauri-plugin-prevent-default = "0.4" tauri-specta = { version = "2.0.0-rc.15", features = ["derive", "typescript"] } nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = ["lmdb"] } +nostr-relay-builder = { git = "https://github.com/rust-nostr/nostr" } specta = "^2.0.0-rc.20" specta-typescript = "0.0.7" diff --git a/src-tauri/src/commands/account.rs b/src-tauri/src/commands/account.rs index f678c426..39dc18e2 100644 --- a/src-tauri/src/commands/account.rs +++ b/src-tauri/src/commands/account.rs @@ -256,7 +256,7 @@ pub async fn login( }; // Connect to user's relay (NIP-65) - init_nip65(client).await; + init_nip65(client, &public_key).await; tauri::async_runtime::spawn(async move { let state = handle.state::(); diff --git a/src-tauri/src/common.rs b/src-tauri/src/common.rs index f5f19c54..ea85f9ab 100644 --- a/src-tauri/src/common.rs +++ b/src-tauri/src/common.rs @@ -252,26 +252,12 @@ pub fn create_event_tags(content: &str) -> Vec { tags } -pub async fn init_nip65(client: &Client) { - let signer = match client.signer().await { - Ok(signer) => signer, - Err(e) => { - eprintln!("Failed to get signer: {:?}", e); - return; - } - }; - let public_key = match signer.public_key().await { - Ok(public_key) => public_key, - Err(e) => { - eprintln!("Failed to get public key: {:?}", e); - return; - } - }; +pub async fn init_nip65(client: &Client, public_key: &str) { + let author = PublicKey::from_str(public_key).unwrap(); + let filter = Filter::new().author(author).kind(Kind::RelayList).limit(1); - let filter = Filter::new() - .author(public_key) - .kind(Kind::RelayList) - .limit(1); + // client.add_relay("ws://127.0.0.1:1984").await.unwrap(); + // client.connect_relay("ws://127.0.0.1:1984").await.unwrap(); if let Ok(events) = client .get_events_of( diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 5baf28dc..9dd02192 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -7,6 +7,7 @@ use border::WebviewWindowExt as BorderWebviewWindowExt; use commands::{account::*, event::*, metadata::*, relay::*, window::*}; use common::parse_event; +use nostr_relay_builder::prelude::*; use nostr_sdk::prelude::*; use serde::{Deserialize, Serialize}; use specta::Type; @@ -175,6 +176,19 @@ fn main() { let handle_clone_child = handle_clone.clone(); let main_window = app.get_webview_window("main").unwrap(); + let config_dir = handle + .path() + .app_config_dir() + .expect("Error: app config directory not found."); + + let data_dir = handle + .path() + .app_data_dir() + .expect("Error: app data directory not found."); + + let _ = fs::create_dir_all(&config_dir); + let _ = fs::create_dir_all(&data_dir); + // Set custom decoration for Windows #[cfg(target_os = "windows")] main_window.create_overlay_titlebar().unwrap(); @@ -198,15 +212,8 @@ fn main() { }); let client = tauri::async_runtime::block_on(async move { - // Create data folder if not exist - let dir = handle - .path() - .app_config_dir() - .expect("Error: app config directory not found."); - let _ = fs::create_dir_all(&dir); - // Setup database - let database = NostrLMDB::open(dir.join("nostr-lmdb")) + let database = NostrLMDB::open(config_dir.join("nostr-lmdb")) .expect("Error: cannot create database."); // Config @@ -286,27 +293,39 @@ fn main() { SubKind::Subscribe => { let subscription_id = SubscriptionId::new(payload.label); - let filter = if let Some(id) = payload.event_id { - let event_id = EventId::from_str(&id).unwrap(); + match payload.event_id { + Some(id) => { + let event_id = EventId::from_str(&id).unwrap(); + let filter = + Filter::new().event(event_id).since(Timestamp::now()); - Filter::new().event(event_id).since(Timestamp::now()) - } else { - let contact_list = state.contact_list.lock().await; - let authors: Vec = - contact_list.iter().map(|f| f.public_key).collect(); + if let Err(e) = client + .subscribe_with_id(subscription_id, vec![filter], None) + .await + { + println!("Subscription error: {}", e) + } + } + None => { + let contact_list = state.contact_list.lock().await; + if !contact_list.is_empty() { + let authors: Vec = + contact_list.iter().map(|f| f.public_key).collect(); - Filter::new() - .kinds(vec![Kind::TextNote, Kind::Repost]) - .authors(authors) - .since(Timestamp::now()) + let filter = Filter::new() + .kinds(vec![Kind::TextNote, Kind::Repost]) + .authors(authors) + .since(Timestamp::now()); + + if let Err(e) = client + .subscribe_with_id(subscription_id, vec![filter], None) + .await + { + println!("Subscription error: {}", e) + } + } + } }; - - if let Err(e) = client - .subscribe_with_id(subscription_id, vec![filter], None) - .await - { - println!("Subscription error: {}", e) - } } SubKind::Unsubscribe => { let subscription_id = SubscriptionId::new(payload.label); @@ -316,6 +335,22 @@ fn main() { }); }); + // Run local relay thread + tauri::async_runtime::spawn(async move { + let database = NostrLMDB::open(data_dir.join("local-relay")) + .expect("Error: cannot create database."); + let builder = RelayBuilder::default().database(database).port(1984); + + if let Ok(relay) = LocalRelay::run(builder).await { + println!("Running local relay: {}", relay.url()) + } + + loop { + tokio::time::sleep(Duration::from_secs(60)).await; + } + }); + + // Run notification thread tauri::async_runtime::spawn(async move { let state = handle_clone.state::(); let client = &state.client; diff --git a/src/routes/$account/_settings/general.lazy.tsx b/src/routes/$account/_settings/general.lazy.tsx index 7879df0b..b54c0824 100644 --- a/src/routes/$account/_settings/general.lazy.tsx +++ b/src/routes/$account/_settings/general.lazy.tsx @@ -128,11 +128,12 @@ function Screen() { -
+
+