feat: revamp the chat panel ui #7

Merged
reya merged 9 commits from revamp-chat-ui into master 2026-02-19 07:25:08 +00:00
9 changed files with 184 additions and 166 deletions
Showing only changes of commit 2c33670ba5 - Show all commits

89
Cargo.lock generated
View File

@@ -286,9 +286,9 @@ dependencies = [
[[package]] [[package]]
name = "async-compression" name = "async-compression"
version = "0.4.39" version = "0.4.40"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68650b7df54f0293fd061972a0fb05aaf4fc0879d3b3d21a638a182c5c543b9f" checksum = "7d67d43201f4d20c78bcda740c142ca52482d81da80681533d33bf3f0596c8e2"
dependencies = [ dependencies = [
"compression-codecs", "compression-codecs",
"compression-core", "compression-core",
@@ -605,9 +605,9 @@ dependencies = [
[[package]] [[package]]
name = "aws-lc-rs" name = "aws-lc-rs"
version = "1.15.4" version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b7b6141e96a8c160799cc2d5adecd5cbbe5054cb8c7c4af53da0f83bb7ad256" checksum = "d9a7b350e3bb1767102698302bc37256cbd48422809984b98d292c40e2579aa9"
dependencies = [ dependencies = [
"aws-lc-sys", "aws-lc-sys",
"zeroize", "zeroize",
@@ -812,9 +812,9 @@ checksum = "f4ad8f11f288f48ca24471bbd51ac257aaeaaa07adae295591266b792902ae64"
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.19.1" version = "3.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" checksum = "5c6f81257d10a0f602a294ae4182251151ff97dbb504ef9afcdda4a64b24d9b4"
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
@@ -1193,7 +1193,7 @@ dependencies = [
[[package]] [[package]]
name = "collections" name = "collections"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
@@ -1252,9 +1252,9 @@ dependencies = [
[[package]] [[package]]
name = "compression-codecs" name = "compression-codecs"
version = "0.4.36" version = "0.4.37"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00828ba6fd27b45a448e57dbfe84f1029d4c9f26b368157e9a448a5f49a2ec2a" checksum = "eb7b51a7d9c967fc26773061ba86150f19c50c0d65c887cb1fbe295fd16619b7"
dependencies = [ dependencies = [
"compression-core", "compression-core",
"deflate64", "deflate64",
@@ -1497,9 +1497,9 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-text" name = "cosmic-text"
version = "0.17.1" version = "0.17.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c5c9868e64aa6c5410629a83450e142c80e721c727a5bc0fb18107af6c2d66b" checksum = "5d8c4e3a1d02f5269ed15c2d70b4647167856f66f228dcdf99050ab77bbb5a56"
dependencies = [ dependencies = [
"bitflags 2.11.0", "bitflags 2.11.0",
"fontdb", "fontdb",
@@ -1638,7 +1638,7 @@ dependencies = [
[[package]] [[package]]
name = "derive_refineable" name = "derive_refineable"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -2588,7 +2588,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui" name = "gpui"
version = "0.2.2" version = "0.2.2"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"as-raw-xcb-connection", "as-raw-xcb-connection",
@@ -2688,7 +2688,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui_macros" name = "gpui_macros"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"heck 0.5.0", "heck 0.5.0",
"proc-macro2", "proc-macro2",
@@ -2699,7 +2699,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui_tokio" name = "gpui_tokio"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"gpui", "gpui",
@@ -2928,7 +2928,7 @@ dependencies = [
[[package]] [[package]]
name = "http_client" name = "http_client"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-compression", "async-compression",
@@ -2953,7 +2953,7 @@ dependencies = [
[[package]] [[package]]
name = "http_client_tls" name = "http_client_tls"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"rustls", "rustls",
"rustls-platform-verifier", "rustls-platform-verifier",
@@ -3711,6 +3711,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "matchers"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
dependencies = [
"regex-automata",
]
[[package]] [[package]]
name = "maybe-rayon" name = "maybe-rayon"
version = "0.1.1" version = "0.1.1"
@@ -3734,7 +3743,7 @@ dependencies = [
[[package]] [[package]]
name = "media" name = "media"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bindgen", "bindgen",
@@ -3899,9 +3908,9 @@ dependencies = [
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.16" version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5d26952a508f321b4d3d2e80e78fc2603eaefcdf0c30783867f19586518bdc" checksum = "465500e14ea162429d264d44189adc38b199b62b1c21eea9f69e4b73cb03bbf2"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
@@ -3993,7 +4002,7 @@ checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8"
[[package]] [[package]]
name = "nostr" name = "nostr"
version = "0.44.1" version = "0.44.1"
source = "git+https://github.com/rust-nostr/nostr#07de813024f930803a1dd81942edb50eb8a87ba3" source = "git+https://github.com/rust-nostr/nostr#bd92fd901e8b64856ad4f8373fbb87376314161c"
dependencies = [ dependencies = [
"aes", "aes",
"base64", "base64",
@@ -4018,7 +4027,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-connect" name = "nostr-connect"
version = "0.44.0" version = "0.44.0"
source = "git+https://github.com/rust-nostr/nostr#07de813024f930803a1dd81942edb50eb8a87ba3" source = "git+https://github.com/rust-nostr/nostr#bd92fd901e8b64856ad4f8373fbb87376314161c"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"futures-core", "futures-core",
@@ -4031,7 +4040,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-database" name = "nostr-database"
version = "0.44.0" version = "0.44.0"
source = "git+https://github.com/rust-nostr/nostr#07de813024f930803a1dd81942edb50eb8a87ba3" source = "git+https://github.com/rust-nostr/nostr#bd92fd901e8b64856ad4f8373fbb87376314161c"
dependencies = [ dependencies = [
"btreecap", "btreecap",
"flatbuffers", "flatbuffers",
@@ -4043,7 +4052,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-gossip" name = "nostr-gossip"
version = "0.44.0" version = "0.44.0"
source = "git+https://github.com/rust-nostr/nostr#07de813024f930803a1dd81942edb50eb8a87ba3" source = "git+https://github.com/rust-nostr/nostr#bd92fd901e8b64856ad4f8373fbb87376314161c"
dependencies = [ dependencies = [
"nostr", "nostr",
] ]
@@ -4051,7 +4060,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-gossip-memory" name = "nostr-gossip-memory"
version = "0.44.0" version = "0.44.0"
source = "git+https://github.com/rust-nostr/nostr#07de813024f930803a1dd81942edb50eb8a87ba3" source = "git+https://github.com/rust-nostr/nostr#bd92fd901e8b64856ad4f8373fbb87376314161c"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"lru", "lru",
@@ -4063,7 +4072,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-lmdb" name = "nostr-lmdb"
version = "0.44.0" version = "0.44.0"
source = "git+https://github.com/rust-nostr/nostr#07de813024f930803a1dd81942edb50eb8a87ba3" source = "git+https://github.com/rust-nostr/nostr#bd92fd901e8b64856ad4f8373fbb87376314161c"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"flume", "flume",
@@ -4077,7 +4086,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-sdk" name = "nostr-sdk"
version = "0.44.1" version = "0.44.1"
source = "git+https://github.com/rust-nostr/nostr#07de813024f930803a1dd81942edb50eb8a87ba3" source = "git+https://github.com/rust-nostr/nostr#bd92fd901e8b64856ad4f8373fbb87376314161c"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"async-wsocket", "async-wsocket",
@@ -4551,7 +4560,7 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]] [[package]]
name = "perf" name = "perf"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"collections", "collections",
"serde", "serde",
@@ -5227,7 +5236,7 @@ dependencies = [
[[package]] [[package]]
name = "refineable" name = "refineable"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"derive_refineable", "derive_refineable",
] ]
@@ -5332,7 +5341,7 @@ dependencies = [
[[package]] [[package]]
name = "reqwest_client" name = "reqwest_client"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bytes", "bytes",
@@ -5387,7 +5396,7 @@ dependencies = [
[[package]] [[package]]
name = "rope" name = "rope"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"log", "log",
@@ -5649,7 +5658,7 @@ dependencies = [
[[package]] [[package]]
name = "scheduler" name = "scheduler"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"async-task", "async-task",
"backtrace", "backtrace",
@@ -6230,7 +6239,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]] [[package]]
name = "sum_tree" name = "sum_tree"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"log", "log",
@@ -6940,10 +6949,14 @@ version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e"
dependencies = [ dependencies = [
"matchers",
"nu-ansi-term", "nu-ansi-term",
"once_cell",
"regex-automata",
"sharded-slab", "sharded-slab",
"smallvec", "smallvec",
"thread_local", "thread_local",
"tracing",
"tracing-core", "tracing-core",
"tracing-log", "tracing-log",
] ]
@@ -7186,7 +7199,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "util" name = "util"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-fs", "async-fs",
@@ -7224,7 +7237,7 @@ dependencies = [
[[package]] [[package]]
name = "util_macros" name = "util_macros"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"perf", "perf",
"quote", "quote",
@@ -9032,7 +9045,7 @@ dependencies = [
[[package]] [[package]]
name = "zlog" name = "zlog"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",
@@ -9049,7 +9062,7 @@ checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
[[package]] [[package]]
name = "ztracing" name = "ztracing"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
dependencies = [ dependencies = [
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@@ -9060,7 +9073,7 @@ dependencies = [
[[package]] [[package]]
name = "ztracing_macro" name = "ztracing_macro"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#deb26f8c5f89341206a1c5da7c79f6c3654f5fc5" source = "git+https://github.com/zed-industries/zed#f07cec59def58dd8199bdced27b1aafbeb5755d8"
[[package]] [[package]]
name = "zune-core" name = "zune-core"

View File

@@ -10,11 +10,11 @@ use common::EventUtils;
use fuzzy_matcher::skim::SkimMatcherV2; use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher; use fuzzy_matcher::FuzzyMatcher;
use gpui::{ use gpui::{
App, AppContext, Context, Entity, EventEmitter, Global, Subscription, Task, WeakEntity, App, AppContext, Context, Entity, EventEmitter, Global, Subscription, Task, WeakEntity, Window,
}; };
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::{NostrRegistry, RelayState, DEVICE_GIFTWRAP, USER_GIFTWRAP}; use state::{NostrRegistry, DEVICE_GIFTWRAP, USER_GIFTWRAP};
mod message; mod message;
mod room; mod room;
@@ -22,8 +22,8 @@ mod room;
pub use message::*; pub use message::*;
pub use room::*; pub use room::*;
pub fn init(cx: &mut App) { pub fn init(window: &mut Window, cx: &mut App) {
ChatRegistry::set_global(cx.new(ChatRegistry::new), cx); ChatRegistry::set_global(cx.new(|cx| ChatRegistry::new(window, cx)), cx);
} }
struct GlobalChatRegistry(Entity<ChatRegistry>); struct GlobalChatRegistry(Entity<ChatRegistry>);
@@ -59,11 +59,8 @@ pub struct ChatRegistry {
/// Tracking the status of unwrapping gift wrap events. /// Tracking the status of unwrapping gift wrap events.
tracking_flag: Arc<AtomicBool>, tracking_flag: Arc<AtomicBool>,
/// Handle tracking asynchronous task /// Async tasks
tracking_task: Option<Task<Result<(), Error>>>, tasks: SmallVec<[Task<Result<(), Error>>; 2]>,
/// Handle notification asynchronous task
notification_task: Option<Task<()>>,
/// Subscriptions /// Subscriptions
_subscriptions: SmallVec<[Subscription; 1]>, _subscriptions: SmallVec<[Subscription; 1]>,
@@ -83,31 +80,28 @@ impl ChatRegistry {
} }
/// Create a new chat registry instance /// Create a new chat registry instance
fn new(cx: &mut Context<Self>) -> Self { fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let nip17 = nostr.read(cx).nip17_state(); let nip17 = nostr.read(cx).nip17_state();
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
subscriptions.push( subscriptions.push(
// Observe the identity // Observe the nip17 state and load chat rooms on every state change
cx.observe(&nip17, |this, state, cx| { cx.observe(&nip17, |this, _state, cx| {
if state.read(cx) == &RelayState::Configured {
// Handle nostr notifications
this.handle_notifications(cx);
// Track unwrapping progress
this.tracking(cx);
}
// Get chat rooms from the database on every state changes
this.get_rooms(cx); this.get_rooms(cx);
}), }),
); );
cx.defer_in(window, |this, _window, cx| {
this.handle_notifications(cx);
this.tracking(cx);
});
Self { Self {
rooms: vec![], rooms: vec![],
tracking_flag: Arc::new(AtomicBool::new(false)), tracking_flag: Arc::new(AtomicBool::new(false)),
tracking_task: None, tasks: smallvec![],
notification_task: None,
_subscriptions: subscriptions, _subscriptions: subscriptions,
} }
} }
@@ -126,7 +120,7 @@ impl ChatRegistry {
// Channel for communication between nostr and gpui // Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<Signal>(1024); let (tx, rx) = flume::bounded::<Signal>(1024);
cx.background_spawn(async move { self.tasks.push(cx.background_spawn(async move {
let device_signer = signer.get_encryption_signer().await; let device_signer = signer.get_encryption_signer().await;
let mut notifications = client.notifications(); let mut notifications = client.notifications();
let mut processed_events = HashSet::new(); let mut processed_events = HashSet::new();
@@ -149,7 +143,7 @@ impl ChatRegistry {
continue; continue;
} }
log::info!("Received gift-wrap event: {event:?}"); log::info!("Received gift wrap event: {:?}", event);
// Extract the rumor from the gift wrap event // Extract the rumor from the gift wrap event
match Self::extract_rumor(&client, &device_signer, event.as_ref()).await { match Self::extract_rumor(&client, &device_signer, event.as_ref()).await {
@@ -158,7 +152,7 @@ impl ChatRegistry {
let new_message = NewMessage::new(event.id, rumor); let new_message = NewMessage::new(event.id, rumor);
let signal = Signal::Message(new_message); let signal = Signal::Message(new_message);
tx.send_async(signal).await.ok(); tx.send_async(signal).await?;
} }
false => { false => {
status.store(true, Ordering::Release); status.store(true, Ordering::Release);
@@ -171,32 +165,33 @@ impl ChatRegistry {
} }
RelayMessage::EndOfStoredEvents(id) => { RelayMessage::EndOfStoredEvents(id) => {
if id.as_ref() == &sub_id1 || id.as_ref() == &sub_id2 { if id.as_ref() == &sub_id1 || id.as_ref() == &sub_id2 {
tx.send_async(Signal::Eose).await.ok(); tx.send_async(Signal::Eose).await?;
} }
} }
_ => {} _ => {}
} }
} }
})
.detach();
self.notification_task = Some(cx.spawn(async move |this, cx| { Ok(())
}));
self.tasks.push(cx.spawn(async move |this, cx| {
while let Ok(message) = rx.recv_async().await { while let Ok(message) = rx.recv_async().await {
match message { match message {
Signal::Message(message) => { Signal::Message(message) => {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.new_message(message, cx); this.new_message(message, cx);
}) })?;
.ok();
} }
Signal::Eose => { Signal::Eose => {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.get_rooms(cx); this.get_rooms(cx);
}) })?;
.ok();
} }
}; };
} }
Ok(())
})); }));
} }
@@ -204,7 +199,7 @@ impl ChatRegistry {
fn tracking(&mut self, cx: &mut Context<Self>) { fn tracking(&mut self, cx: &mut Context<Self>) {
let status = self.tracking_flag.clone(); let status = self.tracking_flag.clone();
self.tracking_task = Some(cx.background_spawn(async move { self.tasks.push(cx.background_spawn(async move {
let loop_duration = Duration::from_secs(10); let loop_duration = Duration::from_secs(10);
loop { loop {

View File

@@ -60,4 +60,4 @@ futures.workspace = true
oneshot.workspace = true oneshot.workspace = true
indexset = "0.12.3" indexset = "0.12.3"
tracing-subscriber = { version = "0.3.18", features = ["fmt"] } tracing-subscriber = { version = "0.3.18", features = ["fmt", "env-filter"] }

View File

@@ -82,7 +82,7 @@ fn main() {
settings::init(cx); settings::init(cx);
// Initialize the nostr client // Initialize the nostr client
state::init(cx); state::init(window, cx);
// Initialize relay auth registry // Initialize relay auth registry
relay_auth::init(window, cx); relay_auth::init(window, cx);
@@ -96,7 +96,7 @@ fn main() {
device::init(window, cx); device::init(window, cx);
// Initialize app registry // Initialize app registry
chat::init(cx); chat::init(window, cx);
// Initialize auto update // Initialize auto update
auto_update::init(cx); auto_update::init(cx);

View File

@@ -67,7 +67,7 @@ impl DeviceRegistry {
RelayState::Idle => { RelayState::Idle => {
this.reset(cx); this.reset(cx);
} }
RelayState::Configured => { RelayState::Configured(_) => {
this.get_announcement(cx); this.get_announcement(cx);
} }
_ => {} _ => {}

View File

@@ -1,14 +1,14 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::Cell; use std::cell::Cell;
use std::collections::HashSet; use std::collections::HashSet;
use std::hash::{Hash, Hasher}; use std::hash::Hash;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use anyhow::{anyhow, Context as AnyhowContext, Error}; use anyhow::{anyhow, Context as AnyhowContext, Error};
use gpui::{ use gpui::{
App, AppContext, Context, Entity, Global, IntoElement, ParentElement, SharedString, Styled, App, AppContext, Context, Entity, Global, IntoElement, ParentElement, SharedString, Styled,
Subscription, Task, Window, Task, Window,
}; };
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use settings::{AppSettings, AuthMode}; use settings::{AppSettings, AuthMode};
@@ -27,18 +27,12 @@ pub fn init(window: &mut Window, cx: &mut App) {
} }
/// Authentication request /// Authentication request
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct AuthRequest { struct AuthRequest {
url: RelayUrl, url: RelayUrl,
challenge: String, challenge: String,
} }
impl Hash for AuthRequest {
fn hash<H: Hasher>(&self, state: &mut H) {
self.challenge.hash(state);
}
}
impl AuthRequest { impl AuthRequest {
pub fn new(challenge: impl Into<String>, url: RelayUrl) -> Self { pub fn new(challenge: impl Into<String>, url: RelayUrl) -> Self {
Self { Self {
@@ -74,9 +68,6 @@ pub struct RelayAuth {
/// Tasks for asynchronous operations /// Tasks for asynchronous operations
tasks: SmallVec<[Task<()>; 2]>, tasks: SmallVec<[Task<()>; 2]>,
/// Event subscriptions
_subscriptions: SmallVec<[Subscription; 1]>,
} }
impl RelayAuth { impl RelayAuth {
@@ -92,22 +83,13 @@ impl RelayAuth {
/// Create a new relay auth instance /// Create a new relay auth instance
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx); cx.defer_in(window, |this, window, cx| {
let mut subscriptions = smallvec![]; this.handle_notifications(window, cx);
});
subscriptions.push(
// Observe the nostr state
cx.observe_in(&nostr, window, move |this, state, window, cx| {
if state.read(cx).connected() {
this.handle_notifications(window, cx)
}
}),
);
Self { Self {
pending_events: HashSet::default(), pending_events: HashSet::default(),
tasks: smallvec![], tasks: smallvec![],
_subscriptions: subscriptions,
} }
} }
@@ -119,7 +101,8 @@ impl RelayAuth {
// Channel for communication between nostr and gpui // Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<Signal>(256); let (tx, rx) = flume::bounded::<Signal>(256);
cx.background_spawn(async move { self.tasks.push(cx.background_spawn(async move {
log::info!("Started handling nostr notifications");
let mut notifications = client.notifications(); let mut notifications = client.notifications();
let mut challenges: HashSet<Cow<'_, str>> = HashSet::default(); let mut challenges: HashSet<Cow<'_, str>> = HashSet::default();
@@ -128,8 +111,8 @@ impl RelayAuth {
match message { match message {
RelayMessage::Auth { challenge } => { RelayMessage::Auth { challenge } => {
if challenges.insert(challenge.clone()) { if challenges.insert(challenge.clone()) {
let request = AuthRequest::new(challenge, relay_url); let request = Arc::new(AuthRequest::new(challenge, relay_url));
let signal = Signal::Auth(Arc::new(request)); let signal = Signal::Auth(request);
tx.send_async(signal).await.ok(); tx.send_async(signal).await.ok();
} }
@@ -149,8 +132,7 @@ impl RelayAuth {
} }
} }
} }
}) }));
.detach();
self.tasks.push(cx.spawn_in(window, async move |this, cx| { self.tasks.push(cx.spawn_in(window, async move |this, cx| {
while let Ok(signal) = rx.recv_async().await { while let Ok(signal) = rx.recv_async().await {

View File

@@ -52,8 +52,8 @@ pub enum AuthMode {
/// Signer kind /// Signer kind
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum SignerKind { pub enum SignerKind {
#[default]
Auto, Auto,
#[default]
User, User,
Encryption, Encryption,
} }

View File

@@ -37,7 +37,9 @@ pub const USER_GIFTWRAP: &str = "user-gift-wraps";
pub const WOT_RELAYS: [&str; 1] = ["wss://relay.vertexlab.io"]; pub const WOT_RELAYS: [&str; 1] = ["wss://relay.vertexlab.io"];
/// Default search relays /// Default search relays
pub const SEARCH_RELAYS: [&str; 2] = ["wss://antiprimal.net", "wss://search.nos.today"]; pub const SEARCH_RELAYS: [&str; 1] = ["wss://antiprimal.net"];
pub const INDEXER_RELAYS: [&str; 1] = ["wss://indexer.coracle.social"];
/// Default bootstrap relays /// Default bootstrap relays
pub const BOOTSTRAP_RELAYS: [&str; 3] = [ pub const BOOTSTRAP_RELAYS: [&str; 3] = [

View File

@@ -5,7 +5,7 @@ use std::time::Duration;
use anyhow::{anyhow, Context as AnyhowContext, Error}; use anyhow::{anyhow, Context as AnyhowContext, Error};
use common::config_dir; use common::config_dir;
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task}; use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task, Window};
use nostr_connect::prelude::*; use nostr_connect::prelude::*;
use nostr_gossip_memory::prelude::*; use nostr_gossip_memory::prelude::*;
use nostr_lmdb::NostrLmdb; use nostr_lmdb::NostrLmdb;
@@ -19,7 +19,7 @@ pub use constants::*;
pub use nip05::*; pub use nip05::*;
pub use signer::*; pub use signer::*;
pub fn init(cx: &mut App) { pub fn init(window: &mut Window, cx: &mut App) {
// rustls uses the `aws_lc_rs` provider by default // rustls uses the `aws_lc_rs` provider by default
// This only errors if the default provider has already // This only errors if the default provider has already
// been installed. We can ignore this `Result`. // been installed. We can ignore this `Result`.
@@ -30,7 +30,7 @@ pub fn init(cx: &mut App) {
// Initialize the tokio runtime // Initialize the tokio runtime
gpui_tokio::init(cx); gpui_tokio::init(cx);
NostrRegistry::set_global(cx.new(NostrRegistry::new), cx); NostrRegistry::set_global(cx.new(|cx| NostrRegistry::new(window, cx)), cx);
} }
struct GlobalNostrRegistry(Entity<NostrRegistry>); struct GlobalNostrRegistry(Entity<NostrRegistry>);
@@ -82,7 +82,7 @@ impl NostrRegistry {
} }
/// Create a new nostr instance /// Create a new nostr instance
fn new(cx: &mut Context<Self>) -> Self { fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
// Construct the nostr lmdb instance // Construct the nostr lmdb instance
let lmdb = cx.foreground_executor().block_on(async move { let lmdb = cx.foreground_executor().block_on(async move {
NostrLmdb::open(config_dir().join("nostr")) NostrLmdb::open(config_dir().join("nostr"))
@@ -122,7 +122,7 @@ impl NostrRegistry {
subscriptions.push( subscriptions.push(
// Observe the NIP-65 state // Observe the NIP-65 state
cx.observe(&nip65, |this, state, cx| { cx.observe(&nip65, |this, state, cx| {
if state.read(cx).configured() { if state.read(cx).configured().is_some() {
this.get_profile(cx); this.get_profile(cx);
this.get_messaging_relays(cx); this.get_messaging_relays(cx);
} }
@@ -131,21 +131,16 @@ impl NostrRegistry {
subscriptions.push( subscriptions.push(
// Observe the NIP-17 state // Observe the NIP-17 state
cx.observe(&nip17, |this, state, cx| { cx.observe(&nip17, |this, nip17, cx| {
if state.read(cx) == &RelayState::Configured { if let Some(event) = nip17.read(cx).configured().cloned() {
this.get_messages(cx); this.subscribe_to_giftwrap_events(&event, cx);
}; };
}), }),
); );
cx.defer(|cx| { cx.defer_in(window, |this, _window, cx| {
let nostr = NostrRegistry::global(cx);
// Connect to the bootstrapping relays
nostr.update(cx, |this, cx| {
this.connect(cx); this.connect(cx);
}); });
});
Self { Self {
client, client,
@@ -164,36 +159,35 @@ impl NostrRegistry {
fn connect(&mut self, cx: &mut Context<Self>) { fn connect(&mut self, cx: &mut Context<Self>) {
let client = self.client(); let client = self.client();
let task: Task<Result<(), Error>> = cx.background_spawn(async move { self.tasks.push(cx.spawn(async move |this, cx| {
cx.background_executor()
.await_on_background(async move {
// Add search relay to the relay pool
for url in INDEXER_RELAYS.into_iter() {
client
.add_relay(url)
.capabilities(RelayCapabilities::DISCOVERY)
.await
.ok();
}
// Add search relay to the relay pool // Add search relay to the relay pool
for url in SEARCH_RELAYS.into_iter() { for url in SEARCH_RELAYS.into_iter() {
client.add_relay(url).and_connect().await?; client.add_relay(url).await.ok();
} }
// Add bootstrap relay to the relay pool // Add bootstrap relay to the relay pool
for url in BOOTSTRAP_RELAYS.into_iter() { for url in BOOTSTRAP_RELAYS.into_iter() {
client.add_relay(url).and_connect().await?; client.add_relay(url).await.ok();
} }
Ok(()) client.connect().await;
}); })
self.tasks.push(cx.spawn(async move |this, cx| {
// Wait for the task to complete
task.await?;
// Update the state
this.update(cx, |this, cx| {
this.set_connected(cx);
})?;
// Small delay
cx.background_executor()
.timer(Duration::from_millis(200))
.await; .await;
// Update the state // Update the state
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.set_connected(cx);
this.get_signer(cx); this.get_signer(cx);
})?; })?;
@@ -284,15 +278,36 @@ impl NostrRegistry {
} }
/// Reset all relay states /// Reset all relay states
pub fn reset_relay_states(&mut self, cx: &mut Context<Self>) { pub fn reset_relays(&mut self, cx: &mut Context<Self>) {
let client = self.client();
self.nip65.update(cx, |this, cx| { self.nip65.update(cx, |this, cx| {
*this = RelayState::default(); *this = RelayState::default();
cx.notify(); cx.notify();
}); });
self.nip17.update(cx, |this, cx| { self.nip17.update(cx, |this, cx| {
*this = RelayState::default(); *this = RelayState::default();
cx.notify(); cx.notify();
}); });
self.tasks.push(cx.background_spawn(async move {
let relays = client.relays().await;
for (relay_url, relay) in relays.iter() {
let url = relay_url.as_str();
let default_relay = BOOTSTRAP_RELAYS.contains(&url)
|| SEARCH_RELAYS.contains(&url)
|| INDEXER_RELAYS.contains(&url);
if !default_relay {
relay.unsubscribe_all().await?;
relay.disconnect();
}
}
Ok(())
}));
} }
/// Set the signer for the nostr client and verify the public key /// Set the signer for the nostr client and verify the public key
@@ -308,6 +323,9 @@ impl NostrRegistry {
// Update signer // Update signer
signer.switch(new, owned).await; signer.switch(new, owned).await;
// Unsubscribe from all subscriptions
client.unsubscribe_all().await?;
// Verify signer // Verify signer
let signer = client.signer().context("Signer not found")?; let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?; let public_key = signer.get_public_key().await?;
@@ -322,7 +340,7 @@ impl NostrRegistry {
// Update states // Update states
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.reset_relay_states(cx); this.reset_relays(cx);
this.get_relay_list(cx); this.get_relay_list(cx);
})?; })?;
@@ -365,8 +383,6 @@ impl NostrRegistry {
while let Some((_url, res)) = stream.next().await { while let Some((_url, res)) = stream.next().await {
match res { match res {
Ok(event) => { Ok(event) => {
log::info!("Received relay list event: {event:?}");
// Construct a filter to continuously receive relay list events // Construct a filter to continuously receive relay list events
let filter = Filter::new() let filter = Filter::new()
.kind(Kind::RelayList) .kind(Kind::RelayList)
@@ -382,7 +398,7 @@ impl NostrRegistry {
// Subscribe to the relay list events // Subscribe to the relay list events
client.subscribe(target).await?; client.subscribe(target).await?;
return Ok(RelayState::Configured); return Ok(RelayState::Configured(Box::new(event)));
} }
Err(e) => { Err(e) => {
log::error!("Failed to receive relay list event: {e}"); log::error!("Failed to receive relay list event: {e}");
@@ -443,8 +459,6 @@ impl NostrRegistry {
while let Some((_url, res)) = stream.next().await { while let Some((_url, res)) = stream.next().await {
match res { match res {
Ok(event) => { Ok(event) => {
log::info!("Received messaging relays event: {event:?}");
// Construct a filter to continuously receive relay list events // Construct a filter to continuously receive relay list events
let filter = Filter::new() let filter = Filter::new()
.kind(Kind::InboxRelays) .kind(Kind::InboxRelays)
@@ -454,7 +468,7 @@ impl NostrRegistry {
// Subscribe to the relay list events // Subscribe to the relay list events
client.subscribe(filter).await?; client.subscribe(filter).await?;
return Ok(RelayState::Configured); return Ok(RelayState::Configured(Box::new(event)));
} }
Err(e) => { Err(e) => {
log::error!("Failed to get messaging relays: {e}"); log::error!("Failed to get messaging relays: {e}");
@@ -485,24 +499,33 @@ impl NostrRegistry {
} }
/// Continuously get gift wrap events for the current user in their messaging relays /// Continuously get gift wrap events for the current user in their messaging relays
fn get_messages(&mut self, cx: &mut Context<Self>) { fn subscribe_to_giftwrap_events(&mut self, relay_list: &Event, cx: &mut Context<Self>) {
let client = self.client(); let client = self.client();
let signer = self.signer(); let signer = self.signer();
let messaging_relays = self.messaging_relays(cx); let relay_urls: Vec<RelayUrl> = nip17::extract_relay_list(relay_list).cloned().collect();
let task: Task<Result<(), Error>> = cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let urls = messaging_relays.await;
let public_key = signer.get_public_key().await?; let public_key = signer.get_public_key().await?;
for url in relay_urls.iter() {
client.add_relay(url).and_connect().await?;
}
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key); let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let id = SubscriptionId::new(USER_GIFTWRAP); let id = SubscriptionId::new(USER_GIFTWRAP);
// Construct target for subscription // Construct target for subscription
let target: HashMap<&RelayUrl, Filter> = let target: HashMap<&RelayUrl, Filter> = relay_urls
urls.iter().map(|relay| (relay, filter.clone())).collect(); .iter()
.map(|relay| (relay, filter.clone()))
.collect();
client.subscribe(target).with_id(id).await?; let output = client.subscribe(target).with_id(id).await?;
log::info!("Subscribed to user gift-wrap messages");
log::info!(
"Successfully subscribed to user gift-wrap messages on: {:?}",
output.success
);
Ok(()) Ok(())
}); });
@@ -973,7 +996,7 @@ fn default_relay_list() -> Vec<(RelayUrl, Option<RelayMetadata>)> {
Some(RelayMetadata::Write), Some(RelayMetadata::Write),
), ),
( (
RelayUrl::parse("wss://relay.primal.net/").unwrap(), RelayUrl::parse("wss://relay.damus.io/").unwrap(),
Some(RelayMetadata::Read), Some(RelayMetadata::Read),
), ),
( (
@@ -985,18 +1008,18 @@ fn default_relay_list() -> Vec<(RelayUrl, Option<RelayMetadata>)> {
fn default_messaging_relays() -> Vec<RelayUrl> { fn default_messaging_relays() -> Vec<RelayUrl> {
vec![ vec![
RelayUrl::parse("wss://auth.nostr1.com/").unwrap(), //RelayUrl::parse("wss://auth.nostr1.com/").unwrap(),
RelayUrl::parse("wss://nip17.com/").unwrap(), RelayUrl::parse("wss://nip17.com/").unwrap(),
] ]
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] #[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum RelayState { pub enum RelayState {
#[default] #[default]
Idle, Idle,
Checking, Checking,
NotConfigured, NotConfigured,
Configured, Configured(Box<Event>),
} }
impl RelayState { impl RelayState {
@@ -1012,8 +1035,11 @@ impl RelayState {
matches!(self, RelayState::NotConfigured) matches!(self, RelayState::NotConfigured)
} }
pub fn configured(&self) -> bool { pub fn configured(&self) -> Option<&Event> {
matches!(self, RelayState::Configured) match self {
RelayState::Configured(event) => Some(event),
_ => None,
}
} }
} }