Altersangaben: Genre-Signal-Chip (Kinder/Jugend aus Audible-Genres) + manuelle Altersnote pro Buch (labels.json, Inline-Edit mit Stift-Button)

This commit is contained in:
2026-11-06 20:20:51 +01:00
parent 14b93e2fb1
commit 0b7e223ad5
7 changed files with 162 additions and 2 deletions

View File

@ -32,6 +32,8 @@ pub struct Book {
pub is_finished: bool,
#[serde(default)]
pub date_added: String,
#[serde(default)]
pub genres: String,
/// lokal vorhanden (book.m4b im Datenverzeichnis) — vom Backend gesetzt
#[serde(default, skip_deserializing)]
pub local: bool,
@ -263,6 +265,7 @@ pub fn local_books() -> Vec<Book> {
percent_complete: 0.0,
is_finished: false,
date_added: String::new(),
genres: String::new(),
local: true,
});
}
@ -306,6 +309,54 @@ fn probe_m4b(m4b: &Path) -> (String, String, i64) {
(title, artist, minutes)
}
// Manuelle Altersnotizen (bewertbar pro Buch, lokale JSON-Ablage)
#[derive(Debug, Clone, Serialize)]
pub struct Labels(pub std::collections::BTreeMap<String, String>);
fn labels_path() -> PathBuf {
books_dir()
.parent()
.map(|p| p.join("labels.json"))
.unwrap_or_else(|| PathBuf::from("labels.json"))
}
pub fn alter_notes() -> Labels {
let map: std::collections::BTreeMap<String, String> = fs::read_to_string(labels_path())
.ok()
.and_then(|txt| serde_json::from_str(&txt).ok())
.unwrap_or_default();
Labels(map)
}
pub fn set_alter_note(asin: &str, note: Option<String>) -> Result<(), String> {
let asin = asin.trim().to_string();
if asin.is_empty() {
return Err("keine ASIN angegeben".to_string());
}
let mut l = alter_notes();
let note = note.map(|n| n.trim().to_string()).filter(|n| !n.is_empty());
match note {
Some(n) => {
l.0.insert(asin, n);
}
None => {
l.0.remove(&asin);
}
}
if let Some(parent) = labels_path().parent() {
fs::create_dir_all(parent).map_err(|e| format!("Notiz-Ordner: {e}"))?;
}
let path = labels_path();
let tmp = path.with_extension("json.tmp");
fs::write(&tmp, serde_json::to_vec(&l).unwrap_or_default())
.map_err(|e| format!("Notiz schreiben: {e}"))?;
fs::rename(&tmp, &path).map_err(|e| format!("Notiz speichern: {e}")
)
}
// Manuelle Altersnotizen — lokale JSON-Ablage (siehe labels_path)
/// Taugt stderr von audible-cli: Warnungen und Passwort-Prompts raus,
/// echte Fehlertexte bleiben.
pub(crate) fn reduce_noise(s: &str) -> String {

View File

@ -730,6 +730,23 @@ mod tests {
let _ = fs::remove_dir_all(&root);
}
#[test]
fn alter_note_roundtrip() {
let tmp = std::env::temp_dir().join(format!("hw-labels-{}", std::process::id()));
unsafe {
std::env::set_var("HOERWERK_BOOKS_DIR", &tmp);
}
crate::audible::set_alter_note("B08L4WJMDM", Some("ab 6".into())).unwrap();
let l = crate::audible::alter_notes();
assert_eq!(l.0.get("B08L4WJMDM").map(|s| s.as_str()), Some("ab 6"));
crate::audible::set_alter_note("B08L4WJMDM", None).unwrap();
assert!(crate::audible::alter_notes().0.get("B08L4WJMDM").is_none());
unsafe {
std::env::remove_var("HOERWERK_BOOKS_DIR");
}
let _ = fs::remove_dir_all(&tmp);
}
#[test]
fn dl_item_serde_status() {
let it = DlItem {

View File

@ -68,6 +68,23 @@ async fn stream_only() -> Result<Vec<String>, String> {
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))
}
#[tauri::command]
async fn get_alter_notes() -> Result<audible::Labels, String> {
tauri::async_runtime::spawn_blocking(audible::alter_notes)
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))
}
#[tauri::command]
async fn set_alter_note(
asin: String,
note: Option<String>,
) -> Result<(), String> {
tauri::async_runtime::spawn_blocking(move || audible::set_alter_note(&asin, note))
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
@ -86,6 +103,8 @@ pub fn run() {
get_library,
get_local_books,
stream_only,
get_alter_notes,
set_alter_note,
download_list,
download_enqueue,
download_cancel,