refactor auth
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m48s

This commit is contained in:
2026-02-13 18:31:29 +07:00
parent 7e4d42b967
commit 911b841918
2 changed files with 95 additions and 142 deletions

View File

@@ -118,10 +118,7 @@ pub struct AppSettings {
values: Settings,
/// Event subscriptions
_subscriptions: SmallVec<[Subscription; 1]>,
/// Background tasks
tasks: SmallVec<[Task<Result<(), Error>>; 1]>,
_subscriptions: SmallVec<[Subscription; 2]>,
}
impl AppSettings {
@@ -136,9 +133,7 @@ impl AppSettings {
}
fn new(cx: &mut Context<Self>) -> Self {
let load_settings = Self::get_from_database(cx);
let mut tasks = smallvec![];
let nostr = NostrRegistry::global(cx);
let mut subscriptions = smallvec![];
subscriptions.push(
@@ -148,24 +143,15 @@ impl AppSettings {
}),
);
tasks.push(
// Load the initial settings
cx.spawn(async move |this, cx| {
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(())
subscriptions.push(
// Observe and automatically save settings on changes
cx.observe(&nostr, |this, _state, cx| {
this.load(cx);
}),
);
Self {
values: Settings::default(),
tasks,
_subscriptions: subscriptions,
}
}
@@ -176,50 +162,38 @@ impl AppSettings {
cx.notify();
}
/// Get settings from the database
fn get_from_database(cx: &App) -> Task<Result<Settings, Error>> {
/// Load settings
fn load(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
cx.background_spawn(async move {
// Construct a filter to get the latest settings
let mut filter = Filter::new()
let task: Task<Result<Settings, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(SETTINGS_IDENTIFIER)
.author(public_key)
.limit(1);
// If the signer is available, get settings belonging to the current user
if let Some(signer) = client.signer() {
if let Ok(public_key) = signer.get_public_key().await {
// Push author to the filter
filter = filter.author(public_key);
}
}
if let Some(event) = client.database().query(filter).await?.first_owned() {
if let Some(event) = client.database().query(filter).await?.last_owned() {
Ok(serde_json::from_str(&event.content)?)
} else {
Err(anyhow!("Not found"))
}
});
cx.spawn(async move |this, cx| {
let settings = task.await.unwrap_or(Settings::default());
// Update settings
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})
.ok();
})
}
/// Load settings
pub fn load(&mut self, cx: &mut Context<Self>) {
let task = Self::get_from_database(cx);
self.tasks.push(
// Run task in the background
cx.spawn(async move |this, cx| {
let settings = task.await?;
// Update settings
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})?;
Ok(())
}),
);
.detach();
}
/// Save settings
@@ -228,7 +202,7 @@ impl AppSettings {
let client = nostr.read(cx).client();
let settings = self.values.clone();
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 public_key = signer.get_public_key().await?;
let content = serde_json::to_string(&settings)?;
@@ -243,7 +217,9 @@ impl AppSettings {
client.database().save_event(&event).await?;
Ok(())
}));
});
task.detach();
}
/// Check if the given relay is already authenticated