[library] Listening: the audiobook position as an event (latest write wins), replayed by the export
This commit is contained in:
@@ -180,6 +180,12 @@ pub enum Op {
|
||||
list: String,
|
||||
upto: u32,
|
||||
},
|
||||
/// Day 4: where an audiobook file is, to the millisecond (the latest write wins, so any device resumes there).
|
||||
AudioPosition {
|
||||
book: String,
|
||||
sha256: String,
|
||||
ms: u64,
|
||||
},
|
||||
/// Day 3: a book file is an edition in another language (`None`: it is the book's own language again).
|
||||
Edition {
|
||||
book: String,
|
||||
|
||||
@@ -129,6 +129,9 @@ pub fn ops_from(lib: &Library) -> Vec<Op> {
|
||||
if b.to_read {
|
||||
ops.push(set(Field::ToRead, json!(true)));
|
||||
}
|
||||
if let Some((sha, ms)) = &b.audio_position {
|
||||
ops.push(Op::AudioPosition { book: id.clone(), sha256: sha.clone(), ms: *ms });
|
||||
}
|
||||
if b.familiar {
|
||||
ops.push(set(Field::Familiar, json!(true)));
|
||||
}
|
||||
|
||||
@@ -212,6 +212,9 @@ pub struct Book {
|
||||
pub position: Option<Position>,
|
||||
/// The most recent position, when it is behind the furthest: "Jump back to page X?".
|
||||
pub last_position: Option<Position>,
|
||||
/// Day 4: the audiobook's place: (file sha256, milliseconds).
|
||||
#[serde(default)]
|
||||
pub audio_position: Option<(String, u64)>,
|
||||
/// Day 3: read before in the reader's own language.
|
||||
#[serde(default)]
|
||||
pub familiar: bool,
|
||||
@@ -242,6 +245,7 @@ impl Book {
|
||||
last_position: None,
|
||||
familiar: false,
|
||||
editions: BTreeMap::new(),
|
||||
audio_position: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -773,6 +777,15 @@ impl Library {
|
||||
self.bands.insert(lang.clone(), Band { list: list.clone(), upto: (*upto).min(1_000_000) });
|
||||
}
|
||||
}
|
||||
Op::AudioPosition { book, sha256, ms } => {
|
||||
let ok =
|
||||
sha256.len() == 64 && sha256.bytes().all(|b| b.is_ascii_hexdigit()) && *ms < 1_000 * 3_600 * 1_000;
|
||||
if let Some(b) = self.book_mut(book)
|
||||
&& ok
|
||||
{
|
||||
b.audio_position = Some((sha256.to_ascii_lowercase(), *ms));
|
||||
}
|
||||
}
|
||||
Op::Edition { book, sha256, language } => {
|
||||
let ok = sha256.len() == 64 && sha256.bytes().all(|b| b.is_ascii_hexdigit());
|
||||
if let Some(b) = self.book_mut(book)
|
||||
|
||||
@@ -94,3 +94,23 @@ fn familiar_and_editions_on_a_book_and_through_an_export() {
|
||||
evs2.push(ev("7", "x", &mut c, 7, Op::Edition { book: "b".into(), sha256: sha, language: None }));
|
||||
assert!(Library::from_events(&evs2).books["b"].editions.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_audiobook_position_is_the_latest_write_and_survives_an_export() {
|
||||
let (mut pc, mut dc) = (Clock::default(), Clock::default());
|
||||
let sha = "cd".repeat(32);
|
||||
let pos = |ms: u64| Op::AudioPosition { book: "b".into(), sha256: sha.clone(), ms };
|
||||
let evs = vec![
|
||||
ev("0", "desk", &mut dc, 1, Op::BookAdd { book: "b".into(), info: info(1), restore: false }),
|
||||
ev("1", "desk", &mut dc, 1_000, pos(61_000)),
|
||||
ev("2", "phone", &mut pc, 2_000, pos(95_250)),
|
||||
ev("3", "phone", &mut pc, 3_000, Op::AudioPosition { book: "b".into(), sha256: "nope".into(), ms: 5 }),
|
||||
];
|
||||
let mut rev = evs.clone();
|
||||
rev.reverse();
|
||||
let lib = Library::from_events(&evs);
|
||||
assert_eq!(lib, Library::from_events(&rev));
|
||||
assert_eq!(lib.books["b"].audio_position, Some((sha.clone(), 95_250)), "to the millisecond, the latest write");
|
||||
let back = Library::from_events(&events_from(&lib, "restore", 10));
|
||||
assert_eq!(back.books.values().next().and_then(|b| b.audio_position.clone()), Some((sha, 95_250)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user