refactor settings
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m24s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 1m34s

This commit is contained in:
2026-02-14 09:00:05 +07:00
parent 911b841918
commit e921725df9
8 changed files with 541 additions and 454 deletions

View File

@@ -5,10 +5,11 @@ edition.workspace = true
publish.workspace = true
[dependencies]
state = { path = "../state" }
common = { path = "../common" }
nostr-sdk.workspace = true
gpui.workspace = true
smol.workspace = true
anyhow.workspace = true
log.workspace = true
smallvec.workspace = true

View File

@@ -1,13 +1,11 @@
use std::collections::{HashMap, HashSet};
use anyhow::{anyhow, Context as AnyhowContext, Error};
use anyhow::{anyhow, Error};
use common::config_dir;
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec};
use state::NostrRegistry;
const SETTINGS_IDENTIFIER: &str = "coop:settings";
pub fn init(cx: &mut App) {
AppSettings::set_global(cx.new(AppSettings::new), cx)
@@ -133,7 +131,6 @@ impl AppSettings {
}
fn new(cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx);
let mut subscriptions = smallvec![];
subscriptions.push(
@@ -143,12 +140,13 @@ impl AppSettings {
}),
);
subscriptions.push(
// Observe and automatically save settings on changes
cx.observe(&nostr, |this, _state, cx| {
cx.defer(|cx| {
let settings = AppSettings::global(cx);
settings.update(cx, |this, cx| {
this.load(cx);
}),
);
});
});
Self {
values: Settings::default(),
@@ -164,21 +162,11 @@ impl AppSettings {
/// Load settings
fn load(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
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 path = config_dir().join(".settings");
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(SETTINGS_IDENTIFIER)
.author(public_key)
.limit(1);
if let Some(event) = client.database().query(filter).await?.last_owned() {
Ok(serde_json::from_str(&event.content)?)
if let Ok(content) = smol::fs::read_to_string(&path).await {
Ok(serde_json::from_str(&content)?)
} else {
Err(anyhow!("Not found"))
}
@@ -198,23 +186,14 @@ impl AppSettings {
/// Save settings
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();
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 path = config_dir().join(".settings");
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?;
// Save event to the local database only
client.database().save_event(&event).await?;
// Write settings to file
smol::fs::write(&path, content).await?;
Ok(())
});