92 lines
3.0 KiB
Rust
92 lines
3.0 KiB
Rust
use axum::{extract::Extension, Json};
|
|
use tauri::Manager;
|
|
|
|
use crate::app_error::AppCommandError;
|
|
use crate::commands::system_settings as settings_commands;
|
|
use crate::db::service::app_metadata_service;
|
|
use crate::db::AppDatabase;
|
|
use crate::models::*;
|
|
use crate::network::proxy;
|
|
|
|
const SYSTEM_PROXY_SETTINGS_KEY: &str = "system_proxy_settings";
|
|
const SYSTEM_LANGUAGE_SETTINGS_KEY: &str = "system_language_settings";
|
|
const LANGUAGE_SETTINGS_UPDATED_EVENT: &str = "app://language-settings-updated";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Read handlers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
pub async fn get_system_proxy_settings(
|
|
Extension(app): Extension<tauri::AppHandle>,
|
|
) -> Result<Json<SystemProxySettings>, AppCommandError> {
|
|
let db = app.state::<AppDatabase>();
|
|
let settings = settings_commands::load_system_proxy_settings(&db.conn).await?;
|
|
Ok(Json(settings))
|
|
}
|
|
|
|
pub async fn get_system_language_settings(
|
|
Extension(app): Extension<tauri::AppHandle>,
|
|
) -> Result<Json<SystemLanguageSettings>, AppCommandError> {
|
|
let db = app.state::<AppDatabase>();
|
|
let settings =
|
|
settings_commands::load_system_language_settings(&db.conn).await?;
|
|
Ok(Json(settings))
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Update handlers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
pub async fn update_system_proxy_settings(
|
|
Extension(app): Extension<tauri::AppHandle>,
|
|
Json(settings): Json<SystemProxySettings>,
|
|
) -> Result<Json<SystemProxySettings>, AppCommandError> {
|
|
let db = app.state::<AppDatabase>();
|
|
|
|
// TODO: call normalize_proxy_settings once it is made pub(crate) in
|
|
// commands/system_settings.rs. For now the frontend validates the URL.
|
|
let serialized = serde_json::to_string(&settings).map_err(|e| {
|
|
AppCommandError::invalid_input("Failed to serialize proxy settings")
|
|
.with_detail(e.to_string())
|
|
})?;
|
|
|
|
app_metadata_service::upsert_value(
|
|
&db.conn,
|
|
SYSTEM_PROXY_SETTINGS_KEY,
|
|
&serialized,
|
|
)
|
|
.await
|
|
.map_err(AppCommandError::from)?;
|
|
|
|
proxy::apply_system_proxy_settings(&settings)?;
|
|
Ok(Json(settings))
|
|
}
|
|
|
|
pub async fn update_system_language_settings(
|
|
Extension(app): Extension<tauri::AppHandle>,
|
|
Json(settings): Json<SystemLanguageSettings>,
|
|
) -> Result<Json<SystemLanguageSettings>, AppCommandError> {
|
|
let db = app.state::<AppDatabase>();
|
|
|
|
let serialized = serde_json::to_string(&settings).map_err(|e| {
|
|
AppCommandError::invalid_input("Failed to serialize language settings")
|
|
.with_detail(e.to_string())
|
|
})?;
|
|
|
|
app_metadata_service::upsert_value(
|
|
&db.conn,
|
|
SYSTEM_LANGUAGE_SETTINGS_KEY,
|
|
&serialized,
|
|
)
|
|
.await
|
|
.map_err(AppCommandError::from)?;
|
|
|
|
crate::web::event_bridge::emit_event(
|
|
&app,
|
|
LANGUAGE_SETTINGS_UPDATED_EVENT,
|
|
settings.clone(),
|
|
);
|
|
|
|
Ok(Json(settings))
|
|
}
|