From 703fe988bdeae9afb4896099cb5a2e548886026b Mon Sep 17 00:00:00 2001 From: reya Date: Fri, 12 Dec 2025 09:09:30 +0700 Subject: [PATCH] feat: add simple account state --- Cargo.lock | 120 ++++++++++++++++++++++++++++++-------- crates/account/Cargo.toml | 19 ++++++ crates/account/src/lib.rs | 96 ++++++++++++++++++++++++++++++ crates/lume/Cargo.toml | 1 + crates/lume/src/main.rs | 6 ++ 5 files changed, 218 insertions(+), 24 deletions(-) create mode 100644 crates/account/Cargo.toml create mode 100644 crates/account/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 5042f398..1a7a430a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,22 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "account" +version = "0.0.1" +dependencies = [ + "anyhow", + "common", + "gpui", + "log", + "nostr-sdk", + "serde", + "serde_json", + "smallvec", + "smol", + "state", +] + [[package]] name = "adler2" version = "2.0.1" @@ -250,9 +266,9 @@ dependencies = [ [[package]] name = "async-compression" -version = "0.4.35" +version = "0.4.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07a926debf178f2d355197f9caddb08e54a9329d44748034bba349c5848cb519" +checksum = "98ec5f6c2f8bc326c994cb9e241cc257ddaba9afa8555a43cffbb5dd86efaa37" dependencies = [ "compression-codecs", "compression-core", @@ -999,6 +1015,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", + "serde", "wasm-bindgen", "windows-link 0.2.1", ] @@ -1114,7 +1131,7 @@ dependencies = [ [[package]] name = "collections" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "indexmap", "rustc-hash 2.1.1", @@ -1166,9 +1183,9 @@ dependencies = [ [[package]] name = "compression-codecs" -version = "0.4.34" +version = "0.4.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34a3cbbb8b6eca96f3a5c4bf6938d5b27ced3675d69f95bb51948722870bc323" +checksum = "b0f7ac3e5b97fdce45e8922fb05cae2c37f7bbd63d30dd94821dacfd8f3f2bf2" dependencies = [ "compression-core", "deflate64", @@ -1503,7 +1520,7 @@ dependencies = [ [[package]] name = "derive_refineable" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "proc-macro2", "quote", @@ -2374,7 +2391,7 @@ dependencies = [ [[package]] name = "gpui" version = "0.2.2" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "anyhow", "as-raw-xcb-connection", @@ -2473,7 +2490,7 @@ dependencies = [ [[package]] name = "gpui-component" version = "0.5.0" -source = "git+https://github.com/longbridge/gpui-component#2d753268ef049eb019d06f5cd8f229e30080dde9" +source = "git+https://github.com/longbridge/gpui-component#67608353f227f4f40d8d150f33adc0c891be318b" dependencies = [ "aho-corasick", "anyhow", @@ -2511,7 +2528,7 @@ dependencies = [ [[package]] name = "gpui-component-macros" version = "0.5.0" -source = "git+https://github.com/longbridge/gpui-component#2d753268ef049eb019d06f5cd8f229e30080dde9" +source = "git+https://github.com/longbridge/gpui-component#67608353f227f4f40d8d150f33adc0c891be318b" dependencies = [ "proc-macro2", "quote", @@ -2533,7 +2550,7 @@ dependencies = [ [[package]] name = "gpui_macros" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -2544,7 +2561,7 @@ dependencies = [ [[package]] name = "gpui_tokio" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "anyhow", "gpui", @@ -2771,7 +2788,7 @@ dependencies = [ [[package]] name = "http_client" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "anyhow", "async-compression", @@ -2796,7 +2813,7 @@ dependencies = [ [[package]] name = "http_client_tls" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "rustls", "rustls-platform-verifier", @@ -2886,7 +2903,7 @@ dependencies = [ "tokio", "tower-service", "tracing", - "windows-registry 0.5.3", + "windows-registry 0.6.1", ] [[package]] @@ -2901,7 +2918,7 @@ dependencies = [ "js-sys", "log", "wasm-bindgen", - "windows-core 0.61.2", + "windows-core 0.62.2", ] [[package]] @@ -3482,6 +3499,7 @@ dependencies = [ name = "lume" version = "0.0.1" dependencies = [ + "account", "anyhow", "assets", "common", @@ -3638,7 +3656,7 @@ dependencies = [ [[package]] name = "media" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "anyhow", "bindgen", @@ -4496,7 +4514,7 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" [[package]] name = "perf" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "collections", "serde", @@ -5114,7 +5132,7 @@ dependencies = [ [[package]] name = "refineable" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "derive_refineable", ] @@ -5195,7 +5213,7 @@ dependencies = [ [[package]] name = "reqwest_client" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "anyhow", "bytes", @@ -6173,7 +6191,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sum_tree" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "arrayvec", "log", @@ -7129,7 +7147,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] name = "util" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "anyhow", "async-fs", @@ -7165,7 +7183,7 @@ dependencies = [ [[package]] name = "util_macros" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "perf", "quote", @@ -7666,6 +7684,19 @@ dependencies = [ "windows-strings 0.4.2", ] +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement 0.60.2", + "windows-interface 0.59.3", + "windows-link 0.2.1", + "windows-result 0.4.1", + "windows-strings 0.5.1", +] + [[package]] name = "windows-future" version = "0.2.1" @@ -7765,6 +7796,17 @@ dependencies = [ "windows-strings 0.4.2", ] +[[package]] +name = "windows-registry" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" +dependencies = [ + "windows-link 0.2.1", + "windows-result 0.4.1", + "windows-strings 0.5.1", +] + [[package]] name = "windows-result" version = "0.1.2" @@ -7783,6 +7825,15 @@ dependencies = [ "windows-link 0.1.3", ] +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link 0.2.1", +] + [[package]] name = "windows-strings" version = "0.3.1" @@ -7801,6 +7852,15 @@ dependencies = [ "windows-link 0.1.3", ] +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link 0.2.1", +] + [[package]] name = "windows-sys" version = "0.45.0" @@ -8603,20 +8663,32 @@ dependencies = [ "syn 2.0.111", ] +[[package]] +name = "zlog" +version = "0.1.0" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +dependencies = [ + "anyhow", + "chrono", + "collections", + "log", +] + [[package]] name = "ztracing" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" dependencies = [ "tracing", "tracing-subscriber", + "zlog", "ztracing_macro", ] [[package]] name = "ztracing_macro" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#37077a8ebb4c1d8f486e61a2a8bcf0b472af1ab2" +source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" [[package]] name = "zune-core" diff --git a/crates/account/Cargo.toml b/crates/account/Cargo.toml new file mode 100644 index 00000000..74015f4a --- /dev/null +++ b/crates/account/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "account" +version.workspace = true +edition.workspace = true +publish.workspace = true + +[dependencies] +common = { path = "../common" } +state = { path = "../state" } + +gpui.workspace = true +nostr-sdk.workspace = true + +anyhow.workspace = true +smallvec.workspace = true +smol.workspace = true +log.workspace = true +serde.workspace = true +serde_json.workspace = true diff --git a/crates/account/src/lib.rs b/crates/account/src/lib.rs new file mode 100644 index 00000000..6fa94b9f --- /dev/null +++ b/crates/account/src/lib.rs @@ -0,0 +1,96 @@ +use std::env; + +use gpui::{App, AppContext, Context, Entity, Global, Task}; +use nostr_sdk::prelude::*; +use smallvec::{smallvec, SmallVec}; +use state::NostrRegistry; + +pub fn init(cx: &mut App) { + Account::set_global(cx.new(Account::new), cx); +} + +struct GlobalAccount(Entity); + +impl Global for GlobalAccount {} + +pub struct Account { + /// The public key of the account + public_key: Option, + + /// Tasks for asynchronous operations + _tasks: SmallVec<[Task<()>; 1]>, +} + +impl Account { + /// Retrieve the global account state + pub fn global(cx: &App) -> Entity { + cx.global::().0.clone() + } + + /// Check if the global account state exists + pub fn has_global(cx: &App) -> bool { + cx.has_global::() + } + + /// Remove the global account state + pub fn remove_global(cx: &mut App) { + cx.remove_global::(); + } + + /// Set the global account instance + fn set_global(state: Entity, cx: &mut App) { + cx.set_global(GlobalAccount(state)); + } + + /// Create a new account instance + fn new(cx: &mut Context) -> Self { + let nostr = NostrRegistry::global(cx); + let client = nostr.read(cx).client(); + + // Collect command line arguments + let args: Vec = env::args().collect(); + let account = args.get(1).and_then(|s| Keys::parse(s).ok()); + + let mut tasks = smallvec![]; + + if let Some(keys) = account { + tasks.push( + // Background + cx.spawn(async move |this, cx| { + let public_key = keys.public_key(); + + // Set the signer + cx.background_executor() + .await_on_background(async move { + client.set_signer(keys).await; + log::info!("Signer is set"); + }) + .await; + + // Update state + this.update(cx, |this, cx| { + this.public_key = Some(public_key); + cx.notify(); + }) + .ok(); + }), + ); + } + + Self { + public_key: None, + _tasks: tasks, + } + } + + /// Check if the account entity has a public key + pub fn has_account(&self) -> bool { + self.public_key.is_some() + } + + /// Get the public key of the account + pub fn public_key(&self) -> PublicKey { + // This method is only called when user is logged in, so unwrap safely + self.public_key.unwrap() + } +} diff --git a/crates/lume/Cargo.toml b/crates/lume/Cargo.toml index 9b3d5db1..5590392a 100644 --- a/crates/lume/Cargo.toml +++ b/crates/lume/Cargo.toml @@ -12,6 +12,7 @@ path = "src/main.rs" common = { path = "../common" } assets = { path = "../assets" } state = { path = "../state" } +account = { path = "../account" } gpui.workspace = true gpui-component.workspace = true diff --git a/crates/lume/src/main.rs b/crates/lume/src/main.rs index 93e3d622..79f7d9df 100644 --- a/crates/lume/src/main.rs +++ b/crates/lume/src/main.rs @@ -81,6 +81,12 @@ fn main() { // Initialize themes themes::init(cx); + // Initialize app state + state::init(cx); + + // Initialize account + account::init(cx); + let workspace = cx.new(|cx| Workspace::new(window, cx)); cx.new(|cx| Root::new(workspace, window, cx)) })