User requested a checkpoint and stopped all agents. Deployed artifacts remain the last verified releases; unfinished settings/categories/forums are preserved as WIP. See TODO/roadmap for verification and remaining release/updater work.
187 lines
6.3 KiB
Rust
187 lines
6.3 KiB
Rust
//! Successful server preference and account-gated restoration. Contains no credentials.
|
|
use super::state::ConnState;
|
|
use crate::{AppState, net::NetCommand};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
#[serde(default)]
|
|
pub struct Preferences {
|
|
last_server: Option<String>,
|
|
auto_reconnect: bool,
|
|
manually_disconnected: bool,
|
|
#[serde(skip)]
|
|
pending: Option<String>,
|
|
#[serde(skip)]
|
|
pub authentication_required: bool,
|
|
}
|
|
impl Default for Preferences {
|
|
fn default() -> Self {
|
|
Self {
|
|
last_server: None,
|
|
auto_reconnect: true,
|
|
manually_disconnected: false,
|
|
pending: None,
|
|
authentication_required: false,
|
|
}
|
|
}
|
|
}
|
|
impl Preferences {
|
|
fn restore_target(&self, authenticated: bool) -> Option<String> {
|
|
(authenticated && self.auto_reconnect && !self.manually_disconnected)
|
|
.then(|| self.last_server.clone())
|
|
.flatten()
|
|
}
|
|
fn successful(&mut self) {
|
|
if let Some(address) = self.pending.take() {
|
|
self.last_server = Some(address);
|
|
self.manually_disconnected = false;
|
|
}
|
|
}
|
|
}
|
|
fn path() -> std::path::PathBuf {
|
|
dirs::config_dir()
|
|
.unwrap_or_else(|| ".".into())
|
|
.join("vnox/connection.json")
|
|
}
|
|
pub fn load(ui: &mut super::state::UiState) {
|
|
ui.connection = std::fs::read(path())
|
|
.ok()
|
|
.and_then(|bytes| serde_json::from_slice::<Preferences>(&bytes).ok())
|
|
.unwrap_or_default();
|
|
// Never let an edited/corrupted file bypass the same target parser as transport.
|
|
ui.connection.last_server = ui
|
|
.connection
|
|
.last_server
|
|
.as_deref()
|
|
.and_then(|address| crate::net::trust::parse_address(address).ok())
|
|
.map(|a| a.key);
|
|
ui.auto_reconnect = ui.connection.auto_reconnect;
|
|
if let Some(address) = &ui.connection.last_server {
|
|
ui.connect_input = address.clone();
|
|
}
|
|
}
|
|
fn save(ui: &super::state::UiState) {
|
|
let result = (|| -> anyhow::Result<()> {
|
|
let path = path();
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent)?;
|
|
}
|
|
let mut prefs = ui.connection.clone();
|
|
prefs.auto_reconnect = ui.auto_reconnect;
|
|
let temporary = path.with_extension("json.tmp");
|
|
std::fs::write(&temporary, serde_json::to_vec_pretty(&prefs)?)?;
|
|
std::fs::rename(temporary, path)?;
|
|
Ok(())
|
|
})();
|
|
if let Err(error) = result {
|
|
tracing::warn!(%error, "could not save connection preferences");
|
|
}
|
|
}
|
|
pub fn set_auto_reconnect(state: &mut AppState, value: bool) {
|
|
state.ui.auto_reconnect = value;
|
|
state.ui.connection.auto_reconnect = value;
|
|
save(&state.ui);
|
|
}
|
|
pub fn disconnect(state: &mut AppState) {
|
|
state.ui.connection.manually_disconnected = true;
|
|
state.ui.connection.pending = None;
|
|
save(&state.ui);
|
|
}
|
|
pub fn connected(state: &mut AppState) {
|
|
state.ui.connection.successful();
|
|
save(&state.ui);
|
|
}
|
|
pub fn restore(state: &mut AppState, authenticated: bool) {
|
|
if !matches!(state.ui.conn, ConnState::Disconnected) {
|
|
return;
|
|
}
|
|
if let Some(address) = state.ui.connection.restore_target(authenticated) {
|
|
connect(state, address);
|
|
}
|
|
}
|
|
pub fn connect(state: &mut AppState, address: String) {
|
|
let address = match crate::net::trust::parse_address(&address) {
|
|
Ok(address) => address.key,
|
|
Err(error) => {
|
|
state.ui.status_msg = Some(error.to_string().into());
|
|
return;
|
|
}
|
|
};
|
|
state.ui.connection.pending = Some(address.clone());
|
|
state.ui.connection.authentication_required = false;
|
|
state.ui.conn = ConnState::Connecting;
|
|
state.ui.connect_input = address.clone();
|
|
state.ui.status_msg = Some("Connecting…".into());
|
|
let auto_reconnect = state.ui.auto_reconnect;
|
|
let net = state.net.clone();
|
|
tokio::spawn(async move {
|
|
net.send(NetCommand::Connect {
|
|
address,
|
|
auto_reconnect,
|
|
})
|
|
.await;
|
|
});
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn restoration_requires_account_and_auto_reconnect_and_no_manual_leave() {
|
|
let mut p = Preferences {
|
|
last_server: Some("example.org:7600".into()),
|
|
..Default::default()
|
|
};
|
|
assert!(p.restore_target(false).is_none());
|
|
assert_eq!(p.restore_target(true).as_deref(), Some("example.org:7600"));
|
|
p.auto_reconnect = false;
|
|
assert!(p.restore_target(true).is_none());
|
|
p.auto_reconnect = true;
|
|
p.manually_disconnected = true;
|
|
assert!(p.restore_target(true).is_none());
|
|
}
|
|
#[test]
|
|
fn only_successful_address_survives_serialization() {
|
|
let mut p = Preferences {
|
|
last_server: Some("old.example:7600".into()),
|
|
pending: Some("new.example:7600".into()),
|
|
..Default::default()
|
|
};
|
|
let loaded: Preferences = serde_json::from_slice(&serde_json::to_vec(&p).unwrap()).unwrap();
|
|
assert_eq!(loaded.last_server.as_deref(), Some("old.example:7600"));
|
|
assert!(loaded.pending.is_none());
|
|
p.successful();
|
|
let loaded: Preferences = serde_json::from_slice(&serde_json::to_vec(&p).unwrap()).unwrap();
|
|
assert_eq!(loaded.last_server.as_deref(), Some("new.example:7600"));
|
|
}
|
|
#[test]
|
|
fn successful_target_uses_transport_normalization() {
|
|
assert_eq!(
|
|
crate::net::trust::parse_address("lnexs://Example.ORG")
|
|
.unwrap()
|
|
.key,
|
|
"example.org:7600"
|
|
);
|
|
assert_eq!(
|
|
crate::net::trust::parse_address("[::1]").unwrap().key,
|
|
"[::1]:7600"
|
|
);
|
|
assert!(crate::net::trust::parse_address("example.org:invalid").is_err());
|
|
}
|
|
#[test]
|
|
fn manual_leave_survives_restart_and_failed_attempt() {
|
|
let mut p = Preferences {
|
|
last_server: Some("example.org:7600".into()),
|
|
manually_disconnected: true,
|
|
pending: Some("failed.example:7600".into()),
|
|
..Default::default()
|
|
};
|
|
let loaded: Preferences = serde_json::from_slice(&serde_json::to_vec(&p).unwrap()).unwrap();
|
|
assert!(loaded.restore_target(true).is_none());
|
|
p.successful();
|
|
assert_eq!(
|
|
p.restore_target(true).as_deref(),
|
|
Some("failed.example:7600")
|
|
);
|
|
}
|
|
}
|