Skip to main content

spoolframe_protocol/
recently_played.rs

1//! Home's "Recently played" row (feature `spoolframe:recently-played-row`):
2//! one row of every kind, each card once at its latest counted play.
3//!
4//! A client says where it started a track ([`PlaySource`]) as it starts
5//! the playback (`StartPlayback.source`) or records a play made offline
6//! (`OfflinePlay.source`). The field needs no feature: a server that does
7//! not know it ignores it. The server keeps it with the play, and
8//! `RecentlyPlayedRow` answers cards ([`RecentCard`]) by it and by the
9//! user's [`RecentlyPlayedPreferences`]: for music, a play from a
10//! playlist shows the playlist, one from an album the album, and a track
11//! queued by hand the track; shows by show, season or episode; films each
12//! their own. The preferences only change what the row shows: every play
13//! is recorded and counted whatever they say.
14
15use serde::{Deserialize, Serialize};
16use spoolframe_types::{Album, Episode, MediaId, Movie, Season, Show, Track};
17pub use spoolframe_types::{
18    RecentMoviesMode, RecentMusicMode, RecentShowsMode, RecentlyPlayedPreferences,
19};
20
21use crate::playlists::PlaylistSummary;
22
23/// The most cards a `RecentlyPlayedRow` answers.
24pub const MAX_ROW: u32 = 50;
25
26/// Where a track's play was started.
27#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
28#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
29#[serde(tag = "kind", rename_all = "snake_case")]
30pub enum PlaySource {
31    /// Playing album `id`, in order or shuffled.
32    Album { id: MediaId },
33    /// Playing playlist `id` (Liked and All included).
34    Playlist { id: String },
35    /// Added to the queue by hand ("Play next", "Add to queue").
36    Queued,
37    /// A source this build does not know: kept as no source.
38    #[serde(other)]
39    Unknown,
40}
41
42/// One card of Home's "Recently played" row. A film, an episode or a
43/// track has its watch state and play count filled.
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
46#[serde(tag = "type", rename_all = "snake_case")]
47// Few cards a row; boxed, the apps' pattern would change for nothing.
48#[allow(clippy::large_enum_variant)]
49pub enum RecentCard {
50    Movie {
51        movie: Movie,
52    },
53    /// A show, at its latest episode played.
54    Show {
55        show: Show,
56    },
57    /// A season, at its latest episode played.
58    Season {
59        season: Season,
60    },
61    Episode {
62        episode: Episode,
63    },
64    Album {
65        album: Album,
66    },
67    Playlist {
68        playlist: PlaylistSummary,
69    },
70    Track {
71        track: Track,
72    },
73    /// A card this build does not know: leave it out.
74    #[serde(other)]
75    Unknown,
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use crate::codec::{decode, encode};
82    use spoolframe_types::{MediaKind, Namespace};
83
84    fn round_trip<T: Serialize + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug>(t: T) {
85        assert_eq!(decode::<T>(&encode(&t).unwrap()).unwrap(), t);
86    }
87
88    /// The names are promised to the apps.
89    #[test]
90    fn the_wire_names_are_fixed() {
91        let album = MediaId::new(Namespace::Plex, MediaKind::Album, "7").unwrap();
92        let cases = [
93            (
94                PlaySource::Album { id: album },
95                serde_json::json!({"kind": "album", "id": "plex:album:7"}),
96            ),
97            (
98                PlaySource::Playlist { id: "p1".into() },
99                serde_json::json!({"kind": "playlist", "id": "p1"}),
100            ),
101            (PlaySource::Queued, serde_json::json!({"kind": "queued"})),
102        ];
103        for (source, json) in cases {
104            assert_eq!(serde_json::to_value(&source).unwrap(), json);
105            round_trip(source);
106        }
107        let preferences = RecentlyPlayedPreferences {
108            music: RecentMusicMode::Albums,
109            shows: RecentShowsMode::Season,
110            movies: RecentMoviesMode::Hidden,
111        };
112        assert_eq!(
113            serde_json::to_value(preferences).unwrap(),
114            serde_json::json!({"music": "albums", "shows": "season", "movies": "hidden"})
115        );
116        round_trip(preferences);
117    }
118
119    #[test]
120    fn the_defaults_are_by_source_by_episode_and_each_film() {
121        assert_eq!(
122            RecentlyPlayedPreferences::default(),
123            RecentlyPlayedPreferences {
124                music: RecentMusicMode::Sources,
125                shows: RecentShowsMode::Episode,
126                movies: RecentMoviesMode::Movie,
127            }
128        );
129    }
130
131    /// What a newer peer adds reads as unknown, through CBOR too.
132    #[test]
133    fn what_a_newer_peer_adds_reads_as_unknown() {
134        let source = serde_json::json!({"kind": "radio", "station": "x"});
135        assert_eq!(
136            decode::<PlaySource>(&encode(&source).unwrap()).unwrap(),
137            PlaySource::Unknown
138        );
139        assert_eq!(
140            serde_json::from_value::<RecentMusicMode>(serde_json::json!("shuffled")).unwrap(),
141            RecentMusicMode::Unknown
142        );
143        let card = serde_json::json!({"type": "station", "name": "x"});
144        assert_eq!(
145            decode::<RecentCard>(&encode(&card).unwrap()).unwrap(),
146            RecentCard::Unknown
147        );
148    }
149}