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
@@ -3,273 +3,116 @@
|
||||
//! the channel tree, and the callbacks' ways into the transitions of
|
||||
//! `voelin_ui_vm::servers::logic`.
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
use slint::ComponentHandle;
|
||||
use tracing::{debug, info, warn};
|
||||
use voelin_core::{Command, JoinFailure, ObserveState};
|
||||
use voelin_core::JoinFailure;
|
||||
use voelin_core::stream::PeerConfig;
|
||||
use voelin_model::ChannelId;
|
||||
use voelin_store::{Bookmark, QueryTransport};
|
||||
use voelin_store::Bookmark;
|
||||
|
||||
use crate::app::{App, Bridge, later};
|
||||
use crate::bind::convert;
|
||||
use crate::vm;
|
||||
use crate::vm::bridge::BookmarkForm;
|
||||
use crate::vm::servers::logic::{self as logic, GatewayFound, ObservePlan};
|
||||
use crate::vm::servers::logic;
|
||||
use crate::vm::servers::sequence;
|
||||
|
||||
impl App {
|
||||
/// Connect the server on screen with voice, into its default channel
|
||||
/// ([`sequence::connect_voice`]).
|
||||
pub(crate) fn connect_voice(&mut self) {
|
||||
let channel = self
|
||||
.state
|
||||
.current
|
||||
.and_then(|id| self.bookmark(id))
|
||||
.and_then(|b| b.default_channel.clone());
|
||||
self.connect_voice_to(channel, None, None);
|
||||
let peer = self.stream_peer();
|
||||
self.transition_with(|state, cx, backing| {
|
||||
sequence::connect_voice(state, cx, backing, Some(peer))
|
||||
});
|
||||
}
|
||||
|
||||
/// Connect the current server with voice, into `channel` (a path, or
|
||||
/// `/<id>`, with its password) if given, with a privilege key if given;
|
||||
/// whether it was asked for.
|
||||
/// whether it was asked for ([`sequence::connect_voice_to`]).
|
||||
pub(crate) fn connect_voice_to(
|
||||
&mut self,
|
||||
channel: Option<String>,
|
||||
channel_password: Option<String>,
|
||||
token: Option<String>,
|
||||
) -> bool {
|
||||
let Some(b) = self.state.current.and_then(|id| self.bookmark(id)).cloned() else {
|
||||
return false;
|
||||
};
|
||||
if self.demo_ui {
|
||||
return false;
|
||||
}
|
||||
let mut options = match logic::voice_options(&b, channel, channel_password, token) {
|
||||
Ok(options) => options,
|
||||
Err(error) => {
|
||||
self.set_status(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
options.identity = Some(voelin_core::ts::identity::to_blob(&self.identity_for(Some(b.id))));
|
||||
options.server_password = self.secrets.get(&b.server_password_key()).ok().flatten();
|
||||
options.stream_peer = self.video.peer_config(options.stream_peer);
|
||||
self.engine
|
||||
.send(Command::ConnectVoice { session: b.id as u64, options: Box::new(options) });
|
||||
self.set_status(format!("Connecting to {}…", b.address));
|
||||
// The admin may have published the gateway since it was added.
|
||||
self.discover_gateway(b.id);
|
||||
true
|
||||
let peer = self.stream_peer();
|
||||
self.transition_with(|state, cx, backing| {
|
||||
sequence::connect_voice_to(
|
||||
state,
|
||||
cx,
|
||||
backing,
|
||||
channel,
|
||||
channel_password,
|
||||
token,
|
||||
Some(peer),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// The nickname a new server gets: the default identity's (one imported
|
||||
/// from TeamSpeak keeps the nickname used there), else the system user's.
|
||||
/// What this front end can decode of a stream, to connect with: the
|
||||
/// codecs of the machine, the configured one first.
|
||||
fn stream_peer(&self) -> PeerConfig {
|
||||
self.video.peer_config(PeerConfig::default())
|
||||
}
|
||||
|
||||
/// The nickname a new server gets ([`sequence::default_nickname`]).
|
||||
pub(crate) fn default_nickname(&self) -> String {
|
||||
self.store
|
||||
.default_identity()
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|identity| identity.name)
|
||||
.filter(|name| {
|
||||
![logic::CREATED_IDENTITY, voelin_core::ts::identity::DEFAULT_NICKNAME]
|
||||
.contains(&name.as_str())
|
||||
})
|
||||
.or_else(|| std::env::var("USER").or_else(|_| std::env::var("USERNAME")).ok())
|
||||
// Servers refuse an empty nickname (Android has no user name).
|
||||
.filter(|name| !name.trim().is_empty())
|
||||
.unwrap_or_else(|| "Voelin user".to_owned())
|
||||
sequence::default_nickname(&self.store)
|
||||
}
|
||||
|
||||
/// Look up the gateway of a server ([`voelin_core::discover`]), once a
|
||||
/// run ([`logic::discover_wanted`]): every one the server publishes is
|
||||
/// kept, best first, and tried in turn; they take the place of a stored
|
||||
/// one that is no longer published (a gateway that moved, or one typed
|
||||
/// into an older version, which showed the field). Nothing found keeps
|
||||
/// what is stored.
|
||||
pub(crate) fn discover_gateway(&mut self, id: i64) {
|
||||
let Some(address) = self.transition(|state, cx| logic::discover_wanted(state, cx, id))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
/// Look the gateway of a server up on the engine's runtime
|
||||
/// ([`sequence::find_gateways`]), as `Update::discover` asks; what is found
|
||||
/// comes back through [`later`].
|
||||
pub(crate) fn look_up_gateway(&mut self, id: i64, address: String) {
|
||||
self.engine.runtime().spawn(async move {
|
||||
let started = Instant::now();
|
||||
let urls = voelin_core::discover::gateways(&address).await;
|
||||
let elapsed_ms = started.elapsed().as_millis() as u64;
|
||||
if urls.is_empty() {
|
||||
info!(server = %address, elapsed_ms, "no gateway published");
|
||||
} else {
|
||||
later(move |app| app.gateway_found(id, &address, urls, elapsed_ms));
|
||||
if let Some(found) = sequence::find_gateways(&address).await {
|
||||
later(move |app| app.gateway_found(id, &address, found.urls, found.elapsed_ms));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Gateways were found for the server at `address` (best first); they
|
||||
/// are kept unless the server changed meanwhile ([`logic::gateway_found`]),
|
||||
/// and the server is observed through them if it is the one shown. Users
|
||||
/// never hear of them (logged only).
|
||||
/// Gateways were found for the server at `address`
|
||||
/// ([`sequence::gateway_found`]).
|
||||
fn gateway_found(&mut self, id: i64, address: &str, urls: Vec<String>, elapsed_ms: u64) {
|
||||
let found = self.transition(|state, cx| logic::gateway_found(state, cx, id, address, urls));
|
||||
match found {
|
||||
GatewayFound::Same => {
|
||||
debug!(server = %address, elapsed_ms, "gateway found, as kept");
|
||||
}
|
||||
GatewayFound::Kept { observe } => {
|
||||
info!(server = %address, elapsed_ms, kept = true, "gateway found");
|
||||
if observe {
|
||||
self.observe(id);
|
||||
}
|
||||
}
|
||||
GatewayFound::Changed { stop, observe, urls } => {
|
||||
info!(server = %address, ?urls, elapsed_ms, kept = false, "gateway found");
|
||||
if stop {
|
||||
self.engine.send(Command::StopObserving { session: id as u64 });
|
||||
}
|
||||
if observe {
|
||||
self.observe(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.transition_with(|state, cx, backing| {
|
||||
sequence::gateway_found(state, cx, backing, id, address, urls, elapsed_ms)
|
||||
});
|
||||
}
|
||||
|
||||
/// Observe the server invisibly ([`logic::observe_plan`]): through its
|
||||
/// gateway, else its own query login. Without either the gateway is
|
||||
/// looked up first, and observing starts when it is found
|
||||
/// (`gateway_found`).
|
||||
/// Observe the server invisibly ([`sequence::observe`]).
|
||||
pub(crate) fn observe(&mut self, id: i64) {
|
||||
let Some(b) = self.bookmark(id).cloned() else { return };
|
||||
let session = b.id as u64;
|
||||
match self.transition(|state, cx| logic::observe_plan(state, cx, id)) {
|
||||
ObservePlan::Nothing => return,
|
||||
ObservePlan::Retry(urls) => {
|
||||
debug!(server = %b.address, "still connecting to the gateway: try now");
|
||||
self.engine.send(Command::ObserveGateway {
|
||||
session,
|
||||
urls,
|
||||
identity: Box::new(voelin_core::ts::identity::to_blob(
|
||||
&self.identity_for(Some(b.id)),
|
||||
)),
|
||||
});
|
||||
return;
|
||||
}
|
||||
ObservePlan::Gateway(urls) => {
|
||||
info!(server = %b.address, ?urls, "observing through the gateway");
|
||||
self.engine.send(Command::ObserveGateway {
|
||||
session,
|
||||
urls,
|
||||
identity: Box::new(voelin_core::ts::identity::to_blob(
|
||||
&self.identity_for(Some(b.id)),
|
||||
)),
|
||||
});
|
||||
// Still the one the server publishes?
|
||||
self.discover_gateway(b.id);
|
||||
}
|
||||
ObservePlan::Query(q) => {
|
||||
let secret = self.secrets.get(&b.query_password_key()).ok().flatten();
|
||||
let transport = match q.transport {
|
||||
QueryTransport::Raw => voelin_core::QueryTransport::Raw,
|
||||
QueryTransport::Ssh => voelin_core::QueryTransport::Ssh,
|
||||
QueryTransport::Http => voelin_core::QueryTransport::Http,
|
||||
};
|
||||
let connect = voelin_core::QueryLogin {
|
||||
transport,
|
||||
address: format!("{}:{}", q.host, q.port),
|
||||
user: q.user.clone(),
|
||||
secret,
|
||||
server_port: q.server_port,
|
||||
server_id: None,
|
||||
allowlisted: false,
|
||||
};
|
||||
self.engine.send(Command::ObserveQuery { session, connect: Box::new(connect) });
|
||||
}
|
||||
ObservePlan::Discover => {
|
||||
self.discover_gateway(b.id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.set_status("Observing invisibly…");
|
||||
self.transition_with(|state, cx, backing| sequence::observe(state, cx, backing, id));
|
||||
}
|
||||
|
||||
/// Show a server, and observe it.
|
||||
/// Show a server, and observe it ([`sequence::select_server`]).
|
||||
pub(crate) fn select_server(&mut self, id: i64) {
|
||||
self.transition(|state, cx| logic::select_server(state, cx, id));
|
||||
self.observe(id);
|
||||
self.transition_with(|state, cx, backing| sequence::select_server(state, cx, backing, id));
|
||||
}
|
||||
|
||||
pub(crate) fn bookmark_form(&self, id: i64) -> BookmarkForm {
|
||||
let Some(b) = self.bookmark(id) else {
|
||||
return BookmarkForm {
|
||||
id: -1,
|
||||
nickname: self.default_nickname().into(),
|
||||
..Default::default()
|
||||
};
|
||||
};
|
||||
let password = self.secrets.get(&b.server_password_key()).ok().flatten();
|
||||
let state = self.state.sessions.get(&b.id).map(|view| &view.state);
|
||||
vm::servers::form(b, state, password.as_deref().unwrap_or_default())
|
||||
sequence::bookmark_form(&self.state, &self.store, &*self.secrets, id)
|
||||
}
|
||||
|
||||
/// Save the server dialog's server, and show it; its id, none when it
|
||||
/// could not be saved (the toast says why).
|
||||
/// could not be saved (the toast says why) ([`sequence::save_bookmark`]).
|
||||
pub(crate) fn save_bookmark(&mut self, form: &BookmarkForm) -> Option<i64> {
|
||||
let old = self.bookmark(form.id as i64).cloned();
|
||||
let mut bookmark =
|
||||
logic::bookmark_from_form(old.as_ref(), form, || self.default_nickname());
|
||||
let result = if bookmark.id < 0 {
|
||||
self.store.add_bookmark(&bookmark).map(|id| bookmark.id = id)
|
||||
} else {
|
||||
self.store.update_bookmark(&bookmark)
|
||||
};
|
||||
if let Err(e) = result {
|
||||
self.set_status(format!("Could not save: {e}"));
|
||||
return None;
|
||||
}
|
||||
let key = bookmark.server_password_key();
|
||||
let result = if form.server_password.is_empty() {
|
||||
self.secrets.delete(&key)
|
||||
} else {
|
||||
self.secrets.set(&key, &form.server_password)
|
||||
};
|
||||
if let Err(e) = result {
|
||||
warn!(%e, "could not store secret");
|
||||
}
|
||||
// Another server: what was observed is the old one's, and its
|
||||
// gateway is looked up anew.
|
||||
if let Some(old) = old.filter(|old| old.address != bookmark.address) {
|
||||
if old.query.is_some() {
|
||||
let _ = self.secrets.delete(&old.query_password_key());
|
||||
}
|
||||
self.state.gateways_looked_up.remove(&bookmark.id);
|
||||
self.engine.send(Command::StopObserving { session: bookmark.id as u64 });
|
||||
if let Some(view) = self.state.sessions.get_mut(&bookmark.id) {
|
||||
view.state.observe = ObserveState::Off;
|
||||
}
|
||||
}
|
||||
self.state.bookmarks = self.store.bookmarks().unwrap_or_default();
|
||||
self.select_server(bookmark.id);
|
||||
Some(bookmark.id)
|
||||
self.transition_with(|state, cx, backing| sequence::save_bookmark(state, cx, backing, form))
|
||||
}
|
||||
|
||||
/// The server dialog's Connect: save the server, then connect it with
|
||||
/// voice, into the channel of the link that filled the form if one did
|
||||
/// ([`vm::servers::connect_to`]). Only once saved: connecting what is
|
||||
/// shown after a failed save would connect the server shown before.
|
||||
/// Whether it was saved.
|
||||
/// ([`sequence::save_and_connect`]). Whether it was saved.
|
||||
pub(crate) fn save_and_connect(&mut self, form: &BookmarkForm) -> bool {
|
||||
let saved = self.save_bookmark(form);
|
||||
let Some(to) = vm::servers::connect_to(form, saved.and_then(|id| self.bookmark(id))) else {
|
||||
return false;
|
||||
};
|
||||
self.connect_voice_to(to.channel, to.channel_password, to.token);
|
||||
true
|
||||
let peer = self.stream_peer();
|
||||
self.transition_with(|state, cx, backing| {
|
||||
sequence::save_and_connect(state, cx, backing, form, Some(peer))
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn delete_bookmark(&mut self, id: i64) {
|
||||
self.engine.send(Command::CloseSession { session: id as u64 });
|
||||
if let Some(b) = self.bookmark(id).cloned() {
|
||||
let _ = self.secrets.delete(&b.server_password_key());
|
||||
let _ = self.secrets.delete(&b.query_password_key());
|
||||
}
|
||||
let _ = self.store.delete_bookmark(id);
|
||||
let bookmarks = self.store.bookmarks().unwrap_or_default();
|
||||
self.transition(|state, cx| logic::forget_server(state, cx, id, bookmarks));
|
||||
self.transition_with(|state, cx, backing| {
|
||||
sequence::delete_bookmark(state, cx, backing, id)
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) fn server_list_icon(&self, bookmark: &Bookmark) -> vm::ImageRef {
|
||||
|
||||
Reference in new issue
Block a user