capsule_library/db/album_order.rs
1//! The one album order every native app and the CLI list container albums in (SSoT:
2//! design/organization § Album Order and Recency).
3//!
4//! **Most recent asset first.** An album ranks by the latest *effective* capture time among its
5//! assets that are neither hidden nor trashed: the index's `COALESCE(capture_utc,
6//! capture_timestamp)`, which the write path and a rebuild both project from the sidecar's
7//! effective capture (the user's correction over the as-imported value). An album with no such
8//! asset ranks by its creation time, read from its UUIDv7 id so it is the same on every device
9//! and across an index rebuild. An album whose id carries no creation time (the derived default
10//! album) and holds no visible asset ranks after every dated album. Ties are broken by ascending
11//! `album_id`.
12//!
13//! Recency is derived, never stored: [`DatabaseDriver::album_recency`](super::DatabaseDriver::album_recency)
14//! reads it from the index on demand, so imports, moves, deletes, restores and capture-date
15//! edits change it with nothing to keep in step.
16
17use std::cmp::Reverse;
18use std::collections::HashMap;
19
20use uuid::Uuid;
21
22/// The latest effective capture (Unix seconds) of each album's visible, untrashed assets, keyed
23/// by the album id as the index stores it. An album absent from the map has no such asset.
24#[derive(Debug, Clone, Default, PartialEq, Eq)]
25pub struct AlbumRecency {
26 latest_capture: HashMap<String, i64>,
27}
28
29impl AlbumRecency {
30 /// Recency over `latest_capture` (album id → latest effective capture, Unix seconds).
31 pub fn from_latest_captures(latest_capture: HashMap<String, i64>) -> Self {
32 Self { latest_capture }
33 }
34
35 /// The latest effective capture among `album_id`'s visible, untrashed assets, if it has any.
36 pub fn latest_capture(&self, album_id: &str) -> Option<i64> {
37 self.latest_capture.get(album_id).copied()
38 }
39
40 /// The instant `album_id` ranks by: its latest capture, else its creation time, else `None`
41 /// (ranked after every album that has one).
42 pub fn rank(&self, album_id: &str) -> Option<i64> {
43 self.latest_capture(album_id)
44 .or_else(|| album_creation_secs(album_id))
45 }
46
47 /// Sort `items` into the album order: by [`rank`](Self::rank), latest first and undated
48 /// last, then by ascending `album_id`.
49 pub fn sort<T>(&self, items: &mut [T], album_id: impl Fn(&T) -> String) {
50 items.sort_by_cached_key(|item| {
51 let id = album_id(item);
52 (Reverse(self.rank(&id)), id)
53 });
54 }
55}
56
57/// An album's creation time (Unix seconds), read from its UUIDv7 id; `None` for an id that
58/// carries none (a derived or non-v7 id, or one that is not a UUID).
59pub fn album_creation_secs(album_id: &str) -> Option<i64> {
60 let id = Uuid::parse_str(album_id).ok()?;
61 if id.get_version_num() != 7 {
62 return None;
63 }
64 let (secs, _nanos) = id.get_timestamp()?.to_unix();
65 i64::try_from(secs).ok()
66}
67
68#[cfg(test)]
69mod tests;