voelin-ui-vm: the server sequences both front ends ran twice are one
The GPUI shell repeated the Slint crate's connect, observe, gateway lookup, save and remove of a server, its default nickname, its FallbackSecrets and its settings opening and key registration. They now live once and both front ends call them: - voelin_ui_vm::servers::sequence: connect_voice(_to), observe, discover_gateway, gateway_found, select_server, save_bookmark, save_and_connect, delete_bookmark, bookmark_form and default_nickname, as transitions over the state, the Cx and a Backing (the store, the secrets, the identity a connection uses). The commands, the status line and what to keep go out through the Update as for every other transition; a new field, Update::discover, asks the front end to look a gateway up (the thread hop stays with each front end: Later in GPUI, later() in Slint). stream_peer is the one input a front end gives (Slint's codecs; GPUI passes none). - voelin_ui_vm::settings::open_settings and register_keys; a front end adds its own key (ui.window, studio.ui) through a function it passes, so Slint's settings page still lists what it listed. - voelin_store::FallbackSecrets (feature keyring) and Store::identity_for. - voelin-ui: the methods of servers/bind.rs, open_settings and default_secrets call them, so their call sites are unchanged; host.rs gets transition_with and the discover arm of perform. voelin-gpui: AppState's methods and the Backend call them. What cannot move is named in docs/ui.md. voelin-ui-vm now depends on tracing (the sequences log as the copies did): one line of Cargo.lock, THIRD_PARTY_NOTICES.md unchanged (scripts/notices.sh --check clean), cargo deny check clean. The sequences have ten tests over a fake front end (commands, status, store, secrets, gateways), open_settings and Store::identity_for one each; the GPUI and Slint suites pass unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
01bf128c58
commit
a77ccc0415
15 files changed
+1148
-578
No files matched your search
@@ -27,7 +27,7 @@ pub use chat::{
|
||||
pub use contacts::{Contact, ContactSeen, Relation};
|
||||
pub use keystore::SqliteKeyStore;
|
||||
#[cfg(feature = "keyring")]
|
||||
pub use secrets::KeyringSecrets;
|
||||
pub use secrets::{FallbackSecrets, KeyringSecrets};
|
||||
pub use secrets::{MemorySecrets, Secrets};
|
||||
pub use store::{
|
||||
Bookmark, CachedServerIcon, IdentityEntry, IdentityOrigin, QueryConfig, QueryTransport, Store,
|
||||
|
||||
@@ -74,6 +74,47 @@ impl Secrets for KeyringSecrets {
|
||||
}
|
||||
}
|
||||
|
||||
/// The keyring first, memory when the keyring cannot keep a secret (no Secret
|
||||
/// Service on a headless Linux): what the desktop front ends keep the servers'
|
||||
/// passwords in. A secret the keyring refused is kept for this run only.
|
||||
#[cfg(feature = "keyring")]
|
||||
pub struct FallbackSecrets {
|
||||
keyring: KeyringSecrets,
|
||||
memory: MemorySecrets,
|
||||
}
|
||||
|
||||
#[cfg(feature = "keyring")]
|
||||
impl FallbackSecrets {
|
||||
/// Secrets in the keyring entries of `service` (the app's name), with
|
||||
/// memory behind them.
|
||||
pub fn new(service: impl Into<String>) -> Self {
|
||||
Self { keyring: KeyringSecrets::new(service), memory: MemorySecrets::default() }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "keyring")]
|
||||
impl Secrets for FallbackSecrets {
|
||||
fn get(&self, key: &str) -> Result<Option<String>> {
|
||||
match self.memory.get(key)? {
|
||||
Some(v) => Ok(Some(v)),
|
||||
None => Ok(self.keyring.get(key).unwrap_or(None)),
|
||||
}
|
||||
}
|
||||
|
||||
fn set(&self, key: &str, value: &str) -> Result<()> {
|
||||
if let Err(error) = self.keyring.set(key, value) {
|
||||
tracing::warn!(%error, "keyring unavailable, keeping the secret for this session only");
|
||||
return self.memory.set(key, value);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete(&self, key: &str) -> Result<()> {
|
||||
let _ = self.keyring.delete(key);
|
||||
self.memory.delete(key)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -407,6 +407,24 @@ impl Store {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The identity entry a connection uses: `named` (the identity a
|
||||
/// bookmark names) if the store still has it, else the default identity's.
|
||||
/// None when the store has no identity.
|
||||
pub fn identity_id_for(&self, named: Option<i64>) -> Option<i64> {
|
||||
let entries = self.identities().unwrap_or_default();
|
||||
named
|
||||
.filter(|id| entries.iter().any(|e| e.id == *id))
|
||||
.or_else(|| entries.iter().find(|e| e.is_default).or(entries.first()).map(|e| e.id))
|
||||
}
|
||||
|
||||
/// The identity a connection uses ([`Store::identity_id_for`]); `fallback`
|
||||
/// when the store has none it can read.
|
||||
pub fn identity_for(&self, named: Option<i64>, fallback: &Identity) -> Identity {
|
||||
self.identity_id_for(named)
|
||||
.and_then(|id| self.identity(id).ok())
|
||||
.unwrap_or_else(|| fallback.clone())
|
||||
}
|
||||
|
||||
pub fn identity(&self, id: i64) -> Result<Identity> {
|
||||
let data: Option<String> = self
|
||||
.db
|
||||
@@ -718,6 +736,28 @@ mod tests {
|
||||
assert_eq!(store.default_identity().unwrap().unwrap().id, made);
|
||||
}
|
||||
|
||||
/// A bookmark's own identity if the store has it, else the default one,
|
||||
/// else the fallback.
|
||||
#[test]
|
||||
fn a_connection_uses_the_named_identity_else_the_default_else_the_fallback() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let uid = |identity: &Identity| identity.key().to_pub().get_uid();
|
||||
let fallback = Identity::create();
|
||||
assert_eq!(store.identity_id_for(Some(1)), None);
|
||||
assert_eq!(uid(&store.identity_for(None, &fallback)), uid(&fallback), "no identity at all");
|
||||
let (default, alt) = (Identity::create(), Identity::create());
|
||||
let default_id = store.add_identity("Default", &default, IdentityOrigin::Created).unwrap();
|
||||
let alt_id = store.add_identity("Alt", &alt, IdentityOrigin::Created).unwrap();
|
||||
assert_eq!(uid(&store.identity_for(None, &fallback)), uid(&default), "the default");
|
||||
assert_eq!(store.identity_id_for(Some(alt_id)), Some(alt_id));
|
||||
assert_eq!(uid(&store.identity_for(Some(alt_id), &fallback)), uid(&alt), "the named one");
|
||||
let gone = alt_id + 100;
|
||||
assert_eq!(store.identity_id_for(Some(gone)), Some(default_id), "one that is gone");
|
||||
assert_eq!(uid(&store.identity_for(Some(gone), &fallback)), uid(&default));
|
||||
store.set_default_identity(alt_id, false).unwrap();
|
||||
assert_eq!(store.identity_id_for(None), Some(alt_id), "the default moved");
|
||||
}
|
||||
|
||||
/// A database of version 3 learns where its identities came from: the
|
||||
/// first one, named `Default`, is the one the app made; the others were
|
||||
/// imported; the first stays the default.
|
||||
|
||||
Reference in new issue
Block a user