Compare commits
2 Commits
ff5ae8280c
...
b7ffdc8431
| Author | SHA1 | Date | |
|---|---|---|---|
| b7ffdc8431 | |||
| e152154c3b |
90
Cargo.lock
generated
90
Cargo.lock
generated
@@ -1912,7 +1912,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1976,6 +1976,18 @@ dependencies = [
|
|||||||
"zune-inflate",
|
"zune-inflate",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fallible-iterator"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fallible-streaming-iterator"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "1.9.0"
|
version = "1.9.0"
|
||||||
@@ -3589,6 +3601,17 @@ dependencies = [
|
|||||||
"redox_syscall 0.7.2",
|
"redox_syscall 0.7.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libsqlite3-sys"
|
||||||
|
version = "0.36.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "95b4103cffefa72eb8428cb6b47d6627161e51c2739fc5e3b734584157bc642a"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"pkg-config",
|
||||||
|
"vcpkg",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linebender_resource_handle"
|
name = "linebender_resource_handle"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
@@ -4095,6 +4118,18 @@ dependencies = [
|
|||||||
"nostr",
|
"nostr",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nostr-gossip-sqlite"
|
||||||
|
version = "0.44.0"
|
||||||
|
source = "git+https://github.com/rust-nostr/nostr#b1ac65997d05424ad7b888dbdb5214b8999924ff"
|
||||||
|
dependencies = [
|
||||||
|
"async-utility",
|
||||||
|
"nostr",
|
||||||
|
"nostr-gossip",
|
||||||
|
"rusqlite",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nostr-lmdb"
|
name = "nostr-lmdb"
|
||||||
version = "0.44.0"
|
version = "0.44.0"
|
||||||
@@ -4144,7 +4179,7 @@ version = "0.50.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -4977,7 +5012,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tracing",
|
"tracing",
|
||||||
"windows-sys 0.60.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -5396,6 +5431,30 @@ version = "0.20.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
|
checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rsqlite-vfs"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a8a1f2315036ef6b1fbacd1972e8ee7688030b0a2121edfc2a6550febd41574d"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown 0.16.1",
|
||||||
|
"thiserror 2.0.18",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rusqlite"
|
||||||
|
version = "0.38.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1c93dd1c9683b438c392c492109cb702b8090b2bfc8fed6f6e4eb4523f17af3"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.11.0",
|
||||||
|
"fallible-iterator",
|
||||||
|
"fallible-streaming-iterator",
|
||||||
|
"libsqlite3-sys",
|
||||||
|
"smallvec",
|
||||||
|
"sqlite-wasm-rs",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust-embed"
|
name = "rust-embed"
|
||||||
version = "8.11.0"
|
version = "8.11.0"
|
||||||
@@ -5491,7 +5550,7 @@ dependencies = [
|
|||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys 0.12.1",
|
"linux-raw-sys 0.12.1",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -6117,6 +6176,18 @@ dependencies = [
|
|||||||
"bitflags 2.11.0",
|
"bitflags 2.11.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sqlite-wasm-rs"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2f4206ed3a67690b9c29b77d728f6acc3ce78f16bf846d83c94f76400320181b"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"js-sys",
|
||||||
|
"rsqlite-vfs",
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stable_deref_trait"
|
name = "stable_deref_trait"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
@@ -6171,6 +6242,7 @@ dependencies = [
|
|||||||
"nostr",
|
"nostr",
|
||||||
"nostr-blossom",
|
"nostr-blossom",
|
||||||
"nostr-connect",
|
"nostr-connect",
|
||||||
|
"nostr-gossip-sqlite",
|
||||||
"nostr-lmdb",
|
"nostr-lmdb",
|
||||||
"nostr-sdk",
|
"nostr-sdk",
|
||||||
"petname",
|
"petname",
|
||||||
@@ -6471,7 +6543,7 @@ dependencies = [
|
|||||||
"getrandom 0.4.1",
|
"getrandom 0.4.1",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rustix 1.1.4",
|
"rustix 1.1.4",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -7287,6 +7359,12 @@ dependencies = [
|
|||||||
"sval_serde",
|
"sval_serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcpkg"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.5"
|
version = "0.9.5"
|
||||||
@@ -7845,7 +7923,7 @@ version = "0.1.11"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ reqwest_client = { git = "https://github.com/zed-industries/zed" }
|
|||||||
nostr-lmdb = { git = "https://github.com/rust-nostr/nostr" }
|
nostr-lmdb = { git = "https://github.com/rust-nostr/nostr" }
|
||||||
nostr-connect = { git = "https://github.com/rust-nostr/nostr" }
|
nostr-connect = { git = "https://github.com/rust-nostr/nostr" }
|
||||||
nostr-blossom = { git = "https://github.com/rust-nostr/nostr" }
|
nostr-blossom = { git = "https://github.com/rust-nostr/nostr" }
|
||||||
|
nostr-gossip-sqlite = { git = "https://github.com/rust-nostr/nostr" }
|
||||||
nostr-sdk = { git = "https://github.com/rust-nostr/nostr" }
|
nostr-sdk = { git = "https://github.com/rust-nostr/nostr" }
|
||||||
nostr = { git = "https://github.com/rust-nostr/nostr", features = [ "nip96", "nip59", "nip49", "nip44" ] }
|
nostr = { git = "https://github.com/rust-nostr/nostr", features = [ "nip96", "nip59", "nip49", "nip44" ] }
|
||||||
|
|
||||||
|
|||||||
@@ -285,24 +285,17 @@ impl ChatRegistry {
|
|||||||
|
|
||||||
let signer = nostr.read(cx).signer();
|
let signer = nostr.read(cx).signer();
|
||||||
let public_key = signer.public_key().unwrap();
|
let public_key = signer.public_key().unwrap();
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
|
||||||
|
|
||||||
// Construct filter for inbox relays
|
// Construct filter for inbox relays
|
||||||
let filter = Filter::new()
|
let filter = Filter::new()
|
||||||
.kind(Kind::InboxRelays)
|
.kind(Kind::InboxRelays)
|
||||||
.author(public_key)
|
.author(public_key)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
// Construct target for subscription
|
|
||||||
let target: HashMap<&RelayUrl, Filter> =
|
|
||||||
urls.iter().map(|relay| (relay, filter.clone())).collect();
|
|
||||||
|
|
||||||
// Stream events from user's write relays
|
// Stream events from user's write relays
|
||||||
let mut stream = client
|
let mut stream = client
|
||||||
.stream_events(target)
|
.stream_events(filter)
|
||||||
.timeout(Duration::from_secs(TIMEOUT))
|
.timeout(Duration::from_secs(TIMEOUT))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::collections::HashMap;
|
|||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{anyhow, Error};
|
use anyhow::Error;
|
||||||
use common::EventUtils;
|
use common::EventUtils;
|
||||||
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task};
|
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@@ -323,6 +323,7 @@ impl Room {
|
|||||||
/// Get gossip relays for each member
|
/// Get gossip relays for each member
|
||||||
pub fn connect(&self, cx: &App) -> HashMap<PublicKey, Task<Result<(bool, bool), Error>>> {
|
pub fn connect(&self, cx: &App) -> HashMap<PublicKey, Task<Result<(bool, bool), Error>>> {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
|
|
||||||
let signer = nostr.read(cx).signer();
|
let signer = nostr.read(cx).signer();
|
||||||
let public_key = signer.public_key().unwrap();
|
let public_key = signer.public_key().unwrap();
|
||||||
|
|
||||||
@@ -330,56 +331,37 @@ impl Room {
|
|||||||
let mut tasks = HashMap::new();
|
let mut tasks = HashMap::new();
|
||||||
|
|
||||||
for member in members.into_iter() {
|
for member in members.into_iter() {
|
||||||
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
// Skip if member is the current user
|
// Skip if member is the current user
|
||||||
if member == public_key {
|
if member == public_key {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let client = nostr.read(cx).client();
|
|
||||||
let write_relays = nostr.read(cx).write_relays(&member, cx);
|
|
||||||
|
|
||||||
tasks.insert(
|
tasks.insert(
|
||||||
member,
|
member,
|
||||||
cx.background_spawn(async move {
|
cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
let mut has_inbox = false;
|
||||||
|
let mut has_announcement = false;
|
||||||
|
|
||||||
// Return if no relays are available
|
// Construct filters for inbox
|
||||||
if urls.is_empty() {
|
let inbox = Filter::new()
|
||||||
return Err(anyhow!(
|
|
||||||
"User has not set up any relays. You cannot send messages to them."
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construct filters for inbox and announcement
|
|
||||||
let inbox_filter = Filter::new()
|
|
||||||
.kind(Kind::InboxRelays)
|
.kind(Kind::InboxRelays)
|
||||||
.author(member)
|
.author(member)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
let announcement_filter = Filter::new()
|
|
||||||
|
// Construct filters for announcement
|
||||||
|
let announcement = Filter::new()
|
||||||
.kind(Kind::Custom(10044))
|
.kind(Kind::Custom(10044))
|
||||||
.author(member)
|
.author(member)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
// Create subscription targets
|
|
||||||
let target = urls
|
|
||||||
.into_iter()
|
|
||||||
.map(|relay| {
|
|
||||||
(
|
|
||||||
relay,
|
|
||||||
vec![inbox_filter.clone(), announcement_filter.clone()],
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect::<HashMap<_, _>>();
|
|
||||||
|
|
||||||
// Stream events from user's write relays
|
// Stream events from user's write relays
|
||||||
let mut stream = client
|
let mut stream = client
|
||||||
.stream_events(target)
|
.stream_events(vec![inbox.clone(), announcement.clone()])
|
||||||
.timeout(Duration::from_secs(TIMEOUT))
|
.timeout(Duration::from_secs(TIMEOUT))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut has_inbox = false;
|
|
||||||
let mut has_announcement = false;
|
|
||||||
|
|
||||||
while let Some((_url, res)) = stream.next().await {
|
while let Some((_url, res)) = stream.next().await {
|
||||||
let event = res?;
|
let event = res?;
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ pub struct EncryptionPanel {
|
|||||||
/// Whether the panel is loading
|
/// Whether the panel is loading
|
||||||
loading: bool,
|
loading: bool,
|
||||||
|
|
||||||
|
/// Whether the encryption is resetting
|
||||||
|
resetting: bool,
|
||||||
|
|
||||||
/// Tasks
|
/// Tasks
|
||||||
tasks: Vec<Task<Result<(), Error>>>,
|
tasks: Vec<Task<Result<(), Error>>>,
|
||||||
}
|
}
|
||||||
@@ -47,6 +50,7 @@ impl EncryptionPanel {
|
|||||||
focus_handle: cx.focus_handle(),
|
focus_handle: cx.focus_handle(),
|
||||||
public_key,
|
public_key,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
resetting: false,
|
||||||
tasks: vec![],
|
tasks: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,6 +95,42 @@ impl EncryptionPanel {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_resetting(&mut self, status: bool, cx: &mut Context<Self>) {
|
||||||
|
self.resetting = status;
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reset(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
|
let device = DeviceRegistry::global(cx);
|
||||||
|
let task = device.read(cx).create_encryption(cx);
|
||||||
|
|
||||||
|
// Update the reset status
|
||||||
|
self.set_resetting(true, cx);
|
||||||
|
|
||||||
|
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
|
||||||
|
match task.await {
|
||||||
|
Ok(keys) => {
|
||||||
|
this.update_in(cx, |this, _window, cx| {
|
||||||
|
this.set_resetting(false, cx);
|
||||||
|
|
||||||
|
device.update(cx, |this, cx| {
|
||||||
|
this.set_signer(keys, cx);
|
||||||
|
this.listen_request(cx);
|
||||||
|
});
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
this.update_in(cx, |this, window, cx| {
|
||||||
|
this.set_resetting(false, cx);
|
||||||
|
window.push_notification(Notification::error(e.to_string()), cx);
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
fn render_requests(&mut self, cx: &mut Context<Self>) -> Vec<impl IntoElement> {
|
fn render_requests(&mut self, cx: &mut Context<Self>) -> Vec<impl IntoElement> {
|
||||||
const TITLE: &str = "You've requested for the Encryption Key from:";
|
const TITLE: &str = "You've requested for the Encryption Key from:";
|
||||||
|
|
||||||
@@ -269,8 +309,7 @@ impl Render for EncryptionPanel {
|
|||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.when(state.set(), |this| {
|
.child(
|
||||||
this.child(
|
|
||||||
v_flex()
|
v_flex()
|
||||||
.gap_1()
|
.gap_1()
|
||||||
.child(
|
.child(
|
||||||
@@ -279,7 +318,10 @@ impl Render for EncryptionPanel {
|
|||||||
.label("Reset")
|
.label("Reset")
|
||||||
.warning()
|
.warning()
|
||||||
.small()
|
.small()
|
||||||
.font_semibold(),
|
.font_semibold()
|
||||||
|
.on_click(
|
||||||
|
cx.listener(move |this, _ev, window, cx| this.reset(window, cx)),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.child(
|
.child(
|
||||||
div()
|
div()
|
||||||
@@ -289,6 +331,5 @@ impl Render for EncryptionPanel {
|
|||||||
.child(SharedString::from(NOTICE)),
|
.child(SharedString::from(NOTICE)),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use gpui::{
|
|||||||
};
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
use state::NostrRegistry;
|
use state::{NostrRegistry, TIMEOUT};
|
||||||
use theme::ActiveTheme;
|
use theme::ActiveTheme;
|
||||||
use ui::button::{Button, ButtonVariants};
|
use ui::button::{Button, ButtonVariants};
|
||||||
use ui::dock_area::panel::{Panel, PanelEvent};
|
use ui::dock_area::panel::{Panel, PanelEvent};
|
||||||
@@ -170,15 +170,6 @@ impl MessagingRelayPanel {
|
|||||||
|
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
let signer = nostr.read(cx).signer();
|
|
||||||
|
|
||||||
let Some(public_key) = signer.public_key() else {
|
|
||||||
window.push_notification("Public Key not found", cx);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Get user's write relays
|
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
|
||||||
|
|
||||||
// Construct event tags
|
// Construct event tags
|
||||||
let tags: Vec<Tag> = self
|
let tags: Vec<Tag> = self
|
||||||
@@ -191,14 +182,16 @@ impl MessagingRelayPanel {
|
|||||||
self.set_updating(true, cx);
|
self.set_updating(true, cx);
|
||||||
|
|
||||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
|
||||||
|
|
||||||
// Construct nip17 event builder
|
// Construct nip17 event builder
|
||||||
let builder = EventBuilder::new(Kind::InboxRelays, "").tags(tags);
|
let builder = EventBuilder::new(Kind::InboxRelays, "").tags(tags);
|
||||||
let event = client.sign_event_builder(builder).await?;
|
let event = client.sign_event_builder(builder).await?;
|
||||||
|
|
||||||
// Set messaging relays
|
// Set messaging relays
|
||||||
client.send_event(&event).to(urls).await?;
|
client
|
||||||
|
.send_event(&event)
|
||||||
|
.to_nip65()
|
||||||
|
.ok_timeout(Duration::from_secs(TIMEOUT))
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use ::settings::AppSettings;
|
use ::settings::AppSettings;
|
||||||
use chat::{ChatEvent, ChatRegistry, InboxState};
|
use chat::{ChatEvent, ChatRegistry, InboxState};
|
||||||
|
use device::DeviceRegistry;
|
||||||
use gpui::prelude::FluentBuilder;
|
use gpui::prelude::FluentBuilder;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, px, rems, Action, App, AppContext, Axis, Context, Entity, InteractiveElement, IntoElement,
|
div, px, rems, Action, App, AppContext, Axis, Context, Entity, InteractiveElement, IntoElement,
|
||||||
@@ -36,6 +37,7 @@ enum Command {
|
|||||||
|
|
||||||
RefreshRelayList,
|
RefreshRelayList,
|
||||||
RefreshMessagingRelays,
|
RefreshMessagingRelays,
|
||||||
|
RefreshEncryption,
|
||||||
|
|
||||||
ShowRelayList,
|
ShowRelayList,
|
||||||
ShowMessaging,
|
ShowMessaging,
|
||||||
@@ -260,6 +262,12 @@ impl Workspace {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Command::RefreshEncryption => {
|
||||||
|
let device = DeviceRegistry::global(cx);
|
||||||
|
device.update(cx, |this, cx| {
|
||||||
|
this.get_announcement(cx);
|
||||||
|
});
|
||||||
|
}
|
||||||
Command::RefreshRelayList => {
|
Command::RefreshRelayList => {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
nostr.update(cx, |this, cx| {
|
nostr.update(cx, |this, cx| {
|
||||||
@@ -440,8 +448,19 @@ impl Workspace {
|
|||||||
.tooltip("Decoupled encryption key")
|
.tooltip("Decoupled encryption key")
|
||||||
.small()
|
.small()
|
||||||
.ghost()
|
.ghost()
|
||||||
.on_click(|_ev, window, cx| {
|
.dropdown_menu(|this, _window, _cx| {
|
||||||
window.dispatch_action(Box::new(Command::ShowEncryption), cx);
|
this.min_w(px(260.))
|
||||||
|
.label("Encryption")
|
||||||
|
.menu_with_icon(
|
||||||
|
"Reload",
|
||||||
|
IconName::Refresh,
|
||||||
|
Box::new(Command::RefreshEncryption),
|
||||||
|
)
|
||||||
|
.menu_with_icon(
|
||||||
|
"View encryption",
|
||||||
|
IconName::Settings,
|
||||||
|
Box::new(Command::ShowEncryption),
|
||||||
|
)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.child(
|
.child(
|
||||||
@@ -563,47 +582,6 @@ impl Workspace {
|
|||||||
.dropdown_menu(move |this, _window, _cx| {
|
.dropdown_menu(move |this, _window, _cx| {
|
||||||
this.min_w(px(260.))
|
this.min_w(px(260.))
|
||||||
.label("Relays")
|
.label("Relays")
|
||||||
.menu_element_with_disabled(
|
|
||||||
Box::new(Command::ShowRelayList),
|
|
||||||
true,
|
|
||||||
move |_window, cx| {
|
|
||||||
let nostr = NostrRegistry::global(cx);
|
|
||||||
let urls = nostr.read(cx).read_only_relays(&pkey, cx);
|
|
||||||
|
|
||||||
v_flex()
|
|
||||||
.gap_1()
|
|
||||||
.w_full()
|
|
||||||
.items_start()
|
|
||||||
.justify_start()
|
|
||||||
.children({
|
|
||||||
let mut items = vec![];
|
|
||||||
|
|
||||||
for url in urls.into_iter() {
|
|
||||||
items.push(
|
|
||||||
h_flex()
|
|
||||||
.h_6()
|
|
||||||
.w_full()
|
|
||||||
.gap_2()
|
|
||||||
.px_2()
|
|
||||||
.text_xs()
|
|
||||||
.bg(cx
|
|
||||||
.theme()
|
|
||||||
.elevated_surface_background)
|
|
||||||
.rounded(cx.theme().radius)
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.size_1()
|
|
||||||
.rounded_full()
|
|
||||||
.bg(gpui::green()),
|
|
||||||
)
|
|
||||||
.child(url),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
items
|
|
||||||
})
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.separator()
|
.separator()
|
||||||
.menu_with_icon(
|
.menu_with_icon(
|
||||||
"Reload",
|
"Reload",
|
||||||
|
|||||||
@@ -157,7 +157,7 @@ impl DeviceRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set the decoupled encryption key for the current user
|
/// Set the decoupled encryption key for the current user
|
||||||
fn set_signer<S>(&mut self, new: S, cx: &mut Context<Self>)
|
pub fn set_signer<S>(&mut self, new: S, cx: &mut Context<Self>)
|
||||||
where
|
where
|
||||||
S: NostrSigner + 'static,
|
S: NostrSigner + 'static,
|
||||||
{
|
{
|
||||||
@@ -248,31 +248,23 @@ impl DeviceRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get device announcement for current user
|
/// Get device announcement for current user
|
||||||
fn get_announcement(&mut self, cx: &mut Context<Self>) {
|
pub fn get_announcement(&mut self, cx: &mut Context<Self>) {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
let signer = nostr.read(cx).signer();
|
let signer = nostr.read(cx).signer();
|
||||||
let public_key = signer.public_key().unwrap();
|
let public_key = signer.public_key().unwrap();
|
||||||
|
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
|
||||||
|
|
||||||
let task: Task<Result<Event, Error>> = cx.background_spawn(async move {
|
let task: Task<Result<Event, Error>> = cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
|
||||||
|
|
||||||
// Construct the filter for the device announcement event
|
// Construct the filter for the device announcement event
|
||||||
let filter = Filter::new()
|
let filter = Filter::new()
|
||||||
.kind(Kind::Custom(10044))
|
.kind(Kind::Custom(10044))
|
||||||
.author(public_key)
|
.author(public_key)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
// Construct target for subscription
|
|
||||||
let target: HashMap<&RelayUrl, Filter> =
|
|
||||||
urls.iter().map(|relay| (relay, filter.clone())).collect();
|
|
||||||
|
|
||||||
// Stream events from user's write relays
|
// Stream events from user's write relays
|
||||||
let mut stream = client
|
let mut stream = client
|
||||||
.stream_events(target)
|
.stream_events(filter)
|
||||||
.timeout(Duration::from_secs(TIMEOUT))
|
.timeout(Duration::from_secs(TIMEOUT))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -309,25 +301,16 @@ impl DeviceRegistry {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new device signer and announce it
|
pub fn create_encryption(&self, cx: &App) -> Task<Result<Keys, Error>> {
|
||||||
fn announce(&mut self, cx: &mut Context<Self>) {
|
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
// Get current user
|
// Generate encryption keys
|
||||||
let signer = nostr.read(cx).signer();
|
|
||||||
let public_key = signer.public_key().unwrap();
|
|
||||||
|
|
||||||
// Get user's write relays
|
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
|
||||||
|
|
||||||
let keys = Keys::generate();
|
let keys = Keys::generate();
|
||||||
let secret = keys.secret_key().to_secret_hex();
|
let secret = keys.secret_key().to_secret_hex();
|
||||||
let n = keys.public_key();
|
let n = keys.public_key();
|
||||||
|
|
||||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
|
||||||
|
|
||||||
// Construct an announcement event
|
// Construct an announcement event
|
||||||
let event = client
|
let event = client
|
||||||
.sign_event_builder(EventBuilder::new(Kind::Custom(10044), "").tags(vec![
|
.sign_event_builder(EventBuilder::new(Kind::Custom(10044), "").tags(vec![
|
||||||
@@ -337,22 +320,35 @@ impl DeviceRegistry {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Publish announcement
|
// Publish announcement
|
||||||
client.send_event(&event).to(urls).await?;
|
client
|
||||||
|
.send_event(&event)
|
||||||
|
.to_nip65()
|
||||||
|
.ok_timeout(Duration::from_secs(TIMEOUT))
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Save device keys to the database
|
// Save device keys to the database
|
||||||
set_keys(&client, &secret).await?;
|
set_keys(&client, &secret).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(keys)
|
||||||
});
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new device signer and announce it
|
||||||
|
fn announce(&mut self, cx: &mut Context<Self>) {
|
||||||
|
let task = self.create_encryption(cx);
|
||||||
|
|
||||||
self.tasks.push(cx.spawn(async move |this, cx| {
|
self.tasks.push(cx.spawn(async move |this, cx| {
|
||||||
if task.await.is_ok() {
|
match task.await {
|
||||||
|
Ok(keys) => {
|
||||||
this.update(cx, |this, cx| {
|
this.update(cx, |this, cx| {
|
||||||
this.set_signer(keys, cx);
|
this.set_signer(keys, cx);
|
||||||
this.listen_request(cx);
|
this.listen_request(cx);
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Failed to create encryption key: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -401,30 +397,22 @@ impl DeviceRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Listen for device key requests on user's write relays
|
/// Listen for device key requests on user's write relays
|
||||||
fn listen_request(&mut self, cx: &mut Context<Self>) {
|
pub fn listen_request(&mut self, cx: &mut Context<Self>) {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
let signer = nostr.read(cx).signer();
|
let signer = nostr.read(cx).signer();
|
||||||
let public_key = signer.public_key().unwrap();
|
let public_key = signer.public_key().unwrap();
|
||||||
|
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
|
||||||
|
|
||||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
|
||||||
|
|
||||||
// Construct a filter for device key requests
|
// Construct a filter for device key requests
|
||||||
let filter = Filter::new()
|
let filter = Filter::new()
|
||||||
.kind(Kind::Custom(4454))
|
.kind(Kind::Custom(4454))
|
||||||
.author(public_key)
|
.author(public_key)
|
||||||
.since(Timestamp::now());
|
.since(Timestamp::now());
|
||||||
|
|
||||||
// Construct target for subscription
|
|
||||||
let target: HashMap<&RelayUrl, Filter> =
|
|
||||||
urls.iter().map(|relay| (relay, filter.clone())).collect();
|
|
||||||
|
|
||||||
// Subscribe to the device key requests on user's write relays
|
// Subscribe to the device key requests on user's write relays
|
||||||
client.subscribe(target).await?;
|
client.subscribe(filter).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
@@ -440,23 +428,15 @@ impl DeviceRegistry {
|
|||||||
let signer = nostr.read(cx).signer();
|
let signer = nostr.read(cx).signer();
|
||||||
let public_key = signer.public_key().unwrap();
|
let public_key = signer.public_key().unwrap();
|
||||||
|
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
|
||||||
|
|
||||||
self.tasks.push(cx.background_spawn(async move {
|
self.tasks.push(cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
|
||||||
|
|
||||||
// Construct a filter for device key requests
|
// Construct a filter for device key requests
|
||||||
let filter = Filter::new()
|
let filter = Filter::new()
|
||||||
.kind(Kind::Custom(4455))
|
.kind(Kind::Custom(4455))
|
||||||
.author(public_key)
|
.author(public_key)
|
||||||
.since(Timestamp::now());
|
.since(Timestamp::now());
|
||||||
|
|
||||||
// Construct target for subscription
|
|
||||||
let target: HashMap<&RelayUrl, Filter> =
|
|
||||||
urls.iter().map(|relay| (relay, filter.clone())).collect();
|
|
||||||
|
|
||||||
// Subscribe to the device key requests on user's write relays
|
// Subscribe to the device key requests on user's write relays
|
||||||
client.subscribe(target).await?;
|
client.subscribe(filter).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}));
|
}));
|
||||||
@@ -466,11 +446,7 @@ impl DeviceRegistry {
|
|||||||
fn request(&mut self, cx: &mut Context<Self>) {
|
fn request(&mut self, cx: &mut Context<Self>) {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
let signer = nostr.read(cx).signer();
|
let signer = nostr.read(cx).signer();
|
||||||
let public_key = signer.public_key().unwrap();
|
|
||||||
|
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
|
||||||
|
|
||||||
let app_keys = nostr.read(cx).app_keys().clone();
|
let app_keys = nostr.read(cx).app_keys().clone();
|
||||||
let app_pubkey = app_keys.public_key();
|
let app_pubkey = app_keys.public_key();
|
||||||
@@ -502,18 +478,20 @@ impl DeviceRegistry {
|
|||||||
Ok(Some(keys))
|
Ok(Some(keys))
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let urls = write_relays.await;
|
|
||||||
|
|
||||||
// Construct an event for device key request
|
// Construct an event for device key request
|
||||||
let event = client
|
let event = client
|
||||||
.sign_event_builder(EventBuilder::new(Kind::Custom(4454), "").tags(vec![
|
.sign_event_builder(EventBuilder::new(Kind::Custom(4454), "").tags(vec![
|
||||||
Tag::client(app_name()),
|
|
||||||
Tag::custom(TagKind::custom("P"), vec![app_pubkey]),
|
Tag::custom(TagKind::custom("P"), vec![app_pubkey]),
|
||||||
|
Tag::client(app_name()),
|
||||||
]))
|
]))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Send the event to write relays
|
// Send the event to write relays
|
||||||
client.send_event(&event).to(urls).await?;
|
client
|
||||||
|
.send_event(&event)
|
||||||
|
.to_nip65()
|
||||||
|
.ok_timeout(Duration::from_secs(TIMEOUT))
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
@@ -580,18 +558,12 @@ impl DeviceRegistry {
|
|||||||
pub fn approve(&self, event: &Event, cx: &App) -> Task<Result<(), Error>> {
|
pub fn approve(&self, event: &Event, cx: &App) -> Task<Result<(), Error>> {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
// Get current user
|
|
||||||
let signer = nostr.read(cx).signer();
|
let signer = nostr.read(cx).signer();
|
||||||
let public_key = signer.public_key().unwrap();
|
|
||||||
|
|
||||||
// Get user's write relays
|
// Get user's write relays
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
|
||||||
let event = event.clone();
|
let event = event.clone();
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
|
||||||
|
|
||||||
// Get device keys
|
// Get device keys
|
||||||
let keys = get_keys(&client).await?;
|
let keys = get_keys(&client).await?;
|
||||||
let secret = keys.secret_key().to_secret_hex();
|
let secret = keys.secret_key().to_secret_hex();
|
||||||
@@ -619,7 +591,11 @@ impl DeviceRegistry {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Send the response event to the user's relay list
|
// Send the response event to the user's relay list
|
||||||
client.send_event(&event).to(urls).await?;
|
client
|
||||||
|
.send_event(&event)
|
||||||
|
.to_nip65()
|
||||||
|
.ok_timeout(Duration::from_secs(TIMEOUT))
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
@@ -671,6 +647,8 @@ async fn get_keys(client: &Client) -> Result<Keys, Error> {
|
|||||||
let secret = SecretKey::parse(&content)?;
|
let secret = SecretKey::parse(&content)?;
|
||||||
let keys = Keys::new(secret);
|
let keys = Keys::new(secret);
|
||||||
|
|
||||||
|
log::info!("Encryption keys retrieved successfully");
|
||||||
|
|
||||||
Ok(keys)
|
Ok(keys)
|
||||||
} else {
|
} else {
|
||||||
Err(anyhow!("Key not found"))
|
Err(anyhow!("Key not found"))
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ impl PersonRegistry {
|
|||||||
loop {
|
loop {
|
||||||
match flume::Selector::new()
|
match flume::Selector::new()
|
||||||
.recv(rx, |result| result.ok())
|
.recv(rx, |result| result.ok())
|
||||||
.wait_timeout(Duration::from_secs(2))
|
.wait_timeout(Duration::from_secs(TIMEOUT))
|
||||||
{
|
{
|
||||||
Ok(Some(public_key)) => {
|
Ok(Some(public_key)) => {
|
||||||
batch.insert(public_key);
|
batch.insert(public_key);
|
||||||
@@ -307,16 +307,22 @@ where
|
|||||||
.timeout(Some(Duration::from_secs(TIMEOUT)));
|
.timeout(Some(Duration::from_secs(TIMEOUT)));
|
||||||
|
|
||||||
// Construct the filter for metadata
|
// Construct the filter for metadata
|
||||||
let filter = Filter::new()
|
let metadata = Filter::new()
|
||||||
.kind(Kind::Metadata)
|
.kind(Kind::Metadata)
|
||||||
|
.authors(authors.clone())
|
||||||
|
.limit(limit);
|
||||||
|
|
||||||
|
// Construct the filter for relay list
|
||||||
|
let gossip = Filter::new()
|
||||||
|
.kind(Kind::RelayList)
|
||||||
.authors(authors)
|
.authors(authors)
|
||||||
.limit(limit);
|
.limit(limit);
|
||||||
|
|
||||||
// Construct target for subscription
|
// Construct target for subscription
|
||||||
let target = BOOTSTRAP_RELAYS
|
let target: HashMap<&str, Vec<Filter>> = BOOTSTRAP_RELAYS
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|relay| (relay, vec![filter.clone()]))
|
.map(|relay| (relay, vec![metadata.clone(), gossip.clone()]))
|
||||||
.collect::<HashMap<_, _>>();
|
.collect();
|
||||||
|
|
||||||
client.subscribe(target).close_on(opts).await?;
|
client.subscribe(target).close_on(opts).await?;
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ nostr.workspace = true
|
|||||||
nostr-sdk.workspace = true
|
nostr-sdk.workspace = true
|
||||||
nostr-lmdb.workspace = true
|
nostr-lmdb.workspace = true
|
||||||
nostr-connect.workspace = true
|
nostr-connect.workspace = true
|
||||||
|
nostr-gossip-sqlite.workspace = true
|
||||||
nostr-blossom.workspace = true
|
nostr-blossom.workspace = true
|
||||||
|
|
||||||
gpui.workspace = true
|
gpui.workspace = true
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ pub const APP_ID: &str = "su.reya.coop";
|
|||||||
/// Keyring name
|
/// Keyring name
|
||||||
pub const KEYRING: &str = "Coop Safe Storage";
|
pub const KEYRING: &str = "Coop Safe Storage";
|
||||||
|
|
||||||
/// Default timeout for subscription
|
/// Default timeout in second for subscription
|
||||||
pub const TIMEOUT: u64 = 3;
|
pub const TIMEOUT: u64 = 2;
|
||||||
|
|
||||||
/// Default delay for searching
|
/// Default delay for searching
|
||||||
pub const FIND_DELAY: u64 = 600;
|
pub const FIND_DELAY: u64 = 600;
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
use std::collections::{HashMap, HashSet};
|
|
||||||
|
|
||||||
use gpui::SharedString;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
|
|
||||||
/// Gossip
|
|
||||||
#[derive(Debug, Clone, Default)]
|
|
||||||
pub struct Gossip {
|
|
||||||
relays: HashMap<PublicKey, HashSet<(RelayUrl, Option<RelayMetadata>)>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Gossip {
|
|
||||||
pub fn read_only_relays(&self, public_key: &PublicKey) -> Vec<SharedString> {
|
|
||||||
self.relays
|
|
||||||
.get(public_key)
|
|
||||||
.map(|relays| {
|
|
||||||
relays
|
|
||||||
.iter()
|
|
||||||
.map(|(url, _)| url.to_string().into())
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get read relays for a given public key
|
|
||||||
pub fn read_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
|
|
||||||
self.relays
|
|
||||||
.get(public_key)
|
|
||||||
.map(|relays| {
|
|
||||||
relays
|
|
||||||
.iter()
|
|
||||||
.filter_map(|(url, metadata)| {
|
|
||||||
if metadata.is_none() || metadata == &Some(RelayMetadata::Read) {
|
|
||||||
Some(url.to_owned())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get write relays for a given public key
|
|
||||||
pub fn write_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
|
|
||||||
self.relays
|
|
||||||
.get(public_key)
|
|
||||||
.map(|relays| {
|
|
||||||
relays
|
|
||||||
.iter()
|
|
||||||
.filter_map(|(url, metadata)| {
|
|
||||||
if metadata.is_none() || metadata == &Some(RelayMetadata::Write) {
|
|
||||||
Some(url.to_owned())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Insert gossip relays for a public key
|
|
||||||
pub fn insert_relays(&mut self, event: &Event) {
|
|
||||||
self.relays.entry(event.pubkey).or_default().extend(
|
|
||||||
event
|
|
||||||
.tags
|
|
||||||
.iter()
|
|
||||||
.filter_map(|tag| {
|
|
||||||
if let Some(TagStandard::RelayMetadata {
|
|
||||||
relay_url,
|
|
||||||
metadata,
|
|
||||||
}) = tag.clone().to_standardized()
|
|
||||||
{
|
|
||||||
Some((relay_url, metadata))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.take(3),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +1,25 @@
|
|||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::HashMap;
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
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, SharedString, Task, Window};
|
use gpui::{App, AppContext, Context, Entity, Global, Task, Window};
|
||||||
use nostr_connect::prelude::*;
|
use nostr_connect::prelude::*;
|
||||||
|
use nostr_gossip_sqlite::prelude::*;
|
||||||
use nostr_lmdb::prelude::*;
|
use nostr_lmdb::prelude::*;
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
|
|
||||||
mod blossom;
|
mod blossom;
|
||||||
mod constants;
|
mod constants;
|
||||||
mod device;
|
mod device;
|
||||||
mod gossip;
|
|
||||||
mod nip05;
|
mod nip05;
|
||||||
mod signer;
|
mod signer;
|
||||||
|
|
||||||
pub use blossom::*;
|
pub use blossom::*;
|
||||||
pub use constants::*;
|
pub use constants::*;
|
||||||
pub use device::*;
|
pub use device::*;
|
||||||
pub use gossip::*;
|
|
||||||
pub use nip05::*;
|
pub use nip05::*;
|
||||||
pub use signer::*;
|
pub use signer::*;
|
||||||
|
|
||||||
@@ -56,9 +55,6 @@ pub struct NostrRegistry {
|
|||||||
/// Used for Nostr Connect and NIP-4e operations
|
/// Used for Nostr Connect and NIP-4e operations
|
||||||
app_keys: Keys,
|
app_keys: Keys,
|
||||||
|
|
||||||
/// Custom gossip implementation
|
|
||||||
gossip: Entity<Gossip>,
|
|
||||||
|
|
||||||
/// Relay list state
|
/// Relay list state
|
||||||
relay_list_state: RelayState,
|
relay_list_state: RelayState,
|
||||||
|
|
||||||
@@ -89,9 +85,6 @@ impl NostrRegistry {
|
|||||||
let app_keys = get_or_init_app_keys().unwrap_or(Keys::generate());
|
let app_keys = get_or_init_app_keys().unwrap_or(Keys::generate());
|
||||||
let signer = Arc::new(CoopSigner::new(app_keys.clone()));
|
let signer = Arc::new(CoopSigner::new(app_keys.clone()));
|
||||||
|
|
||||||
// Construct the gossip entity
|
|
||||||
let gossip = cx.new(|_| Gossip::default());
|
|
||||||
|
|
||||||
// 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"))
|
||||||
@@ -99,13 +92,21 @@ impl NostrRegistry {
|
|||||||
.expect("Failed to initialize database")
|
.expect("Failed to initialize database")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Construct the nostr gossip instance
|
||||||
|
let gossip = cx.foreground_executor().block_on(async move {
|
||||||
|
NostrGossipSqlite::open(config_dir().join("gossip"))
|
||||||
|
.await
|
||||||
|
.expect("Failed to initialize gossip database")
|
||||||
|
});
|
||||||
|
|
||||||
// Construct the nostr client
|
// Construct the nostr client
|
||||||
let client = ClientBuilder::default()
|
let client = ClientBuilder::default()
|
||||||
.signer(signer.clone())
|
.signer(signer.clone())
|
||||||
.database(lmdb)
|
.database(lmdb)
|
||||||
|
.gossip(gossip)
|
||||||
.automatic_authentication(false)
|
.automatic_authentication(false)
|
||||||
.verify_subscriptions(false)
|
.verify_subscriptions(false)
|
||||||
.connect_timeout(Duration::from_secs(TIMEOUT))
|
.connect_timeout(Duration::from_secs(5))
|
||||||
.sleep_when_idle(SleepWhenIdle::Enabled {
|
.sleep_when_idle(SleepWhenIdle::Enabled {
|
||||||
timeout: Duration::from_secs(600),
|
timeout: Duration::from_secs(600),
|
||||||
})
|
})
|
||||||
@@ -114,14 +115,12 @@ impl NostrRegistry {
|
|||||||
// Run at the end of current cycle
|
// Run at the end of current cycle
|
||||||
cx.defer_in(window, |this, _window, cx| {
|
cx.defer_in(window, |this, _window, cx| {
|
||||||
this.connect(cx);
|
this.connect(cx);
|
||||||
this.handle_notifications(cx);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
client,
|
client,
|
||||||
signer,
|
signer,
|
||||||
app_keys,
|
app_keys,
|
||||||
gossip,
|
|
||||||
relay_list_state: RelayState::Idle,
|
relay_list_state: RelayState::Idle,
|
||||||
connected: false,
|
connected: false,
|
||||||
creating: false,
|
creating: false,
|
||||||
@@ -146,10 +145,7 @@ impl NostrRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect to all added relays
|
// Connect to all added relays
|
||||||
client
|
client.connect().and_wait(Duration::from_secs(5)).await;
|
||||||
.connect()
|
|
||||||
.and_wait(Duration::from_secs(TIMEOUT))
|
|
||||||
.await;
|
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@@ -163,60 +159,6 @@ impl NostrRegistry {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle nostr notifications
|
|
||||||
fn handle_notifications(&mut self, cx: &mut Context<Self>) {
|
|
||||||
let client = self.client();
|
|
||||||
let gossip = self.gossip.downgrade();
|
|
||||||
|
|
||||||
// Channel for communication between nostr and gpui
|
|
||||||
let (tx, rx) = flume::bounded::<Event>(2048);
|
|
||||||
|
|
||||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
|
||||||
// Handle nostr notifications
|
|
||||||
let mut notifications = client.notifications();
|
|
||||||
let mut processed_events = HashSet::new();
|
|
||||||
|
|
||||||
while let Some(notification) = notifications.next().await {
|
|
||||||
if let ClientNotification::Message {
|
|
||||||
message: RelayMessage::Event { event, .. },
|
|
||||||
..
|
|
||||||
} = notification
|
|
||||||
{
|
|
||||||
// Skip if the event has already been processed
|
|
||||||
if processed_events.insert(event.id) {
|
|
||||||
match event.kind {
|
|
||||||
Kind::RelayList => {
|
|
||||||
tx.send_async(event.into_owned()).await?;
|
|
||||||
}
|
|
||||||
Kind::InboxRelays => {
|
|
||||||
tx.send_async(event.into_owned()).await?;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
// Run task in the background
|
|
||||||
task.detach();
|
|
||||||
|
|
||||||
self.tasks.push(cx.spawn(async move |_this, cx| {
|
|
||||||
while let Ok(event) = rx.recv_async().await {
|
|
||||||
if let Kind::RelayList = event.kind {
|
|
||||||
gossip.update(cx, |this, cx| {
|
|
||||||
this.insert_relays(&event);
|
|
||||||
cx.notify();
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the nostr client
|
/// Get the nostr client
|
||||||
pub fn client(&self) -> Client {
|
pub fn client(&self) -> Client {
|
||||||
self.client.clone()
|
self.client.clone()
|
||||||
@@ -247,41 +189,6 @@ impl NostrRegistry {
|
|||||||
self.relay_list_state.clone()
|
self.relay_list_state.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all relays for a given public key without ensuring connections
|
|
||||||
pub fn read_only_relays(&self, public_key: &PublicKey, cx: &App) -> Vec<SharedString> {
|
|
||||||
self.gossip.read(cx).read_only_relays(public_key)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a list of write relays for a given public key
|
|
||||||
pub fn write_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
|
|
||||||
let client = self.client();
|
|
||||||
let relays = self.gossip.read(cx).write_relays(public_key);
|
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
|
||||||
// Ensure relay connections
|
|
||||||
for url in relays.iter() {
|
|
||||||
client.add_relay(url).and_connect().await.ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
relays
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a list of read relays for a given public key
|
|
||||||
pub fn read_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
|
|
||||||
let client = self.client();
|
|
||||||
let relays = self.gossip.read(cx).read_relays(public_key);
|
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
|
||||||
// Ensure relay connections
|
|
||||||
for url in relays.iter() {
|
|
||||||
client.add_relay(url).and_connect().await.ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
relays
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set the connected status of the client
|
/// Set the connected status of the client
|
||||||
fn set_connected(&mut self, cx: &mut Context<Self>) {
|
fn set_connected(&mut self, cx: &mut Context<Self>) {
|
||||||
self.connected = true;
|
self.connected = true;
|
||||||
@@ -488,16 +395,24 @@ impl NostrRegistry {
|
|||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set relay list state
|
||||||
|
fn set_relay_list_state(&mut self, state: RelayState, cx: &mut Context<Self>) {
|
||||||
|
self.relay_list_state = state;
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ensure_relay_list(&mut self, cx: &mut Context<Self>) {
|
pub fn ensure_relay_list(&mut self, cx: &mut Context<Self>) {
|
||||||
let task = self.verify_relay_list(cx);
|
let task = self.verify_relay_list(cx);
|
||||||
|
|
||||||
|
// Reset state
|
||||||
|
self.set_relay_list_state(RelayState::default(), cx);
|
||||||
|
|
||||||
self.tasks.push(cx.spawn(async move |this, cx| {
|
self.tasks.push(cx.spawn(async move |this, cx| {
|
||||||
let result = task.await?;
|
let result = task.await?;
|
||||||
|
|
||||||
// Update state
|
// Update state
|
||||||
this.update(cx, |this, cx| {
|
this.update(cx, |this, cx| {
|
||||||
this.relay_list_state = result;
|
this.set_relay_list_state(result, cx);
|
||||||
cx.notify();
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user