feat: add commands

This commit is contained in:
reya
2024-07-23 15:11:17 +07:00
parent 7cd5f06122
commit 9b1edf7f62
14 changed files with 850 additions and 153 deletions

312
src-tauri/Cargo.lock generated
View File

@@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "Inflector"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3"
[[package]]
name = "addr2line"
version = "0.22.0"
@@ -101,6 +107,41 @@ version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "arboard"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fb4009533e8ff8f1450a5bcbc30f4242a1d34442221f72314bea1f5dc9c7f89"
dependencies = [
"clipboard-win",
"core-graphics",
"image",
"log",
"objc2",
"objc2-app-kit",
"objc2-foundation",
"parking_lot",
"windows-sys 0.48.0",
"x11rb",
]
[[package]]
name = "ashpd"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd884d7c72877a94102c3715f3b1cd09ff4fac28221add3e57cfbe25c236d093"
dependencies = [
"enumflags2",
"futures-channel",
"futures-util",
"rand 0.8.5",
"serde",
"serde_repr",
"tokio",
"url",
"zbus",
]
[[package]]
name = "async-broadcast"
version = "0.7.1"
@@ -534,6 +575,12 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "byteorder-lite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
[[package]]
name = "bytes"
version = "1.6.1"
@@ -712,6 +759,15 @@ dependencies = [
"zeroize",
]
[[package]]
name = "clipboard-win"
version = "5.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15efe7a882b08f34e38556b14f2fb3daa98769d06c7f0c1b076dfd0d983bc892"
dependencies = [
"error-code",
]
[[package]]
name = "cocoa"
version = "0.25.0"
@@ -787,10 +843,15 @@ dependencies = [
"nostr-sdk",
"serde",
"serde_json",
"specta",
"tauri",
"tauri-build",
"tauri-plugin-clipboard-manager",
"tauri-plugin-decorum",
"tauri-plugin-dialog",
"tauri-plugin-os",
"tauri-plugin-shell",
"tauri-specta",
]
[[package]]
@@ -1187,6 +1248,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "error-code"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0474425d51df81997e2f90a21591180b38eccf27292d755f3e30750225c175b"
[[package]]
name = "event-listener"
version = "5.3.1"
@@ -1550,6 +1617,16 @@ dependencies = [
"version_check",
]
[[package]]
name = "gethostname"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818"
dependencies = [
"libc",
"windows-targets 0.48.5",
]
[[package]]
name = "getrandom"
version = "0.1.16"
@@ -1991,6 +2068,19 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "image"
version = "0.25.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99314c8a2152b8ddb211f924cdae532d8c5e4c8bb54728e12fff1b0cd5963a10"
dependencies = [
"bytemuck",
"byteorder-lite",
"num-traits",
"png",
"tiff",
]
[[package]]
name = "indexmap"
version = "1.9.3"
@@ -2013,6 +2103,12 @@ dependencies = [
"serde",
]
[[package]]
name = "indoc"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5"
[[package]]
name = "infer"
version = "0.15.0"
@@ -2135,6 +2231,12 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130"
[[package]]
name = "jpeg-decoder"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0"
[[package]]
name = "js-sys"
version = "0.3.69"
@@ -2948,6 +3050,17 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "os_info"
version = "3.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae99c7fa6dd38c7cafe1ec085e804f8f555a2f8659b0dbe03f1f9963a9b51092"
dependencies = [
"log",
"serde",
"windows-sys 0.52.0",
]
[[package]]
name = "os_pipe"
version = "1.2.0"
@@ -3029,6 +3142,12 @@ dependencies = [
"subtle",
]
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pathdiff"
version = "0.2.1"
@@ -3639,6 +3758,30 @@ dependencies = [
"winreg",
]
[[package]]
name = "rfd"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a73a7337fc24366edfca76ec521f51877b114e42dab584008209cca6719251"
dependencies = [
"ashpd",
"block",
"dispatch",
"glib-sys",
"gobject-sys",
"gtk-sys",
"js-sys",
"log",
"objc",
"objc-foundation",
"objc_id",
"raw-window-handle 0.6.2",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"windows-sys 0.48.0",
]
[[package]]
name = "ring"
version = "0.17.8"
@@ -4205,6 +4348,31 @@ dependencies = [
"system-deps",
]
[[package]]
name = "specta"
version = "2.0.0-rc.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3624a07cbde326fdf1ec37cbd39d06a224660fa0199b7db7316f2349583df981"
dependencies = [
"once_cell",
"paste",
"serde",
"specta-macros",
"thiserror",
]
[[package]]
name = "specta-macros"
version = "2.0.0-rc.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef33e9678ae36993fcbfc46aa29568ef10d32fd54428808759c6a450998c43ec"
dependencies = [
"Inflector",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "spin"
version = "0.9.8"
@@ -4309,6 +4477,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
[[package]]
name = "sys-locale"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e801cf239ecd6ccd71f03d270d67dd53d13e90aab208bf4b8fe4ad957ea949b0"
dependencies = [
"libc",
]
[[package]]
name = "system-deps"
version = "6.2.2"
@@ -4410,6 +4587,7 @@ dependencies = [
"serde_json",
"serde_repr",
"serialize-to-javascript",
"specta",
"state",
"swift-rs",
"tauri-build",
@@ -4508,6 +4686,21 @@ dependencies = [
"walkdir",
]
[[package]]
name = "tauri-plugin-clipboard-manager"
version = "2.0.0-beta.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8cf4b7fde295126b30b8279aa2addedda7689027a6a7fc4cdf9bea43a86ad84"
dependencies = [
"arboard",
"log",
"serde",
"serde_json",
"tauri",
"tauri-plugin",
"thiserror",
]
[[package]]
name = "tauri-plugin-decorum"
version = "0.1.5"
@@ -4524,6 +4717,61 @@ dependencies = [
"tauri-plugin",
]
[[package]]
name = "tauri-plugin-dialog"
version = "2.0.0-beta.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8860dd73c96969eb14813f9f04d8665f2853342670456fb6619d637137ef0d09"
dependencies = [
"dunce",
"log",
"raw-window-handle 0.6.2",
"rfd",
"serde",
"serde_json",
"tauri",
"tauri-plugin",
"tauri-plugin-fs",
"thiserror",
]
[[package]]
name = "tauri-plugin-fs"
version = "2.0.0-beta.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "461853268fe115ca19ee21e5986d505944f0b826048fe1bd726d74753fdf1df6"
dependencies = [
"anyhow",
"glob",
"schemars",
"serde",
"serde_json",
"serde_repr",
"tauri",
"tauri-plugin",
"thiserror",
"url",
"uuid",
]
[[package]]
name = "tauri-plugin-os"
version = "2.0.0-beta.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79a0466f11f45fd3f640a17b5ba5e34c62912c9b391141c818155125ae9f0917"
dependencies = [
"gethostname",
"log",
"os_info",
"serde",
"serde_json",
"serialize-to-javascript",
"sys-locale",
"tauri",
"tauri-plugin",
"thiserror",
]
[[package]]
name = "tauri-plugin-shell"
version = "2.0.0-beta.9"
@@ -4588,6 +4836,32 @@ dependencies = [
"wry",
]
[[package]]
name = "tauri-specta"
version = "2.0.0-rc.11"
source = "git+https://github.com/reyamir/tauri-specta?branch=feat/tauri-v2#5c09319b345814bfce3c4c02527e481d18339051"
dependencies = [
"heck 0.5.0",
"indoc",
"serde",
"serde_json",
"specta",
"tauri",
"tauri-specta-macros",
"thiserror",
]
[[package]]
name = "tauri-specta-macros"
version = "2.0.0-rc.5"
source = "git+https://github.com/reyamir/tauri-specta?branch=feat/tauri-v2#5c09319b345814bfce3c4c02527e481d18339051"
dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
"syn 2.0.72",
]
[[package]]
name = "tauri-utils"
version = "2.0.0-beta.19"
@@ -4692,6 +4966,17 @@ dependencies = [
"once_cell",
]
[[package]]
name = "tiff"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba1310fcea54c6a9a4fd1aad794ecc02c31682f6bfbecdf460bf19533eed1e3e"
dependencies = [
"flate2",
"jpeg-decoder",
"weezl",
]
[[package]]
name = "time"
version = "0.3.36"
@@ -4750,8 +5035,10 @@ dependencies = [
"mio",
"num_cpus",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"tracing",
"windows-sys 0.48.0",
]
@@ -5428,6 +5715,12 @@ dependencies = [
"windows-core 0.57.0",
]
[[package]]
name = "weezl"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "winapi"
version = "0.3.9"
@@ -5858,6 +6151,23 @@ dependencies = [
"pkg-config",
]
[[package]]
name = "x11rb"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d91ffca73ee7f68ce055750bf9f6eca0780b8c85eff9bc046a3b0da41755e12"
dependencies = [
"gethostname",
"rustix",
"x11rb-protocol",
]
[[package]]
name = "x11rb-protocol"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec107c4503ea0b4a98ef47356329af139c0a4f7750e621cf2973cd3385ebcb3d"
[[package]]
name = "xdg-home"
version = "1.2.0"
@@ -5898,6 +6208,7 @@ dependencies = [
"serde_repr",
"sha1",
"static_assertions",
"tokio",
"tracing",
"uds_windows",
"windows-sys 0.52.0",
@@ -5968,6 +6279,7 @@ dependencies = [
"enumflags2",
"serde",
"static_assertions",
"url",
"zvariant_derive",
]

View File

@@ -17,6 +17,12 @@ tauri = { version = "2.0.0-beta", features = [
"macos-private-api",
"protocol-asset",
] }
tauri-specta = { git = "https://github.com/reyamir/tauri-specta", branch = "feat/tauri-v2", features = [
"typescript",
] }
tauri-plugin-os = "2.0.0-beta"
tauri-plugin-clipboard-manager = "2.0.0-beta"
tauri-plugin-dialog = "2.0.0-beta"
tauri-plugin-shell = "2.0.0-beta"
tauri-plugin-decorum = "0.1.5"
serde = { version = "1", features = ["derive"] }
@@ -28,6 +34,7 @@ keyring = { version = "3", features = [
] }
keyring-search = "1.2.0"
itertools = "0.13.0"
specta = "^2.0.0-rc.12"
[target.'cfg(target_os = "macos")'.dependencies]
border = { git = "https://github.com/ahkohd/tauri-toolkit", branch = "v2" }

View File

@@ -3,11 +3,12 @@ use keyring::Entry;
use keyring_search::{Limit, List, Search};
use nostr_sdk::prelude::*;
use std::{collections::HashSet, str::FromStr};
use tauri::State;
use tauri::{Manager, State};
use crate::Nostr;
#[tauri::command]
#[specta::specta]
pub fn get_accounts() -> Vec<String> {
let search = Search::new().expect("Unexpected.");
let results = search.by_user("nostr_secret");
@@ -19,6 +20,7 @@ pub fn get_accounts() -> Vec<String> {
}
#[tauri::command]
#[specta::specta]
pub async fn get_profile(id: String, state: State<'_, Nostr>) -> Result<String, ()> {
let client = &state.client;
let public_key = PublicKey::from_str(&id).unwrap();
@@ -34,7 +36,12 @@ pub async fn get_profile(id: String, state: State<'_, Nostr>) -> Result<String,
}
#[tauri::command]
pub async fn login(id: String, state: State<'_, Nostr>) -> Result<(), String> {
#[specta::specta]
pub async fn login(
id: String,
state: State<'_, Nostr>,
handle: tauri::AppHandle,
) -> Result<(), String> {
let client = &state.client;
let keyring = Entry::new(&id, "nostr_secret").expect("Unexpected.");
@@ -50,7 +57,6 @@ pub async fn login(id: String, state: State<'_, Nostr>) -> Result<(), String> {
client.set_signer(Some(signer)).await;
let public_key = PublicKey::from_str(&id).unwrap();
let incoming = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1);
if let Ok(events) = client.get_events_of(vec![inbox], None).await {
@@ -67,34 +73,43 @@ pub async fn login(id: String, state: State<'_, Nostr>) -> Result<(), String> {
}
}
if let Ok(report) = client.reconcile(incoming.clone(), NegentropyOptions::default()).await {
let receives = report.received.clone();
let ids = receives.into_iter().collect::<Vec<_>>();
tauri::async_runtime::spawn(async move {
let window = handle.get_webview_window("main").unwrap();
let state = window.state::<Nostr>();
let client = &state.client;
if let Ok(events) = client.database().query(vec![Filter::new().ids(ids)], Order::Desc).await
{
let pubkeys = events
.into_iter()
.unique_by(|ev| ev.pubkey)
.map(|ev| ev.pubkey)
.collect::<Vec<_>>();
let incoming = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
if client
.reconcile(
Filter::new().kind(Kind::GiftWrap).pubkeys(pubkeys),
NegentropyOptions::default(),
)
.await
.is_ok()
if let Ok(report) = client.reconcile(incoming.clone(), NegentropyOptions::default()).await {
let receives = report.received.clone();
let ids = receives.into_iter().collect::<Vec<_>>();
if let Ok(events) =
client.database().query(vec![Filter::new().ids(ids)], Order::Desc).await
{
println!("Sync done.")
let pubkeys = events
.into_iter()
.unique_by(|ev| ev.pubkey)
.map(|ev| ev.pubkey)
.collect::<Vec<_>>();
if client
.reconcile(
Filter::new().kind(Kind::GiftWrap).pubkeys(pubkeys),
NegentropyOptions::default(),
)
.await
.is_ok()
{
println!("Sync done.")
}
}
}
}
if client.subscribe(vec![incoming.limit(0)], None).await.is_ok() {
println!("Waiting for new message...")
}
if client.subscribe(vec![incoming.limit(0)], None).await.is_ok() {
println!("Waiting for new message...")
}
});
Ok(())
}

View File

@@ -19,6 +19,20 @@ pub struct Nostr {
}
fn main() {
let mut ctx = tauri::generate_context!();
let invoke_handler = {
let builder = tauri_specta::ts::builder().commands(tauri_specta::collect_commands![
get_accounts,
login,
get_profile
]);
#[cfg(debug_assertions)]
let builder = builder.path("../src/commands.ts");
builder.build().unwrap()
};
tauri::Builder::default()
.setup(|app| {
#[cfg(not(target_os = "linux"))]
@@ -58,9 +72,13 @@ fn main() {
Ok(())
})
.enable_macos_default_menu(false)
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_decorum::init())
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![login, get_accounts, get_profile])
.run(tauri::generate_context!())
.invoke_handler(invoke_handler)
.run(ctx)
.expect("error while running tauri application");
}