Continue redesign for the v1 stable release #5

Merged
reya merged 11 commits from redesign-2 into master 2026-02-12 08:32:17 +00:00
6 changed files with 142 additions and 120 deletions
Showing only changes of commit a9d2a0a24b - Show all commits

View File

@@ -190,11 +190,7 @@ impl CommandBar {
// Block the input until the search completes // Block the input until the search completes
self.set_finding(true, window, cx); self.set_finding(true, window, cx);
let find_users = if nostr.read(cx).owned_signer() { let find_users = nostr.read(cx).search(&query, cx);
nostr.read(cx).wot_search(&query, cx)
} else {
nostr.read(cx).search(&query, cx)
};
// Run task in the main thread // Run task in the main thread
self.find_task = Some(cx.spawn_in(window, async move |this, cx| { self.find_task = Some(cx.spawn_in(window, async move |this, cx| {

View File

@@ -250,6 +250,8 @@ impl DeviceRegistry {
*this = Some(Arc::new(signer)); *this = Some(Arc::new(signer));
cx.notify(); cx.notify();
}); });
log::info!("Device Signer set");
} }
/// Set the device state /// Set the device state
@@ -308,7 +310,7 @@ impl DeviceRegistry {
Ok(()) Ok(())
}); });
task.detach(); task.detach_and_log_err(cx);
} }
/// Get device announcement for current user /// Get device announcement for current user

View File

