From 00a689d4822ae6adb045c123a64daf3931979ce7 Mon Sep 17 00:00:00 2001 From: Jeremy Liu Date: Fri, 20 Nov 2026 17:38:57 -0800 Subject: [PATCH 1/6] feat(plaud): support Plaud 4.0 sync, download, content import, and title push --- Cargo.lock | 1 + crates/oah/Cargo.toml | 1 + crates/oah/src/plaud/api.rs | 1025 ++++++++++++++++++++++++++++++- crates/oah/src/plaud/connect.rs | 11 +- crates/oah/src/plaud/import.rs | 140 ++++- crates/oah/src/plaud/mod.rs | 1 + crates/oah/src/plaud/sync.rs | 78 ++- crates/oah/src/plaud/v4.rs | 638 +++++++++++++++++++ 8 files changed, 1820 insertions(+), 75 deletions(-) create mode 100644 crates/oah/src/plaud/v4.rs diff --git a/Cargo.lock b/Cargo.lock index c263b9b..0458b42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3420,6 +3420,7 @@ dependencies = [ "futures-util", "hex", "hmac 0.12.1", + "jiff", "lettre", "md-5 0.10.6", "ort", diff --git a/crates/oah/Cargo.toml b/crates/oah/Cargo.toml index 24d5bfd..f9ee630 100644 --- a/crates/oah/Cargo.toml +++ b/crates/oah/Cargo.toml @@ -17,6 +17,7 @@ aws-sdk-s3 = "1.144" argon2 = "0.6.0" axum = { version = "0.8", features = ["multipart"] } hex = "0.4.3" +jiff = "0.2" rand = "0.9" scrypt = "0.12.0" serde = { version = "1", features = ["derive"] } diff --git a/crates/oah/src/plaud/api.rs b/crates/oah/src/plaud/api.rs index 5cdc01b..7483149 100644 --- a/crates/oah/src/plaud/api.rs +++ b/crates/oah/src/plaud/api.rs @@ -1,5 +1,8 @@ //! The Plaud web API as the app calls it (spec/plaud.md, "Plaud web API used"). +use std::collections::{HashMap, HashSet}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, RwLock}; use std::time::Duration; use reqwest::Method; @@ -7,6 +10,7 @@ use reqwest::header::{AUTHORIZATION, RETRY_AFTER, USER_AGENT}; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; +use super::v4::{self, V4FileDetail, V4RecordingItem}; use crate::ai; const BROWSER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"; @@ -15,6 +19,8 @@ const DOWNLOAD_TIMEOUT: Duration = Duration::from_secs(15 * 60); /// Waits before each of the three retries. const BACKOFF_SECS: [u64; 3] = [1, 2, 4]; pub const PAGE_SIZE: usize = 50; +const MAX_V4_LIST_PAGES: usize = 400; +const MAX_V4_FOLDER_PAGES: usize = 10; /// Why a Plaud call failed. #[derive(Debug)] @@ -81,6 +87,19 @@ fn field(v: &Value, key: &str) -> String { v.get(key).and_then(text).unwrap_or_default() } +fn encode_query_value(raw: &str) -> String { + let mut out = String::with_capacity(raw.len()); + for b in raw.bytes() { + if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') { + out.push(b as char); + } else { + use std::fmt::Write; + let _ = write!(out, "%{b:02X}"); + } + } + out +} + /// A device on the account. Serialized as returned by the verify and connect-token calls. #[derive(Debug, Clone, Serialize)] pub struct Device { @@ -114,23 +133,70 @@ pub struct Recording { pub timezone: Option, pub zonemins: Option, pub scene: Option, + #[serde(skip)] + pub edit_time: Option, + #[serde(skip)] + pub is_v4: bool, +} + +struct V4Runtime { + v4_mode: AtomicBool, + active_token: RwLock, + remint_lock: tokio::sync::Mutex<()>, + list_cache: tokio::sync::Mutex>>, + detail_cache: tokio::sync::Mutex>, } /// A Plaud API base with the token to send. Built per call site; never logged. #[derive(Clone)] pub struct Client { base: String, - token: String, + workspace_id: Option, + account_token: Option, + runtime: Arc, } impl Client { pub fn new(base: &str, token: &str) -> Self { + Self::with_workspace(base, token, None, None) + } + + pub fn with_workspace( + base: &str, + workspace_token: &str, + workspace_id: Option, + account_token: Option, + ) -> Self { Self { base: base.to_owned(), - token: token.to_owned(), + workspace_id: workspace_id.filter(|s| !s.trim().is_empty()), + account_token: account_token.filter(|s| !s.trim().is_empty()), + runtime: Arc::new(V4Runtime { + v4_mode: AtomicBool::new(false), + active_token: RwLock::new(workspace_token.to_owned()), + remint_lock: tokio::sync::Mutex::new(()), + list_cache: tokio::sync::Mutex::new(None), + detail_cache: tokio::sync::Mutex::new(HashMap::new()), + }), } } + fn current_token(&self) -> String { + self.runtime + .active_token + .read() + .unwrap_or_else(|e| e.into_inner()) + .clone() + } + + fn is_v4_mode(&self) -> bool { + self.runtime.v4_mode.load(Ordering::Acquire) + } + + fn enable_v4_mode(&self) { + self.runtime.v4_mode.store(true, Ordering::Release); + } + /// An authenticated call with retries. A business `status` other than 0 becomes `Api`. pub async fn api( &self, @@ -148,6 +214,55 @@ impl Client { } } + /// Calls a Plaud 4.0 route, re-minting the workspace token once on `-419` or `-420`. + async fn v4_api( + &self, + method: Method, + path: &str, + body: Option, + ) -> Result { + let used_token = self.current_token(); + match self.api(method.clone(), path, body.clone()).await { + Ok(v) => Ok(v), + Err(PlaudError::Api { status, message }) + if v4::is_token_revoked_status(status) + && self.workspace_id.is_some() + && self.account_token.is_some() => + { + self.remint_workspace_token(&used_token) + .await + .map_err(|e| match e { + PlaudError::Rejected { message } => PlaudError::Rejected { message }, + PlaudError::RateLimited { retry_after } => { + PlaudError::RateLimited { retry_after } + } + PlaudError::Upstream => PlaudError::Upstream, + PlaudError::Api { .. } => PlaudError::Api { status, message }, + })?; + self.api(method, path, body).await + } + Err(e) => Err(e), + } + } + + async fn remint_workspace_token(&self, failed_token: &str) -> Result<(), PlaudError> { + let (Some(ws), Some(acct)) = (&self.workspace_id, &self.account_token) else { + return Err(unexpected("Plaud workspace token cannot be refreshed")); + }; + let _guard = self.runtime.remint_lock.lock().await; + if self.current_token() != failed_token { + return Ok(()); + } + let fresh = Client::new(&self.base, acct).workspace_token(ws).await?; + let mut active = self + .runtime + .active_token + .write() + .unwrap_or_else(|e| e.into_inner()); + *active = fresh; + Ok(()) + } + /// One JSON call. `auth` sends the token; `retry` applies the retry policy. /// Only HTTP-level errors are mapped here. pub async fn exchange( @@ -191,7 +306,16 @@ impl Client { .timeout(API_TIMEOUT) .header(USER_AGENT, BROWSER_AGENT); if auth { - req = req.header(AUTHORIZATION, format!("Bearer {}", self.token)); + let token = self.current_token(); + req = req.header(AUTHORIZATION, format!("Bearer {token}")); + } + if path.starts_with("/file-app/v4/") { + req = req.header("app-platform", "web").header("edit-from", "web"); + if let Some(ws) = &self.workspace_id { + req = req + .header("x-scope-type", "workspace") + .header("x-scope-id", ws); + } } if let Some(body) = body { req = req.json(body); @@ -266,45 +390,483 @@ impl Client { Ok(list.iter().filter_map(device_from).collect()) } + /// Validates a 4.0 session when `/device/list` returns the migrated status. + pub async fn validate_v4_session(&self) -> Result<(), PlaudError> { + self.enable_v4_mode(); + self.v4_api(Method::GET, "/file-app/v4/recordings/all?page_size=1", None) + .await?; + Ok(()) + } + /// One page of the recording list, newest edits first, trashed recordings excluded. pub async fn recordings(&self, page: usize) -> Result, PlaudError> { - let path = format!( - "/file/simple/web?skip={}&limit={PAGE_SIZE}&is_trash=0&sort_by=edit_time&is_desc=true", - page * PAGE_SIZE - ); - let v = self.api(Method::GET, &path, None).await?; - let list = v - .get("data_file_list") - .cloned() - .unwrap_or_else(|| json!([])); - serde_json::from_value(list) - .map_err(|_| unexpected("Plaud returned an unreadable recording list")) + if !self.is_v4_mode() { + let path = format!( + "/file/simple/web?skip={}&limit={PAGE_SIZE}&is_trash=0&sort_by=edit_time&is_desc=true", + page * PAGE_SIZE + ); + match self.api(Method::GET, &path, None).await { + Ok(v) if !v4::is_migrated_payload(&v) => { + let list = v + .get("data_file_list") + .cloned() + .unwrap_or_else(|| json!([])); + return serde_json::from_value(list) + .map_err(|_| unexpected("Plaud returned an unreadable recording list")); + } + Ok(_) => self.enable_v4_mode(), + Err(ref e) if v4::is_migrated_error(e) => self.enable_v4_mode(), + Err(e) => return Err(e), + } + } + self.recordings_v4(page).await } - /// Presigned link to the original audio (never Opus). - pub async fn audio_url(&self, file_id: &str) -> Result { - let v = self - .api( + async fn recordings_v4(&self, page: usize) -> Result, PlaudError> { + let mut cache = self.runtime.list_cache.lock().await; + if cache.is_none() { + let mut all = Vec::new(); + let mut cursor: Option = None; + let mut seen_cursors = HashSet::new(); + for _ in 0..MAX_V4_LIST_PAGES { + let path = match &cursor { + Some(c) => format!( + "/file-app/v4/recordings/all?sort_by=created&sort_order=desc&page_size={PAGE_SIZE}&cursor={}", + encode_query_value(c) + ), + None => format!( + "/file-app/v4/recordings/all?sort_by=created&sort_order=desc&page_size={PAGE_SIZE}" + ), + }; + let resp = self.v4_api(Method::GET, &path, None).await?; + let data = resp.get("data").unwrap_or(&Value::Null); + let raw_items = data.get("items").cloned().unwrap_or_else(|| json!([])); + let items: Vec = serde_json::from_value(raw_items) + .map_err(|_| unexpected("Plaud returned an unreadable recording list"))?; + for item in &items { + all.push(v4::map_list_item(item)); + } + let next = data + .get("next_cursor") + .and_then(text) + .map(|s| s.trim().to_owned()) + .filter(|s| !s.is_empty()); + match next { + Some(c) if seen_cursors.insert(c.clone()) => cursor = Some(c), + _ => break, + } + } + v4::sort_recordings_desc(&mut all); + *cache = Some(all); + } + let list = cache.as_deref().unwrap_or(&[]); + let start = page.saturating_mul(PAGE_SIZE); + if start >= list.len() { + return Ok(Vec::new()); + } + let end = (start + PAGE_SIZE).min(list.len()); + Ok(list[start..end].to_vec()) + } + + async fn fetch_v4_detail( + &self, + file_id: &str, + force_refresh: bool, + ) -> Result { + let v4_id = v4::local_id_to_v4_file_id(file_id); + if !force_refresh + && let Some(cached) = self.runtime.detail_cache.lock().await.get(&v4_id).cloned() + { + return Ok(cached); + } + let resp = self + .v4_api( Method::GET, - &format!("/file/temp-url/{file_id}?is_opus=0"), + &format!("/file-app/v4/files/detail/{v4_id}"), None, ) .await?; - v.get("temp_url") - .and_then(Value::as_str) - .map(str::to_owned) - .ok_or_else(|| unexpected("Plaud returned no download link")) + let data = resp.get("data").cloned().unwrap_or(Value::Null); + let detail: V4FileDetail = serde_json::from_value(data) + .map_err(|_| unexpected("Plaud returned an unreadable file detail"))?; + self.runtime + .detail_cache + .lock() + .await + .insert(v4_id, detail.clone()); + Ok(detail) + } + + async fn invalidate_v4_detail(&self, file_id: &str) { + let v4_id = v4::local_id_to_v4_file_id(file_id); + self.runtime.detail_cache.lock().await.remove(&v4_id); + } + + async fn resolve_content_urls( + &self, + content_ids: &[String], + ) -> Result, PlaudError> { + if content_ids.is_empty() { + return Ok(HashMap::new()); + } + let resp = self + .v4_api( + Method::POST, + "/file-app/v4/files/content/url", + Some(json!({ "content_ids": content_ids })), + ) + .await?; + let map = resp + .pointer("/data/url_map") + .or_else(|| resp.get("url_map")) + .and_then(Value::as_object); + let mut out = HashMap::new(); + if let Some(map) = map { + for (k, v) in map { + if let Some(url) = v.as_str().map(str::trim).filter(|s| !s.is_empty()) { + out.insert(k.clone(), url.to_owned()); + } + } + } + Ok(out) + } + + /// Hydrates metadata for a recording from 4.0 detail when in 4.0 mode. + pub async fn hydrate_recording(&self, r: &Recording) -> Result { + if !r.is_v4 && !self.is_v4_mode() { + return Ok(r.clone()); + } + let detail = self.fetch_v4_detail(&r.id, false).await?; + Ok(v4::apply_detail(r, &detail)) + } + + /// Presigned link to the original audio (never Opus). + pub async fn audio_url(&self, file_id: &str) -> Result { + if !self.is_v4_mode() { + match self + .api( + Method::GET, + &format!("/file/temp-url/{file_id}?is_opus=0"), + None, + ) + .await + { + Ok(v) if !v4::is_migrated_payload(&v) => { + return v + .get("temp_url") + .and_then(Value::as_str) + .map(str::to_owned) + .ok_or_else(|| unexpected("Plaud returned no download link")); + } + Ok(_) => self.enable_v4_mode(), + Err(ref e) if v4::is_migrated_error(e) => self.enable_v4_mode(), + Err(e) => return Err(e), + } + } + self.audio_url_v4(file_id).await + } + + async fn audio_url_v4(&self, file_id: &str) -> Result { + let detail = self.fetch_v4_detail(file_id, false).await?; + let candidates = v4::select_audio_candidates(&detail); + let mut last_err = None; + for obj in candidates { + if let Some(url) = obj + .content_url + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + { + return Ok(url.to_owned()); + } + if let Some(cid) = obj + .content_id + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + { + match self.resolve_content_urls(&[cid.to_owned()]).await { + Ok(map) => { + if let Some(url) = map.get(cid) { + return Ok(url.clone()); + } + } + Err(e @ PlaudError::Rejected { .. }) => return Err(e), + Err(e) => last_err = Some(e), + } + } + } + Err(last_err.unwrap_or_else(|| unexpected("Plaud returned no download link"))) } /// The `data` object of a recording's detail, with its content list. pub async fn detail(&self, file_id: &str) -> Result { - let v = self - .api(Method::GET, &format!("/file/detail/{file_id}"), None) - .await?; - Ok(v.get("data").cloned().unwrap_or(Value::Null)) + if !self.is_v4_mode() { + match self + .api(Method::GET, &format!("/file/detail/{file_id}"), None) + .await + { + Ok(v) if !v4::is_migrated_payload(&v) => { + return Ok(v.get("data").cloned().unwrap_or(Value::Null)); + } + Ok(_) => self.enable_v4_mode(), + Err(ref e) if v4::is_migrated_error(e) => self.enable_v4_mode(), + Err(e) => return Err(e), + } + } + self.detail_v4(file_id).await + } + + async fn detail_v4(&self, file_id: &str) -> Result { + let mut detail = self.fetch_v4_detail(file_id, false).await?; + let mut missing_ids = Vec::new(); + let mut seen = HashSet::new(); + for obj in &detail.objects { + if v4::is_content_object(obj) + && v4::is_object_ready(obj) + && !v4::has_non_empty(&obj.content_url) + && let Some(cid) = obj + .content_id + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + && seen.insert(cid.to_owned()) + { + missing_ids.push(cid.to_owned()); + } + } + if !missing_ids.is_empty() { + let url_map = self.resolve_content_urls(&missing_ids).await?; + for obj in &mut detail.objects { + if !v4::has_non_empty(&obj.content_url) + && let Some(cid) = obj.content_id.as_deref().map(str::trim) + && let Some(url) = url_map.get(cid) + { + obj.content_url = Some(url.clone()); + } + } + let v4_id = v4::local_id_to_v4_file_id(file_id); + self.runtime + .detail_cache + .lock() + .await + .insert(v4_id, detail.clone()); + } + Ok(v4::detail_to_legacy_content(&detail)) + } + + /// Renames a recording in Plaud, using legacy `PATCH /file/{id}` or 4.0 node rename. + pub async fn rename_file(&self, file_id: &str, title: &str) -> Result<(), PlaudError> { + if !self.is_v4_mode() { + match self + .api( + Method::PATCH, + &format!("/file/{file_id}"), + Some(json!({ "filename": title })), + ) + .await + { + Ok(v) if !v4::is_migrated_payload(&v) => return Ok(()), + Ok(_) => self.enable_v4_mode(), + Err(ref e) if v4::is_migrated_error(e) => self.enable_v4_mode(), + Err(e) => return Err(e), + } + } + self.rename_file_v4(file_id, title).await + } + + async fn rename_file_v4(&self, file_id: &str, title: &str) -> Result<(), PlaudError> { + let mut detail = self.fetch_v4_detail(file_id, false).await?; + let mut attempt = 0; + let (rename_resp, previous_name) = loop { + let node = detail + .node + .as_ref() + .ok_or_else(|| unexpected("Plaud file detail has no node metadata"))?; + let node_id = node + .node_id + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + .ok_or_else(|| unexpected("Plaud file detail has no node id"))?; + let previous_name = node.name.clone().unwrap_or_default(); + let origin_version = node + .version_ms + .or_else(|| detail.meta.as_ref().and_then(|m| m.version_ms)) + .unwrap_or(0); + let res = self + .v4_api( + Method::PATCH, + &format!("/file-app/v4/nodes/rename/{node_id}"), + Some(json!({ + "name": title, + "origin_version": origin_version, + })), + ) + .await; + match res { + Ok(v) => break (v, previous_name), + Err(PlaudError::Api { status, .. }) + if status == v4::VERSION_CONFLICT_STATUS && attempt == 0 => + { + attempt += 1; + detail = self.fetch_v4_detail(file_id, true).await?; + } + Err(e) => return Err(e), + } + }; + + self.invalidate_v4_detail(file_id).await; + self.rename_container_folder_best_effort(&detail, &rename_resp, &previous_name, title) + .await; + Ok(()) + } + + async fn rename_container_folder_best_effort( + &self, + detail: &V4FileDetail, + rename_resp: &Value, + previous_name: &str, + title: &str, + ) { + let Some(node) = &detail.node else { return }; + let Some(folder) = &node.folder else { return }; + let folder_name = folder.name.as_deref().unwrap_or_default(); + if previous_name.is_empty() || folder_name != previous_name || folder_name == title { + return; + } + let Some(folder_id) = folder + .folder_id + .as_deref() + .or(node.parent_id.as_deref()) + .map(str::trim) + .filter(|s| !s.is_empty()) + else { + return; + }; + let parent_folder_id = node + .display_folder + .as_ref() + .and_then(|f| f.folder_id.as_deref()) + .map(str::trim) + .filter(|s| !s.is_empty() && *s != folder_id) + .or_else(|| { + node.breadcrumbs + .iter() + .filter_map(|b| b.node_id.as_deref().map(str::trim)) + .find(|s| !s.is_empty() && *s != folder_id) + }); + + let initial = find_node_in_items(rename_resp.pointer("/data/parent_items"), folder_id) + .or_else(|| { + folder + .version_ms + .map(|ver| (folder_name.to_owned(), Some(ver))) + }); + let (mut current_name, mut origin_version) = match initial { + Some((name, Some(ver))) => (name, ver), + _ => match self.lookup_folder_node(parent_folder_id, folder_id).await { + Some((name, Some(ver))) => (name, ver), + _ => return, + }, + }; + if current_name != previous_name || current_name == title { + return; + } + + for attempt in 0..2 { + let res = self + .v4_api( + Method::PATCH, + &format!("/file-app/v4/nodes/rename/{folder_id}"), + Some(json!({ + "name": title, + "origin_version": origin_version, + })), + ) + .await; + match res { + Ok(_) => return, + Err(PlaudError::Api { status, .. }) + if status == v4::VERSION_CONFLICT_STATUS && attempt == 0 => + { + let Some((refreshed_name, Some(refreshed_ver))) = + self.lookup_folder_node(parent_folder_id, folder_id).await + else { + return; + }; + current_name = refreshed_name; + origin_version = refreshed_ver; + if current_name != previous_name || current_name == title { + return; + } + } + Err(_) => return, + } + } + } + + async fn lookup_folder_node( + &self, + parent_folder_id: Option<&str>, + folder_id: &str, + ) -> Option<(String, Option)> { + let mut cursor: Option = None; + let mut seen_cursors = HashSet::new(); + for _ in 0..MAX_V4_FOLDER_PAGES { + let mut path = match parent_folder_id { + Some(pid) => format!( + "/file-app/v4/folders/children?node_id={}&page_size={PAGE_SIZE}", + encode_query_value(pid) + ), + None => format!("/file-app/v4/folders/children?page_size={PAGE_SIZE}"), + }; + if let Some(c) = &cursor { + path.push_str("&cursor="); + path.push_str(&encode_query_value(c)); + } + let resp = self.v4_api(Method::GET, &path, None).await.ok()?; + let items = resp + .pointer("/data/items") + .or_else(|| resp.pointer("/data/children")) + .or_else(|| resp.pointer("/data/folders")) + .or_else(|| resp.get("data")); + if let Some(found) = find_node_in_items(items, folder_id) { + return Some(found); + } + let next = resp + .pointer("/data/next_cursor") + .and_then(text) + .map(|s| s.trim().to_owned()) + .filter(|s| !s.is_empty()); + match next { + Some(c) if seen_cursors.insert(c.clone()) => cursor = Some(c), + _ => break, + } + } + None } } +fn find_node_in_items(items: Option<&Value>, target_id: &str) -> Option<(String, Option)> { + let list = items?.as_array()?; + let item = list.iter().find(|entry| { + entry + .get("node_id") + .or_else(|| entry.get("folder_id")) + .or_else(|| entry.get("id")) + .and_then(Value::as_str) + == Some(target_id) + })?; + let name = item + .get("name") + .and_then(Value::as_str) + .unwrap_or_default() + .to_owned(); + let version_ms = item.get("version_ms").and_then(Value::as_i64); + Some((name, version_ms)) +} + /// Downloads a presigned link. No Plaud headers and one attempt. pub async fn fetch(url: &str) -> Result { let resp = ai::client() @@ -321,6 +883,13 @@ pub async fn fetch(url: &str) -> Result { #[cfg(test)] mod tests { + use std::sync::atomic::AtomicUsize; + + use axum::extract::{Path, Query, State}; + use axum::http::{HeaderMap, StatusCode}; + use axum::routing::{get, patch, post}; + use axum::{Json, Router}; + use super::*; #[test] @@ -364,4 +933,406 @@ mod tests { ); assert!(device_from(&json!({"name": "no serial"})).is_none()); } + + async fn spawn_mock(app: Router) -> String { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + tokio::spawn(async move { + axum::serve(listener, app).await.unwrap(); + }); + format!("http://{addr}") + } + + #[tokio::test] + async fn recordings_auto_detects_v4_sends_headers_paginates_and_sorts() { + let app = Router::new() + .route( + "/file/simple/web", + get(|| async { + Json(json!({ + "status": v4::MIGRATED_STATUS, + "msg": "Account migrated to 4.0." + })) + }), + ) + .route( + "/file-app/v4/recordings/all", + get( + |headers: HeaderMap, Query(q): Query>| async move { + assert_eq!( + headers.get("app-platform").and_then(|v| v.to_str().ok()), + Some("web") + ); + assert_eq!( + headers.get("edit-from").and_then(|v| v.to_str().ok()), + Some("web") + ); + assert_eq!( + headers.get("x-scope-type").and_then(|v| v.to_str().ok()), + Some("workspace") + ); + assert_eq!( + headers.get("x-scope-id").and_then(|v| v.to_str().ok()), + Some("ws-1") + ); + match q.get("cursor").map(String::as_str) { + None => Json(json!({ + "status": 0, + "data": { + "items": [ + { + "file_id": "f_newer_created", + "name": "Newer Created", + "duration_ms": 1000, + "created_at_show_ms": 2000, + "updated_at_show_ms": 2000, + "version_ms": 2000 + } + ], + "next_cursor": "c2" + } + })), + Some("c2") => Json(json!({ + "status": 0, + "data": { + "items": [ + { + "file_id": "of_older_edited", + "name": "Older Edited", + "duration_ms": 2000, + "created_at_show_ms": 1000, + "updated_at_show_ms": 5000, + "version_ms": 5000 + } + ], + "next_cursor": "c2" + } + })), + other => panic!("unexpected cursor: {other:?}"), + } + }, + ), + ); + let base = spawn_mock(app).await; + let client = + Client::with_workspace(&base, "wt-1", Some("ws-1".into()), Some("acct".into())); + let page0 = client.recordings(0).await.unwrap(); + assert_eq!(page0.len(), 2); + assert_eq!(page0[0].id, "older_edited"); + assert_eq!(page0[1].id, "f_newer_created"); + assert!(page0[0].is_v4); + assert!(client.recordings(1).await.unwrap().is_empty()); + } + + #[tokio::test] + async fn v4_remints_once_across_concurrent_revoked_calls_and_maps_401_to_rejected() { + #[derive(Clone)] + struct MockState { + remints: Arc, + reject_remint: bool, + } + let state = MockState { + remints: Arc::new(AtomicUsize::new(0)), + reject_remint: false, + }; + let app = Router::new() + .route( + "/file-app/v4/recordings/all", + get(|headers: HeaderMap| async move { + let auth = headers + .get(AUTHORIZATION) + .and_then(|v| v.to_str().ok()) + .unwrap_or_default(); + if auth == "Bearer stale-wt" { + Json(json!({ "status": -419, "msg": "workspace token revoked" })) + } else if auth == "Bearer fresh-wt" { + Json(json!({ "status": 0, "data": { "items": [], "next_cursor": null } })) + } else { + panic!("unexpected auth: {auth}"); + } + }), + ) + .route( + "/user-app/auth/workspace/token/{ws}", + post( + |State(s): State, headers: HeaderMap, Path(ws): Path| async move { + assert_eq!(ws, "ws-9"); + assert_eq!( + headers.get(AUTHORIZATION).and_then(|v| v.to_str().ok()), + Some("Bearer acct-tok") + ); + tokio::time::sleep(Duration::from_millis(30)).await; + s.remints.fetch_add(1, Ordering::SeqCst); + if s.reject_remint { + ( + StatusCode::UNAUTHORIZED, + Json(json!({ "msg": "expired account token" })), + ) + } else { + ( + StatusCode::OK, + Json(json!({ + "status": 0, + "data": { "workspace_token": "fresh-wt" } + })), + ) + } + }, + ), + ) + .with_state(state.clone()); + let base = spawn_mock(app).await; + let client = Client::with_workspace( + &base, + "stale-wt", + Some("ws-9".into()), + Some("acct-tok".into()), + ); + let c1 = client.clone(); + let c2 = client.clone(); + let (r1, r2) = tokio::join!(c1.validate_v4_session(), c2.validate_v4_session()); + assert!(r1.is_ok()); + assert!(r2.is_ok()); + assert_eq!(state.remints.load(Ordering::SeqCst), 1); + + let rejected_state = MockState { + remints: Arc::new(AtomicUsize::new(0)), + reject_remint: true, + }; + let app2 = Router::new() + .route( + "/file-app/v4/recordings/all", + get(|| async { Json(json!({ "status": -420, "msg": "revoked" })) }), + ) + .route( + "/user-app/auth/workspace/token/{ws}", + post(|State(s): State| async move { + s.remints.fetch_add(1, Ordering::SeqCst); + ( + StatusCode::UNAUTHORIZED, + Json(json!({ "msg": "account token invalid" })), + ) + }), + ) + .with_state(rejected_state); + let base2 = spawn_mock(app2).await; + let client2 = Client::with_workspace( + &base2, + "stale-wt", + Some("ws-9".into()), + Some("acct-tok".into()), + ); + assert!(matches!( + client2.validate_v4_session().await, + Err(PlaudError::Rejected { .. }) + )); + } + + #[tokio::test] + async fn audio_url_and_detail_resolve_content_ids_in_v4() { + let app = Router::new() + .route( + "/file-app/v4/files/detail/{file_id}", + get(|Path(file_id): Path| async move { + assert_eq!(file_id, "of_rec1"); + Json(json!({ + "status": 0, + "data": { + "node": { "node_id": "n1", "name": "Demo", "version_ms": 10 }, + "meta": { "file_id": "of_rec1", "duration": 1000 }, + "objects": [ + { + "object_id": "o_audio", + "object_type": "AUDIO_TRANSCODED", + "content_id": "cid_audio", + "content_url": null, + "task_status": 1 + }, + { + "object_id": "o_tr", + "object_type": "TRANSCRIPT", + "content_id": "cid_tr", + "content_url": null, + "task_status": 1 + } + ] + } + })) + }), + ) + .route( + "/file-app/v4/files/content/url", + post(|Json(body): Json| async move { + let ids = body["content_ids"].as_array().unwrap(); + let mut map = serde_json::Map::new(); + for id in ids { + let s = id.as_str().unwrap(); + map.insert(s.to_owned(), json!(format!("https://cdn.example/{s}"))); + } + Json(json!({ "status": 0, "data": { "url_map": map } })) + }), + ); + let base = spawn_mock(app).await; + let client = Client::with_workspace(&base, "wt", Some("ws".into()), None); + client.enable_v4_mode(); + + let audio = client.audio_url("rec1").await.unwrap(); + assert_eq!(audio, "https://cdn.example/cid_audio"); + + let detail = client.detail("rec1").await.unwrap(); + let list = detail["content_list"].as_array().unwrap(); + assert_eq!(list.len(), 1); + assert_eq!(list[0]["data_link"], "https://cdn.example/cid_tr"); + } + + #[tokio::test] + async fn rename_file_v4_retries_on_conflict_and_renames_matching_folder() { + #[derive(Clone)] + struct RenameState { + node_version: Arc>, + folder_name: Arc>, + folder_version: Arc>, + node_conflict_once: Arc, + folder_conflict_once: Arc, + } + let st = RenameState { + node_version: Arc::new(std::sync::Mutex::new(10)), + folder_name: Arc::new(std::sync::Mutex::new("Old Title".into())), + folder_version: Arc::new(std::sync::Mutex::new(20)), + node_conflict_once: Arc::new(AtomicBool::new(true)), + folder_conflict_once: Arc::new(AtomicBool::new(true)), + }; + let app = Router::new() + .route( + "/file/{id}", + patch(|| async { + Json(json!({ + "status": v4::MIGRATED_STATUS, + "msg": "Account migrated to 4.0" + })) + }), + ) + .route( + "/file-app/v4/files/detail/{file_id}", + get(|State(s): State| async move { + let nv = *s.node_version.lock().unwrap(); + let fnm = s.folder_name.lock().unwrap().clone(); + Json(json!({ + "status": 0, + "data": { + "node": { + "node_id": "node_1", + "parent_id": "wrap_1", + "name": "Old Title", + "version_ms": nv, + "folder": { "folder_id": "wrap_1", "name": fnm }, + "display_folder": { "folder_id": "display_parent", "name": "Projects" }, + "breadcrumbs": [ + { "node_id": "root_folder", "name": "Recordings" }, + { "node_id": "wrap_1", "name": fnm } + ] + }, + "meta": { "file_id": "of_1", "version_ms": nv }, + "objects": [] + } + })) + }), + ) + .route( + "/file-app/v4/folders/children", + get( + |State(s): State, Query(q): Query>| async move { + assert_eq!(q.get("node_id").map(String::as_str), Some("display_parent")); + assert_eq!( + q.get("page_size").map(String::as_str), + Some(PAGE_SIZE.to_string().as_str()) + ); + match q.get("cursor").map(String::as_str) { + None => Json(json!({ + "status": 0, + "data": { + "items": [{ + "node_id": "wrap_other", + "name": "Other", + "version_ms": 1 + }], + "next_cursor": "fc2" + } + })), + Some("fc2") => { + let fnm = s.folder_name.lock().unwrap().clone(); + let fv = *s.folder_version.lock().unwrap(); + Json(json!({ + "status": 0, + "data": { + "items": [{ + "node_id": "wrap_1", + "name": fnm, + "version_ms": fv + }], + "next_cursor": null + } + })) + } + other => panic!("unexpected folder cursor: {other:?}"), + } + }, + ), + ) + .route( + "/file-app/v4/nodes/rename/{node_id}", + patch( + |State(s): State, + Path(node_id): Path, + Json(body): Json| async move { + let new_name = body["name"].as_str().unwrap().to_owned(); + let origin = body["origin_version"].as_i64().unwrap(); + if node_id == "node_1" { + if s.node_conflict_once.swap(false, Ordering::SeqCst) { + *s.node_version.lock().unwrap() = 11; + return Json(json!({ + "status": v4::VERSION_CONFLICT_STATUS, + "msg": "Version conflict" + })); + } + assert_eq!(origin, 11); + *s.node_version.lock().unwrap() = 12; + let fnm = s.folder_name.lock().unwrap().clone(); + let fv = *s.folder_version.lock().unwrap(); + Json(json!({ + "status": 0, + "data": { + "items": [{ "node_id": "node_1", "name": new_name, "version_ms": 12 }], + "parent_items": [{ "node_id": "wrap_1", "name": fnm, "version_ms": fv }] + } + })) + } else if node_id == "wrap_1" { + if s.folder_conflict_once.swap(false, Ordering::SeqCst) { + *s.folder_version.lock().unwrap() = 21; + return Json(json!({ + "status": v4::VERSION_CONFLICT_STATUS, + "msg": "Version conflict" + })); + } + assert_eq!(origin, 21); + *s.folder_name.lock().unwrap() = new_name.clone(); + *s.folder_version.lock().unwrap() = 22; + Json(json!({ + "status": 0, + "data": { "items": [{ "node_id": "wrap_1", "name": new_name, "version_ms": 22 }] } + })) + } else { + panic!("unexpected node_id: {node_id}"); + } + }, + ), + ) + .with_state(st.clone()); + let base = spawn_mock(app).await; + let client = Client::with_workspace(&base, "wt", Some("ws".into()), None); + client.rename_file("1", "New Title").await.unwrap(); + assert_eq!(*st.node_version.lock().unwrap(), 12); + assert_eq!(st.folder_name.lock().unwrap().as_str(), "New Title"); + assert_eq!(*st.folder_version.lock().unwrap(), 22); + } } diff --git a/crates/oah/src/plaud/connect.rs b/crates/oah/src/plaud/connect.rs index 4d3ceb4..ba813a5 100644 --- a/crates/oah/src/plaud/connect.rs +++ b/crates/oah/src/plaud/connect.rs @@ -189,7 +189,7 @@ pub(super) async fn connect( let workspace = account.personal_workspace().await.ok().flatten(); let checker = match &workspace { Some(ws) => match account.workspace_token(ws).await { - Ok(wt) => Client::new(base, &wt), + Ok(wt) => Client::with_workspace(base, &wt, Some(ws.clone()), Some(token.to_owned())), Err(PlaudError::Api { status, message }) => { return Err( AppError::bad_request("PLAUD_WORKSPACE_UNAVAILABLE", message) @@ -200,7 +200,14 @@ pub(super) async fn connect( }, None => account, }; - let devices = checker.devices().await.map_err(generic)?; + let devices = match checker.devices().await { + Ok(devices) => devices, + Err(ref e) if super::v4::is_migrated_error(e) => { + checker.validate_v4_session().await.map_err(generic)?; + Vec::new() + } + Err(e) => return Err(generic(e)), + }; let now = now_ms(); let mut tx = crate::db::begin_write(&state.db).await?; diff --git a/crates/oah/src/plaud/import.rs b/crates/oah/src/plaud/import.rs index c09aa2a..7c9dc97 100644 --- a/crates/oah/src/plaud/import.rs +++ b/crates/oah/src/plaud/import.rs @@ -14,6 +14,8 @@ pub struct Stored { pub storage_type: &'static str, pub key: String, pub peaks: Option>, + pub byte_len: i64, + pub file_md5: String, } /// Downloads a recording's original audio to the active storage backend and returns where it went. @@ -31,11 +33,14 @@ pub async fn store_audio( .await .map_err(|_| ItemError::Failed("could not create the storage folder".into()))?; let temp = dir.join(format!(".plaud-{}.partial", ids::token_hex(8))); - if let Err(e) = write_stream(&mut resp, &temp).await { - let _ = tokio::fs::remove_file(&temp).await; - return Err(e); - } - match stage_downloaded(state, user_id, file_id, &temp).await { + let (byte_len, file_md5) = match write_stream(&mut resp, &temp).await { + Ok(meta) => meta, + Err(e) => { + let _ = tokio::fs::remove_file(&temp).await; + return Err(e); + } + }; + match stage_downloaded(state, user_id, file_id, &temp, byte_len, file_md5).await { Ok(stored) => Ok(stored), Err(e) => { // Every failure after the download leaves no temporary file behind. @@ -51,6 +56,8 @@ async fn stage_downloaded( user_id: &str, file_id: &str, temp: &Path, + byte_len: i64, + file_md5: String, ) -> Result { let head = read_head(temp).await?; let ext = sniff_ext(&head); @@ -67,13 +74,21 @@ async fn stage_downloaded( storage_type: state.storage.active_type(), key, peaks, + byte_len, + file_md5, }) } -async fn write_stream(resp: &mut reqwest::Response, path: &Path) -> Result<(), ItemError> { +async fn write_stream( + resp: &mut reqwest::Response, + path: &Path, +) -> Result<(i64, String), ItemError> { + use md5::Digest; let mut file = tokio::fs::File::create(path) .await .map_err(|_| ItemError::Failed("could not write the audio".into()))?; + let mut hasher = md5::Md5::new(); + let mut byte_len: i64 = 0; while let Some(chunk) = resp .chunk() .await @@ -82,6 +97,8 @@ async fn write_stream(resp: &mut reqwest::Response, path: &Path) -> Result<(), I file.write_all(&chunk) .await .map_err(|_| ItemError::Failed("could not write the audio".into()))?; + hasher.update(&chunk); + byte_len = byte_len.saturating_add(i64::try_from(chunk.len()).unwrap_or(i64::MAX)); } file.flush() .await @@ -89,7 +106,7 @@ async fn write_stream(resp: &mut reqwest::Response, path: &Path) -> Result<(), I file.sync_all() .await .map_err(|_| ItemError::Failed("could not write the audio".into()))?; - Ok(()) + Ok((byte_len, hex::encode(hasher.finalize()))) } async fn read_head(path: &Path) -> Result, ItemError> { @@ -262,22 +279,17 @@ pub async fn import_content( } if !has_summary { - let is_summary = |item: &Value| { - item.get("data_id") - .and_then(Value::as_str) - .is_some_and(|id| id.starts_with("auto_sum:")) - }; let inline = detail .get("pre_download_content_list") .and_then(Value::as_array) - .and_then(|list| list.iter().find(|i| is_summary(i))) + .and_then(|list| list.iter().find(|i| is_summary_item(i))) .and_then(|i| i.get("data_content")) .and_then(Value::as_str) - .and_then(|s| serde_json::from_str::(s).ok()); + .and_then(|s| parse_summary_body(s.as_bytes())); let summary = match inline { Some(v) => Some(v), - None => match ready_link(&items, is_summary) { - Some(link) => serde_json::from_slice::(&fetch_body(&link).await?).ok(), + None => match ready_link(&items, is_summary_item) { + Some(link) => parse_summary_body(&fetch_body(&link).await?), None => None, }, }; @@ -349,17 +361,60 @@ fn gunzip(body: &[u8]) -> Option> { Some(out) } +fn is_summary_item(item: &Value) -> bool { + item.get("data_id") + .and_then(Value::as_str) + .is_some_and(|id| id.starts_with("auto_sum:")) + || item.get("data_type").and_then(Value::as_str) == Some("auto_sum_note") +} + +fn parse_summary_body(body: &[u8]) -> Option { + match serde_json::from_slice::(body) { + Ok(v) if v.is_object() => Some(v), + Ok(Value::String(s)) => { + let trimmed = s.trim(); + (!trimmed.is_empty()).then(|| serde_json::json!({ "ai_content": trimmed })) + } + Ok(_) => None, + Err(_) => { + let text = std::str::from_utf8(body).ok()?.trim(); + (!text.is_empty()).then(|| serde_json::json!({ "ai_content": text })) + } + } +} + +fn transcript_segments(body: &[u8]) -> Option> { + let parsed: Value = serde_json::from_slice(body).ok()?; + if let Some(arr) = parsed.as_array() { + return Some(arr.clone()); + } + parsed + .get("trans_result") + .or_else(|| parsed.get("segments")) + .or_else(|| parsed.pointer("/data/trans_result")) + .or_else(|| parsed.pointer("/data/segments")) + .and_then(Value::as_array) + .cloned() +} + fn speaker_number(v: &Value) -> Option { v.as_i64().or_else(|| v.as_str()?.parse().ok()) } /// Each segment becomes a line, prefixed with `Speaker :` when the speaker is a number. fn transcript_text(body: &[u8]) -> Option { - let segments: Vec = serde_json::from_slice(body).ok()?; + let segments = transcript_segments(body)?; let lines: Vec = segments .iter() .filter_map(|s| { - let content = s.get("content")?.as_str()?.trim(); + let content = s + .get("content") + .or_else(|| s.get("text")) + .and_then(Value::as_str)? + .trim(); + if content.is_empty() { + return None; + } Some(match s.get("speaker").and_then(speaker_number) { Some(n) => format!("Speaker {n}: {content}"), None => content.to_owned(), @@ -382,7 +437,7 @@ fn segment_ms(v: &Value) -> Option { /// Timed lines for the timeline. A segment without text or a usable start is skipped, not fatal. fn transcript_timeline(body: &[u8]) -> Option> { - let segments: Vec = serde_json::from_slice(body).ok()?; + let segments = transcript_segments(body)?; let mut timeline = Vec::new(); for s in &segments { let Some(content) = s @@ -491,4 +546,51 @@ mod tests { ); assert!(super::gunzip(b"not gzip").is_none()); } + + #[test] + fn summary_parser_accepts_json_and_plain_markdown() { + let json_val = parse_summary_body(br#"{"ai_content":"Summary","key_points":["a"]}"#) + .expect("json summary"); + assert_eq!(json_val["ai_content"], "Summary"); + + let md_val = + parse_summary_body(b"# Summary\nFake v4 markdown summary\n").expect("markdown summary"); + assert_eq!(md_val["ai_content"], "# Summary\nFake v4 markdown summary"); + + assert!(parse_summary_body(b" \n\t ").is_none()); + assert!(parse_summary_body(b"null").is_none()); + assert!(parse_summary_body(b"[]").is_none()); + } + + #[test] + fn summary_matcher_accepts_prefix_and_data_type() { + assert!(is_summary_item( + &serde_json::json!({ "data_id": "auto_sum:1" }) + )); + assert!(is_summary_item( + &serde_json::json!({ "data_id": "other", "data_type": "auto_sum_note" }) + )); + assert!(!is_summary_item( + &serde_json::json!({ "data_id": "source_transaction:1", "data_type": "transaction" }) + )); + } + + #[test] + fn transcript_unwraps_trans_result_and_segments_objects() { + let wrapped_tr = + br#"{"trans_result":[{"start_time":100,"end_time":500,"speaker":1,"content":"Hello"}]}"#; + assert_eq!( + transcript_text(wrapped_tr).as_deref(), + Some("Speaker 1: Hello") + ); + let tl = transcript_timeline(wrapped_tr).unwrap(); + assert_eq!(tl.len(), 1); + assert_eq!(tl[0]["start_ms"], 100); + + let wrapped_seg = br#"{"segments":[{"start_ms":200,"end_ms":400,"text":"World"}]}"#; + assert_eq!(transcript_text(wrapped_seg).as_deref(), Some("World")); + let tl2 = transcript_timeline(wrapped_seg).unwrap(); + assert_eq!(tl2.len(), 1); + assert_eq!(tl2[0]["start_ms"], 200); + } } diff --git a/crates/oah/src/plaud/mod.rs b/crates/oah/src/plaud/mod.rs index 7555355..cf6482f 100644 --- a/crates/oah/src/plaud/mod.rs +++ b/crates/oah/src/plaud/mod.rs @@ -4,6 +4,7 @@ mod api; mod connect; mod import; pub(crate) mod sync; +mod v4; pub use sync::push_title; pub mod token; diff --git a/crates/oah/src/plaud/sync.rs b/crates/oah/src/plaud/sync.rs index c489380..94eb362 100644 --- a/crates/oah/src/plaud/sync.rs +++ b/crates/oah/src/plaud/sync.rs @@ -92,7 +92,7 @@ impl From for ItemError { } enum Outcome { - New(String), + New(String, String), Updated(String), Unchanged { imported: bool }, Skipped, @@ -286,11 +286,11 @@ async fn sync_pages( let results = join_all(chunk.iter().map(|r| process(state, ctx, r))).await; for (r, result) in chunk.iter().zip(results) { match result { - Ok(Outcome::New(id)) => { + Ok(Outcome::New(id, title)) => { report.new += 1; webhooks::emit_soft(state, &ctx.user_id, &id, "recording.synced", None) .await; - new_titles.push(title_for(&ctx.title_mode, r).await.0); + new_titles.push(title); new_ids.push(id); changed = true; } @@ -356,19 +356,21 @@ async fn process(state: &AppState, ctx: &Ctx, r: &Recording) -> Result { + let hydrated = ctx.client.hydrate_recording(r).await?; // A stored file is kept: a changed Plaud version only refreshes metadata, so a rename // or an edit in Plaud never replaces a file the user may be playing. The exception is a // missing object, which would leave the recording unplayable for good. if audio_present(state, &prev_type, &prev_key).await { let follow = may_follow_plaud(&prev_source); - match update_row(state, r, &id, follow).await { + match update_row(state, &hydrated, &id, follow).await { Ok(true) => {} Ok(false) => return Ok(Outcome::Skipped), Err(e) => return Err(e), } } else { - let storage = import::store_audio(state, &ctx.user_id, &ctx.client, &r.id).await?; - match update_row_with_audio(state, r, &id, Some(&storage), true).await { + let storage = + import::store_audio(state, &ctx.user_id, &ctx.client, &hydrated.id).await?; + match update_row_with_audio(state, &hydrated, &id, Some(&storage), true).await { Ok(true) => {} Ok(false) => { delete_audio(state, storage.storage_type, &storage.key).await; @@ -380,20 +382,22 @@ async fn process(state: &AppState, ctx: &Ctx, r: &Recording) -> Result { - let storage = import::store_audio(state, &ctx.user_id, &ctx.client, &r.id).await?; - let id = match insert_row(state, ctx, r, &storage).await { - Ok(id) => id, + let hydrated = ctx.client.hydrate_recording(r).await?; + let storage = + import::store_audio(state, &ctx.user_id, &ctx.client, &hydrated.id).await?; + let (id, title) = match insert_row(state, ctx, &hydrated, &storage).await { + Ok(pair) => pair, Err(e) => { delete_audio(state, storage.storage_type, &storage.key).await; return Err(e); } }; - import_for(state, ctx, &id, &r.id).await; - Ok(Outcome::New(id)) + import_for(state, ctx, &id, &hydrated.id).await; + Ok(Outcome::New(id, title)) } } } @@ -476,11 +480,18 @@ async fn insert_row( ctx: &Ctx, r: &Recording, storage: &import::Stored, -) -> Result { +) -> Result<(String, String), ItemError> { let id = ids::new_id(); let now = now_ms(); let start = r.start_time.unwrap_or(0); let (title, title_source) = title_for(&ctx.title_mode, r).await; + let filesize = r.filesize.filter(|&n| n > 0).unwrap_or(storage.byte_len); + let file_md5 = r + .file_md5 + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + .unwrap_or(&storage.file_md5); let peaks_json = storage .peaks .as_ref() @@ -499,8 +510,8 @@ async fn insert_row( .bind(r.duration.unwrap_or(0)) .bind(start) .bind(r.end_time.unwrap_or(start)) - .bind(r.filesize.unwrap_or(0)) - .bind(r.file_md5.clone().unwrap_or_default()) + .bind(filesize) + .bind(file_md5) .bind(storage.storage_type) .bind(storage.key.as_str()) .bind(now) @@ -515,7 +526,7 @@ async fn insert_row( .bind(crate::export::auto::auto_export_eligible(state, &ctx.user_id).await) .execute(&state.db) .await?; - Ok(id) + Ok((id, title)) } /// Overwrites a row with Plaud's values, keeping the stored audio. False when the row was deleted @@ -550,12 +561,23 @@ async fn update_row_with_audio( .unwrap_or_else(|| r.id.clone()), ) }); + let filesize = r + .filesize + .filter(|&n| n > 0) + .or_else(|| storage.map(|s| s.byte_len)); + let file_md5 = r + .file_md5 + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + .map(str::to_owned) + .or_else(|| storage.map(|s| s.file_md5.clone())); let peaks = storage .and_then(|s| s.peaks.as_ref()) .and_then(|p| serde_json::to_string(p).ok()); let done = sqlx::query( "UPDATE recordings SET device_sn = ?, filename = COALESCE(?, filename), duration = ?, start_time = ?, end_time = ?, \ - filesize = ?, file_md5 = ?, plaud_version = ?, timezone = ?, zonemins = ?, scene = ?, is_trash = 0, updated_at = ?, \ + filesize = COALESCE(?, filesize), file_md5 = COALESCE(?, file_md5), plaud_version = ?, timezone = ?, zonemins = ?, scene = ?, is_trash = 0, updated_at = ?, \ storage_type = COALESCE(?, storage_type), storage_path = COALESCE(?, storage_path), \ downloaded_at = COALESCE(?, downloaded_at), waveform_peaks = COALESCE(?, waveform_peaks) \ WHERE id = ? AND deleted_at IS NULL", @@ -565,8 +587,8 @@ async fn update_row_with_audio( .bind(r.duration.unwrap_or(0)) .bind(r.start_time.unwrap_or(0)) .bind(r.end_time.unwrap_or(0)) - .bind(r.filesize.unwrap_or(0)) - .bind(r.file_md5.clone().unwrap_or_default()) + .bind(filesize) + .bind(file_md5) .bind(version_text(r)) .bind(r.timezone) .bind(r.zonemins) @@ -629,8 +651,13 @@ async fn client_for(state: &AppState, conn: &ConnRow) -> Result<(Client, Option< }; let client = match &workspace { Some(ws) => match account.workspace_token(ws).await { - Ok(wt) => Client::new(&conn.api_base, &wt), - Err(_) => account, + Ok(wt) => Client::with_workspace(&conn.api_base, &wt, Some(ws.clone()), Some(token)), + Err(_) => Client::with_workspace( + &conn.api_base, + &token, + Some(ws.clone()), + Some(token.clone()), + ), }, None => account, }; @@ -673,13 +700,8 @@ pub async fn push_title(state: &AppState, user_id: &str, recording_id: &str, tit .await .map_err(|_| "no usable Plaud token".to_owned())?; client - .api( - reqwest::Method::PATCH, - &format!("/file/{file_id}"), - Some(serde_json::json!({ "filename": title })), - ) + .rename_file(&file_id, title) .await - .map(|_| ()) .map_err(|e| super::api::describe(&e)) } .await; @@ -767,6 +789,8 @@ mod title_tests { timezone: None, zonemins: None, scene: None, + edit_time: None, + is_v4: false, } } diff --git a/crates/oah/src/plaud/v4.rs b/crates/oah/src/plaud/v4.rs new file mode 100644 index 0000000..1bdd4f5 --- /dev/null +++ b/crates/oah/src/plaud/v4.rs @@ -0,0 +1,638 @@ +//! Plaud 4.0 models, ID mapping, timezone conversion, and response adapters. + +use serde::Deserialize; +use serde_json::{Value, json}; + +use super::api::{PlaudError, Recording}; + +pub const MIGRATED_STATUS: i64 = -1800907; +pub const VERSION_CONFLICT_STATUS: i64 = -1800313; +pub const TOKEN_REVOKED_STATUSES: [i64; 2] = [-419, -420]; + +fn null_as_default<'de, D, T>(d: D) -> Result +where + D: serde::Deserializer<'de>, + T: Default + Deserialize<'de>, +{ + Ok(Option::::deserialize(d)?.unwrap_or_default()) +} + +pub fn has_non_empty(opt: &Option) -> bool { + opt.as_deref().is_some_and(|s| !s.trim().is_empty()) +} + +pub fn is_token_revoked_status(status: i64) -> bool { + TOKEN_REVOKED_STATUSES.contains(&status) +} + +/// True when a Plaud API error indicates the account moved to 4.0 routes. +pub fn is_migrated_error(err: &PlaudError) -> bool { + match err { + PlaudError::Api { status, message } => { + *status == MIGRATED_STATUS || message.to_ascii_lowercase().contains("migrated to 4.0") + } + _ => false, + } +} + +/// True when a raw Plaud JSON body indicates the account moved to 4.0 routes. +pub fn is_migrated_payload(body: &Value) -> bool { + if body.get("status").and_then(Value::as_i64) == Some(MIGRATED_STATUS) { + return true; + } + body.get("msg") + .and_then(Value::as_str) + .is_some_and(|m| m.to_ascii_lowercase().contains("migrated to 4.0")) +} + +/// Converts a 4.0 `file_id` to the local `plaud_file_id` stored in SQLite. +/// Pre-migration items drop their `of_` prefix so they match existing rows and tombstones. +pub fn v4_file_id_to_local(v4_id: &str) -> String { + v4_id.strip_prefix("of_").unwrap_or(v4_id).to_owned() +} + +/// Converts a local `plaud_file_id` to the 4.0 `file_id` expected by `/file-app/v4/` routes. +pub fn local_id_to_v4_file_id(local_id: &str) -> String { + if local_id.starts_with("f_") || local_id.starts_with("of_") { + local_id.to_owned() + } else { + format!("of_{local_id}") + } +} + +#[derive(Debug, Clone, Deserialize)] +pub struct V4RecordingItem { + pub file_id: String, + #[allow(dead_code)] + #[serde(default)] + pub node_id: Option, + #[allow(dead_code)] + #[serde(default)] + pub parent_id: Option, + #[serde(default)] + pub name: Option, + #[serde(default)] + pub duration_ms: Option, + #[serde(default)] + pub created_at_show_ms: Option, + #[serde(default)] + pub updated_at_show_ms: Option, + #[serde(default)] + pub version_ms: Option, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct V4FolderInfo { + #[serde(default, alias = "node_id", alias = "id")] + pub folder_id: Option, + #[serde(default)] + pub name: Option, + #[serde(default)] + pub version_ms: Option, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct V4Breadcrumb { + #[serde(default, alias = "folder_id", alias = "id")] + pub node_id: Option, + #[allow(dead_code)] + #[serde(default)] + pub name: Option, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct V4Node { + #[serde(default)] + pub node_id: Option, + #[serde(default)] + pub parent_id: Option, + #[serde(default)] + pub name: Option, + #[serde(default)] + pub version_ms: Option, + #[serde(default)] + pub folder: Option, + #[serde(default)] + pub display_folder: Option, + #[serde(default, deserialize_with = "null_as_default")] + pub breadcrumbs: Vec, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct V4FileMeta { + #[allow(dead_code)] + #[serde(default)] + pub file_id: Option, + #[serde(default)] + pub duration: Option, + #[serde(default)] + pub size_bytes: Option, + #[serde(default)] + pub timezone_iana: Option, + #[serde(default)] + pub start_time: Option, + #[serde(default)] + pub serial_number: Option, + #[serde(default)] + pub version_ms: Option, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct V4FileObject { + #[serde(default)] + pub object_id: Option, + #[serde(default)] + pub object_type: Option, + #[serde(default)] + pub content_id: Option, + #[serde(default)] + pub content_url: Option, + #[allow(dead_code)] + #[serde(default)] + pub mime_type: Option, + #[serde(default)] + pub object_size: Option, + #[serde(default)] + pub task_status: Option, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct V4FileDetail { + #[serde(default)] + pub node: Option, + #[serde(default)] + pub meta: Option, + #[serde(default, deserialize_with = "null_as_default")] + pub objects: Vec, +} + +/// Maps a 4.0 list item into the shared `Recording` shape before lazy detail hydration. +pub fn map_list_item(item: &V4RecordingItem) -> Recording { + let edit_time = match (item.updated_at_show_ms, item.version_ms) { + (Some(u), Some(v)) => Some(u.max(v)), + (u, v) => u.or(v), + }; + let version_ms = item + .version_ms + .or(item.updated_at_show_ms) + .or(item.created_at_show_ms); + let start_time = item.created_at_show_ms; + let end_time = start_time.map(|start| start.saturating_add(item.duration_ms.unwrap_or(0))); + Recording { + id: v4_file_id_to_local(&item.file_id), + filename: item.name.clone(), + duration: item.duration_ms, + start_time, + end_time, + filesize: None, + file_md5: None, + serial_number: None, + version_ms, + timezone: None, + zonemins: None, + scene: None, + edit_time, + is_v4: true, + } +} + +/// Sorts recordings by edit time descending, breaking ties by start time descending. +pub fn sort_recordings_desc(items: &mut [Recording]) { + items.sort_by(|a, b| { + b.edit_time + .unwrap_or(0) + .cmp(&a.edit_time.unwrap_or(0)) + .then_with(|| b.start_time.unwrap_or(0).cmp(&a.start_time.unwrap_or(0))) + }); +} + +/// Converts an IANA timezone name into `(hours, minutes)` UTC offset at `at_ms`. +pub fn iana_offset_parts(iana: &str, at_ms: i64) -> Option<(i64, i64)> { + let iana = iana.trim(); + if iana.is_empty() { + return None; + } + let tz = jiff::tz::TimeZone::get(iana).ok()?; + let ts = jiff::Timestamp::from_millisecond(at_ms).ok()?; + let offset = tz.to_offset(ts); + let total_mins = i64::from(offset.seconds()) / 60; + Some((total_mins / 60, total_mins % 60)) +} + +/// Ranks playable audio objects: `AUDIO_TRANSCODED` first, then `AUDIO`, +/// preferring objects with a direct `content_url` over `content_id`-only entries. +pub fn select_audio_candidates(detail: &V4FileDetail) -> Vec<&V4FileObject> { + let mut ranked: Vec<(u8, u8, &V4FileObject)> = detail + .objects + .iter() + .filter_map(|obj| { + let type_rank = match obj.object_type.as_deref()?.to_ascii_uppercase().as_str() { + "AUDIO_TRANSCODED" => 0, + "AUDIO" => 1, + _ => return None, + }; + let has_url = has_non_empty(&obj.content_url); + let has_id = has_non_empty(&obj.content_id); + if !has_url && !has_id { + return None; + } + let source_rank = if has_url { 0 } else { 1 }; + Some((type_rank, source_rank, obj)) + }) + .collect(); + ranked.sort_by_key(|&(t, s, _)| (t, s)); + ranked.into_iter().map(|(_, _, obj)| obj).collect() +} + +pub fn is_content_object(obj: &V4FileObject) -> bool { + matches!( + obj.object_type + .as_deref() + .map(str::to_ascii_uppercase) + .as_deref(), + Some("TRANSCRIPT" | "SUMMARY") + ) +} + +/// Ready when `task_status == 1` with either URL or content id, or migrated (`task_status == null`) with a URL. +pub fn is_object_ready(obj: &V4FileObject) -> bool { + let has_url = has_non_empty(&obj.content_url); + let has_id = has_non_empty(&obj.content_id); + match obj.task_status { + Some(1) => has_url || has_id, + None => has_url, + _ => false, + } +} + +/// Applies hydrated 4.0 detail fields onto a list recording while keeping the list's change token. +pub fn apply_detail(base: &Recording, detail: &V4FileDetail) -> Recording { + let mut out = base.clone(); + out.is_v4 = true; + + if let Some(node) = &detail.node { + if let Some(name) = node + .name + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + { + out.filename = Some(name.to_owned()); + } + if out.version_ms.is_none() { + out.version_ms = node.version_ms; + } + } + + if let Some(meta) = &detail.meta { + if let Some(sn) = meta + .serial_number + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + { + out.serial_number = Some(sn.to_owned()); + } + if let Some(duration) = meta.duration.filter(|&d| d > 0) { + out.duration = Some(duration); + } else if out.duration.is_none() { + out.duration = meta.duration; + } + if let Some(start) = meta.start_time.filter(|&t| t > 0) { + out.start_time = Some(start); + } else if out.start_time.is_none() { + out.start_time = meta.start_time; + } + if out.version_ms.is_none() { + out.version_ms = meta.version_ms; + } + if let Some(iana) = meta.timezone_iana.as_deref() { + let at_ms = out.start_time.unwrap_or(0); + if let Some((hours, mins)) = iana_offset_parts(iana, at_ms) { + out.timezone = Some(hours); + out.zonemins = Some(mins); + } + } + } + + let audio_size = select_audio_candidates(detail) + .first() + .and_then(|o| o.object_size) + .filter(|&s| s > 0); + let meta_size = detail + .meta + .as_ref() + .and_then(|m| m.size_bytes) + .filter(|&s| s > 0); + if let Some(size) = audio_size.or(meta_size) { + out.filesize = Some(size); + } + + if let Some(start) = out.start_time { + out.end_time = Some(start.saturating_add(out.duration.unwrap_or(0))); + } + + out +} + +/// Adapts ready 4.0 `TRANSCRIPT` and `SUMMARY` objects into the legacy `content_list` payload. +pub fn detail_to_legacy_content(detail: &V4FileDetail) -> Value { + let mut content_list = Vec::new(); + for obj in &detail.objects { + if !is_object_ready(obj) { + continue; + } + let Some(url) = obj + .content_url + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + else { + continue; + }; + let obj_id = obj + .object_id + .as_deref() + .or(obj.content_id.as_deref()) + .unwrap_or_default(); + match obj + .object_type + .as_deref() + .map(str::to_ascii_uppercase) + .as_deref() + { + Some("TRANSCRIPT") => { + content_list.push(json!({ + "data_id": format!("source_transaction:{obj_id}"), + "data_type": "transaction", + "data_link": url, + "task_status": 1, + })); + } + Some("SUMMARY") => { + content_list.push(json!({ + "data_id": format!("auto_sum:{obj_id}"), + "data_type": "auto_sum_note", + "data_link": url, + "task_status": 1, + })); + } + _ => {} + } + } + json!({ + "content_list": content_list, + "pre_download_content_list": [], + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn object( + object_type: &str, + content_url: Option<&str>, + content_id: Option<&str>, + task_status: Option, + ) -> V4FileObject { + V4FileObject { + object_id: Some(format!("obj_{object_type}")), + object_type: Some(object_type.to_owned()), + content_id: content_id.map(str::to_owned), + content_url: content_url.map(str::to_owned), + mime_type: None, + object_size: None, + task_status, + } + } + + #[test] + fn migrated_detection_checks_status_and_message() { + assert!(is_migrated_error(&PlaudError::Api { + status: MIGRATED_STATUS, + message: "anything".into(), + })); + assert!(is_migrated_error(&PlaudError::Api { + status: -1, + message: "Account Migrated To 4.0 already".into(), + })); + assert!(!is_migrated_error(&PlaudError::Api { + status: -419, + message: "token revoked".into(), + })); + assert!(!is_migrated_error(&PlaudError::Upstream)); + + assert!(is_migrated_payload(&json!({ "status": -1800907 }))); + assert!(is_migrated_payload( + &json!({ "status": -1, "msg": "User migrated to 4.0" }) + )); + assert!(!is_migrated_payload(&json!({ "status": 0, "msg": "ok" }))); + } + + #[test] + fn id_mapping_strips_of_and_keeps_f_prefix() { + assert_eq!(v4_file_id_to_local("of_abc123"), "abc123"); + assert_eq!(v4_file_id_to_local("f_new999"), "f_new999"); + assert_eq!(v4_file_id_to_local("plain_id"), "plain_id"); + + assert_eq!(local_id_to_v4_file_id("abc123"), "of_abc123"); + assert_eq!(local_id_to_v4_file_id("of_abc123"), "of_abc123"); + assert_eq!(local_id_to_v4_file_id("f_new999"), "f_new999"); + } + + #[test] + fn list_mapping_and_desc_sorting_handle_edits_and_ties() { + let older_edited = map_list_item(&V4RecordingItem { + file_id: "of_old".into(), + node_id: Some("n1".into()), + parent_id: Some("p1".into()), + name: Some("Old Edited".into()), + duration_ms: Some(5_000), + created_at_show_ms: Some(1_000), + updated_at_show_ms: Some(9_000), + version_ms: Some(8_000), + }); + assert_eq!(older_edited.id, "old"); + assert_eq!(older_edited.edit_time, Some(9_000)); + assert_eq!(older_edited.version_ms, Some(8_000)); + assert_eq!(older_edited.end_time, Some(6_000)); + assert!(older_edited.is_v4); + + let tie_newer_start = map_list_item(&V4RecordingItem { + file_id: "f_tie_a".into(), + node_id: None, + parent_id: None, + name: Some("Tie Newer".into()), + duration_ms: Some(1_000), + created_at_show_ms: Some(5_000), + updated_at_show_ms: None, + version_ms: Some(7_000), + }); + let tie_older_start = map_list_item(&V4RecordingItem { + file_id: "f_tie_b".into(), + node_id: None, + parent_id: None, + name: Some("Tie Older".into()), + duration_ms: Some(1_000), + created_at_show_ms: Some(4_000), + updated_at_show_ms: Some(7_000), + version_ms: None, + }); + assert_eq!(tie_older_start.version_ms, Some(7_000)); + + let mut list = vec![tie_older_start, tie_newer_start, older_edited]; + sort_recordings_desc(&mut list); + let ids: Vec<&str> = list.iter().map(|r| r.id.as_str()).collect(); + assert_eq!(ids, vec!["old", "f_tie_a", "f_tie_b"]); + } + + #[test] + fn iana_offset_handles_dst_and_fractional_zones() { + // 2026-01-15T12:00:00Z (winter) vs 2026-07-15T12:00:00Z (summer) + let winter = 1_768_478_400_000; + let summer = 1_784_116_800_000; + assert_eq!( + iana_offset_parts("America/Los_Angeles", winter), + Some((-8, 0)) + ); + assert_eq!( + iana_offset_parts("America/Los_Angeles", summer), + Some((-7, 0)) + ); + assert_eq!(iana_offset_parts("Asia/Kolkata", winter), Some((5, 30))); + assert_eq!( + iana_offset_parts("America/St_Johns", winter), + Some((-3, -30)) + ); + assert_eq!(iana_offset_parts("", winter), None); + assert_eq!(iana_offset_parts("Not/A_Zone", winter), None); + } + + #[test] + fn audio_candidates_prefer_transcoded_and_direct_url() { + let detail = V4FileDetail { + node: None, + meta: None, + objects: vec![ + object("TRANSCRIPT", Some("https://t"), None, Some(1)), + object("AUDIO", Some("https://raw"), None, Some(1)), + object("AUDIO_TRANSCODED", None, Some("cid_tr"), Some(1)), + object("AUDIO_TRANSCODED", Some("https://tr"), None, Some(1)), + object("AUDIO", None, None, Some(0)), + ], + }; + let ranked = select_audio_candidates(&detail); + assert_eq!(ranked.len(), 3); + assert_eq!(ranked[0].content_url.as_deref(), Some("https://tr")); + assert_eq!(ranked[1].content_id.as_deref(), Some("cid_tr")); + assert_eq!(ranked[2].content_url.as_deref(), Some("https://raw")); + } + + #[test] + fn object_readiness_handles_status_1_and_migrated_null() { + assert!(is_object_ready(&object( + "TRANSCRIPT", + Some("https://t"), + None, + Some(1) + ))); + assert!(is_object_ready(&object( + "TRANSCRIPT", + None, + Some("cid"), + Some(1) + ))); + assert!(!is_object_ready(&object("TRANSCRIPT", None, None, Some(1)))); + assert!(is_object_ready(&object( + "SUMMARY", + Some("https://s"), + None, + None + ))); + assert!(!is_object_ready(&object( + "SUMMARY", + None, + Some("cid"), + None + ))); + assert!(!is_object_ready(&object( + "SUMMARY", + Some("https://s"), + None, + Some(0) + ))); + } + + #[test] + fn apply_detail_and_legacy_content_adapter_map_fields() { + let base = map_list_item(&V4RecordingItem { + file_id: "f_1".into(), + node_id: Some("node_f_1".into()), + parent_id: Some("wrap_f_1".into()), + name: Some("Initial".into()), + duration_ms: Some(1_000), + created_at_show_ms: Some(1_768_478_400_000), + updated_at_show_ms: Some(1_768_478_500_000), + version_ms: Some(42), + }); + let detail = V4FileDetail { + node: Some(V4Node { + node_id: Some("node_f_1".into()), + parent_id: Some("wrap_f_1".into()), + name: Some("Hydrated Name".into()), + version_ms: Some(99), + folder: None, + display_folder: None, + breadcrumbs: vec![], + }), + meta: Some(V4FileMeta { + file_id: Some("f_1".into()), + duration: Some(12_000), + size_bytes: Some(8_192), + timezone_iana: Some("Asia/Kolkata".into()), + start_time: Some(1_768_478_400_000), + serial_number: Some("SN-400".into()), + version_ms: Some(99), + }), + objects: vec![ + object("TRANSCRIPT", Some("https://t"), None, None), + object("SUMMARY", Some("https://s"), None, Some(1)), + object("SUMMARY", Some("https://pending"), None, Some(0)), + ], + }; + let hydrated = apply_detail(&base, &detail); + assert_eq!(hydrated.filename.as_deref(), Some("Hydrated Name")); + assert_eq!(hydrated.serial_number.as_deref(), Some("SN-400")); + assert_eq!(hydrated.duration, Some(12_000)); + assert_eq!(hydrated.end_time, Some(1_768_478_412_000)); + assert_eq!(hydrated.filesize, Some(8_192)); + assert_eq!(hydrated.timezone, Some(5)); + assert_eq!(hydrated.zonemins, Some(30)); + // Preserves list version_ms so sync change detection matches next pass. + assert_eq!(hydrated.version_ms, Some(42)); + + let mut detail_with_transcoded = detail.clone(); + detail_with_transcoded.objects.push(V4FileObject { + object_id: Some("obj_tr_audio".into()), + object_type: Some("AUDIO_TRANSCODED".into()), + content_id: Some("cid_tr_audio".into()), + content_url: Some("https://audio.mp3".into()), + mime_type: Some("audio/mpeg".into()), + object_size: Some(4_096), + task_status: Some(1), + }); + let hydrated_transcoded = apply_detail(&base, &detail_with_transcoded); + assert_eq!(hydrated_transcoded.filesize, Some(4_096)); + + let legacy = detail_to_legacy_content(&detail); + let items = legacy["content_list"].as_array().unwrap(); + assert_eq!(items.len(), 2); + assert_eq!(items[0]["data_id"], "source_transaction:obj_TRANSCRIPT"); + assert_eq!(items[0]["data_type"], "transaction"); + assert_eq!(items[0]["task_status"], 1); + assert_eq!(items[1]["data_id"], "auto_sum:obj_SUMMARY"); + assert_eq!(items[1]["data_type"], "auto_sum_note"); + assert_eq!(items[1]["task_status"], 1); + } +} From 7eedb52a9953800b51cdf29f90ca782519d3636c Mon Sep 17 00:00:00 2001 From: Jeremy Liu Date: Fri, 20 Nov 2026 17:38:57 -0800 Subject: [PATCH 2/6] fix(web): avoid duplicating Sync failed prefix in sync error toast --- web/src/components/shell/shell-data.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/src/components/shell/shell-data.tsx b/web/src/components/shell/shell-data.tsx index 6719aa7..29db2d7 100644 --- a/web/src/components/shell/shell-data.tsx +++ b/web/src/components/shell/shell-data.tsx @@ -199,7 +199,8 @@ export function ShellDataProvider({ children }: { children: ReactNode }) { if (failed) { const message = problem ?? "Sync failed"; setSync((s) => ({ status: "failed", lastSyncAt: s.lastSyncAt, error: message })); - if (!options.quiet) toast.error(`Sync failed: ${message}`); + const toastMessage = message.startsWith("Sync failed:") ? message : `Sync failed: ${message}`; + if (!options.quiet) toast.error(toastMessage); } else { setSync({ status: "ok", lastSyncAt: Date.now(), error: null }); if (!options.quiet && result.newRecordings === 0) toast.success("Plaud is up to date"); From 3e5fe2b67c198378d40178936c02d1e1a3cc4591 Mon Sep 17 00:00:00 2001 From: Jeremy Liu Date: Fri, 20 Nov 2026 17:38:57 -0800 Subject: [PATCH 3/6] docs(spec): document Plaud 4.0 endpoints and sync behavior --- spec/plaud.md | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/spec/plaud.md b/spec/plaud.md index eb13e2d..c86ecf7 100644 --- a/spec/plaud.md +++ b/spec/plaud.md @@ -319,7 +319,7 @@ The page does not talk to the extension directly. The extension posts the messag Run by every sign-in path before anything is saved. 1. Find the personal workspace: list workspaces and pick the one with type `"0"`, falling back to the first. If listing fails or the account has no workspace, continue without a workspace id. -2. Validate the account: when a workspace was found, mint a workspace token for it and fetch the device list with that token. Otherwise fetch the device list with the account token. If minting the workspace token fails, the request fails with `PLAUD_WORKSPACE_UNAVAILABLE`. A refusal of the device list is returned to the caller (`PLAUD_INVALID_TOKEN` when Plaud rejects the token). Nothing is saved. +2. Validate the account: when a workspace was found, mint a workspace token for it and fetch the device list with that token. Otherwise fetch the device list with the account token. If minting the workspace token fails, the request fails with `PLAUD_WORKSPACE_UNAVAILABLE`. If `/device/list` returns HTTP 200 with `status: -1800907` (or a message containing `migrated to 4.0`), the check falls back to `GET /file-app/v4/recordings/all?page_size=1` and returns an empty device list on success. Any other refusal of the device list is returned to the caller (`PLAUD_INVALID_TOKEN` when Plaud rejects the token). Nothing is saved. 3. Save the connection and upsert devices in one transaction. ## Manual sync @@ -370,7 +370,7 @@ Exporting is not part of the run. Recordings added by the run are marked for the ### Listing recordings -- Plaud's recording list is requested sorted by edit time, newest first, with trashed recordings excluded. +- Plaud's recording list is requested sorted by edit time, newest first, with trashed recordings excluded. On Plaud 4.0 accounts, the client fetches `GET /file-app/v4/recordings/all` (up to 400 pages with cursor cycle protection), sorts the collected items in memory by edit time descending, and serves them in 50-item pages. - Pages of 50 recordings. Stop when a page has fewer than 50 recordings, or a page is empty. - Stop after two consecutive pages that have no new recording, no updated recording and no imported content. - A failure to read a page stops the run with the error text in `errors`. The last sync time is not updated in that case. @@ -381,7 +381,7 @@ Plaud recording to the OpenAudioHub recording row: | Plaud field | Recording field | Notes | | --- | --- | --- | -| `id` | `plaudFileId` | Unique per user. | +| `id` | `plaudFileId` | Unique per user. On Plaud 4.0, pre-migration ids (`of_`) strip `of_` to match existing rows and tombstones, and new v4 ids (`f_`) are kept as-is. | | `filename` | `filename` | Stored encrypted, returned decrypted. On insert, replaced by `YYYY-MM-DD` (server local time) when `recordingTitleMode` is `date`, and kept as Plaud's name when the recording has no start time. On update, set to Plaud's name only while the stored title is still the original one; a date, generated, hand-edited or unknown title is kept. | | `duration` (ms) | `duration` | Milliseconds. | | `start_time` (epoch ms) | `startTime` | | @@ -429,7 +429,7 @@ When `importPlaudContent` is on, sync also fills gaps from Plaud's own transcrip - A transcript is imported when the recording has no Plaud transcript yet. It is stored with source `plaud`, provider `plaud`, model `plaud-native`. No webhook event is sent for it. When the import adds a transcript and auto-summarize is on with no summary yet, an automatic summary job is queued once (`workers.md`, section 5). A repeated sync does not queue another. - A summary is imported when the recording has no summary with content yet. It is stored with source `plaud`. The inline copy is preferred over the presigned link. -- Items must be ready (`task_status` equal to 1) and have a link. +- Items must be ready (`task_status` equal to `1`, or `null` with `content_url` on Plaud 4.0) and have a link. - If the account token is refused during this pass, the pass stops. Other failures skip one item. Audio already synced is kept. ### Sync errors @@ -552,10 +552,15 @@ The app calls Plaud over HTTPS. Plaud is the third-party service. Hosts are the - `User-Agent`: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36`. Set on API calls. Presigned downloads and content links send no User-Agent from the app. - `Content-Type: application/json` on JSON API calls. - `Authorization: Bearer ` on calls that need auth. The token is the workspace token when one is in use, otherwise the account token. Sign-in calls and presigned downloads have no `Authorization` header. +- On Plaud 4.0 calls (`/file-app/v4/...`): `app-platform: web`, `edit-from: web`, and `x-scope-type: workspace` with `x-scope-id: ` when a workspace id is known. ### Plaud status convention -Most JSON responses carry a `status` field alongside HTTP 200. `status: 0` means success. Any other value is a business error with a `msg`. The one exception is `status: -302` on the OTP send, which means "try this regional base" (see below). +Most JSON responses carry a `status` field alongside HTTP 200. `status: 0` means success. Any other value is a business error with a `msg`. The exceptions are: + +- `status: -302` on the OTP send, which means "try this regional base" (see below). +- `status: -1800907` (or a message containing `migrated to 4.0`) on a legacy `/file/...` call or `/device/list`, which means the account has migrated to Plaud 4.0. The client switches to `/file-app/v4/` routes for the rest of the run. Non-migrated accounts keep using legacy routes unchanged. +- `status: -419` or `status: -420` mid-sync, which means the workspace token needs to be re-minted once from the account token and the call retried. HTTP errors are mapped as: 401 means the token is rejected, 429 means rate limited, 5xx means upstream, other 4xx means an API error with Plaud's `msg`. A body that is not JSON is mapped the same way by status. @@ -600,7 +605,7 @@ HTTP errors are mapped as: 401 means the token is rejected, 429 means rate limit - Auth: workspace token when one is in use, otherwise the account token. - Reads: `status`, `msg`, `data_devices[]` with `sn`, `name`, `model`, `version_number`. -- Validation: `status` must be 0 and `data_devices` must be an array. Otherwise `PLAUD_API_ERROR`. +- Validation: `status` must be 0 and `data_devices` must be an array. If `status` is `-1800907` (or the message contains `migrated to 4.0`), connect checks fall back to `GET {base}/file-app/v4/recordings/all?page_size=1` and return an empty device list on success. Otherwise `PLAUD_API_ERROR`. - Used at connect time to validate the token. ### 7. GET {base}/file/simple/web?skip={n}&limit={n}&is_trash=0&sort_by=edit_time&is_desc=true @@ -645,6 +650,17 @@ HTTP errors are mapped as: 401 means the token is rejected, 429 means rate limit - Used only when the user chooses to send an AI-generated title back to Plaud. Failure is logged and does not change the local title. - Reads: `status`. +### Plaud 4.0 (/file-app/v4/) + +Used when a legacy `/file/...` call or `/device/list` returns `status: -1800907` (or a message containing `migrated to 4.0`). + +- **ID mapping.** Pre-migration ids (`of_`) strip `of_` to match existing `plaudFileId` rows and tombstones. New v4 ids (`f_`) are kept as-is. When calling v4 endpoints for a bare legacy id, the client prepends `of_`. +- **Listing.** `GET {base}/file-app/v4/recordings/all?sort_by=created&sort_order=desc&page_size=50[&cursor=]` fetches up to 400 pages with cursor cycle protection. Items are sorted in memory by edit time descending and served in 50-item pages. +- **Detail and lazy hydration.** `GET {base}/file-app/v4/files/detail/{file_id}` is called only for new or changed recordings. It hydrates `serial_number`, `start_time`, `duration`, `filesize`, and IANA `timezone_iana` (converted to `timezone` and `zonemins` offsets). When `filesize` or `file_md5` is omitted, the downloaded audio byte size and MD5 are used as fallback. +- **Audio selection.** Selects `AUDIO_TRANSCODED` first, then `AUDIO`. If `content_url` is missing on the selected item, it is resolved via `POST {base}/file-app/v4/files/content/url`. +- **Content import.** `TRANSCRIPT` and `SUMMARY` items are ready when `task_status` is `1` or `null` with `content_url`. Summary payloads may be JSON or plain Markdown. Transcript payloads may be wrapped segment arrays. +- **Title push.** `PATCH {base}/file-app/v4/nodes/rename/{node_id}` sends `origin_version` and retries once on `status: -1800313`. When the container folder's name still matches the file's previous name, the client also renames the folder best-effort. + ### Direct calls All Plaud calls go directly from the server to the Plaud host. The app has no outbound proxy for Plaud calls. From 8125c8591eae03b733d38ded2ba1ed9cc97d5fee Mon Sep 17 00:00:00 2001 From: Jeremy Liu Date: Fri, 20 Nov 2026 17:38:57 -0800 Subject: [PATCH 4/6] test(contract): cover Plaud 4.0 migration, pagination, audio, content, and rename --- contract-tests/fakes/fake-plaud.mjs | 343 ++++++++++++++++++- contract-tests/tests/plaud.spec.ts | 493 +++++++++++++++++++++++++++- 2 files changed, 817 insertions(+), 19 deletions(-) diff --git a/contract-tests/fakes/fake-plaud.mjs b/contract-tests/fakes/fake-plaud.mjs index e899800..77ab77e 100644 --- a/contract-tests/fakes/fake-plaud.mjs +++ b/contract-tests/fakes/fake-plaud.mjs @@ -12,12 +12,18 @@ const origin = `http://127.0.0.1:${port}`; const AUDIO = readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), "../fixtures/tone-3s.mp3")); const DAY = 24 * 3600; -const accounts = new Map(); // email -> { recordings: Map, devices: [], revoked: boolean } +const accounts = new Map(); // email -> { recordings: Map, devices: [], revoked: boolean, migratedV4: boolean } const tokens = new Map(); // bearer -> { kind: "ut" | "wt", email } -const failures = []; // { path, status, retryAfter?, email?, times } -const log = []; // { method, path, query, ua, auth, at } +const contentUrls = new Map(); // content_id -> url +const failures = []; // { path, status, plaudStatus?, msg?, retryAfter?, email?, times } +const log = []; // { method, path, query, ua, auth, "x-scope-type", "x-scope-id", at } let seq = 0; +const MIGRATED_BODY = { + status: -1800907, + msg: "Account migrated to 4.0. Please update and reinitialize.", +}; + const b64 = (o) => Buffer.from(JSON.stringify(o)).toString("base64url"); const jwt = (claims) => `${b64({ alg: "HS256", typ: "JWT" })}.${b64(claims)}.ZmFrZQ`; const now = () => Math.floor(Date.now() / 1000); @@ -29,17 +35,39 @@ function account(email) { recordings: new Map(), devices: [{ sn: "SN-FAKE-1", name: "Fake Pin", model: "PLAUD-NOTE", version_number: 1234 }], revoked: false, + migratedV4: false, }); } return accounts.get(email); } +function v4FileIdOf(rec) { + if (rec.v4FileId) return rec.v4FileId; + if (rec.isV4New || rec.id.startsWith("f_") || rec.id.startsWith("of_")) return rec.id; + return `of_${rec.id}`; +} + +function findByV4FileId(acct, fileId) { + const direct = acct.recordings.get(fileId); + if (direct) return direct; + if (fileId.startsWith("of_")) { + const stripped = acct.recordings.get(fileId.slice(3)); + if (stripped) return stripped; + } + for (const rec of acct.recordings.values()) { + if (v4FileIdOf(rec) === fileId) return rec; + } + return null; +} + function recordingDefaults(partial) { const start = Date.now() - 3600_000; const id = partial.id ?? `rec-${++seq}`; + const filename = partial.filename ?? `Fake recording ${id}`; + const versionMs = partial.version_ms ?? 1000; return { id, - filename: `Fake recording ${id}`, + filename, keywords: [], filesize: AUDIO.length, filetype: "mp3", @@ -47,7 +75,7 @@ function recordingDefaults(partial) { file_md5: "d41d8cd98f00b204e9800998ecf8427e", ori_ready: true, version: 1, - version_ms: 1000, + version_ms: versionMs, edit_time: start, edit_from: "web", is_trash: false, @@ -61,9 +89,32 @@ function recordingDefaults(partial) { serial_number: "SN-FAKE-1", is_trans: false, is_summary: false, + timezone_iana: null, + audioMode: "transcoded", + migratedContentStatusNull: false, + summaryMarkdown: false, + v4FileId: undefined, + isV4New: false, + renameConflictOnce: false, + folderName: partial.folderName ?? filename, + folderVersionMs: partial.folderVersionMs ?? versionMs, }; } +function logRequest(req, path, query) { + const bearer = bearerOf(req); + log.push({ + method: req.method, + path, + query, + ua: req.headers["user-agent"] ?? null, + auth: bearer ? (tokens.get(bearer)?.kind ?? "unknown") : "none", + "x-scope-type": req.headers["x-scope-type"] ?? null, + "x-scope-id": req.headers["x-scope-id"] ?? null, + at: Date.now(), + }); +} + function bearerOf(req) { const h = req.headers.authorization ?? ""; return h.startsWith("Bearer ") ? h.slice(7) : null; @@ -98,6 +149,7 @@ async function control(req, res, url, body) { if (url.pathname === "/__control/reset") { accounts.clear(); tokens.clear(); + contentUrls.clear(); failures.length = 0; log.length = 0; return send(res, 200, { ok: true }); @@ -105,6 +157,7 @@ async function control(req, res, url, body) { if (url.pathname === "/__control/account") { const acct = account(data.email); if (typeof data.revoked === "boolean") acct.revoked = data.revoked; + if (typeof data.migratedV4 === "boolean") acct.migratedV4 = data.migratedV4; if (Array.isArray(data.devices)) acct.devices = data.devices; for (const partial of data.recordings ?? []) { const prev = acct.recordings.get(partial.id); @@ -114,7 +167,15 @@ async function control(req, res, url, body) { return send(res, 200, { ok: true, recordings: acct.recordings.size }); } if (url.pathname === "/__control/fail") { - failures.push({ path: data.path, status: data.status, retryAfter: data.retryAfter, email: data.email, times: data.times ?? 1 }); + failures.push({ + path: data.path, + status: data.status ?? 200, + plaudStatus: data.plaudStatus, + msg: data.msg, + retryAfter: data.retryAfter, + email: data.email, + times: data.times ?? 1, + }); return send(res, 200, { ok: true }); } if (url.pathname === "/__control/requests") return send(res, 200, { requests: log }); @@ -129,12 +190,13 @@ const server = createServer(async (req, res) => { // Audio and content links are presigned in production, so these routes take no bearer token. const audio = url.pathname.match(/^\/audio\/[^/]+$/); if (req.method === "GET" && audio) { - log.push({ method: "GET", path: url.pathname, query: "", ua: req.headers["user-agent"] ?? null, auth: "none", at: Date.now() }); + logRequest(req, url.pathname, ""); res.writeHead(200, { "content-type": "audio/mpeg", "content-length": AUDIO.length }); return res.end(AUDIO); } const content = url.pathname.match(/^\/content\/([^/]+)\/(transcript|summary)\.json$/); if (req.method === "GET" && content) { + logRequest(req, url.pathname, ""); const [, id, kind] = content; if (kind === "transcript") { const segments = [{ start_time: 0, end_time: 1000, speaker: "1", content: `Fake transcript line for ${id}` }]; @@ -144,25 +206,28 @@ const server = createServer(async (req, res) => { res.writeHead(200, { "content-type": "application/json" }); return res.end(JSON.stringify({ ai_content: "Fake summary from Plaud", key_points: ["Key point"] })); } + const mdSummary = url.pathname.match(/^\/content\/([^/]+)\/summary\.md$/); + if (req.method === "GET" && mdSummary) { + logRequest(req, url.pathname, ""); + res.writeHead(200, { "content-type": "application/octet-stream" }); + return res.end(gzipSync(Buffer.from("# Summary\nFake v4 markdown summary"))); + } // "/eu" and "/loop" are the regional prefixes the sign-in redirect hands out. They route to the same handlers. const path = url.pathname.replace(/^\/(eu|loop)(?=\/)/, ""); const auth = authed(req); - const bearer = bearerOf(req); - log.push({ - method: req.method, - path, - query: url.search.slice(1), - ua: req.headers["user-agent"] ?? null, - auth: bearer ? (tokens.get(bearer)?.kind ?? "unknown") : "none", - at: Date.now(), - }); + logRequest(req, path, url.search.slice(1)); const failure = takeFailure(path, auth?.email); if (failure) { const headers = failure.retryAfter !== undefined ? { "retry-after": String(failure.retryAfter) } : {}; - return send(res, failure.status, { status: -1, msg: "injected failure" }, headers); + return send( + res, + failure.status, + { status: failure.plaudStatus ?? -1, msg: failure.msg ?? "injected failure" }, + headers, + ); } if (req.method === "POST" && path === "/auth/otp-send-code") { @@ -214,7 +279,15 @@ const server = createServer(async (req, res) => { if (decodeURIComponent(wtMatch[1]) !== wsId(auth.email)) { return send(res, 404, { status: -1, msg: "Workspace not found" }); } - const wt = jwt({ sub: auth.email, iat: now(), exp: now() + DAY, ut_ref: "ut-ref", wid: wsId(auth.email), wtype: "0" }); + const wt = jwt({ + sub: auth.email, + iat: now(), + exp: now() + DAY, + jti: ++seq, + ut_ref: "ut-ref", + wid: wsId(auth.email), + wtype: "0", + }); tokens.set(wt, { kind: "wt", email: auth.email }); return send(res, 200, { status: 0, @@ -228,6 +301,7 @@ const server = createServer(async (req, res) => { } if (req.method === "GET" && path === "/file/simple/web") { + if (auth.acct.migratedV4) return send(res, 200, MIGRATED_BODY); const skip = Number(url.searchParams.get("skip") ?? 0); const limit = Number(url.searchParams.get("limit") ?? 50); const trash = url.searchParams.get("is_trash") === "1"; @@ -244,6 +318,7 @@ const server = createServer(async (req, res) => { const tempMatch = path.match(/^\/file\/temp-url\/([^/]+)$/); if (req.method === "GET" && tempMatch) { + if (auth.acct.migratedV4) return send(res, 200, MIGRATED_BODY); const id = tempMatch[1]; if (!auth.acct.recordings.has(id)) return send(res, 404, { status: -1, msg: "File not found" }); return send(res, 200, { @@ -255,6 +330,7 @@ const server = createServer(async (req, res) => { const detailMatch = path.match(/^\/file\/detail\/([^/]+)$/); if (req.method === "GET" && detailMatch) { + if (auth.acct.migratedV4) return send(res, 200, MIGRATED_BODY); const rec = auth.acct.recordings.get(detailMatch[1]); if (!rec) return send(res, 404, { status: -1, msg: "File not found" }); const content_list = []; @@ -291,6 +367,7 @@ const server = createServer(async (req, res) => { } if (req.method === "PATCH" && path.startsWith("/file/")) { + if (auth.acct.migratedV4) return send(res, 200, MIGRATED_BODY); const id = path.slice("/file/".length); const rec = auth.acct.recordings.get(id); if (!rec) return send(res, 404, { status: -1, msg: "File not found" }); @@ -298,6 +375,236 @@ const server = createServer(async (req, res) => { return send(res, 200, { status: 0, msg: "ok", data_file: {} }); } + if (req.method === "GET" && path === "/file-app/v4/recordings/all") { + const pageSize = Math.max(1, Number(url.searchParams.get("page_size") ?? 50)); + const cursorRaw = url.searchParams.get("cursor"); + const parsedOffset = cursorRaw ? Number(cursorRaw) : 0; + const offset = Number.isFinite(parsedOffset) && parsedOffset >= 0 ? parsedOffset : 0; + const all = [...auth.acct.recordings.values()] + .filter((r) => !r.is_trash) + .sort((a, b) => { + const diff = (b.created_at_show_ms ?? b.start_time) - (a.created_at_show_ms ?? a.start_time); + return diff !== 0 ? diff : b.id.localeCompare(a.id); + }); + const slice = all.slice(offset, offset + pageSize); + const items = slice.map((r) => { + const fileId = v4FileIdOf(r); + return { + file_id: fileId, + node_id: `node_${fileId}`, + parent_id: `wrap_${fileId}`, + name: r.filename, + duration_ms: r.duration, + created_at_show_ms: r.created_at_show_ms ?? r.start_time, + updated_at_show_ms: r.updated_at_show_ms ?? r.edit_time, + version_ms: r.version_ms, + }; + }); + const next_cursor = offset + slice.length < all.length ? String(offset + slice.length) : null; + return send(res, 200, { status: 0, msg: "ok", data: { items, next_cursor } }); + } + + const v4DetailMatch = path.match(/^\/file-app\/v4\/files\/detail\/([^/]+)$/); + if (req.method === "GET" && v4DetailMatch) { + const rec = findByV4FileId(auth.acct, decodeURIComponent(v4DetailMatch[1])); + if (!rec) return send(res, 404, { status: -1, msg: "File not found" }); + const fileId = v4FileIdOf(rec); + const nodeId = `node_${fileId}`; + const parentId = `wrap_${fileId}`; + const objects = []; + const audioMode = rec.audioMode ?? "transcoded"; + const mp3Url = `${origin}/audio/${rec.id}.mp3`; + const opusUrl = `${origin}/audio/${rec.id}.opus`; + const cidTranscoded = `cid_at_${fileId}`; + const cidAudio = `cid_a_${fileId}`; + contentUrls.set(cidTranscoded, mp3Url); + contentUrls.set(cidAudio, mp3Url); + + if (audioMode === "transcoded") { + objects.push({ + object_id: `obj_at_${fileId}`, + object_type: "AUDIO_TRANSCODED", + content_id: cidTranscoded, + content_url: mp3Url, + mime_type: "audio/mpeg", + object_size: rec.filesize, + task_status: 1, + }); + objects.push({ + object_id: `obj_a_${fileId}`, + object_type: "AUDIO", + content_id: cidAudio, + content_url: opusUrl, + mime_type: "audio/ogg", + object_size: rec.filesize, + task_status: 1, + }); + } else if (audioMode === "audio_only") { + objects.push({ + object_id: `obj_a_${fileId}`, + object_type: "AUDIO", + content_id: cidAudio, + content_url: mp3Url, + mime_type: "audio/mpeg", + object_size: rec.filesize, + task_status: 1, + }); + } else if (audioMode === "content_id_only") { + objects.push({ + object_id: `obj_a_${fileId}`, + object_type: "AUDIO", + content_id: cidAudio, + content_url: null, + mime_type: "audio/mpeg", + object_size: rec.filesize, + task_status: 1, + }); + } + + if (rec.is_trans) { + const cidTr = `cid_tr_${fileId}`; + const trUrl = `${origin}/content/${rec.id}/transcript.json`; + contentUrls.set(cidTr, trUrl); + objects.push({ + object_id: `obj_tr_${fileId}`, + object_type: "TRANSCRIPT", + content_id: cidTr, + content_url: trUrl, + mime_type: "application/json", + object_size: 128, + task_status: rec.migratedContentStatusNull ? null : 1, + }); + } + if (rec.is_summary) { + const cidSum = `cid_sum_${fileId}`; + const sumUrl = rec.summaryMarkdown + ? `${origin}/content/${rec.id}/summary.md` + : `${origin}/content/${rec.id}/summary.json`; + contentUrls.set(cidSum, sumUrl); + objects.push({ + object_id: `obj_sum_${fileId}`, + object_type: "SUMMARY", + content_id: cidSum, + content_url: sumUrl, + mime_type: rec.summaryMarkdown ? "text/markdown" : "application/json", + object_size: 128, + task_status: rec.migratedContentStatusNull ? null : 1, + }); + } + + return send(res, 200, { + status: 0, + msg: "ok", + data: { + node: { + node_id: nodeId, + parent_id: parentId, + name: rec.filename, + version_ms: rec.version_ms, + folder: { + folder_id: parentId, + name: rec.folderName, + }, + breadcrumbs: [{ node_id: "root_folder", name: "Recordings" }], + }, + meta: { + file_id: fileId, + duration: rec.duration, + size_bytes: rec.filesize, + timezone_iana: rec.timezone_iana ?? null, + start_time: rec.start_time, + serial_number: rec.serial_number, + version_ms: rec.version_ms, + }, + objects, + }, + }); + } + + if (req.method === "POST" && path === "/file-app/v4/files/content/url") { + const { content_ids = [] } = JSON.parse(body || "{}"); + const url_map = {}; + for (const cid of Array.isArray(content_ids) ? content_ids : []) { + if (contentUrls.has(cid)) url_map[cid] = contentUrls.get(cid); + } + return send(res, 200, { status: 0, msg: "ok", data: { url_map } }); + } + + const v4RenameMatch = path.match(/^\/file-app\/v4\/nodes\/rename\/([^/]+)$/); + if (req.method === "PATCH" && v4RenameMatch) { + const nodeId = decodeURIComponent(v4RenameMatch[1]); + const { name, origin_version } = JSON.parse(body || "{}"); + if (nodeId.startsWith("node_")) { + const rec = findByV4FileId(auth.acct, nodeId.slice(5)); + if (!rec) return send(res, 404, { status: -1, msg: "Node not found" }); + if (rec.renameConflictOnce) { + rec.renameConflictOnce = false; + rec.version_ms += 1000; + return send(res, 200, { status: -1800313, msg: "Version conflict" }); + } + if ( + origin_version !== undefined && + origin_version !== null && + Number(origin_version) !== Number(rec.version_ms) + ) { + return send(res, 200, { status: -1800313, msg: "Version conflict" }); + } + rec.filename = name ?? rec.filename; + rec.version_ms += 1000; + const fileId = v4FileIdOf(rec); + const fileNode = { + node_id: `node_${fileId}`, + parent_id: `wrap_${fileId}`, + name: rec.filename, + version_ms: rec.version_ms, + }; + const folderNode = { + node_id: `wrap_${fileId}`, + parent_id: "root_folder", + name: rec.folderName, + version_ms: rec.folderVersionMs, + }; + return send(res, 200, { status: 0, msg: "ok", data: { items: [fileNode], parent_items: [folderNode] } }); + } + if (nodeId.startsWith("wrap_")) { + const rec = findByV4FileId(auth.acct, nodeId.slice(5)); + if (!rec) return send(res, 404, { status: -1, msg: "Node not found" }); + if ( + origin_version !== undefined && + origin_version !== null && + Number(origin_version) !== Number(rec.folderVersionMs) + ) { + return send(res, 200, { status: -1800313, msg: "Version conflict" }); + } + rec.folderName = name ?? rec.folderName; + rec.folderVersionMs += 1000; + const fileId = v4FileIdOf(rec); + const folderNode = { + node_id: `wrap_${fileId}`, + parent_id: "root_folder", + name: rec.folderName, + version_ms: rec.folderVersionMs, + }; + return send(res, 200, { status: 0, msg: "ok", data: { items: [folderNode], parent_items: [] } }); + } + return send(res, 404, { status: -1, msg: "Node not found" }); + } + + if (req.method === "GET" && path === "/file-app/v4/folders/children") { + const items = [...auth.acct.recordings.values()] + .filter((r) => !r.is_trash) + .map((r) => { + const fileId = v4FileIdOf(r); + return { + node_id: `wrap_${fileId}`, + parent_id: "root_folder", + name: r.folderName, + version_ms: r.folderVersionMs, + }; + }); + return send(res, 200, { status: 0, msg: "ok", data: { items, next_cursor: null } }); + } + return send(res, 404, { status: -1, msg: "not found" }); }); diff --git a/contract-tests/tests/plaud.spec.ts b/contract-tests/tests/plaud.spec.ts index 3d7bef7..b0996b6 100644 --- a/contract-tests/tests/plaud.spec.ts +++ b/contract-tests/tests/plaud.spec.ts @@ -2,12 +2,13 @@ import { type ChildProcess } from "node:child_process"; import { randomBytes } from "node:crypto"; import { expect, test } from "@playwright/test"; import { type APIRequestContext } from "@playwright/test"; -import { anonymousContext, baseURL } from "../helpers/api"; +import { anonymousContext, baseURL, fakeAiBaseUrl } from "../helpers/api"; import { ensureFakePlaud, freshUser, mainUser, FAKE_PLAUD_URL, + type FakeRequest, fakeControl, fakeRequests, jwt, @@ -891,3 +892,493 @@ test.describe("plaud: sign-in and sync against the fake Plaud API", () => { await api.dispose(); }); }); + +// ---------------------------------------------------------------- Plaud 4.0 + +type V4FakeRequest = FakeRequest & { + "x-scope-type"?: string | null; + "x-scope-id"?: string | null; +}; + +async function v4FakeRequests(): Promise { + return (await fakeRequests()) as V4FakeRequest[]; +} + +test.describe("plaud: 4.0 migrated accounts", () => { + test.describe.configure({ timeout: 300_000 }); + + let fake: ChildProcess | null = null; + + test.beforeAll(async () => { + fake = await ensureFakePlaud(); + await fakeControl("/reset"); + }); + + test.afterAll(() => { + fake?.kill(); + }); + + async function signInV4(api: APIRequestContext, email: string) { + const sent = await api.post("/api/plaud/auth/send-code", { data: { email } }); + expect(sent.status()).toBe(200); + const { otpToken, apiBase } = await sent.json(); + const res = await api.post("/api/plaud/auth/verify", { + data: { code: "123456", otpToken, apiBase, email }, + }); + expect(res.status()).toBe(200); + return (await res.json()) as { success: boolean; devices: { sn: string }[] }; + } + + async function waitFor(read: () => Promise, timeoutMs = 20_000): Promise { + const deadline = Date.now() + timeoutMs; + for (;;) { + const value = await read(); + if (value !== undefined) return value; + if (Date.now() > deadline) throw new Error("timed out waiting for expected state"); + await new Promise((r) => setTimeout(r, 300)); + } + } + + test("mid-life migration preserves existing rows and tombstones without duplicates or re-downloads", async () => { + const api = await freshUser("plaud-v4-migration"); + const email = uniq("v4-mig"); + const base = Date.now() - 2 * DAY * 1000; + const tag = `${Date.now()}`; + const keepId = `mig-keep-${tag}`; + const editId = `mig-edit-${tag}`; + const tombId = `mig-tomb-${tag}`; + const newV4Id = `f_new_${tag}`; + + await fakeControl("/account", { + email, + recordings: [ + { + id: keepId, + filename: "Keep unchanged", + start_time: base - 300_000, + edit_time: base - 300_000, + version_ms: 1000, + }, + { + id: editId, + filename: "Before v4 edit", + start_time: base - 200_000, + edit_time: base - 200_000, + version_ms: 1000, + }, + { + id: tombId, + filename: "Deleted before v4", + start_time: base - 100_000, + edit_time: base - 100_000, + version_ms: 1000, + }, + ], + }); + await signInV4(api, email); + + const initial = await (await api.post("/api/plaud/sync")).json(); + expect(initial).toMatchObject({ success: true, newRecordings: 3, updatedRecordings: 0, errors: [] }); + + const listBefore = (await (await api.get("/api/recordings")).json()).recordings as Array>; + const tombRow = listBefore.find((x) => x.plaudFileId === tombId); + expect(tombRow).toBeTruthy(); + expect((await api.delete(`/api/recordings/${tombRow!.id as string}`)).status()).toBe(200); + + await fakeControl("/account", { + email, + migratedV4: true, + recordings: [ + { + id: editId, + filename: "Edited after v4", + version_ms: 2000, + edit_time: base + 10_000, + updated_at_show_ms: base + 10_000, + }, + { + id: tombId, + version_ms: 2500, + edit_time: base + 15_000, + updated_at_show_ms: base + 15_000, + }, + { + id: newV4Id, + isV4New: true, + filename: "New v4 recording", + start_time: Date.UTC(2026, 6, 15, 12, 0, 0), + created_at_show_ms: base + 20_000, + updated_at_show_ms: base + 20_000, + duration: 3000, + version_ms: 3000, + serial_number: "SN-V4-99", + timezone_iana: "Asia/Kolkata", + }, + ], + }); + + const before = (await v4FakeRequests()).length; + const migrated = await (await api.post("/api/plaud/sync")).json(); + expect(migrated).toMatchObject({ + success: true, + newRecordings: 1, + updatedRecordings: 1, + errors: [], + needsReconnect: false, + }); + + const listAfter = (await (await api.get("/api/recordings")).json()).recordings as Array>; + expect(listAfter).toHaveLength(3); + expect(listAfter.some((x) => x.plaudFileId === tombId)).toBe(false); + + const keepRow = listAfter.find((x) => x.plaudFileId === keepId); + const editRow = listAfter.find((x) => x.plaudFileId === editId); + const newRow = listAfter.find((x) => x.plaudFileId === newV4Id); + expect(keepRow).toMatchObject({ filename: "Keep unchanged", plaudVersion: "1000" }); + expect(editRow).toMatchObject({ filename: "Edited after v4", plaudVersion: "2000" }); + expect(newRow).toMatchObject({ + filename: "New v4 recording", + deviceSn: "SN-V4-99", + plaudVersion: "3000", + timezone: 5, + zonemins: 30, + }); + expect(Number(newRow!.filesize)).toBeGreaterThan(0); + expect((await api.get(`/api/recordings/${newRow!.id as string}/audio`)).status()).toBe(200); + expect((await api.get(`/api/recordings/${editRow!.id as string}/audio`)).status()).toBe(200); + + const requests = (await v4FakeRequests()).slice(before); + const audioDownloads = requests.filter((q) => q.path.startsWith("/audio/")); + expect(audioDownloads).toHaveLength(1); + expect(audioDownloads[0].path).toBe(`/audio/${newV4Id}.mp3`); + expect(audioDownloads[0].auth).toBe("none"); + expect(audioDownloads[0]["x-scope-type"]).toBeNull(); + + expect(requests.some((q) => q.path === `/file-app/v4/files/detail/of_${keepId}`)).toBe(false); + expect(requests.some((q) => q.path === `/file-app/v4/files/detail/of_${tombId}`)).toBe(false); + + const v4Calls = requests.filter((q) => q.path.startsWith("/file-app/v4/")); + expect(v4Calls.length).toBeGreaterThan(0); + expect(v4Calls.every((q) => q["x-scope-type"] === "workspace")).toBe(true); + expect(v4Calls.every((q) => typeof q["x-scope-id"] === "string" && q["x-scope-id"].length > 0)).toBe(true); + expect(v4Calls.every((q) => q.auth === "wt")).toBe(true); + await api.dispose(); + }); + + test("multi-page cursor pagination and out-of-order edit sorting surface older edited items", async () => { + const api = await freshUser("plaud-v4-pages"); + const email = uniq("v4-pages"); + const base = Date.now() - DAY * 1000; + const tag = `${Date.now()}`; + const total = 52; + const recordings = Array.from({ length: total }, (_, i) => ({ + id: `f_pg_${tag}_${i}`, + isV4New: true, + filename: `Page item ${i}`, + start_time: base - i * 60_000, + created_at_show_ms: base - i * 60_000, + updated_at_show_ms: base - i * 60_000, + edit_time: base - i * 60_000, + version_ms: 1000, + })); + await fakeControl("/account", { email, migratedV4: true, recordings }); + await signInV4(api, email); + + const beforeFirst = (await v4FakeRequests()).length; + const first = await (await api.post("/api/plaud/sync")).json(); + expect(first).toMatchObject({ success: true, newRecordings: total, updatedRecordings: 0, errors: [] }); + + const firstRequests = (await v4FakeRequests()).slice(beforeFirst); + const listCalls = firstRequests.filter((q) => q.path === "/file-app/v4/recordings/all"); + expect(listCalls.length).toBeGreaterThanOrEqual(2); + expect(listCalls.some((q) => q.query.includes("cursor=50"))).toBe(true); + + const oldestId = `f_pg_${tag}_${total - 1}`; + await fakeControl("/account", { + email, + recordings: [ + { + id: oldestId, + filename: "Oldest item edited", + version_ms: 9999, + updated_at_show_ms: base + 100_000, + edit_time: base + 100_000, + }, + ], + }); + + const beforeSecond = (await v4FakeRequests()).length; + const second = await (await api.post("/api/plaud/sync")).json(); + expect(second).toMatchObject({ success: true, newRecordings: 0, updatedRecordings: 1, errors: [] }); + + const secondRequests = (await v4FakeRequests()).slice(beforeSecond); + expect(secondRequests.filter((q) => q.path.startsWith("/audio/"))).toEqual([]); + + const list = (await (await api.get("/api/recordings")).json()).recordings as Array>; + expect(list).toHaveLength(total); + const edited = list.find((x) => x.plaudFileId === oldestId); + expect(edited).toMatchObject({ filename: "Oldest item edited", plaudVersion: "9999" }); + await api.dispose(); + }); + + test("audio fallback supports AUDIO-only objects and content_id resolution", async () => { + const api = await freshUser("plaud-v4-audio-fallback"); + const email = uniq("v4-audio"); + const tag = `${Date.now()}`; + const audioOnlyId = `f_audio_only_${tag}`; + const cidOnlyId = `f_cid_only_${tag}`; + + await fakeControl("/account", { + email, + migratedV4: true, + recordings: [ + { + id: audioOnlyId, + isV4New: true, + filename: "Audio only item", + audioMode: "audio_only", + filesize: 0, + file_md5: "", + version_ms: 1500, + }, + { + id: cidOnlyId, + isV4New: true, + filename: "Content id only item", + audioMode: "content_id_only", + version_ms: 1600, + }, + ], + }); + await signInV4(api, email); + + const before = (await v4FakeRequests()).length; + const sync = await (await api.post("/api/plaud/sync")).json(); + expect(sync).toMatchObject({ success: true, newRecordings: 2, updatedRecordings: 0, errors: [] }); + + const list = (await (await api.get("/api/recordings")).json()).recordings as Array>; + for (const id of [audioOnlyId, cidOnlyId]) { + const row = list.find((x) => x.plaudFileId === id); + expect(row, `recording ${id}`).toBeTruthy(); + expect(Number(row!.filesize)).toBeGreaterThan(0); + expect(String(row!.fileMd5)).toMatch(/^[a-f0-9]{32}$/); + expect((await api.get(`/api/recordings/${row!.id as string}/audio`)).status()).toBe(200); + } + + const requests = (await v4FakeRequests()).slice(before); + const contentUrlCalls = requests.filter( + (q) => q.method === "POST" && q.path === "/file-app/v4/files/content/url", + ); + expect(contentUrlCalls.length).toBeGreaterThanOrEqual(1); + await api.dispose(); + }); + + test("importPlaudContent imports v4 gzipped transcripts and Markdown summaries including migrated null status", async () => { + const api = await freshUser("plaud-v4-content"); + const email = uniq("v4-content"); + const tag = `${Date.now()}`; + const v4MdId = `f_content_md_${tag}`; + const migratedNullId = `mig_content_null_${tag}`; + + await fakeControl("/account", { + email, + migratedV4: true, + recordings: [ + { + id: v4MdId, + isV4New: true, + filename: "V4 content recording", + is_trans: true, + is_summary: true, + summaryMarkdown: true, + migratedContentStatusNull: false, + version_ms: 2100, + }, + { + id: migratedNullId, + filename: "Migrated null-status content", + is_trans: true, + is_summary: true, + summaryMarkdown: true, + migratedContentStatusNull: true, + version_ms: 2200, + }, + ], + }); + await signInV4(api, email); + expect((await api.put("/api/settings/user", { data: { importPlaudContent: true } })).status()).toBe(200); + + const before = (await v4FakeRequests()).length; + const sync = await (await api.post("/api/plaud/sync")).json(); + expect(sync).toMatchObject({ success: true, newRecordings: 2, updatedRecordings: 0, errors: [] }); + + const list = (await (await api.get("/api/recordings")).json()).recordings as Array>; + for (const id of [v4MdId, migratedNullId]) { + const row = list.find((x) => x.plaudFileId === id); + expect(row, `recording ${id}`).toBeTruthy(); + + const detail = await (await api.get(`/api/recordings/${row!.id as string}`)).json(); + expect(detail.transcription).toBeTruthy(); + expect(detail.transcription.source).toBe("plaud"); + expect(detail.transcription.text).toContain(`Fake transcript line for ${id}`); + + const summary = await (await api.get(`/api/recordings/${row!.id as string}/summary`)).json(); + expect(summary.summary).toContain("Fake v4 markdown summary"); + } + + const requests = (await v4FakeRequests()).slice(before); + expect(requests.some((q) => q.path === `/content/${v4MdId}/summary.md`)).toBe(true); + expect(requests.some((q) => q.path === `/content/${migratedNullId}/summary.md`)).toBe(true); + await api.dispose(); + }); + + test("mid-sync -419 workspace token revocation re-mints once and completes sync", async () => { + const api = await freshUser("plaud-v4-remint"); + const email = uniq("v4-remint"); + const id = `f_remint_${Date.now()}`; + + await fakeControl("/account", { + email, + migratedV4: true, + recordings: [{ id, isV4New: true, filename: "After remint", version_ms: 3100 }], + }); + await signInV4(api, email); + await fakeControl("/fail", { + path: "/file-app/v4/recordings/all", + status: 200, + plaudStatus: -419, + msg: "Workspace token revoked", + times: 1, + email, + }); + + const before = (await v4FakeRequests()).length; + const sync = await (await api.post("/api/plaud/sync")).json(); + expect(sync).toMatchObject({ + success: true, + newRecordings: 1, + updatedRecordings: 0, + errors: [], + needsReconnect: false, + }); + + const requests = (await v4FakeRequests()).slice(before); + const mintCalls = requests.filter( + (q) => q.method === "POST" && q.path.startsWith("/user-app/auth/workspace/token/"), + ); + expect(mintCalls.length).toBeGreaterThanOrEqual(2); + const listCalls = requests.filter((q) => q.path === "/file-app/v4/recordings/all"); + expect(listCalls.length).toBeGreaterThanOrEqual(2); + + const status = await (await api.get("/api/plaud/connection")).json(); + expect(status).toMatchObject({ connected: true, needsReconnect: false }); + await api.dispose(); + }); + + test("title push on v4 retries -1800313 version conflicts and renames matching container folders", async () => { + const api = await freshUser("plaud-v4-rename"); + const email = uniq("v4-rename"); + const id = `f_rename_${Date.now()}`; + + await fakeControl("/account", { + email, + migratedV4: true, + recordings: [ + { + id, + isV4New: true, + filename: "Initial Plaud Title", + folderName: "Initial Plaud Title", + renameConflictOnce: true, + version_ms: 5000, + }, + ], + }); + await signInV4(api, email); + expect( + ( + await api.put("/api/settings/user", { + data: { + autoGenerateTitle: true, + syncTitleToPlaud: true, + recordingTitleMode: "ai", + autoSummarize: false, + }, + }) + ).status(), + ).toBe(200); + expect( + ( + await api.post("/api/settings/ai/providers", { + data: { + provider: "Custom", + apiKey: "plaud-v4-title-test-key", + baseUrl: fakeAiBaseUrl, + defaultModel: "fake-chat-1", + isDefaultTranscription: true, + isDefaultEnhancement: true, + }, + }) + ).status(), + ).toBe(200); + + const sync = await (await api.post("/api/plaud/sync")).json(); + expect(sync).toMatchObject({ success: true, newRecordings: 1, errors: [] }); + + const list = (await (await api.get("/api/recordings")).json()).recordings as Array>; + const row = list.find((x) => x.plaudFileId === id); + expect(row).toBeTruthy(); + + const before = (await v4FakeRequests()).length; + const transcribed = await api.post(`/api/recordings/${row!.id as string}/transcribe`); + expect(transcribed.status()).toBe(200); + + const renameCalls = await waitFor(async () => { + const reqs = (await v4FakeRequests()).slice(before); + const nodeRenames = reqs.filter( + (q) => q.method === "PATCH" && q.path === `/file-app/v4/nodes/rename/node_${id}`, + ); + const folderRenames = reqs.filter( + (q) => q.method === "PATCH" && q.path === `/file-app/v4/nodes/rename/wrap_${id}`, + ); + return nodeRenames.length >= 2 && folderRenames.length >= 1 + ? { nodeRenames, folderRenames } + : undefined; + }); + + expect(renameCalls.nodeRenames).toHaveLength(2); + expect(renameCalls.folderRenames).toHaveLength(1); + expect(renameCalls.nodeRenames.every((q) => q["x-scope-type"] === "workspace" && q.auth === "wt")).toBe(true); + expect(renameCalls.folderRenames.every((q) => q["x-scope-type"] === "workspace" && q.auth === "wt")).toBe(true); + + const after = await (await api.get(`/api/recordings/${row!.id as string}`)).json(); + expect(after.recording.filename).toBe("Contract Title"); + await api.dispose(); + }); + + test("connect falls back to v4 listing when /device/list returns -1800907", async () => { + const api = await freshUser("plaud-v4-connect-fallback"); + const email = uniq("v4-connect"); + + await fakeControl("/account", { email, migratedV4: true }); + await fakeControl("/fail", { + path: "/device/list", + status: 200, + plaudStatus: -1800907, + msg: "Account migrated to 4.0. Please update and reinitialize.", + times: 1, + email, + }); + + const before = (await v4FakeRequests()).length; + const verified = await signInV4(api, email); + expect(verified.success).toBe(true); + expect(verified.devices).toEqual([]); + + const requests = (await v4FakeRequests()).slice(before); + expect(requests.some((q) => q.path === "/file-app/v4/recordings/all")).toBe(true); + const status = await (await api.get("/api/plaud/connection")).json(); + expect(status).toMatchObject({ connected: true, needsReconnect: false }); + await api.dispose(); + }); +}); From 22c1123dfe10e52fd04a62f3b7dd5354d39b91a5 Mon Sep 17 00:00:00 2001 From: Jeremy Liu Date: Fri, 20 Nov 2026 18:32:55 -0800 Subject: [PATCH 5/6] chore(release): 1.7.0 --- CHANGELOG.md | 14 +++++++++++++- Cargo.lock | 4 ++-- Cargo.toml | 2 +- desktop-tauri/tauri.conf.json | 2 +- extension/package.json | 2 +- spec/desktop.md | 2 +- web/package.json | 2 +- 7 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd22dac..80885bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,19 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and ## [Unreleased] +## [1.7.0] - 2026-11-20 + +### Added + +- Plaud 4.0 sync support (`/file-app/v4/`). Accounts migrated to Plaud 4.0 are detected automatically on `-1800907` and switch to the v4 listing, detail, audio download, content import, and title push routes without changing settings or reconnecting, while non-migrated accounts keep using the existing routes unchanged. +- Pre-migration recording ids (`of_`) map back to their stored library ids so previously synced recordings and locally deleted tombstones stay matched without duplicates or re-downloads, and new Plaud 4.0 recordings (`f_`) hydrate device serial, file size, and IANA timezone offsets on import. +- Mid-sync workspace token revocations (`-419` and `-420`) re-mint a fresh workspace token once automatically, and connect validation falls back to the v4 listing endpoint if `/device/list` reports a migrated account. + ### Fixed +- Syncing a custom local storage folder (`localStorageFolder`) alongside ZIP auto-merge export no longer creates duplicate library recordings when both folders point to the same directory. File writes, renames, and database commits serialize under a shared folder lock, and existing duplicate pairs self-heal while preserving transcripts, summaries, Ask AI chats, and titles. +- ZIP audio merge now detects and repairs unfinalized `.m4a` and `.mp4` segments missing a `moov` atom, and skips empty or unreadable entries when other valid audio segments exist in the archive. +- Manual Plaud sync error toasts no longer repeat the "Sync failed:" prefix when the server error already includes it. - Failed and stopped Ask AI answers save their final status before notifying clients, so reloading an ended stream cannot leave the answer busy. - Audio pipeline progress keeps updating after temporary network failures, and interrupted Ask AI answers reconnect without duplicating text. - Transcript searches refresh after re-transcription, and browser transcription progress and notifications follow the interface language. @@ -202,7 +213,8 @@ Initial release. - **Self-hosting.** A single container image for linux/amd64 and linux/arm64, a Docker Compose file, a one-line installer, a health endpoint, and a single SQLite data volume. - **Mac app.** A desktop app for Apple Silicon Macs that runs the server locally, with a menu bar item, launch at login, and a local data folder. -[Unreleased]: https://github.com/JeremyL691/OpenAudioHub/compare/v1.6.0...HEAD +[Unreleased]: https://github.com/JeremyL691/OpenAudioHub/compare/v1.7.0...HEAD +[1.7.0]: https://github.com/JeremyL691/OpenAudioHub/compare/v1.6.0...v1.7.0 [1.6.0]: https://github.com/JeremyL691/OpenAudioHub/compare/v1.5.0...v1.6.0 [1.5.0]: https://github.com/JeremyL691/OpenAudioHub/compare/v1.4.0...v1.5.0 [1.4.0]: https://github.com/JeremyL691/OpenAudioHub/compare/v1.3.2...v1.4.0 diff --git a/Cargo.lock b/Cargo.lock index 0458b42..6508f73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3408,7 +3408,7 @@ dependencies = [ [[package]] name = "oah" -version = "1.6.0" +version = "1.7.0" dependencies = [ "aes-gcm", "argon2", @@ -3675,7 +3675,7 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" [[package]] name = "openaudiohub-desktop" -version = "1.6.0" +version = "1.7.0" dependencies = [ "axum", "fs4", diff --git a/Cargo.toml b/Cargo.toml index a65f147..4f93dd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["crates/oah", "desktop-tauri"] resolver = "3" [workspace.package] -version = "1.6.0" +version = "1.7.0" edition = "2024" # The AWS SDK here needs 1.94.1, so that is the floor for the whole workspace. rust-version = "1.94.1" diff --git a/desktop-tauri/tauri.conf.json b/desktop-tauri/tauri.conf.json index 872f655..ebd6cc1 100644 --- a/desktop-tauri/tauri.conf.json +++ b/desktop-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "OpenAudioHub", - "version": "1.6.0", + "version": "1.7.0", "identifier": "io.github.jeremyl691.openaudiohub", "build": {}, "app": { diff --git a/extension/package.json b/extension/package.json index 1700f2b..c72923d 100644 --- a/extension/package.json +++ b/extension/package.json @@ -1,6 +1,6 @@ { "name": "openaudiohub-extension", - "version": "1.6.0", + "version": "1.7.0", "private": true, "type": "module", "packageManager": "pnpm@12.11.2", diff --git a/spec/desktop.md b/spec/desktop.md index ee01e8b..d49e1c3 100644 --- a/spec/desktop.md +++ b/spec/desktop.md @@ -5,7 +5,7 @@ This file specifies the macOS desktop app: what it does at start and quit, what ## 1. Scope - Platform: macOS on Apple Silicon (`aarch64-apple-darwin`), minimum macOS 11.0. The app is distributed as an app bundle and a disk image, signed ad hoc (signing identity `-`). -- Product name `OpenAudioHub`. Bundle identifier `io.github.jeremyl691.openaudiohub`. Version `1.6.0` (the workspace version, which is also the value written to `config.json` as `lastVersion`). +- Product name `OpenAudioHub`. Bundle identifier `io.github.jeremyl691.openaudiohub`. Version `1.7.0` (the workspace version, which is also the value written to `config.json` as `lastVersion`). - One copy of the app runs at a time, on one data folder. It signs in as one local account (section 4). - The server listens on 127.0.0.1 only. The app has no automatic update check. - The app bundles two programs, `ffmpeg` and `ffprobe`, placed next to the app executable. The server uses them to decode and convert audio, compute waveforms and prepare files for transcription. They are built with GPL, nonfree and version 3 components disabled (`desktop-tauri/scripts/build-ffmpeg.sh`). diff --git a/web/package.json b/web/package.json index 167840a..b87d4b9 100644 --- a/web/package.json +++ b/web/package.json @@ -1,6 +1,6 @@ { "name": "openaudiohub-web", - "version": "1.6.0", + "version": "1.7.0", "private": true, "type": "module", "scripts": { From 8522d541268b7f2a82918480bdd5e81a5adc967c Mon Sep 17 00:00:00 2001 From: Jeremy Liu Date: Fri, 20 Nov 2026 19:07:12 -0800 Subject: [PATCH 6/6] fix(plaud): preserve v4 metadata on update, handle message-only device migration and detail-only versions --- crates/oah/src/plaud/api.rs | 18 ++++ crates/oah/src/plaud/sync.rs | 198 ++++++++++++++++++++++++++++++----- 2 files changed, 192 insertions(+), 24 deletions(-) diff --git a/crates/oah/src/plaud/api.rs b/crates/oah/src/plaud/api.rs index 7483149..11c583d 100644 --- a/crates/oah/src/plaud/api.rs +++ b/crates/oah/src/plaud/api.rs @@ -383,6 +383,12 @@ impl Client { pub async fn devices(&self) -> Result, PlaudError> { let v = self.api(Method::GET, "/device/list", None).await?; + if v4::is_migrated_payload(&v) { + return Err(PlaudError::Api { + status: v4::MIGRATED_STATUS, + message: message_of(&v, "Account migrated to 4.0"), + }); + } let list = v .get("data_devices") .and_then(Value::as_array) @@ -1335,4 +1341,16 @@ mod tests { assert_eq!(st.folder_name.lock().unwrap().as_str(), "New Title"); assert_eq!(*st.folder_version.lock().unwrap(), 22); } + + #[tokio::test] + async fn devices_surfaces_message_only_migration_payload() { + let app = Router::new().route( + "/device/list", + get(|| async { Json(json!({ "msg": "Account migrated to 4.0" })) }), + ); + let base = spawn_mock(app).await; + let client = Client::new(&base, "wt"); + let err = client.devices().await.unwrap_err(); + assert!(v4::is_migrated_error(&err), "{err:?}"); + } } diff --git a/crates/oah/src/plaud/sync.rs b/crates/oah/src/plaud/sync.rs index 94eb362..36d9e2d 100644 --- a/crates/oah/src/plaud/sync.rs +++ b/crates/oah/src/plaud/sync.rs @@ -65,6 +65,7 @@ impl From for Stop { } /// A failure of one recording. +#[derive(Debug)] pub enum ItemError { Rejected, Failed(String), @@ -351,12 +352,16 @@ async fn process(state: &AppState, ctx: &Ctx, r: &Recording) -> Result Ok(Outcome::Skipped), - Some((id, None, prev, _, _, _)) if prev == version => { + Some((id, None, prev, _, _, _)) if r.version_ms.is_some() && prev == version => { let imported = import_for(state, ctx, &id, &r.id).await; Ok(Outcome::Unchanged { imported }) } - Some((id, None, _, prev_type, prev_key, prev_source)) => { + Some((id, None, prev, prev_type, prev_key, prev_source)) => { let hydrated = ctx.client.hydrate_recording(r).await?; + if prev == version_text(&hydrated) { + let imported = import_for(state, ctx, &id, &hydrated.id).await; + return Ok(Outcome::Unchanged { imported }); + } // A stored file is kept: a changed Plaud version only refreshes metadata, so a rename // or an edit in Plaud never replaces a file the user may be playing. The exception is a // missing object, which would leave the recording unplayable for good. @@ -575,32 +580,48 @@ async fn update_row_with_audio( let peaks = storage .and_then(|s| s.peaks.as_ref()) .and_then(|p| serde_json::to_string(p).ok()); - let done = sqlx::query( + let device_sn = if r.is_v4 { + r.serial_number + .as_deref() + .map(str::trim) + .filter(|s| !s.is_empty()) + } else { + Some(r.serial_number.as_deref().unwrap_or_default()) + }; + let sql = if r.is_v4 { + "UPDATE recordings SET device_sn = COALESCE(?, device_sn), filename = COALESCE(?, filename), duration = ?, start_time = ?, end_time = ?, \ + filesize = COALESCE(?, filesize), file_md5 = COALESCE(?, file_md5), plaud_version = ?, timezone = COALESCE(?, timezone), \ + zonemins = COALESCE(?, zonemins), scene = COALESCE(?, scene), is_trash = 0, updated_at = ?, \ + storage_type = COALESCE(?, storage_type), storage_path = COALESCE(?, storage_path), \ + downloaded_at = COALESCE(?, downloaded_at), waveform_peaks = COALESCE(?, waveform_peaks) \ + WHERE id = ? AND deleted_at IS NULL" + } else { "UPDATE recordings SET device_sn = ?, filename = COALESCE(?, filename), duration = ?, start_time = ?, end_time = ?, \ filesize = COALESCE(?, filesize), file_md5 = COALESCE(?, file_md5), plaud_version = ?, timezone = ?, zonemins = ?, scene = ?, is_trash = 0, updated_at = ?, \ storage_type = COALESCE(?, storage_type), storage_path = COALESCE(?, storage_path), \ downloaded_at = COALESCE(?, downloaded_at), waveform_peaks = COALESCE(?, waveform_peaks) \ - WHERE id = ? AND deleted_at IS NULL", - ) - .bind(r.serial_number.clone().unwrap_or_default()) - .bind(new_name) - .bind(r.duration.unwrap_or(0)) - .bind(r.start_time.unwrap_or(0)) - .bind(r.end_time.unwrap_or(0)) - .bind(filesize) - .bind(file_md5) - .bind(version_text(r)) - .bind(r.timezone) - .bind(r.zonemins) - .bind(r.scene) - .bind(now) - .bind(storage.map(|s| s.storage_type)) - .bind(storage.map(|s| s.key.as_str())) - .bind(storage.map(|_| now)) - .bind(peaks) - .bind(id) - .execute(&state.db) - .await?; + WHERE id = ? AND deleted_at IS NULL" + }; + let done = sqlx::query(sql) + .bind(device_sn) + .bind(new_name) + .bind(r.duration.unwrap_or(0)) + .bind(r.start_time.unwrap_or(0)) + .bind(r.end_time.unwrap_or(0)) + .bind(filesize) + .bind(file_md5) + .bind(version_text(r)) + .bind(r.timezone) + .bind(r.zonemins) + .bind(r.scene) + .bind(now) + .bind(storage.map(|s| s.storage_type)) + .bind(storage.map(|s| s.key.as_str())) + .bind(storage.map(|_| now)) + .bind(peaks) + .bind(id) + .execute(&state.db) + .await?; Ok(done.rows_affected() == 1) } @@ -884,4 +905,133 @@ mod title_tests { assert!(repair_candidate("Team sync", start, offset).is_none()); assert!(repair_candidate("2026-10-19 09:00", start, offset).is_none()); } + + #[tokio::test] + async fn v4_update_preserves_saved_metadata_when_detail_omits_it() { + let dir = tempfile::tempdir().unwrap(); + let config = crate::Config::for_tests(dir.path()); + let state = crate::AppState::new(config, crate::db::open_memory().await.unwrap()); + let now = crate::db::now_ms(); + sqlx::query("INSERT INTO users (id, email, created_at, updated_at) VALUES ('u1', 'u1@example.test', ?, ?)") + .bind(now) + .bind(now) + .execute(&state.db) + .await + .unwrap(); + sqlx::query( + "INSERT INTO recordings (id, user_id, device_sn, plaud_file_id, filename, duration, start_time, end_time, \ + filesize, file_md5, storage_type, storage_path, plaud_version, timezone, zonemins, scene, is_trash, created_at, updated_at, title_source) \ + VALUES ('r1', 'u1', 'SN-ORIG', 'legacy-1', ?, 3000, 1000, 4000, 5120, 'deadbeef', 'local', 'u1/legacy-1.mp3', '100', -8, 0, 2, 0, ?, ?, 'original')", + ) + .bind(state.config.field_key.encrypt("Old Title")) + .bind(now) + .bind(now) + .execute(&state.db) + .await + .unwrap(); + + let mut v4_rec = recording(Some("Renamed in 4.0"), Some(1000)); + v4_rec.id = "legacy-1".into(); + v4_rec.version_ms = Some(200); + v4_rec.is_v4 = true; + assert!(update_row(&state, &v4_rec, "r1", true).await.unwrap()); + + let row: ( + String, + i64, + String, + Option, + Option, + Option, + String, + ) = sqlx::query_as( + "SELECT device_sn, filesize, file_md5, timezone, zonemins, scene, plaud_version \ + FROM recordings WHERE id = 'r1'", + ) + .fetch_one(&state.db) + .await + .unwrap(); + assert_eq!(row.0, "SN-ORIG"); + assert_eq!(row.1, 5120); + assert_eq!(row.2, "deadbeef"); + assert_eq!(row.3, Some(-8)); + assert_eq!(row.4, Some(0)); + assert_eq!(row.5, Some(2)); + assert_eq!(row.6, "200"); + } + + #[tokio::test] + async fn detail_only_v4_version_does_not_repeat_updates() { + use axum::routing::get; + use axum::{Json, Router}; + + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let app = Router::new().route( + "/file-app/v4/files/detail/f_nodate", + get(|| async { + Json(json!({ + "status": 0, + "data": { + "node": { "node_id": "n1", "name": "Detail Only", "version_ms": 42 }, + "meta": { "file_id": "f_nodate", "duration": 3000, "start_time": 1000, "version_ms": 42 }, + "objects": [] + } + })) + }), + ); + tokio::spawn(async move { + axum::serve(listener, app).await.unwrap(); + }); + + let dir = tempfile::tempdir().unwrap(); + let mut config = crate::Config::for_tests(dir.path()); + config.storage_root = dir.path().join("storage"); + tokio::fs::create_dir_all(config.storage_root.join("u1")) + .await + .unwrap(); + tokio::fs::write(config.storage_root.join("u1/f_nodate.mp3"), b"ID3") + .await + .unwrap(); + let state = crate::AppState::new(config, crate::db::open_memory().await.unwrap()); + let now = crate::db::now_ms(); + sqlx::query("INSERT INTO users (id, email, created_at, updated_at) VALUES ('u1', 'u1@example.test', ?, ?)") + .bind(now) + .bind(now) + .execute(&state.db) + .await + .unwrap(); + sqlx::query( + "INSERT INTO recordings (id, user_id, device_sn, plaud_file_id, filename, duration, start_time, end_time, \ + filesize, file_md5, storage_type, storage_path, plaud_version, is_trash, created_at, updated_at, title_source) \ + VALUES ('r1', 'u1', '', 'f_nodate', ?, 3000, 1000, 4000, 3, 'abc', 'local', 'u1/f_nodate.mp3', '42', 0, ?, ?, 'original')", + ) + .bind(state.config.field_key.encrypt("Detail Only")) + .bind(now) + .bind(now) + .execute(&state.db) + .await + .unwrap(); + + let ctx = Ctx { + user_id: "u1".into(), + client: Client::with_workspace( + &format!("http://{addr}"), + "wt", + Some("ws".into()), + None, + ), + import_content: false, + title_mode: "original".into(), + content_stopped: AtomicBool::new(false), + errors: Mutex::new(Vec::new()), + }; + let mut r = recording(Some("Detail Only"), None); + r.id = "f_nodate".into(); + r.version_ms = None; + r.is_v4 = true; + + let outcome = process(&state, &ctx, &r).await.unwrap(); + assert!(matches!(outcome, Outcome::Unchanged { imported: false })); + } }