feat #14740: link to the euvino admin backend
- Settings section "Backend": backend URL, login status checked live (logged in as / until, not logged in, backend unreachable), Anmelden / Neu anmelden, Abmelden and the link to the key management page - login window "backend-login": the backend's own login page in an incognito webview without any Tauri capability; after a page load behind the login, its cookies create a key (one request, never stored or logged), the key goes to the keyring, the window closes; closing it cancels; one login at a time, later requests wait for the same result - call detail: "Transkript hochladen" / "Erneut hochladen" (with a confirmation), "wird hochgeladen…", "Hochgeladen am … (ID …)" and inline errors; logs in first when there is no usable key - saving a different backend URL discards the key of the old backend Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+218
-11
@@ -17,13 +17,14 @@ use std::time::{Duration, Instant};
|
||||
|
||||
use chrono::{DateTime, Local};
|
||||
use recorder_audio::RenderSession;
|
||||
use recorder_core::backend::{BackendClient, KeyInfo, NewKey, StoredKey};
|
||||
use recorder_core::call_view::{self, CallDetail, CallListItem};
|
||||
use recorder_core::capture::{CaptureSource, OutputTarget};
|
||||
use recorder_core::detect::{CallDetector, DetectorEvent, MicSession};
|
||||
use recorder_core::http::{HttpTransport, ReqwestTransport};
|
||||
use recorder_core::model::{Call, CallId, CallStatus, Speaker};
|
||||
use recorder_core::recorder::Recorder;
|
||||
use recorder_core::secrets::SecretStore;
|
||||
use recorder_core::secrets::{SecretStore, Service};
|
||||
use recorder_core::settings::Settings;
|
||||
use recorder_core::store::Store;
|
||||
use serde::Serialize;
|
||||
@@ -195,15 +196,85 @@ impl AppState {
|
||||
lock(&self.settings).clone()
|
||||
}
|
||||
|
||||
/// Validates, saves and applies the settings; the detector picks up the
|
||||
/// new allowlist and delays immediately.
|
||||
pub fn save_settings(&self, settings: Settings) -> Result<(), String> {
|
||||
settings.validate()?;
|
||||
settings
|
||||
.save(&self.settings_path)
|
||||
.map_err(|e| format!("Einstellungen konnten nicht gespeichert werden: {e}"))?;
|
||||
/// Validates, saves and applies the settings the user edited; the
|
||||
/// detector picks up the new allowlist and delays immediately. The
|
||||
/// backend login is kept, unless the backend URL changed: then its key
|
||||
/// is discarded (it belongs to the old backend) and `true` returned.
|
||||
/// Deletes from the keyring, so call it off the UI thread.
|
||||
pub fn save_settings(&self, edited: Settings) -> Result<bool, String> {
|
||||
let mut current = lock(&self.settings);
|
||||
let (settings, backend_changed) = current.edited(edited)?;
|
||||
if backend_changed {
|
||||
self.secrets
|
||||
.delete(Service::Backend)
|
||||
.map_err(|e| format!("Backend-Schlüssel konnte nicht entfernt werden: {e}"))?;
|
||||
}
|
||||
save(&settings, &self.settings_path)?;
|
||||
lock(&self.detector).set_config(detector_config(&settings));
|
||||
*lock(&self.settings) = settings;
|
||||
*current = settings;
|
||||
Ok(backend_changed)
|
||||
}
|
||||
|
||||
/// Client of the configured backend.
|
||||
pub fn backend_client(&self) -> BackendClient {
|
||||
BackendClient::new(self.transport.clone(), &self.settings().backend_url)
|
||||
}
|
||||
|
||||
/// The stored backend key with its expiry. Reads the keyring, so call
|
||||
/// it off the UI thread.
|
||||
pub fn backend_key(&self) -> Option<StoredKey> {
|
||||
let key = self
|
||||
.secrets
|
||||
.get(Service::Backend)
|
||||
.filter(|key| !key.trim().is_empty())?;
|
||||
Some(StoredKey {
|
||||
key,
|
||||
expires_at: self.settings().backend_key_expires_at,
|
||||
})
|
||||
}
|
||||
|
||||
/// Stores a key the login created (in the keyring) and whose it is and
|
||||
/// when it expires (in the settings).
|
||||
pub fn store_backend_key(&self, key: &NewKey, username: Option<String>) -> Result<(), String> {
|
||||
self.secrets
|
||||
.set(Service::Backend, &key.key)
|
||||
.map_err(|e| format!("Backend-Schlüssel konnte nicht gespeichert werden: {e}"))?;
|
||||
self.update_settings(|s| {
|
||||
s.backend_username = username;
|
||||
s.backend_key_expires_at = Some(key.expires_at);
|
||||
})
|
||||
}
|
||||
|
||||
/// Takes over whose the stored key is and when it expires, as the
|
||||
/// backend reports it.
|
||||
pub fn refresh_backend_login(&self, info: &KeyInfo) -> Result<(), String> {
|
||||
self.update_settings(|s| {
|
||||
s.backend_username = Some(info.username.clone());
|
||||
s.backend_key_expires_at = Some(info.expires_at);
|
||||
})
|
||||
}
|
||||
|
||||
/// Forgets the backend key (keyring) and its login (settings).
|
||||
pub fn discard_backend_key(&self) -> Result<(), String> {
|
||||
self.secrets
|
||||
.delete(Service::Backend)
|
||||
.map_err(|e| format!("Backend-Schlüssel konnte nicht entfernt werden: {e}"))?;
|
||||
self.update_settings(|s| {
|
||||
s.backend_username = None;
|
||||
s.backend_key_expires_at = None;
|
||||
})
|
||||
}
|
||||
|
||||
/// Changes and saves settings the app manages itself; saves only if
|
||||
/// something changed.
|
||||
fn update_settings(&self, change: impl FnOnce(&mut Settings)) -> Result<(), String> {
|
||||
let mut current = lock(&self.settings);
|
||||
let mut settings = current.clone();
|
||||
change(&mut settings);
|
||||
if settings != *current {
|
||||
save(&settings, &self.settings_path)?;
|
||||
*current = settings;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -495,6 +566,15 @@ impl AppState {
|
||||
self.store.set_title(id, title).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Records that call `id`'s transcript was just uploaded and the CRM
|
||||
/// gave it `crm_transcript_id`.
|
||||
pub fn mark_uploaded(&self, id: CallId, crm_transcript_id: &str) -> Result<(), String> {
|
||||
self.find_call(id)?;
|
||||
self.store
|
||||
.set_uploaded(id, crm_transcript_id, Local::now())
|
||||
.map_err(|e| format!("Upload konnte nicht vermerkt werden: {e}"))
|
||||
}
|
||||
|
||||
fn find_call(&self, id: CallId) -> Result<Call, String> {
|
||||
self.store
|
||||
.get_call(id)
|
||||
@@ -696,6 +776,12 @@ fn detected_output_target(
|
||||
}
|
||||
}
|
||||
|
||||
fn save(settings: &Settings, path: &Path) -> Result<(), String> {
|
||||
settings
|
||||
.save(path)
|
||||
.map_err(|e| format!("Einstellungen konnten nicht gespeichert werden: {e}"))
|
||||
}
|
||||
|
||||
fn output_target(settings: &Settings) -> OutputTarget {
|
||||
match &settings.output_device {
|
||||
Some(id) => OutputTarget::Device(id.clone()),
|
||||
@@ -713,8 +799,9 @@ fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::TimeZone;
|
||||
use recorder_core::backend::{KeyInfo, NewKey, StoredKey};
|
||||
use recorder_core::model::{Segment, Speaker};
|
||||
use recorder_core::secrets::MemorySecrets;
|
||||
use recorder_core::secrets::{MemorySecrets, Service};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -1250,10 +1337,130 @@ mod tests {
|
||||
end_after_secs: 30,
|
||||
..Settings::default()
|
||||
};
|
||||
assert_eq!(f.state.save_settings(valid.clone()), Ok(()));
|
||||
assert_eq!(f.state.save_settings(valid.clone()), Ok(false));
|
||||
assert_eq!(f.state.settings(), valid);
|
||||
}
|
||||
|
||||
fn new_key(key: &str) -> NewKey {
|
||||
NewKey {
|
||||
key: key.into(),
|
||||
expires_at: Local.with_ymd_and_hms(2027, 11, 6, 10, 0, 0).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stored_backend_key_comes_with_its_login() {
|
||||
let f = fixture();
|
||||
assert_eq!(f.state.backend_key(), None);
|
||||
|
||||
f.state
|
||||
.store_backend_key(&new_key("euv_k"), Some("hinrichs".into()))
|
||||
.unwrap();
|
||||
|
||||
let expires_at = Local.with_ymd_and_hms(2027, 11, 6, 10, 0, 0).unwrap();
|
||||
assert_eq!(
|
||||
f.state.backend_key(),
|
||||
Some(StoredKey {
|
||||
key: "euv_k".into(),
|
||||
expires_at: Some(expires_at),
|
||||
})
|
||||
);
|
||||
let saved = Settings::load(&f.dir.path().join("settings.json"));
|
||||
assert_eq!(saved.backend_username.as_deref(), Some("hinrichs"));
|
||||
assert_eq!(saved.backend_key_expires_at, Some(expires_at));
|
||||
assert!(!fs::read_to_string(f.dir.path().join("settings.json"))
|
||||
.unwrap()
|
||||
.contains("euv_k"));
|
||||
assert_eq!(f.state.settings(), saved);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_backend_login_is_refreshed_from_the_key_info() {
|
||||
let f = fixture();
|
||||
f.state.store_backend_key(&new_key("euv_k"), None).unwrap();
|
||||
let later = Local.with_ymd_and_hms(2028, 1, 1, 0, 0, 0).unwrap();
|
||||
|
||||
f.state
|
||||
.refresh_backend_login(&KeyInfo {
|
||||
label: "l".into(),
|
||||
hint: "h".into(),
|
||||
username: "hinrichs".into(),
|
||||
created_at: later,
|
||||
expires_at: later,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let settings = f.state.settings();
|
||||
assert_eq!(settings.backend_username.as_deref(), Some("hinrichs"));
|
||||
assert_eq!(settings.backend_key_expires_at, Some(later));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_discarded_backend_key_is_gone_with_its_login() {
|
||||
let f = fixture();
|
||||
f.state
|
||||
.store_backend_key(&new_key("euv_k"), Some("hinrichs".into()))
|
||||
.unwrap();
|
||||
|
||||
f.state.discard_backend_key().unwrap();
|
||||
|
||||
assert_eq!(f.state.backend_key(), None);
|
||||
assert_eq!(f.state.secrets.get(Service::Backend), None);
|
||||
let settings = f.state.settings();
|
||||
assert_eq!(settings.backend_username, None);
|
||||
assert_eq!(settings.backend_key_expires_at, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saving_settings_keeps_the_backend_login_unless_the_url_changes() {
|
||||
let f = fixture();
|
||||
f.state
|
||||
.store_backend_key(&new_key("euv_k"), Some("hinrichs".into()))
|
||||
.unwrap();
|
||||
// The settings page sends what it loaded, maybe before the login.
|
||||
let edited = Settings {
|
||||
retention_days: 14,
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
assert_eq!(f.state.save_settings(edited.clone()), Ok(false));
|
||||
assert!(f.state.backend_key().is_some());
|
||||
assert_eq!(
|
||||
f.state.settings().backend_username.as_deref(),
|
||||
Some("hinrichs")
|
||||
);
|
||||
assert_eq!(f.state.settings().retention_days, 14);
|
||||
|
||||
let other_backend = Settings {
|
||||
backend_url: "https://other.example/".into(),
|
||||
..edited
|
||||
};
|
||||
assert_eq!(f.state.save_settings(other_backend), Ok(true));
|
||||
assert_eq!(f.state.backend_key(), None);
|
||||
assert_eq!(f.state.settings().backend_url, "https://other.example");
|
||||
assert_eq!(f.state.settings().backend_username, None);
|
||||
assert_eq!(
|
||||
f.state.backend_client().login_url(),
|
||||
"https://other.example/euvadm"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_upload_is_recorded_on_the_call() {
|
||||
let f = fixture();
|
||||
let (id, _, _) = finished_call(&f, "a");
|
||||
|
||||
f.state.mark_uploaded(id, "4711").unwrap();
|
||||
|
||||
let call = f.state.store.get_call(id).unwrap().unwrap();
|
||||
assert_eq!(call.crm_transcript_id.as_deref(), Some("4711"));
|
||||
assert!(call.uploaded_at.is_some());
|
||||
assert_eq!(
|
||||
f.state.mark_uploaded(id + 1, "1"),
|
||||
Err("Gespräch nicht gefunden".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
fn render_session(exe: &str, root_pid: u32, device: &str) -> RenderSession {
|
||||
RenderSession {
|
||||
exe: exe.to_string(),
|
||||
|
||||
Reference in New Issue
Block a user