fix #14740: fixed backend url per build, key bound to its server
- the backend URL is no longer a setting: one constant per build (release/dev) in src-tauri/src/backend.rs; Settings shows the server read-only; an old backend_url in settings.json is ignored - the stored key records the backend it was created for; a key of another backend is discarded before any use and never stored - a refused key is discarded only if it still is the stored one - the keyring is never called while the settings are locked - one upload per call at a time, also visible after a view switch - opener plugin without its link-click script (it also ran in the remote login page); a login error says to reload the login page Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+72
-17
@@ -20,7 +20,8 @@ use recorder_core::model::CallId;
|
||||
use serde::Serialize;
|
||||
use tauri::webview::PageLoadEvent;
|
||||
use tauri::{
|
||||
AppHandle, Emitter, Manager, Url, WebviewUrl, WebviewWindow, WebviewWindowBuilder, WindowEvent,
|
||||
AppHandle, Emitter, Manager, State, Url, WebviewUrl, WebviewWindow, WebviewWindowBuilder,
|
||||
WindowEvent,
|
||||
};
|
||||
use tauri_plugin_opener::OpenerExt;
|
||||
use tokio::sync::oneshot;
|
||||
@@ -28,6 +29,20 @@ use tokio::sync::oneshot;
|
||||
use crate::processing;
|
||||
use crate::state::AppState;
|
||||
|
||||
// The backend this build talks to: the one place to change it. It is not a
|
||||
// setting; a key created on one backend is never sent to another.
|
||||
/// Release builds. Switch to "https://backend.euvinopro.eu" here, then
|
||||
/// release, once that backend is live.
|
||||
const RELEASE_BACKEND_URL: &str = "https://test.euvinopro.eu";
|
||||
/// Debug builds (`cargo tauri dev`) always use the test backend.
|
||||
const DEV_BACKEND_URL: &str = "https://test.euvinopro.eu";
|
||||
/// The backend of this build (no trailing slash).
|
||||
pub const BACKEND_URL: &str = if cfg!(debug_assertions) {
|
||||
DEV_BACKEND_URL
|
||||
} else {
|
||||
RELEASE_BACKEND_URL
|
||||
};
|
||||
|
||||
/// Label of the login window.
|
||||
pub const LOGIN_WINDOW: &str = "backend-login";
|
||||
const CANCELLED: &str = "Anmeldung abgebrochen";
|
||||
@@ -85,6 +100,14 @@ impl Drop for CreatingClaim<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Answer of `backend_server`: the backend of this build.
|
||||
#[derive(Serialize, Clone, Debug, PartialEq)]
|
||||
pub struct BackendServer {
|
||||
pub base_url: String,
|
||||
/// Where the admin manages their keys.
|
||||
pub keys_url: String,
|
||||
}
|
||||
|
||||
/// Answer of `backend_status`.
|
||||
#[derive(Serialize, Clone, Debug, PartialEq)]
|
||||
pub struct BackendStatus {
|
||||
@@ -94,8 +117,6 @@ pub struct BackendStatus {
|
||||
/// `false` if the stored key could not be checked; username and expiry
|
||||
/// are then the stored ones.
|
||||
pub reachable: bool,
|
||||
/// Where the admin manages their keys.
|
||||
pub keys_url: String,
|
||||
}
|
||||
|
||||
/// Payload of `backend-login-error`: creating the key failed, the login
|
||||
@@ -105,8 +126,18 @@ struct LoginError {
|
||||
message: String,
|
||||
}
|
||||
|
||||
/// The backend this build talks to, for the settings (no network).
|
||||
#[tauri::command]
|
||||
pub fn backend_server(state: State<'_, AppState>) -> BackendServer {
|
||||
BackendServer {
|
||||
base_url: state.backend_url().to_string(),
|
||||
keys_url: state.backend_client().keys_page_url(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a backend key is stored, and whose it is: checked with the
|
||||
/// backend. A key the backend refuses is discarded.
|
||||
/// backend. A key the backend refuses, or one of another backend, is
|
||||
/// discarded.
|
||||
#[tauri::command]
|
||||
pub async fn backend_status(app: AppHandle) -> CmdResult<BackendStatus> {
|
||||
let state = app.state::<AppState>();
|
||||
@@ -117,7 +148,6 @@ pub async fn backend_status(app: AppHandle) -> CmdResult<BackendStatus> {
|
||||
username: None,
|
||||
expires_at: None,
|
||||
reachable: true,
|
||||
keys_url: client.keys_page_url(),
|
||||
};
|
||||
let Some(stored) = stored else {
|
||||
return Ok(status);
|
||||
@@ -131,7 +161,11 @@ pub async fn backend_status(app: AppHandle) -> CmdResult<BackendStatus> {
|
||||
}
|
||||
KeyCheck::Invalid => {
|
||||
log::info!("backend: the stored key was refused, discarding it");
|
||||
blocking(&app, |state| state.discard_backend_key()).await?;
|
||||
let refused = stored.key;
|
||||
blocking(&app, move |state| {
|
||||
state.discard_refused_backend_key(&refused)
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
KeyCheck::Unknown(e) => {
|
||||
log::info!(
|
||||
@@ -175,24 +209,33 @@ pub fn open_backend_keys_page(app: AppHandle) -> CmdResult<()> {
|
||||
|
||||
/// "Transkript hochladen": uploads the call's transcript as the user sees
|
||||
/// it, logging in first if needed, and records the CRM's id on the call.
|
||||
/// One upload per call at a time; `call-updated` tells the views when it
|
||||
/// starts and ends.
|
||||
#[tauri::command]
|
||||
pub async fn upload_transcript(app: AppHandle, id: CallId) -> CmdResult<()> {
|
||||
let state = app.state::<AppState>();
|
||||
let claim = state.claim_upload(id)?;
|
||||
processing::emit_updated(&app, id);
|
||||
let result = upload(&app, &state, id).await;
|
||||
drop(claim);
|
||||
processing::emit_updated(&app, id);
|
||||
result
|
||||
}
|
||||
|
||||
async fn upload(app: &AppHandle, state: &AppState, id: CallId) -> CmdResult<()> {
|
||||
let detail = state.call_detail(id)?;
|
||||
if detail.call.transcribed_at.is_none() {
|
||||
return Err("Das Gespräch ist noch nicht transkribiert".to_string());
|
||||
}
|
||||
let text = detail.upload_text()?;
|
||||
let client = state.backend_client();
|
||||
let stored = blocking(&app, |state| Ok(state.backend_key())).await?;
|
||||
let stored = blocking(app, |state| Ok(state.backend_key())).await?;
|
||||
let login = AppLogin { app: app.clone() };
|
||||
let crm_id = backend::upload_with_login(&client, &login, stored, Local::now(), &text)
|
||||
.await
|
||||
.inspect_err(|e| log::warn!("call {id}: upload failed: {e}"))?;
|
||||
log::info!("call {id}: transcript uploaded (CRM id {crm_id})");
|
||||
state.mark_uploaded(id, &crm_id)?;
|
||||
processing::emit_updated(&app, id);
|
||||
Ok(())
|
||||
state.mark_uploaded(id, &crm_id)
|
||||
}
|
||||
|
||||
/// [`Login`] through the login window, for the upload.
|
||||
@@ -206,12 +249,20 @@ impl Login for AppLogin {
|
||||
login(&self.app).await
|
||||
}
|
||||
|
||||
async fn discard_key(&self) {
|
||||
log::info!("backend: the key was refused, discarding it");
|
||||
if let Err(e) = blocking(&self.app, |state| state.discard_backend_key()).await {
|
||||
log::error!("backend: {e}");
|
||||
async fn discard_key(&self, refused: &str) {
|
||||
let refused = refused.to_string();
|
||||
match blocking(&self.app, move |state| {
|
||||
state.discard_refused_backend_key(&refused)
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(true) => {
|
||||
log::info!("backend: the key was refused, discarded it");
|
||||
emit_changed(&self.app);
|
||||
}
|
||||
Ok(false) => log::info!("backend: the refused key was already replaced"),
|
||||
Err(e) => log::error!("backend: {e}"),
|
||||
}
|
||||
emit_changed(&self.app);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,6 +345,7 @@ fn create_key(window: WebviewWindow, client: BackendClient) {
|
||||
}
|
||||
LoginStep::Retry(message) => {
|
||||
log::warn!("backend: creating the key failed: {message}");
|
||||
let message = format!("{message} – bitte die Seite im Anmeldefenster neu laden");
|
||||
let _ = app.emit_to("main", "backend-login-error", LoginError { message });
|
||||
}
|
||||
}
|
||||
@@ -340,8 +392,11 @@ async fn store_key(app: &AppHandle, client: &BackendClient, key: &NewKey) -> Res
|
||||
None
|
||||
}
|
||||
};
|
||||
let key = key.clone();
|
||||
blocking(app, move |state| state.store_backend_key(&key, username)).await?;
|
||||
let (key, base_url) = (key.clone(), client.base_url().to_string());
|
||||
blocking(app, move |state| {
|
||||
state.store_backend_key(&key, &base_url, username)
|
||||
})
|
||||
.await?;
|
||||
log::info!("backend: logged in, key stored");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user