@@ -3,6 +3,7 @@ use std::cell::Cell;
use std::collections::HashSet; use std::collections::HashSet;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc;
use anyhow::{anyhow, Context as AnyhowContext, Error}; use anyhow::{anyhow, Context as AnyhowContext, Error};
use gpui::{ use gpui::{
@@ -28,8 +29,8 @@ pub fn init(window: &mut Window, cx: &mut App) {
/// Authentication request /// Authentication request
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AuthRequest { pub struct AuthRequest {
pub url: RelayUrl, url: RelayUrl,
pub challenge: String, challenge: String,
} }
impl Hash for AuthRequest { impl Hash for AuthRequest {
@@ -45,6 +46,14 @@ impl AuthRequest {
url, url,
} }
} }
pub fn url(&self) -> &RelayUrl {
&self.url
}
pub fn challenge(&self) -> &str {
&self.challenge
}
} }
struct GlobalRelayAuth(Entity<RelayAuth>); struct GlobalRelayAuth(Entity<RelayAuth>);
@@ -55,7 +64,7 @@ impl Global for GlobalRelayAuth {}
#[derive(Debug)] #[derive(Debug)]
pub struct RelayAuth { pub struct RelayAuth {
/// Entity for managing auth requests /// Entity for managing auth requests
requests: HashSet<AuthRequest>, requests: HashSet<Arc<AuthRequest>>,
/// Event subscriptions /// Event subscriptions
_subscriptions: SmallVec<[Subscription; 1]>, _subscriptions: SmallVec<[Subscription; 1]>,
@@ -91,14 +100,14 @@ impl RelayAuth {
subscriptions.push( subscriptions.push(
// Observe the current state // Observe the current state
cx.observe_in(&entity, window, |this, _, window, cx| { cx.observe_in(&entity, window, |this, _state, window, cx| {
let settings = AppSettings::global(cx); let settings = AppSettings::global(cx);
let mode = AppSettings::get_auth_mode(cx); let mode = AppSettings::get_auth_mode(cx);
for req in this.requests.clone().into_iter() { for req in this.requests.iter() {
let is_trusted_relay = settings.read(cx).is_trusted_relay(&req.url, cx); let trusted_relay = settings.read(cx).trusted_relay(req.url(), cx);
if is_trusted_relay && mode == AuthMode::Auto { if trusted_relay && mode == AuthMode::Auto {
// Automatically authenticate if the relay is authenticated before // Automatically authenticate if the relay is authenticated before
this.response(req, window, cx); this.response(req, window, cx);
} else { } else {
@@ -111,7 +120,9 @@ impl RelayAuth {
tasks.push( tasks.push(
// Handle nostr notifications // Handle nostr notifications
cx.background_spawn(async move { Self::handle_notifications(&client, &tx).await }), cx.background_spawn(async move {
Self::handle_notifications(&client, &tx).await;
}),
); );
tasks.push( tasks.push(
@@ -136,16 +147,16 @@ impl RelayAuth {
// Handle nostr notifications // Handle nostr notifications
async fn handle_notifications(client: &Client, tx: &flume::Sender<AuthRequest>) { async fn handle_notifications(client: &Client, tx: &flume::Sender<AuthRequest>) {
let mut notifications = client.notifications(); let mut notifications = client.notifications();
let mut challenges: HashSet<Cow<'_, str>> = HashSet::default();
while let Some(notification) = notifications.next().await { while let Some(notification) = notifications.next().await {
match notification { match notification {
ClientNotification::Message { relay_url, message } => { ClientNotification::Message { relay_url, message } => {
match message { match message {
RelayMessage::Auth { challenge } => { RelayMessage::Auth { challenge } => {
let request = AuthRequest::new(challenge, relay_url); if challenges.insert(challenge.clone()) {
let request = AuthRequest::new(challenge, relay_url);
if let Err(e) = tx.send_async(request).await { tx.send_async(request).await.ok();
log::error!("Failed to send auth request: {}", e);
} }
} }
RelayMessage::Ok { RelayMessage::Ok {
@@ -174,7 +185,7 @@ impl RelayAuth {
/// Add a new authentication request. /// Add a new authentication request.
fn add_request(&mut self, request: AuthRequest, cx: &mut Context<Self>) { fn add_request(&mut self, request: AuthRequest, cx: &mut Context<Self>) {
self.requests.insert(request); self.requests.insert(Arc::new(request));
cx.notify(); cx.notify();
} }
@@ -185,35 +196,34 @@ impl RelayAuth {
/// Reask for approval for all pending requests. /// Reask for approval for all pending requests.
pub fn re_ask(&mut self, window: &mut Window, cx: &mut Context<Self>) { pub fn re_ask(&mut self, window: &mut Window, cx: &mut Context<Self>) {
for request in self.requests.clone().into_iter() { for request in self.requests.iter() {
self.ask_for_approval(request, window, cx); self.ask_for_approval(request, window, cx);
} }
} }
/// Respond to an authentication request. /// Respond to an authentication request.
fn response(&mut self, req: AuthRequest, window: &mut Window, cx: &mut Context<Self>) { fn response(&self, req: &Arc<AuthRequest>, window: &Window, cx: &Context<Self>) {
let settings = AppSettings::global(cx); let settings = AppSettings::global(cx);
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let challenge = req.challenge.to_owned(); let req = req.clone();
let url = req.url.to_owned(); let challenge = req.challenge().to_string();
let async_req = req.clone();
let challenge_clone = challenge.clone();
let url_clone = url.clone();
let task: Task<Result<(), Error>> = cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
// Construct event // Construct event
let builder = EventBuilder::auth(challenge_clone, url_clone.clone()); let builder = EventBuilder::auth(async_req.challenge(), async_req.url().clone());
let event = client.sign_event_builder(builder).await?; let event = client.sign_event_builder(builder).await?;
// Get the event ID // Get the event ID
let id = event.id; let id = event.id;
// Get the relay // Get the relay
let relay = client.relay(url_clone).await?.context("Relay not found")?; let relay = client
let relay_url = relay.url(); .relay(async_req.url())
.await?
.context("Relay not found")?;
// Subscribe to notifications // Subscribe to notifications
let mut notifications = relay.notifications(); let mut notifications = relay.notifications();
@@ -234,7 +244,7 @@ impl RelayAuth {
// Get all pending events that need to be resent // Get all pending events that need to be resent
let mut tracker = tracker().write().await; let mut tracker = tracker().write().await;
let ids: Vec<EventId> = tracker.pending_resend(relay_url); let ids: Vec<EventId> = tracker.pending_resend(relay.url());
for id in ids.into_iter() { for id in ids.into_iter() {
if let Some(event) = client.database().event_by_id(&id).await? { if let Some(event) = client.database().event_by_id(&id).await? {
@@ -254,47 +264,56 @@ impl RelayAuth {
Err(anyhow!("Authentication failed")) Err(anyhow!("Authentication failed"))
}); });
self._tasks.push( cx.spawn_in(window, async move |this, cx| {
// Handle response in the background let result = task.await;
cx.spawn_in(window, async move |this, cx| { let url = req.url();
match task.await {
this.update_in(cx, |this, window, cx| {
match result {
Ok(_) => { Ok(_) => {
this.update_in(cx, |this, window, cx| { window.clear_notification(challenge, cx);
// Clear the current notification window.push_notification(format!("{} has been authenticated", url), cx);
window.clear_notification(challenge, cx);
// Push a new notification // Save the authenticated relay to automatically authenticate future requests
window.push_notification(format!("{url} has been authenticated"), cx); settings.update(cx, |this, cx| {
this.add_trusted_relay(url, cx);
});
// Save the authenticated relay to automatically authenticate future requests // Remove the challenge from the list of pending authentications
settings.update(cx, |this, cx| { this.requests.remove(&req);
this.add_trusted_relay(url, cx); cx.notify();
});
// Remove the challenge from the list of pending authentications
this.requests.remove(&req);
cx.notify();
})
.expect("Entity has been released");
} }
Err(e) => { Err(e) => {
this.update_in(cx, |_, window, cx| { window.push_notification(Notification::error(e.to_string()), cx);
window.push_notification(Notification::error(e.to_string()), cx);
})
.expect("Entity has been released");
} }
}; }
}), })
); .ok();
})
.detach();
} }
/// Push a popup to approve the authentication request. /// Push a popup to approve the authentication request.
fn ask_for_approval(&mut self, req: AuthRequest, window: &mut Window, cx: &mut Context<Self>) { fn ask_for_approval(&self, req: &Arc<AuthRequest>, window: &Window, cx: &Context<Self>) {
let url = SharedString::from(req.url.clone().to_string()); let notification = self.notification(req, cx);
cx.spawn_in(window, async move |_this, cx| {
cx.update(|window, cx| {
window.push_notification(notification, cx);
})
.ok();
})
.detach();
}
/// Build a notification for the authentication request.
fn notification(&self, req: &Arc<AuthRequest>, cx: &Context<Self>) -> Notification {
let req = req.clone();
let url = SharedString::from(req.url().to_string());
let entity = cx.entity().downgrade(); let entity = cx.entity().downgrade();
let loading = Rc::new(Cell::new(false)); let loading = Rc::new(Cell::new(false));
let note = Notification::new() Notification::new()
.custom_id(SharedString::from(&req.challenge)) .custom_id(SharedString::from(&req.challenge))
.autohide(false) .autohide(false)
.icon(IconName::Info) .icon(IconName::Info)
@@ -317,7 +336,7 @@ impl RelayAuth {
.into_any_element() .into_any_element()
}) })
.action(move |_window, _cx| { .action(move |_window, _cx| {
let entity = entity.clone(); let view = entity.clone();
let req = req.clone(); let req = req.clone();
Button::new("approve") Button::new("approve")
@@ -328,24 +347,18 @@ impl RelayAuth {
.disabled(loading.get()) .disabled(loading.get())
.on_click({ .on_click({
let loading = Rc::clone(&loading); let loading = Rc::clone(&loading);
move |_ev, window, cx| { move |_ev, window, cx| {
// Set loading state to true // Set loading state to true
loading.set(true); loading.set(true);
// Process to approve the request // Process to approve the request
entity view.update(cx, |this, cx| {
.update(cx, |this, cx| { this.response(&req, window, cx);
this.response(req.clone(), window, cx); })
}) .ok();
.ok();
} }
}) })
}); })
// Push the notification to the current window
window.push_notification(note, cx);
// Bring the window to the front
cx.activate(true);
} }
} }

View File

@@ -47,8 +47,8 @@ setting_accessors! {
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum AuthMode { pub enum AuthMode {
#[default] #[default]
Manual,
Auto, Auto,
Manual,
} }
/// Signer kind /// Signer kind
@@ -121,7 +121,7 @@ pub struct AppSettings {
_subscriptions: SmallVec<[Subscription; 1]>, _subscriptions: SmallVec<[Subscription; 1]>,
/// Background tasks /// Background tasks
tasks: SmallVec<[Task<()>; 1]>, tasks: SmallVec<[Task<Result<(), Error>>; 1]>,
} }
impl AppSettings { impl AppSettings {
@@ -151,13 +151,15 @@ impl AppSettings {
tasks.push( tasks.push(
// Load the initial settings // Load the initial settings
cx.spawn(async move |this, cx| { cx.spawn(async move |this, cx| {
if let Ok(settings) = load_settings.await { let settings = load_settings.await.unwrap_or(Settings::default());
this.update(cx, |this, cx| { log::info!("Settings: {settings:?}");
this.values = settings;
cx.notify(); // Update the settings state
}) this.update(cx, |this, cx| {
.ok(); this.set_settings(settings, cx);
} })?;
Ok(())
}), }),
); );
@@ -168,6 +170,12 @@ impl AppSettings {
} }
} }
/// Update settings
fn set_settings(&mut self, settings: Settings, cx: &mut Context<Self>) {
self.values = settings;
cx.notify();
}
/// Get settings from the database /// Get settings from the database
fn get_from_database(cx: &App) -> Task<Result<Settings, Error>> { fn get_from_database(cx: &App) -> Task<Result<Settings, Error>> {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
@@ -189,7 +197,7 @@ impl AppSettings {
} }
if let Some(event) = client.database().query(filter).await?.first_owned() { if let Some(event) = client.database().query(filter).await?.first_owned() {
Ok(serde_json::from_str(&event.content).unwrap_or(Settings::default())) Ok(serde_json::from_str(&event.content)?)
} else { } else {
Err(anyhow!("Not found")) Err(anyhow!("Not found"))
} }
@@ -203,13 +211,13 @@ impl AppSettings {
self.tasks.push( self.tasks.push(
// Run task in the background // Run task in the background
cx.spawn(async move |this, cx| { cx.spawn(async move |this, cx| {
if let Ok(settings) = task.await { let settings = task.await?;
this.update(cx, |this, cx| { // Update settings
this.values = settings; this.update(cx, |this, cx| {
cx.notify(); this.set_settings(settings, cx);
}) })?;
.ok();
} Ok(())
}), }),
); );
} }
@@ -218,36 +226,37 @@ impl AppSettings {
pub fn save(&mut self, cx: &mut Context<Self>) { pub fn save(&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 settings = self.values.clone();
if let Ok(content) = serde_json::to_string(&self.values) { self.tasks.push(cx.background_spawn(async move {
let task: Task<Result<(), Error>> = cx.background_spawn(async move { 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?; let content = serde_json::to_string(&settings)?;
let event = EventBuilder::new(Kind::ApplicationSpecificData, content) let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tag(Tag::identifier(SETTINGS_IDENTIFIER)) .tag(Tag::identifier(SETTINGS_IDENTIFIER))
.build(public_key) .build(public_key)
.sign(&Keys::generate()) .sign(&Keys::generate())
.await?; .await?;
// Save event to the local database without sending to relays // Save event to the local database only
client.database().save_event(&event).await?; client.database().save_event(&event).await?;
log::info!("Settings saved successfully");
Ok(()) Ok(())
}); }));
task.detach();
}
} }
/// Check if the given relay is trusted /// Check if the given relay is already authenticated
pub fn is_trusted_relay(&self, url: &RelayUrl, _cx: &App) -> bool { pub fn trusted_relay(&self, url: &RelayUrl, _cx: &App) -> bool {
self.values.trusted_relays.contains(url) self.values.trusted_relays.iter().any(|relay| {
relay.as_str_without_trailing_slash() == url.as_str_without_trailing_slash()
})
} }
/// Add a relay to the trusted list /// Add a relay to the trusted list
pub fn add_trusted_relay(&mut self, url: RelayUrl, cx: &mut Context<Self>) { pub fn add_trusted_relay(&mut self, url: &RelayUrl, cx: &mut Context<Self>) {
self.values.trusted_relays.insert(url); self.values.trusted_relays.insert(url.clone());
cx.notify(); cx.notify();
} }

View File

@@ -10,7 +10,7 @@ pub const COOP_PUBKEY: &str = "npub126kl5fruqan90py77gf6pvfvygefl2mu2ukew6xdx5pc
pub const APP_ID: &str = "su.reya.coop"; pub const APP_ID: &str = "su.reya.coop";
/// Keyring name /// Keyring name
pub const KEYRING: &str = "Coop Secret Storage"; pub const KEYRING: &str = "Coop Safe Storage";
/// Default timeout for subscription /// Default timeout for subscription
pub const TIMEOUT: u64 = 3; pub const TIMEOUT: u64 = 3;
@@ -37,7 +37,7 @@ 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; 2] = ["wss://antiprimal.net", "wss://relay.noswhere.com"];
/// Default bootstrap relays /// Default bootstrap relays
pub const BOOTSTRAP_RELAYS: [&str; 4] = [ pub const BOOTSTRAP_RELAYS: [&str; 4] = [

View File

@@ -164,11 +164,6 @@ impl NostrRegistry {
client.add_relay(url).await?; client.add_relay(url).await?;
} }
// Add wot relay to the relay pool
for url in WOT_RELAYS.into_iter() {
client.add_relay(url).await?;
}
// Connect to all added relays // Connect to all added relays
client.connect().await; client.connect().await;
@@ -261,14 +256,21 @@ impl NostrRegistry {
.author(public_key) .author(public_key)
.limit(1); .limit(1);
client let relays: Vec<RelayUrl> = client
.database() .database()
.query(filter) .query(filter)
.await .await
.ok() .ok()
.and_then(|events| events.first_owned()) .and_then(|events| events.first_owned())
.map(|event| nip17::extract_owned_relay_list(event).collect()) .map(|event| nip17::extract_owned_relay_list(event).collect())
.unwrap_or_default() .unwrap_or_default();
for relay in relays.iter() {
client.add_relay(relay).await.ok();
client.connect_relay(relay).await.ok();
}
relays
}) })
} }