improve relay auth
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m46s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 1m28s

This commit is contained in:
2026-02-09 15:11:37 +07:00
parent 6cce0d8bea
commit a9d2a0a24b
6 changed files with 142 additions and 120 deletions

View File

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