diff --git a/src/federation/devices.rs b/src/federation/devices.rs index aad2fdd..edf4b80 100644 --- a/src/federation/devices.rs +++ b/src/federation/devices.rs @@ -1842,7 +1842,6 @@ async fn advance_vector<'e>( /// Device id of the account's web device, creating the identity on first /// use. Local podcast actions stamp their operations with it. -#[cfg_attr(not(test), allow(dead_code))] // podcast UI and RSS job: later commits pub(crate) async fn local_device_id(pool: &sqlx::PgPool, user_id: i64) -> Result { Ok(ensure_identity(pool, user_id, "").await?.device_id) } @@ -1854,7 +1853,6 @@ pub(crate) async fn local_device_id(pool: &sqlx::PgPool, user_id: i64) -> Result /// `make` receives the `op_id`. The caller applies the returned operation /// itself in the same transaction, so the log and the materialized state /// change together (the podcast store does this for its local actions). -#[cfg_attr(not(test), allow(dead_code))] // podcast UI and RSS job: later commits pub(crate) async fn append_local_op( conn: &mut sqlx::PgConnection, user_id: i64, diff --git a/src/federation/devices/gating_tests.rs b/src/federation/devices/gating_tests.rs index f04ca82..a072595 100644 --- a/src/federation/devices/gating_tests.rs +++ b/src/federation/devices/gating_tests.rs @@ -365,7 +365,7 @@ async fn podcast_queue_survives_server_browser_server_round_trip() { assert_eq!(payload["index"], 3); assert_eq!(payload["position_seconds"], 1200.0); assert_eq!(tracks[0]["source"]["kind"], "podcast_episode"); - assert_eq!(tracks[0]["unavailable"], true); + assert_eq!(tracks[0]["unavailable"], false, "episodes play on the web player"); assert_eq!(tracks[1]["source"]["kind"], "private"); // The browser keeps its queue and sends it back (plus a library track diff --git a/src/player/queue_items.rs b/src/player/queue_items.rs index 69a3d04..9e621fb 100644 --- a/src/player/queue_items.rs +++ b/src/player/queue_items.rs @@ -8,7 +8,10 @@ //! plus the verbatim device-sync DTO under [`WIRE_TRACK_KEY`] and its explicit //! [`PlaybackSource`] under [`SOURCE_KEY`]. Converting back to the wire uses //! the verbatim DTO, so such items survive browser -> server -> peers -> -//! server -> browser unchanged even though this web player cannot play them. +//! server -> browser unchanged. The web player plays podcast episodes +//! through `/api/podcasts/episodes/{id}/playback` (publisher URL or the +//! optional proxy); placeholders and unresolved federated music stay +//! unplayable here. //! //! Gating and Jam decisions are delegated to `music_dht::device_sync`; this //! module only maps browser JSON onto the shared DTOs. @@ -34,9 +37,6 @@ pub(crate) const POSITION_UNKNOWN_KEY: &str = "position_unknown"; /// Reason shown for federated music that is not in this library. pub(crate) const FED_UNAVAILABLE_REASON: &str = "Not available on this web player yet"; -/// Reason shown for podcast episodes, which this web player cannot play yet. -pub(crate) const PODCAST_UNAVAILABLE_REASON: &str = - "Podcast episodes cannot be played on the web player yet"; /// Reason shown for Jam placeholders of someone else's private item. pub(crate) const PRIVATE_UNAVAILABLE_REASON: &str = "Private item of the Jam host"; @@ -68,13 +68,15 @@ pub(crate) fn external_track_json( "release_id": 0, "release_title": episode.podcast_title, "release_year": serde_json::Value::Null, - "cover_url": serde_json::Value::Null, + // Artwork is loaded from the publisher, like the audio. + "cover_url": episode.image_url, + // The player asks `/api/podcasts/episodes/{id}/playback` for the + // audio source when it starts the episode. "stream_url": "", "uploader_name": "Podcast", "audio_format": episode.mime, "content_id": serde_json::Value::Null, - "unavailable": true, - "unavailable_reason": PODCAST_UNAVAILABLE_REASON, + "unavailable": false, }), Some(PlaybackSource::Private) => serde_json::json!({ "id": placeholder_track_id(Some("private"), index), @@ -490,8 +492,15 @@ mod tests { let mut wire = PlaybackTrack::podcast_episode(episode()); wire.play_count = 3; let json = external_track_json(&wire, 4, None); - assert_eq!(json["unavailable"], true); - assert_eq!(json["unavailable_reason"], PODCAST_UNAVAILABLE_REASON); + // Playable on the web player (the source is resolved on play). + assert_eq!(json["unavailable"], false); + assert!(json.get("unavailable_reason").is_none()); + assert_eq!(json["stream_url"], ""); + assert_eq!( + json["cover_url"], + Value::Null, + "the test episode has no artwork" + ); assert_eq!(json["title"], "Episode 42"); assert_eq!(json[SOURCE_KEY]["kind"], "podcast_episode"); assert!(json["id"].as_i64().unwrap() < 0); diff --git a/src/podcasts/actions.rs b/src/podcasts/actions.rs index ae16c9b..9414318 100644 --- a/src/podcasts/actions.rs +++ b/src/podcasts/actions.rs @@ -78,7 +78,6 @@ enum ExplicitAction { Start(i64), Seek(i64), Restart, - Complete(i64), MarkPlayed, MarkUnplayed, } @@ -293,6 +292,9 @@ impl PodcastStore { /// is already effectively shared (`effective_share`). Revoking is a no-op /// when no share decision says `true`. The write uses the next podcast /// generation, so it orders after the subscription it observed. + // The publication toggle is part of public discovery (MR-W3); until + // then only the tests call it. + #[cfg_attr(not(test), allow(dead_code))] pub(crate) async fn set_shared( &self, user_id: i64, @@ -347,7 +349,6 @@ impl PodcastStore { (position, ProgressStatus::InProgress) } ExplicitAction::Restart => (0, ProgressStatus::InProgress), - ExplicitAction::Complete(position) => (position, ProgressStatus::Completed), ExplicitAction::MarkPlayed => (current_position, ProgressStatus::Completed), ExplicitAction::MarkUnplayed => (0, ProgressStatus::NotStarted), }; @@ -465,23 +466,74 @@ impl PodcastStore { .await } - /// The session reached the end (`reaches_completion` or end of stream): - /// writes `completed` with an explicit generation. Record the session - /// itself with [`PodcastStore::record_listen_session`]. - pub(crate) async fn complete_session( + /// Completes a running session (end of stream or `reaches_completion`) + /// unless another write took over. Record the session itself with + /// [`PodcastStore::record_listen_session`]. + /// + /// Like [`PodcastStore::checkpoint`], nothing is written when the + /// register holds a greater generation ([`CheckpointOutcome::Superseded`]) + /// or when this session already completed the episode + /// ([`CheckpointOutcome::AlreadyCompleted`]). Otherwise `completed` is + /// written with an explicit generation; the check and the write + /// happen in one transaction, so a periodic save after the end never + /// completes the episode twice. + pub(crate) async fn complete_if_current( &self, user_id: i64, session: &ProgressSession, position_ms: i64, - ) -> Result { - self.explicit_progress( - user_id, - &session.episode, - Some(&session.session_id), - Some(session.generation), - ExplicitAction::Complete(position_ms), - ) - .await + ) -> Result { + let episode = &session.episode; + episode.validate().map_err(invalid)?; + let device_id = devices::local_device_id(&self.pool, user_id).await?; + let mut tx = self.begin_write(user_id).await?; + let current = apply::load_progress(&mut tx, user_id, &episode.episode_id).await?; + if let Some(current) = current.as_ref() { + if current.generation > session.generation { + return Ok(CheckpointOutcome::Superseded { + current: current.clone(), + }); + } + if current.session_id == session.session_id + && current.status == ProgressStatus::Completed + { + return Ok(CheckpointOutcome::AlreadyCompleted { + current: current.clone(), + }); + } + } + let generation = ProgressRecord::explicit_generation( + current + .as_ref() + .map(|record| record.generation) + .into_iter() + .chain(Some(session.generation)) + .max(), + ); + let reference = op_ref(episode); + let op = emit(&mut tx, user_id, &device_id, |_| { + SyncOpPayload::PodcastProgressSet { + episode_id: episode.episode_id.clone(), + podcast_id: episode.podcast_id.clone(), + position_ms, + status: ProgressStatus::Completed, + generation, + session_id: session.session_id.clone(), + episode: Some(reference.clone()), + } + }) + .await?; + tx.commit().await?; + let record = op.podcast_progress().expect("progress payload"); + Ok(CheckpointOutcome::Written(Box::new(ProgressWrite { + record, + session: ProgressSession { + episode: episode.clone(), + session_id: session.session_id.clone(), + generation, + }, + op, + }))) } /// "Start over": position 0, in progress, explicit generation. @@ -568,9 +620,34 @@ impl PodcastStore { Ok(Some(op)) } + /// Makes sure the catalog knows an episode a queue item refers to. + /// + /// Queue items received through a handoff carry a self-contained + /// `PodcastEpisodeRef`; the account may not have refreshed that feed yet. + /// The reference is recorded like one learned through sync + /// (`source = 'sync'`, never replacing feed metadata). Returns whether a + /// row was added. No register is written. + pub(crate) async fn ensure_episode( + &self, + user_id: i64, + episode: &PodcastEpisodeRef, + ) -> Result { + episode.validate().map_err(invalid)?; + let mut tx = self.begin_write(user_id).await?; + let known = super::queries::episode_ref_in(&mut tx, user_id, &episode.episode_id) + .await? + .is_some(); + if !known { + apply::ensure_episode_from_ref(&mut tx, user_id, episode).await?; + } + tx.commit().await?; + Ok(!known) + } + /// Resolves a feed URL to a podcast id: the existing podcast when the /// URL is a known feed URL or alias (`resolve_podcast_id`), otherwise /// the id a first subscription through this URL would derive. + #[cfg(test)] pub(crate) async fn resolve_podcast_id( &self, user_id: i64, diff --git a/src/podcasts/api.rs b/src/podcasts/api.rs new file mode 100644 index 0000000..7ebf052 --- /dev/null +++ b/src/podcasts/api.rs @@ -0,0 +1,996 @@ +//! Request handling of the podcast UI and the web player. +//! +//! [`super::http`] authenticates the request and passes the account here. +//! Every function takes the store, the account and the decoded input and +//! returns a JSON view or an [`ApiError`] with its HTTP status, so the +//! behavior of the endpoints is testable without an HTTP server +//! (`postgres_tests`). +//! +//! Playback follows the progress rules of the frid podcast contract +//! (`PODCASTS.md`, "Progress" and "Listening sessions and counts"): +//! +//! * [`playback`] prepares an episode for the player: the audio source, the +//! position ordinary Play resumes from and, when the browser starts +//! playing (`start`), a new listening session written as an explicit +//! action (`explicit_generation(max(local, carried))`). +//! * [`progress`] stores what the player reports for that session: periodic +//! checkpoints and pause/stop/handoff saves keep the session generation, a +//! user seek is an explicit action, the end of the stream or +//! `reaches_completion` completes the episode once, and the listening +//! session is recorded (history counts only completed sessions). + +use music_dht::device_sync::PlaybackTrack; +use music_dht::podcast::{ + EpisodeId, EpisodeType, FeedUrl, ListenSession, PodcastEpisodeRef, PodcastId, PodcastMeta, + ProgressRecord, ProgressStatus, reaches_completion, +}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +use super::fetch::{FeedFetcher, FetchErrorKind}; +use super::runner::{self, SubscribeUrlError}; +use super::{ + CheckpointOutcome, EpisodeListItem, EpisodeQuery, EpisodeSort, FetchState, PlayedFilter, + PodcastListFilter, PodcastStore, PodcastStoreError, PodcastSummary, ProgressSession, + SeasonFilter, SeasonSummary, +}; +use crate::federation::devices; +use crate::player::queue_items; + +/// Episodes per page when the request does not say. +pub(crate) const DEFAULT_EPISODE_PAGE: usize = 50; +/// Largest episode page. +pub(crate) const MAX_EPISODE_PAGE: usize = 200; + +/// A failed request: HTTP status and a message for the user. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct ApiError { + pub(crate) status: u16, + pub(crate) message: String, +} + +impl ApiError { + pub(crate) fn new(status: u16, message: impl Into) -> Self { + Self { + status, + message: message.into(), + } + } + + fn bad_request(message: impl Into) -> Self { + Self::new(400, message) + } + + fn not_found(message: impl Into) -> Self { + Self::new(404, message) + } +} + +impl From for ApiError { + fn from(error: anyhow::Error) -> Self { + if let Some(error) = error.downcast_ref::() { + return match error { + PodcastStoreError::UnknownPodcast(_) => Self::not_found(error.to_string()), + PodcastStoreError::NotSubscribed(_) => Self::new(409, error.to_string()), + PodcastStoreError::Invalid(_) => Self::bad_request(error.to_string()), + }; + } + if let Some(SubscribeUrlError::Fetch(fetch)) = error.downcast_ref::() { + // The feed could not be used: the request cannot succeed as is. + let status = match fetch.kind { + FetchErrorKind::Timeout | FetchErrorKind::Transport => 502, + _ => 422, + }; + return Self::new(status, error.to_string()); + } + tracing::warn!("podcast API request failed: {error:#}"); + Self::new(500, "internal error") + } +} + +pub(crate) type ApiResult = Result; + +pub(crate) fn parse_podcast_id(value: &str) -> ApiResult { + PodcastId::parse(value).map_err(|_| ApiError::bad_request("invalid podcast id")) +} + +pub(crate) fn parse_episode_id(value: &str) -> ApiResult { + EpisodeId::parse(value).map_err(|_| ApiError::bad_request("invalid episode id")) +} + +fn decode(body: Value) -> ApiResult { + serde_json::from_value(body).map_err(|error| ApiError::bad_request(error.to_string())) +} + +fn status_name(status: ProgressStatus) -> &'static str { + match status { + ProgressStatus::NotStarted => "not_started", + ProgressStatus::InProgress => "in_progress", + ProgressStatus::Completed => "completed", + } +} + +fn episode_type_name(kind: EpisodeType) -> &'static str { + match kind { + EpisodeType::Full => "full", + EpisodeType::Trailer => "trailer", + EpisodeType::Bonus => "bonus", + } +} + +// --------------------------------------------------------------------------- +// Views +// --------------------------------------------------------------------------- + +/// Local fetch state shown next to a podcast (never synced). +#[derive(Debug, Clone, Serialize)] +pub(crate) struct FetchView { + pub(crate) checked_at_ms: Option, + pub(crate) last_success_at_ms: Option, + pub(crate) next_check_at_ms: Option, + pub(crate) last_error: Option, + pub(crate) consecutive_failures: u32, +} + +impl From for FetchView { + fn from(state: FetchState) -> Self { + Self { + checked_at_ms: state.checked_at_ms, + last_success_at_ms: state.last_success_at_ms, + next_check_at_ms: state.next_check_at_ms, + last_error: state.last_error, + consecutive_failures: state.consecutive_failures, + } + } +} + +/// A podcast with its personal state and counters. +#[derive(Debug, Clone, Serialize)] +pub(crate) struct PodcastView { + pub(crate) podcast_id: String, + /// URL this account fetches. + pub(crate) feed_url: String, + pub(crate) title: String, + pub(crate) author: Option, + pub(crate) description: Option, + pub(crate) link: Option, + pub(crate) image_url: Option, + pub(crate) subscribed: bool, + /// `effective_share` (publication is MR-W3; shown read-only). + pub(crate) shared: bool, + pub(crate) episode_count: u64, + pub(crate) unplayed_count: u64, + pub(crate) in_progress_count: u64, + pub(crate) season_count: u64, + pub(crate) latest_published_at_ms: Option, + pub(crate) fetch: FetchView, +} + +impl PodcastView { + fn new(summary: PodcastSummary, fetch: FetchState) -> Self { + Self { + podcast_id: summary.podcast_id.to_string(), + feed_url: summary.meta.feed_url.to_string(), + title: summary.meta.title, + author: summary.meta.author, + description: summary.meta.description, + link: summary.meta.link, + image_url: summary.meta.image_url, + subscribed: summary.subscribed, + shared: summary.shared, + episode_count: summary.episode_count, + unplayed_count: summary.unplayed_count, + in_progress_count: summary.in_progress_count, + season_count: summary.season_count, + latest_published_at_ms: summary.latest_published_at_ms, + fetch: fetch.into(), + } + } +} + +/// One season group (`season: null` holds the episodes without a season). +#[derive(Debug, Clone, Serialize)] +pub(crate) struct SeasonView { + pub(crate) season: Option, + pub(crate) episode_count: u64, + pub(crate) unplayed_count: u64, + pub(crate) first_published_at_ms: Option, + pub(crate) last_published_at_ms: Option, +} + +impl From for SeasonView { + fn from(season: SeasonSummary) -> Self { + Self { + season: season.season, + episode_count: season.episode_count, + unplayed_count: season.unplayed_count, + first_published_at_ms: season.first_published_at_ms, + last_published_at_ms: season.last_published_at_ms, + } + } +} + +/// An episode with its progress and the queue item the player uses. +#[derive(Debug, Clone, Serialize)] +pub(crate) struct EpisodeView { + pub(crate) episode_id: String, + pub(crate) podcast_id: String, + pub(crate) title: String, + pub(crate) description: Option, + pub(crate) season: Option, + pub(crate) episode_number: Option, + pub(crate) episode_type: &'static str, + pub(crate) published_at_ms: Option, + pub(crate) duration_ms: Option, + pub(crate) image_url: Option, + pub(crate) link: Option, + /// Whether the latest refresh listed the episode (older episodes stay). + pub(crate) in_feed: bool, + /// `not_started`, `in_progress` or `completed`. + pub(crate) status: &'static str, + /// Stored position of the progress register. + pub(crate) position_ms: i64, + /// Where ordinary Play resumes (`ProgressRecord::resume_position_ms`). + pub(crate) resume_position_ms: i64, + /// Distinct completed listening sessions. + pub(crate) completed_count: u64, + /// Browser queue item: `PlaybackSource::PodcastEpisode` with the + /// self-contained reference (see `player::queue_items`). + pub(crate) track: Value, +} + +/// The queue reference of a catalog episode, with the register generation +/// as handoff hint. +fn episode_ref(item: &EpisodeListItem, podcast: &PodcastMeta) -> PodcastEpisodeRef { + let mut reference = item.episode.to_ref(podcast); + reference.progress_generation = item.progress.as_ref().map(|record| record.generation); + reference +} + +/// Browser queue item of an episode reference. +pub(crate) fn episode_track_json(reference: PodcastEpisodeRef) -> Value { + queue_items::external_track_json(&PlaybackTrack::podcast_episode(reference), 0, None) +} + +impl EpisodeView { + fn new(item: EpisodeListItem, podcast: &PodcastMeta) -> Self { + let track = episode_track_json(episode_ref(&item, podcast)); + let status = status_name(item.status()); + let resume_position_ms = item.resume_position_ms(); + let position_ms = item + .progress + .as_ref() + .map_or(0, |progress| progress.position_ms); + let episode = item.episode; + Self { + episode_id: episode.episode_id.to_string(), + podcast_id: episode.podcast_id.to_string(), + title: episode.title, + description: episode.description, + season: episode.season, + episode_number: episode.episode_number, + episode_type: episode_type_name(episode.episode_type), + published_at_ms: episode.published_at_ms, + duration_ms: episode.duration_ms, + image_url: episode.image_url, + link: episode.link, + in_feed: item.in_feed, + status, + position_ms, + resume_position_ms, + completed_count: item.completed_count, + track, + } + } +} + +async fn podcast_view( + store: &PodcastStore, + user_id: i64, + podcast_id: &PodcastId, +) -> ApiResult { + let summary = store + .podcast(user_id, podcast_id) + .await? + .ok_or_else(|| ApiError::not_found("unknown podcast"))?; + let fetch = store.fetch_state(user_id, podcast_id).await?; + Ok(PodcastView::new(summary, fetch)) +} + +async fn episode_view( + store: &PodcastStore, + user_id: i64, + episode_id: &EpisodeId, +) -> ApiResult { + let item = store + .episode(user_id, episode_id) + .await? + .ok_or_else(|| ApiError::not_found("unknown episode"))?; + let podcast = store + .podcast(user_id, &item.episode.podcast_id) + .await? + .ok_or_else(|| ApiError::not_found("unknown podcast"))?; + Ok(EpisodeView::new(item, &podcast.meta)) +} + +async fn catalog_ref( + store: &PodcastStore, + user_id: i64, + episode_id: &EpisodeId, +) -> ApiResult { + store + .episode_ref(user_id, episode_id) + .await? + .ok_or_else(|| ApiError::not_found("unknown episode")) +} + +// --------------------------------------------------------------------------- +// Podcasts +// --------------------------------------------------------------------------- + +#[derive(Debug, Serialize)] +pub(crate) struct PodcastList { + pub(crate) podcasts: Vec, +} + +/// Podcasts of the account: subscribed ones, or with `all` also +/// unsubscribed podcasts and podcasts only known from synced progress. +pub(crate) async fn list_podcasts( + store: &PodcastStore, + user_id: i64, + all: bool, +) -> ApiResult { + let filter = if all { + PodcastListFilter::All + } else { + PodcastListFilter::Subscribed + }; + let mut podcasts = Vec::new(); + for summary in store.list_podcasts(user_id, filter).await? { + let fetch = store.fetch_state(user_id, &summary.podcast_id).await?; + podcasts.push(PodcastView::new(summary, fetch)); + } + Ok(PodcastList { podcasts }) +} + +/// Listening sessions shown in a podcast's history. +pub(crate) const DETAIL_HISTORY_LIMIT: usize = 20; + +/// A listening session in the history (completed or at least 30 s). +#[derive(Debug, Clone, Serialize)] +pub(crate) struct HistoryView { + pub(crate) episode_id: String, + pub(crate) episode_title: Option, + pub(crate) started_at_ms: i64, + pub(crate) ended_at_ms: i64, + pub(crate) listened_ms: i64, + pub(crate) completed: bool, +} + +#[derive(Debug, Serialize)] +pub(crate) struct PodcastDetail { + pub(crate) podcast: PodcastView, + pub(crate) seasons: Vec, + /// Newest listening sessions of this podcast, from every device. + pub(crate) history: Vec, +} + +/// One podcast with its season groups. +pub(crate) async fn podcast_detail( + store: &PodcastStore, + user_id: i64, + podcast_id: &PodcastId, +) -> ApiResult { + let podcast = podcast_view(store, user_id, podcast_id).await?; + let seasons = store + .seasons(user_id, podcast_id) + .await? + .into_iter() + .map(SeasonView::from) + .collect(); + let history = store + .listen_history(user_id, Some(podcast_id), DETAIL_HISTORY_LIMIT) + .await? + .into_iter() + .map(|entry| HistoryView { + episode_id: entry.session.episode_id.to_string(), + episode_title: entry.episode_title, + started_at_ms: entry.session.started_at_ms, + ended_at_ms: entry.session.ended_at_ms, + listened_ms: entry.session.listened_ms, + completed: entry.session.completed, + }) + .collect(); + Ok(PodcastDetail { + podcast, + seasons, + history, + }) +} + +#[derive(Debug, Deserialize)] +struct SubscribeRequest { + url: String, +} + +#[derive(Debug, Serialize)] +pub(crate) struct SubscribeResponse { + pub(crate) podcast: PodcastView, + pub(crate) already_subscribed: bool, + pub(crate) new_episodes: usize, +} + +/// "Add RSS": `{ "url": "..." }`. The feed is fetched before anything is +/// stored; the new subscription is private. +pub(crate) async fn subscribe_url( + store: &PodcastStore, + fetcher: &FeedFetcher, + user_id: i64, + body: Value, + now_ms: i64, +) -> ApiResult { + let request: SubscribeRequest = decode(body)?; + let url = FeedUrl::from_user_input(&request.url) + .map_err(|error| ApiError::bad_request(format!("invalid feed URL: {error}")))?; + let subscription = runner::subscribe_url(store, fetcher, user_id, &url, now_ms).await?; + Ok(SubscribeResponse { + podcast: podcast_view(store, user_id, &subscription.podcast_id).await?, + already_subscribed: subscription.already_subscribed, + new_episodes: subscription.refresh.new_episodes, + }) +} + +/// Subscribes again to a known podcast with its stored channel metadata +/// (progress and history continue). The refresh job fetches it on its next +/// run. +pub(crate) async fn resubscribe( + store: &PodcastStore, + user_id: i64, + podcast_id: &PodcastId, +) -> ApiResult { + let summary = store + .podcast(user_id, podcast_id) + .await? + .ok_or_else(|| ApiError::not_found("unknown podcast"))?; + store.subscribe(user_id, &summary.meta).await?; + podcast_view(store, user_id, podcast_id).await +} + +/// Unsubscribes: stops monitoring, revokes a publication, keeps progress +/// and history. +pub(crate) async fn unsubscribe( + store: &PodcastStore, + user_id: i64, + podcast_id: &PodcastId, +) -> ApiResult { + if store.podcast(user_id, podcast_id).await?.is_none() { + return Err(ApiError::not_found("unknown podcast")); + } + store.unsubscribe(user_id, podcast_id).await?; + podcast_view(store, user_id, podcast_id).await +} + +// --------------------------------------------------------------------------- +// Episodes +// --------------------------------------------------------------------------- + +/// Query string of the episode list. +#[derive(Debug, Clone, Default, Deserialize)] +pub(crate) struct EpisodeListParams { + /// Season number, or `none` for the episodes without a season. + #[serde(default)] + pub(crate) season: Option, + /// `unplayed`, `not_started`, `in_progress` or `played`. + #[serde(default)] + pub(crate) played: Option, + /// Search text (titles and descriptions). + #[serde(default)] + pub(crate) q: Option, + /// `newest` (default), `oldest`, `number` or `number_desc`. + #[serde(default)] + pub(crate) sort: Option, + #[serde(default)] + pub(crate) limit: Option, + #[serde(default)] + pub(crate) offset: Option, +} + +impl EpisodeListParams { + /// The store query (one row more than the page, to tell whether more + /// rows follow). + pub(crate) fn query(&self, podcast_id: PodcastId) -> ApiResult<(EpisodeQuery, usize)> { + let blank = |value: &Option| { + value + .as_deref() + .map(str::trim) + .filter(|value| !value.is_empty()) + .map(str::to_string) + }; + let season = match blank(&self.season).as_deref() { + None | Some("all") => SeasonFilter::Any, + Some("none") => SeasonFilter::NoSeason, + Some(value) => SeasonFilter::Season( + value + .parse() + .map_err(|_| ApiError::bad_request("invalid season"))?, + ), + }; + let played = match blank(&self.played).as_deref() { + None | Some("all") => PlayedFilter::Any, + Some("unplayed") => PlayedFilter::Unplayed, + Some("not_started") => PlayedFilter::NotStarted, + Some("in_progress") => PlayedFilter::InProgress, + Some("played") => PlayedFilter::Played, + Some(_) => return Err(ApiError::bad_request("invalid played filter")), + }; + let sort = match blank(&self.sort).as_deref() { + None | Some("newest") => EpisodeSort::NewestFirst, + Some("oldest") => EpisodeSort::OldestFirst, + Some("number") => EpisodeSort::EpisodeNumber, + Some("number_desc") => EpisodeSort::EpisodeNumberDesc, + Some(_) => return Err(ApiError::bad_request("invalid sort")), + }; + let limit = self + .limit + .unwrap_or(DEFAULT_EPISODE_PAGE) + .clamp(1, MAX_EPISODE_PAGE); + let mut query = EpisodeQuery::new(podcast_id); + query.season = season; + query.played = played; + query.search = blank(&self.q); + query.sort = sort; + query.limit = Some(limit + 1); + query.offset = self.offset.unwrap_or(0); + Ok((query, limit)) + } +} + +#[derive(Debug, Serialize)] +pub(crate) struct EpisodePage { + pub(crate) episodes: Vec, + pub(crate) offset: usize, + pub(crate) has_more: bool, +} + +/// A page of episodes of one podcast. +pub(crate) async fn episodes( + store: &PodcastStore, + user_id: i64, + podcast_id: &PodcastId, + params: &EpisodeListParams, +) -> ApiResult { + let podcast = store + .podcast(user_id, podcast_id) + .await? + .ok_or_else(|| ApiError::not_found("unknown podcast"))?; + let (query, limit) = params.query(podcast_id.clone())?; + let mut rows = store.episodes(user_id, &query).await?; + let has_more = rows.len() > limit; + rows.truncate(limit); + Ok(EpisodePage { + episodes: rows + .into_iter() + .map(|item| EpisodeView::new(item, &podcast.meta)) + .collect(), + offset: query.offset, + has_more, + }) +} + +/// Explicit progress actions of the episode list. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum EpisodeAction { + /// "Start over": position 0, in progress. + Restart, + /// "Played": completed, no listening session, no invented time. + MarkPlayed, + /// "Unplayed": not started at 0. + MarkUnplayed, +} + +/// Runs an explicit progress action and returns the updated episode. +pub(crate) async fn episode_action( + store: &PodcastStore, + user_id: i64, + episode_id: &EpisodeId, + action: EpisodeAction, +) -> ApiResult { + let reference = catalog_ref(store, user_id, episode_id).await?; + match action { + EpisodeAction::Restart => { + let session_id = format!("web-{}", uuid::Uuid::new_v4().simple()); + store.restart(user_id, &reference, &session_id).await?; + } + EpisodeAction::MarkPlayed => { + store.mark_played(user_id, &reference).await?; + } + EpisodeAction::MarkUnplayed => { + store.mark_unplayed(user_id, &reference).await?; + } + } + episode_view(store, user_id, episode_id).await +} + +// --------------------------------------------------------------------------- +// Playback +// --------------------------------------------------------------------------- + +/// Body of `POST /episodes/{id}/playback`. +#[derive(Debug, Clone, Default, Deserialize)] +pub(crate) struct PlaybackRequest { + /// The queue item's reference. Used only when the catalog does not know + /// the episode yet (an item received through a handoff); it is recorded + /// like a reference learned through sync. + #[serde(default)] + pub(crate) episode: Option, + /// Start a listening session now (the browser is about to play). + #[serde(default)] + pub(crate) start: bool, + /// Session id generated by the browser (required with `start`). + #[serde(default)] + pub(crate) session_id: Option, + /// Position to start at: a handoff or the user's choice. Without it the + /// episode resumes from its progress register. + #[serde(default)] + pub(crate) position_ms: Option, + /// `PodcastEpisodeRef::progress_generation` of a handed-off queue item. + #[serde(default)] + pub(crate) carried_generation: Option, +} + +/// How the browser plays an episode. +#[derive(Debug, Clone, Serialize)] +pub(crate) struct PlaybackResponse { + pub(crate) episode_id: String, + pub(crate) podcast_id: String, + /// Session started by this request (`start`), with the generation its + /// checkpoints use. + pub(crate) session_id: Option, + pub(crate) generation: Option, + /// Where to start. + pub(crate) position_ms: i64, + /// Register status before this request. + pub(crate) status: &'static str, + pub(crate) duration_ms: Option, + /// Publisher URL (default: the browser streams it directly). + pub(crate) direct_url: String, + /// Same-origin proxy path when the administrator enabled the proxy. + pub(crate) proxy_url: Option, + pub(crate) mime: Option, +} + +/// Prepares an episode for the player and, with `start`, begins a session. +pub(crate) async fn playback( + store: &PodcastStore, + user_id: i64, + episode_id: &EpisodeId, + body: Value, + proxy_enabled: bool, +) -> ApiResult { + let request: PlaybackRequest = decode(body)?; + if store.episode_ref(user_id, episode_id).await?.is_none() { + match request.episode.as_ref() { + Some(reference) if reference.episode_id == *episode_id => { + store.ensure_episode(user_id, reference).await?; + } + Some(_) => return Err(ApiError::bad_request("episode reference mismatch")), + None => return Err(ApiError::not_found("unknown episode")), + } + } + let reference = catalog_ref(store, user_id, episode_id).await?; + let current = store.progress(user_id, episode_id).await?; + let status = current + .as_ref() + .map_or(ProgressStatus::NotStarted, |record| record.status); + let resume = current + .as_ref() + .map_or(0, ProgressRecord::resume_position_ms); + let mut position_ms = request.position_ms.unwrap_or(resume).max(0); + let (mut session_id, mut generation) = (None, None); + if request.start { + let id = request + .session_id + .as_deref() + .map(str::trim) + .filter(|id| !id.is_empty()) + .ok_or_else(|| ApiError::bad_request("session_id is required to start"))?; + let write = store + .start_session( + user_id, + &reference, + id, + position_ms, + request.carried_generation, + ) + .await?; + position_ms = write.record.position_ms; + session_id = Some(write.session.session_id); + generation = Some(write.session.generation); + } + let (direct_url, mime) = store + .audio_source(user_id, episode_id) + .await? + .ok_or_else(|| ApiError::not_found("unknown episode"))?; + Ok(PlaybackResponse { + episode_id: episode_id.to_string(), + podcast_id: reference.podcast_id.to_string(), + session_id, + generation, + position_ms, + status: status_name(status), + duration_ms: reference.duration_ms, + direct_url, + proxy_url: proxy_enabled.then(|| super::http::proxy_path(episode_id)), + mime, + }) +} + +/// What the player reports in `POST /episodes/{id}/progress`. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "snake_case")] +pub(crate) enum ProgressEvent { + /// Periodic save while playing (every 15–30 s). + Checkpoint, + /// The user paused. + Pause, + /// Playback stopped: another item, the page closed, the queue cleared. + Stop, + /// Playback moves to another device. + Handoff, + /// The user moved the position (explicit action). + Seek, + /// The audio reached the end of the stream. + Ended, +} + +impl ProgressEvent { + /// Whether the event ends a stretch of listening that belongs in the + /// session record. + fn records_session(self) -> bool { + matches!(self, Self::Pause | Self::Stop | Self::Handoff | Self::Ended) + } +} + +/// Body of `POST /episodes/{id}/progress`. +#[derive(Debug, Clone, Deserialize)] +pub(crate) struct ProgressRequest { + pub(crate) session_id: String, + /// Generation of the session's latest explicit action. + pub(crate) generation: u64, + pub(crate) event: ProgressEvent, + pub(crate) position_ms: i64, + /// Duration of the loaded audio, when the browser knows it. + #[serde(default)] + pub(crate) duration_ms: Option, + /// Time actually listened in this session (seeks excluded). + #[serde(default)] + pub(crate) listened_ms: Option, + /// When the session started (browser clock, unix ms). + #[serde(default)] + pub(crate) started_at_ms: Option, + #[serde(default)] + pub(crate) start_position_ms: Option, +} + +/// Result of a progress report. +#[derive(Debug, Clone, Serialize)] +pub(crate) struct ProgressResponse { + /// `written`, `superseded` (another device or action took over: stop + /// writing at this generation) or `already_completed`. + pub(crate) outcome: &'static str, + /// Generation to use for the next checkpoints of this session. + pub(crate) generation: u64, + /// Register after the report. + pub(crate) status: &'static str, + pub(crate) position_ms: i64, + /// Whether this report completed the episode. + pub(crate) completed: bool, + /// Whether the listening session was recorded (or grew). + pub(crate) session_recorded: bool, +} + +/// Stores a progress report of the player's current session. +pub(crate) async fn progress( + store: &PodcastStore, + user_id: i64, + episode_id: &EpisodeId, + body: Value, + now_ms: i64, +) -> ApiResult { + let request: ProgressRequest = decode(body)?; + let reference = catalog_ref(store, user_id, episode_id).await?; + let position_ms = request.position_ms.max(0); + let duration_ms = request + .duration_ms + .filter(|duration| *duration > 0) + .or(reference.duration_ms); + let session = ProgressSession { + episode: reference.clone(), + session_id: request.session_id.trim().to_string(), + generation: request.generation, + }; + let reached_end = + request.event == ProgressEvent::Ended || reaches_completion(position_ms, duration_ms); + let outcome = match request.event { + ProgressEvent::Seek => { + CheckpointOutcome::Written(Box::new(store.seek(user_id, &session, position_ms).await?)) + } + _ if reached_end => { + store + .complete_if_current(user_id, &session, position_ms) + .await? + } + _ => store.checkpoint(user_id, &session, position_ms).await?, + }; + let (name, record, completed) = match &outcome { + CheckpointOutcome::Written(write) => ( + "written", + write.record.clone(), + write.record.status == ProgressStatus::Completed && reached_end, + ), + CheckpointOutcome::Superseded { current } => ("superseded", current.clone(), false), + CheckpointOutcome::AlreadyCompleted { current } => { + ("already_completed", current.clone(), false) + } + }; + let mut session_recorded = false; + if request.event.records_session() + && let Some(started_at_ms) = request.started_at_ms + { + let started_at_ms = started_at_ms.clamp(0, now_ms); + let listened_ms = request + .listened_ms + .unwrap_or(0) + .clamp(0, now_ms - started_at_ms); + let listen = ListenSession { + session_id: session.session_id.clone(), + episode_id: episode_id.clone(), + podcast_id: reference.podcast_id.clone(), + device_id: devices::local_device_id(store.pool(), user_id).await?, + started_at_ms, + ended_at_ms: now_ms, + start_position_ms: request.start_position_ms.unwrap_or(0).max(0), + end_position_ms: position_ms, + listened_ms, + completed: reached_end, + }; + session_recorded = store + .record_listen_session(user_id, &listen, Some(&reference)) + .await? + .is_some(); + } + Ok(ProgressResponse { + outcome: name, + generation: record.generation, + status: status_name(record.status), + position_ms: record.position_ms, + completed, + session_recorded, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn podcast() -> PodcastId { + PodcastId::from_feed_url(&FeedUrl::parse("https://podcasts.example/feed.xml").unwrap()) + } + + #[test] + fn episode_list_params_map_to_store_queries() { + let (query, limit) = EpisodeListParams::default().query(podcast()).unwrap(); + assert_eq!(limit, DEFAULT_EPISODE_PAGE); + assert_eq!(query.limit, Some(DEFAULT_EPISODE_PAGE + 1)); + assert_eq!(query.season, SeasonFilter::Any); + assert_eq!(query.played, PlayedFilter::Any); + assert_eq!(query.sort, EpisodeSort::NewestFirst); + assert_eq!(query.search, None); + + let params = EpisodeListParams { + season: Some("3".into()), + played: Some("in_progress".into()), + q: Some(" interview ".into()), + sort: Some("number_desc".into()), + limit: Some(10_000), + offset: Some(40), + }; + let (query, limit) = params.query(podcast()).unwrap(); + assert_eq!(query.season, SeasonFilter::Season(3)); + assert_eq!(query.played, PlayedFilter::InProgress); + assert_eq!(query.search.as_deref(), Some("interview")); + assert_eq!(query.sort, EpisodeSort::EpisodeNumberDesc); + assert_eq!(limit, MAX_EPISODE_PAGE); + assert_eq!(query.offset, 40); + + let none = EpisodeListParams { + season: Some("none".into()), + played: Some("played".into()), + q: Some(" ".into()), + sort: Some("oldest".into()), + ..Default::default() + }; + let (query, _) = none.query(podcast()).unwrap(); + assert_eq!(query.season, SeasonFilter::NoSeason); + assert_eq!(query.played, PlayedFilter::Played); + assert_eq!(query.search, None, "blank search is no filter"); + assert_eq!(query.sort, EpisodeSort::OldestFirst); + + for bad in [ + EpisodeListParams { + season: Some("first".into()), + ..Default::default() + }, + EpisodeListParams { + played: Some("sometimes".into()), + ..Default::default() + }, + EpisodeListParams { + sort: Some("random".into()), + ..Default::default() + }, + ] { + assert_eq!(bad.query(podcast()).unwrap_err().status, 400); + } + } + + #[test] + fn ids_and_bodies_are_validated_before_the_store() { + assert_eq!(parse_podcast_id("nope").unwrap_err().status, 400); + assert_eq!(parse_episode_id("pod1:abc").unwrap_err().status, 400); + assert!(parse_podcast_id(podcast().as_str()).is_ok()); + let error = decode::(serde_json::json!({ + "session_id": "s", "generation": 1, "event": "rewind", "position_ms": 0 + })) + .unwrap_err(); + assert_eq!(error.status, 400); + let request: ProgressRequest = decode(serde_json::json!({ + "session_id": "s", "generation": 3, "event": "handoff", "position_ms": 1200 + })) + .unwrap(); + assert_eq!(request.event, ProgressEvent::Handoff); + assert!(request.event.records_session()); + assert!(!ProgressEvent::Checkpoint.records_session()); + assert!(!ProgressEvent::Seek.records_session()); + // A malformed episode reference in a playback request is a 400. + let error = decode::(serde_json::json!({ + "episode": { "episode_id": "not an id" } + })) + .unwrap_err(); + assert_eq!(error.status, 400); + let empty: PlaybackRequest = decode(serde_json::json!({})).unwrap(); + assert!(!empty.start && empty.episode.is_none() && empty.position_ms.is_none()); + } + + #[test] + fn store_errors_map_to_http_statuses() { + let unknown: ApiError = + anyhow::Error::from(PodcastStoreError::UnknownPodcast(podcast())).into(); + assert_eq!(unknown.status, 404); + let not_subscribed: ApiError = + anyhow::Error::from(PodcastStoreError::NotSubscribed(podcast())).into(); + assert_eq!(not_subscribed.status, 409); + let invalid: ApiError = super::super::invalid("bad").into(); + assert_eq!(invalid.status, 400); + let refused: ApiError = + anyhow::Error::from(SubscribeUrlError::Fetch(super::super::fetch::FetchError { + kind: FetchErrorKind::Refused, + message: "address 127.0.0.1 is not public".into(), + retry_after: None, + })) + .into(); + assert_eq!(refused.status, 422); + assert!(refused.message.contains("not public")); + let timeout: ApiError = + anyhow::Error::from(SubscribeUrlError::Fetch(super::super::fetch::FetchError { + kind: FetchErrorKind::Timeout, + message: "timed out".into(), + retry_after: None, + })) + .into(); + assert_eq!(timeout.status, 502); + let other: ApiError = anyhow::anyhow!("connection reset").into(); + assert_eq!(other.status, 500); + assert_eq!(other.message, "internal error", "internals are not shown"); + } +} diff --git a/src/podcasts/apply.rs b/src/podcasts/apply.rs index ad42958..c698335 100644 --- a/src/podcasts/apply.rs +++ b/src/podcasts/apply.rs @@ -844,6 +844,7 @@ impl PodcastStore { /// A snapshot holding only this account's podcast sections, bounded /// with the default budget (`MAX_PODCAST_SNAPSHOT_BYTES`). + #[cfg(test)] pub(crate) async fn export_snapshot(&self, user_id: i64) -> Result { let mut snapshot = SyncSnapshot::default(); self.fill_snapshot( diff --git a/src/podcasts/http.rs b/src/podcasts/http.rs index bb73527..b6bdbff 100644 --- a/src/podcasts/http.rs +++ b/src/podcasts/http.rs @@ -1,7 +1,30 @@ -//! HTTP endpoints of the podcast runtime, mounted at `/api/podcasts`: +//! HTTP endpoints of the podcast runtime, mounted at `/api/podcasts`. The +//! request handling lives in [`super::api`]; this module authenticates (the +//! session or a bearer token of the signed-in account) and maps results to +//! JSON responses. //! +//! Podcast UI: +//! +//! * `GET /feeds[?all=1]` — subscribed podcasts (with `all`, also +//! unsubscribed ones and podcasts only known from synced progress), with +//! counters and the local fetch state. +//! * `POST /feeds` `{url}` — "add RSS": fetch, then subscribe (private). +//! * `GET /feeds/{podcast_id}` — one podcast and its season groups. +//! * `GET /feeds/{podcast_id}/episodes?season=&played=&q=&sort=&limit=&offset=` +//! * `POST /feeds/{podcast_id}/subscribe`, `POST /feeds/{podcast_id}/unsubscribe` +//! (unsubscribing keeps progress and history). +//! * `POST /episodes/{episode_id}/restart|played|unplayed` — explicit +//! progress actions. //! * `POST /{podcast_id}/refresh` — manual feed refresh of a subscribed //! podcast (ignores the failure backoff, at most every 30 s). +//! +//! Web player: +//! +//! * `POST /episodes/{episode_id}/playback` — audio source, resume position +//! and (when playing starts) a new listening session. +//! * `POST /episodes/{episode_id}/progress` — checkpoints, pause, seek, +//! stop, handoff and end of stream of that session (also sent with +//! `navigator.sendBeacon` when the page is hidden). //! * `GET /episodes/{episode_id}/source` — how the browser gets the audio: //! the publisher URL (default, the browser streams it directly) and the //! proxy URL when the administrator enabled the proxy. @@ -17,7 +40,7 @@ use cot::http::header::{ ACCEPT_RANGES, CACHE_CONTROL, CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, RANGE, RETRY_AFTER, }; use cot::json::Json; -use cot::request::extractors::Path; +use cot::request::extractors::{Path, UrlQuery}; use cot::response::IntoResponse; use cot::router::method::{get, post}; use cot::router::{Route, Router}; @@ -27,6 +50,7 @@ use futures_util::TryStreamExt; use music_dht::podcast::{EpisodeId, PodcastId}; use serde::{Deserialize, Serialize}; +use super::api::{self, ApiError, EpisodeAction, EpisodeListParams}; use super::fetch::FeedFetcher; use super::proxy::AudioProxy; use super::runner::{self, ManualRefreshError, TargetResult}; @@ -48,6 +72,55 @@ fn internal(error: impl std::fmt::Display) -> cot::Error { cot::Error::internal(error.to_string()) } +fn api_error(error: ApiError) -> cot::response::Response { + json_error( + StatusCode::from_u16(error.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR), + &error.message, + ) +} + +/// JSON response of an [`api`] result; personal data is never cached. +fn api_response(result: api::ApiResult) -> cot::Result { + match result { + Ok(value) => { + let mut response = Json(value).into_response()?; + response + .headers_mut() + .insert(CACHE_CONTROL, "private, no-store".parse().expect("static")); + Ok(response) + } + Err(error) => Ok(api_error(error)), + } +} + +/// The signed-in account, or the 401 response. +async fn signed_in( + auth_ctx: &auth::AuthContext, + session: &Session, + db: &Database, +) -> Result { + match auth::get_request_user(auth_ctx, session, db).await { + Some(user) => Ok(user.id), + None => Err(json_error(StatusCode::UNAUTHORIZED, "not authenticated")), + } +} + +/// Largest JSON body accepted (a playback request carries one episode reference). +const MAX_JSON_BODY: usize = 64 * 1024; + +/// Request body as JSON (`null` when empty); malformed JSON is a 400. +async fn json_body(request: cot::request::Request) -> Result { + let bytes = request + .into_body() + .into_bytes_limited(MAX_JSON_BODY) + .await + .map_err(|error| ApiError::new(400, format!("unreadable body: {error}")))?; + if bytes.iter().all(u8::is_ascii_whitespace) { + return Ok(serde_json::Value::Object(Default::default())); + } + serde_json::from_slice(&bytes).map_err(|error| ApiError::new(400, error.to_string())) +} + /// Shared state of the handlers. #[derive(Clone)] struct State { @@ -327,6 +400,163 @@ async fn audio_handler( builder.body(Body::streaming(body)).map_err(internal) } +#[derive(Debug, Deserialize)] +struct ListQuery { + #[serde(default)] + all: Option, +} + +async fn list_handler( + state: State, + auth_ctx: auth::AuthContext, + session: Session, + db: Database, + query: UrlQuery, +) -> cot::Result { + let user_id = match signed_in(&auth_ctx, &session, &db).await { + Ok(user_id) => user_id, + Err(response) => return Ok(response), + }; + let all = matches!(query.0.all.as_deref(), Some("1" | "true")); + let store = state.store().await?; + api_response(api::list_podcasts(&store, user_id, all).await) +} + +async fn subscribe_url_handler( + state: State, + auth_ctx: auth::AuthContext, + session: Session, + db: Database, + request: cot::request::Request, +) -> cot::Result { + let user_id = match signed_in(&auth_ctx, &session, &db).await { + Ok(user_id) => user_id, + Err(response) => return Ok(response), + }; + let body = match json_body(request).await { + Ok(body) => body, + Err(error) => return Ok(api_error(error)), + }; + let store = state.store().await?; + let fetcher = state.fetcher().await?; + api_response(api::subscribe_url(&store, fetcher, user_id, body, now_ms()).await) +} + +/// What a `/feeds/{podcast_id}...` route does. +#[derive(Debug, Clone, Copy)] +enum PodcastRoute { + Detail, + Subscribe, + Unsubscribe, +} + +async fn podcast_handler( + state: State, + route: PodcastRoute, + auth_ctx: auth::AuthContext, + session: Session, + db: Database, + path: Path, +) -> cot::Result { + let user_id = match signed_in(&auth_ctx, &session, &db).await { + Ok(user_id) => user_id, + Err(response) => return Ok(response), + }; + let podcast_id = match api::parse_podcast_id(&path.0.podcast_id) { + Ok(id) => id, + Err(error) => return Ok(api_error(error)), + }; + let store = state.store().await?; + match route { + PodcastRoute::Detail => { + api_response(api::podcast_detail(&store, user_id, &podcast_id).await) + } + PodcastRoute::Subscribe => { + api_response(api::resubscribe(&store, user_id, &podcast_id).await) + } + PodcastRoute::Unsubscribe => { + api_response(api::unsubscribe(&store, user_id, &podcast_id).await) + } + } +} + +async fn episodes_handler( + state: State, + auth_ctx: auth::AuthContext, + session: Session, + db: Database, + path: Path, + query: UrlQuery, +) -> cot::Result { + let user_id = match signed_in(&auth_ctx, &session, &db).await { + Ok(user_id) => user_id, + Err(response) => return Ok(response), + }; + let podcast_id = match api::parse_podcast_id(&path.0.podcast_id) { + Ok(id) => id, + Err(error) => return Ok(api_error(error)), + }; + let store = state.store().await?; + api_response(api::episodes(&store, user_id, &podcast_id, &query.0).await) +} + +/// What a `/episodes/{episode_id}/...` POST route does. +#[derive(Debug, Clone, Copy)] +enum EpisodeRoute { + Action(EpisodeAction), + Playback, + Progress, +} + +async fn episode_handler( + state: State, + route: EpisodeRoute, + auth_ctx: auth::AuthContext, + session: Session, + db: Database, + path: Path, + request: cot::request::Request, +) -> cot::Result { + let user_id = match signed_in(&auth_ctx, &session, &db).await { + Ok(user_id) => user_id, + Err(response) => return Ok(response), + }; + let episode_id = match api::parse_episode_id(&path.0.episode_id) { + Ok(id) => id, + Err(error) => return Ok(api_error(error)), + }; + let store = state.store().await?; + match route { + EpisodeRoute::Action(action) => { + api_response(api::episode_action(&store, user_id, &episode_id, action).await) + } + EpisodeRoute::Playback => { + let body = match json_body(request).await { + Ok(body) => body, + Err(error) => return Ok(api_error(error)), + }; + let (config, _) = AppConfig::load_with_db(&db).await; + api_response( + api::playback( + &store, + user_id, + &episode_id, + body, + config.podcast_audio_proxy_enabled, + ) + .await, + ) + } + EpisodeRoute::Progress => { + let body = match json_body(request).await { + Ok(body) => body, + Err(error) => return Ok(api_error(error)), + }; + api_response(api::progress(&store, user_id, &episode_id, body, now_ms()).await) + } + } +} + impl App for PodcastsApp { fn name(&self) -> &'static str { "podcasts" @@ -336,7 +566,114 @@ impl App for PodcastsApp { let refresh_state = self.state.clone(); let source_state = self.state.clone(); let audio_state = self.state.clone(); + let list_state = self.state.clone(); + let subscribe_state = self.state.clone(); + let episodes_state = self.state.clone(); + let podcast_route = |route: PodcastRoute| { + let state = self.state.clone(); + move |auth_ctx: auth::AuthContext, + session: Session, + db: Database, + path: Path| { + podcast_handler(state.clone(), route, auth_ctx, session, db, path) + } + }; + let episode_route = |route: EpisodeRoute| { + let state = self.state.clone(); + move |auth_ctx: auth::AuthContext, + session: Session, + db: Database, + path: Path, + request: cot::request::Request| { + episode_handler(state.clone(), route, auth_ctx, session, db, path, request) + } + }; + // Routes are matched in order: literal prefixes come before + // `/{podcast_id}/refresh`. Router::with_urls([ + Route::with_handler_and_name( + "/feeds", + get( + move |auth_ctx: auth::AuthContext, + session: Session, + db: Database, + query: UrlQuery| { + list_handler(list_state.clone(), auth_ctx, session, db, query) + }, + ) + .post( + move |auth_ctx: auth::AuthContext, + session: Session, + db: Database, + request: cot::request::Request| { + subscribe_url_handler( + subscribe_state.clone(), + auth_ctx, + session, + db, + request, + ) + }, + ), + "podcasts_feeds", + ), + Route::with_handler_and_name( + "/feeds/{podcast_id}/episodes", + get( + move |auth_ctx: auth::AuthContext, + session: Session, + db: Database, + path: Path, + query: UrlQuery| { + episodes_handler(episodes_state.clone(), auth_ctx, session, db, path, query) + }, + ), + "podcasts_episodes", + ), + Route::with_handler_and_name( + "/feeds/{podcast_id}/subscribe", + post(podcast_route(PodcastRoute::Subscribe)), + "podcasts_subscribe", + ), + Route::with_handler_and_name( + "/feeds/{podcast_id}/unsubscribe", + post(podcast_route(PodcastRoute::Unsubscribe)), + "podcasts_unsubscribe", + ), + Route::with_handler_and_name( + "/feeds/{podcast_id}", + get(podcast_route(PodcastRoute::Detail)), + "podcasts_detail", + ), + Route::with_handler_and_name( + "/episodes/{episode_id}/playback", + post(episode_route(EpisodeRoute::Playback)), + "podcasts_episode_playback", + ), + Route::with_handler_and_name( + "/episodes/{episode_id}/progress", + post(episode_route(EpisodeRoute::Progress)), + "podcasts_episode_progress", + ), + Route::with_handler_and_name( + "/episodes/{episode_id}/restart", + post(episode_route(EpisodeRoute::Action(EpisodeAction::Restart))), + "podcasts_episode_restart", + ), + Route::with_handler_and_name( + "/episodes/{episode_id}/played", + post(episode_route(EpisodeRoute::Action( + EpisodeAction::MarkPlayed, + ))), + "podcasts_episode_played", + ), + Route::with_handler_and_name( + "/episodes/{episode_id}/unplayed", + post(episode_route(EpisodeRoute::Action( + EpisodeAction::MarkUnplayed, + ))), + "podcasts_episode_unplayed", + ), Route::with_handler_and_name( "/episodes/{episode_id}/source", get( diff --git a/src/podcasts/mod.rs b/src/podcasts/mod.rs index 55e1420..6bf2eac 100644 --- a/src/podcasts/mod.rs +++ b/src/podcasts/mod.rs @@ -53,14 +53,12 @@ //! * `proxy` and [`http`] — the optional audio proxy and the //! `/api/podcasts` endpoints. Audio is streamed from the publisher, never //! stored. - -// The podcast UI and its HTTP API, which call the local actions and most -// queries, land in the next commits of this series; until then only sync, -// the refresh job, the proxy endpoints and the tests use the store. Remove -// this allowance with them. -#![allow(dead_code, unused_imports)] +//! * [`api`] — what the endpoints of the podcast UI and the web player do +//! (subscribe by URL, lists, explicit progress actions, playback sessions +//! and progress reports), independent of HTTP. mod actions; +pub(crate) mod api; mod apply; mod fetch; pub(crate) mod http; @@ -94,12 +92,12 @@ use music_dht::podcast::{ use sqlx::Row; use sqlx::postgres::PgRow; -pub(crate) use actions::{CheckpointOutcome, ProgressSession, ProgressWrite, SubscribeOutcome}; -pub(crate) use apply::{ApplyOutcome, SnapshotApplyStats}; +pub(crate) use actions::{CheckpointOutcome, ProgressSession}; +pub(crate) use apply::ApplyOutcome; pub(crate) use fetch::FeedFetcher; pub(crate) use queries::{ - EpisodeListItem, EpisodeQuery, EpisodeSort, PlayedFilter, PodcastListFilter, - PodcastListenEntry, PodcastSummary, SeasonFilter, SeasonSummary, + EpisodeListItem, EpisodeQuery, EpisodeSort, PlayedFilter, PodcastListFilter, PodcastSummary, + SeasonFilter, SeasonSummary, }; pub(crate) use refresh::{FeedRefresh, FetchState, RefreshOutcome}; @@ -151,6 +149,11 @@ impl PodcastStore { Self { pool } } + /// The pool the store runs on. + pub(crate) fn pool(&self) -> &sqlx::PgPool { + &self.pool + } + /// Starts a write transaction holding the account's podcast lock. async fn begin_write( &self, diff --git a/src/podcasts/postgres_tests.rs b/src/podcasts/postgres_tests.rs index 236131b..abceaef 100644 --- a/src/podcasts/postgres_tests.rs +++ b/src/podcasts/postgres_tests.rs @@ -697,10 +697,13 @@ async fn postgres_podcast_local_actions_keep_progress_through_unsubscribe() { ), (0, ProgressStatus::InProgress, 12) ); - let finished = store - .complete_session(user_id, &restarted.session, 1_790_000) + let CheckpointOutcome::Written(finished) = store + .complete_if_current(user_id, &restarted.session, 1_790_000) .await - .unwrap(); + .unwrap() + else { + panic!("the running session completes the episode"); + }; assert_eq!( (finished.record.status, finished.record.generation), (ProgressStatus::Completed, 13) @@ -1725,3 +1728,450 @@ async fn postgres_podcast_refresh_job_schedules_backs_off_and_moves_feeds() { cleanup(&pool, [a, b, c]).await; } + +// --------------------------------------------------------------------------- +// HTTP API of the podcast UI and the web player (`api`, behind `http`) +// --------------------------------------------------------------------------- + +async fn logged_kinds(pool: &sqlx::PgPool, user_id: i64) -> Vec { + sqlx::query_scalar("SELECT kind FROM furumusic__fed_sync_ops WHERE user_id = $1 ORDER BY seq") + .bind(user_id) + .fetch_all(pool) + .await + .unwrap() +} + +#[tokio::test] +#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"] +async fn postgres_podcast_api_subscribes_plays_and_keeps_progress() { + use super::api::{self, EpisodeAction, EpisodeListParams}; + use super::test_server::{Route, TestServer}; + use serde_json::json; + + let pool = test_pool().await; + let store = PodcastStore::new(pool.clone()); + let (user, other) = (random_user(), random_user()); + cleanup(&pool, [user, other]).await; + let server = TestServer::start(); + let items = r#" + Pilots1e11 + 1600 + Mon, 01 Jan 2024 10:00:00 GMTFirst interview + + Bonusbonus120 + Tue, 02 Jan 2024 10:00:00 GMT + "#; + server.set("/show.xml", Route::new(rss(items), "application/rss+xml")); + server.set("/page.html", Route::new("", "text/html")); + server.set("/gone.xml", Route::new("gone", "text/plain").status(404)); + let fetcher = local_fetcher(); + let now = super::now_ms(); + + // "Add RSS": invalid input and unusable feeds subscribe nothing. + for (body, status) in [ + (json!({}), 400), + (json!({"url": "ftp://example.org/feed"}), 400), + (json!({"url": server.url("/page.html")}), 422), + (json!({"url": server.url("/gone.xml")}), 422), + ] { + let error = api::subscribe_url(&store, &fetcher, user, body.clone(), now) + .await + .unwrap_err(); + assert_eq!(error.status, status, "{body}: {}", error.message); + } + // The production policy never reaches the local server. + let refused = api::subscribe_url( + &store, + &FeedFetcher::new().unwrap(), + user, + json!({"url": server.url("/show.xml")}), + now, + ) + .await + .unwrap_err(); + assert_eq!(refused.status, 422, "{}", refused.message); + assert!(logged_kinds(&pool, user).await.is_empty()); + assert_eq!(server.requests("/show.xml").len(), 0); + + let added = api::subscribe_url( + &store, + &fetcher, + user, + json!({"url": format!(" {} ", server.url("/show.xml"))}), + now, + ) + .await + .unwrap(); + assert!(!added.already_subscribed); + assert_eq!(added.new_episodes, 2); + let podcast = added.podcast; + assert!(podcast.subscribed); + assert!(!podcast.shared, "a new subscription is private"); + assert_eq!((podcast.episode_count, podcast.unplayed_count), (2, 2)); + assert_eq!(podcast.title, "Refresh Show"); + assert!(podcast.fetch.last_success_at_ms.is_some()); + assert!(podcast.fetch.next_check_at_ms.unwrap() > now); + let podcast_id = PodcastId::parse(&podcast.podcast_id).unwrap(); + assert_eq!( + logged_kinds(&pool, user).await, + ["podcast_subscription_set"] + ); + // Adding it again writes nothing. + let again = api::subscribe_url( + &store, + &fetcher, + user, + json!({"url": server.url("/show.xml")}), + now, + ) + .await + .unwrap(); + assert!(again.already_subscribed); + assert_eq!(again.podcast.podcast_id, podcast.podcast_id); + assert_eq!(logged_kinds(&pool, user).await.len(), 1); + + // Lists and details are per account. + let list = api::list_podcasts(&store, user, false).await.unwrap(); + assert_eq!(list.podcasts.len(), 1); + assert!( + api::list_podcasts(&store, other, true) + .await + .unwrap() + .podcasts + .is_empty() + ); + assert_eq!( + api::podcast_detail(&store, other, &podcast_id) + .await + .unwrap_err() + .status, + 404 + ); + let detail = api::podcast_detail(&store, user, &podcast_id) + .await + .unwrap(); + let seasons: Vec> = detail.seasons.iter().map(|season| season.season).collect(); + assert_eq!(seasons, [Some(1), None]); + assert!(detail.history.is_empty()); + + // Episodes: newest first, filters, search, paging, queue items. + let page = api::episodes(&store, user, &podcast_id, &EpisodeListParams::default()) + .await + .unwrap(); + let titles: Vec<&str> = page.episodes.iter().map(|e| e.title.as_str()).collect(); + assert_eq!(titles, ["Bonus", "Pilot"]); + assert!(!page.has_more); + let pilot = &page.episodes[1]; + assert_eq!((pilot.season, pilot.episode_number), (Some(1), Some(1))); + assert_eq!(pilot.status, "not_started"); + assert_eq!(pilot.track["source"]["kind"], "podcast_episode"); + assert_eq!( + pilot.track["source"]["episode_id"], + pilot.episode_id.as_str() + ); + assert_eq!(pilot.track["unavailable"], false); + assert_eq!(pilot.track["title"], "Pilot"); + let first = api::episodes( + &store, + user, + &podcast_id, + &EpisodeListParams { + limit: Some(1), + ..Default::default() + }, + ) + .await + .unwrap(); + assert_eq!((first.episodes.len(), first.has_more), (1, true)); + let season = api::episodes( + &store, + user, + &podcast_id, + &EpisodeListParams { + season: Some("none".into()), + ..Default::default() + }, + ) + .await + .unwrap(); + assert_eq!(season.episodes.len(), 1); + assert_eq!(season.episodes[0].title, "Bonus"); + let found = api::episodes( + &store, + user, + &podcast_id, + &EpisodeListParams { + q: Some("INTERVIEW".into()), + ..Default::default() + }, + ) + .await + .unwrap(); + assert_eq!(found.episodes.len(), 1, "description search, any case"); + let episode_id = EpisodeId::parse(&pilot.episode_id).unwrap(); + + // Playback: the source and the resume position, without writing. + let prepared = api::playback(&store, user, &episode_id, json!({}), false) + .await + .unwrap(); + assert_eq!(prepared.direct_url, server.url("/audio/s1e1.mp3")); + assert_eq!(prepared.proxy_url, None); + assert_eq!((prepared.position_ms, prepared.generation), (0, None)); + assert_eq!(prepared.duration_ms, Some(600_000)); + let proxied = api::playback(&store, user, &episode_id, json!({}), true) + .await + .unwrap(); + assert_eq!( + proxied.proxy_url.as_deref(), + Some(format!("/api/podcasts/episodes/{episode_id}/audio").as_str()) + ); + assert_eq!(logged_kinds(&pool, user).await.len(), 1); + assert_eq!( + api::playback(&store, user, &episode_id, json!({"start": true}), false) + .await + .unwrap_err() + .status, + 400, + "starting needs a session id" + ); + + // Play starts a session (explicit generation 1). + let started = api::playback( + &store, + user, + &episode_id, + json!({"start": true, "session_id": "web-s1"}), + false, + ) + .await + .unwrap(); + assert_eq!(started.session_id.as_deref(), Some("web-s1")); + assert_eq!((started.generation, started.position_ms), (Some(1), 0)); + let report = |body: serde_json::Value| { + let store = store.clone(); + let episode_id = episode_id.clone(); + async move { api::progress(&store, user, &episode_id, body, super::now_ms()).await } + }; + let t0 = super::now_ms() - 120_000; + let saved = report(json!({ + "session_id": "web-s1", "generation": 1, "event": "checkpoint", "position_ms": 60_000, + "duration_ms": 600_000, + })) + .await + .unwrap(); + assert_eq!( + (saved.outcome, saved.generation, saved.status), + ("written", 1, "in_progress") + ); + assert!( + !saved.session_recorded, + "checkpoints do not record the session" + ); + // Pause records the session (at least 30 s listened). + let paused = report(json!({ + "session_id": "web-s1", "generation": 1, "event": "pause", "position_ms": 90_000, + "listened_ms": 90_000, "started_at_ms": t0, "start_position_ms": 0, + })) + .await + .unwrap(); + assert!(paused.session_recorded); + // Seeking back is an explicit action; a late checkpoint at the old + // generation is superseded. + let sought = report(json!({ + "session_id": "web-s1", "generation": 1, "event": "seek", "position_ms": 30_000, + })) + .await + .unwrap(); + assert_eq!( + (sought.outcome, sought.generation, sought.position_ms), + ("written", 2, 30_000) + ); + let late = report(json!({ + "session_id": "web-s1", "generation": 1, "event": "checkpoint", "position_ms": 95_000, + })) + .await + .unwrap(); + assert_eq!( + (late.outcome, late.generation, late.position_ms), + ("superseded", 2, 30_000) + ); + // Ordinary Play resumes from the register. + let resumed = api::playback(&store, user, &episode_id, json!({}), false) + .await + .unwrap(); + assert_eq!( + (resumed.position_ms, resumed.status), + (30_000, "in_progress") + ); + // Reaching the completion tail completes once, with the session. + let near_end = report(json!({ + "session_id": "web-s1", "generation": 2, "event": "checkpoint", "position_ms": 590_000, + })) + .await + .unwrap(); + assert_eq!( + (near_end.outcome, near_end.status, near_end.completed), + ("written", "completed", true) + ); + assert_eq!(near_end.generation, 3); + let ended = report(json!({ + "session_id": "web-s1", "generation": 3, "event": "ended", "position_ms": 600_000, + "listened_ms": 400_000, "started_at_ms": t0, "start_position_ms": 0, + })) + .await + .unwrap(); + assert_eq!(ended.outcome, "already_completed"); + assert!( + ended.session_recorded, + "the session grew and is now completed" + ); + let item = store.episode(user, &episode_id).await.unwrap().unwrap(); + assert_eq!(item.completed_count, 1); + // A completed episode plays from the start. + let replay = api::playback(&store, user, &episode_id, json!({}), false) + .await + .unwrap(); + assert_eq!((replay.position_ms, replay.status), (0, "completed")); + let history = api::podcast_detail(&store, user, &podcast_id) + .await + .unwrap() + .history; + assert_eq!(history.len(), 1); + assert!(history[0].completed); + assert_eq!(history[0].episode_title.as_deref(), Some("Pilot")); + + // Explicit list actions. + let restarted = api::episode_action(&store, user, &episode_id, EpisodeAction::Restart) + .await + .unwrap(); + assert_eq!( + (restarted.status, restarted.position_ms), + ("in_progress", 0) + ); + let played = api::episode_action(&store, user, &episode_id, EpisodeAction::MarkPlayed) + .await + .unwrap(); + assert_eq!(played.status, "completed"); + assert_eq!( + played.completed_count, 1, + "marking played invents no session" + ); + let unplayed = api::episode_action(&store, user, &episode_id, EpisodeAction::MarkUnplayed) + .await + .unwrap(); + assert_eq!((unplayed.status, unplayed.position_ms), ("not_started", 0)); + assert_eq!( + api::episode_action(&store, other, &episode_id, EpisodeAction::MarkPlayed) + .await + .unwrap_err() + .status, + 404 + ); + + // A handed-off queue item the catalog does not know yet: the reference + // is recorded and the session starts above the carried generation. + let mut handed = test_episode(); + handed.progress_generation = Some(9); + let handed_id = handed.episode_id.clone(); + assert_eq!( + api::playback(&store, user, &handed_id, json!({}), false) + .await + .unwrap_err() + .status, + 404 + ); + let mut wrong = handed.clone(); + wrong.episode_id = episode_id.clone(); + assert_eq!( + api::playback(&store, user, &handed_id, json!({"episode": wrong}), false) + .await + .unwrap_err() + .status, + 400 + ); + let taken_over = api::playback( + &store, + user, + &handed_id, + json!({ + "episode": handed, "start": true, "session_id": "web-s2", + "position_ms": 1_200_000, "carried_generation": 9, + }), + false, + ) + .await + .unwrap(); + assert_eq!( + (taken_over.generation, taken_over.position_ms), + (Some(10), 1_200_000) + ); + assert_eq!(taken_over.direct_url, "https://cdn.example/ep42.mp3"); + let catalog = store.episode(user, &handed_id).await.unwrap().unwrap(); + assert!(!catalog.in_feed); + // A handoff away writes a final checkpoint at the session generation. + let away = api::progress( + &store, + user, + &handed_id, + json!({ + "session_id": "web-s2", "generation": 10, "event": "handoff", + "position_ms": 1_260_000, "listened_ms": 10_000, + "started_at_ms": super::now_ms() - 60_000, "start_position_ms": 1_200_000, + }), + super::now_ms(), + ) + .await + .unwrap(); + assert_eq!( + (away.outcome, away.generation, away.position_ms), + ("written", 10, 1_260_000) + ); + assert!( + !away.session_recorded, + "10 s is below the history threshold" + ); + + // Unsubscribing keeps progress and history; subscribing again continues. + let left = api::unsubscribe(&store, user, &podcast_id).await.unwrap(); + assert!(!left.subscribed); + assert!( + api::list_podcasts(&store, user, false) + .await + .unwrap() + .podcasts + .is_empty() + ); + let all = api::list_podcasts(&store, user, true).await.unwrap(); + assert!( + all.podcasts + .iter() + .any(|p| p.podcast_id == podcast.podcast_id) + ); + let kept = store.episode(user, &episode_id).await.unwrap().unwrap(); + assert_eq!(kept.completed_count, 1); + let back = api::resubscribe(&store, user, &podcast_id).await.unwrap(); + assert!(back.subscribed); + assert_eq!( + api::unsubscribe(&store, other, &podcast_id) + .await + .unwrap_err() + .status, + 404 + ); + + // Every write went through the sync log, with its own operation kinds. + let kinds = logged_kinds(&pool, user).await; + let count = |kind: &str| kinds.iter().filter(|k| k.as_str() == kind).count(); + assert_eq!(count("podcast_subscription_set"), 3); + assert_eq!( + count("podcast_share_set"), + 1, + "unsubscribing revokes sharing" + ); + assert_eq!(count("podcast_listen_recorded"), 2); + // start, checkpoint, pause, seek, completion, restart, played, + // unplayed, handed-off start, handoff checkpoint + assert_eq!(count("podcast_progress_set"), 10); + + cleanup(&pool, [user, other]).await; +} diff --git a/src/podcasts/queries.rs b/src/podcasts/queries.rs index 08ef21a..df07405 100644 --- a/src/podcasts/queries.rs +++ b/src/podcasts/queries.rs @@ -2,8 +2,8 @@ use anyhow::Result; use music_dht::podcast::{ - EpisodeId, EpisodeMeta, EpisodeType, FeedUrl, ListenSession, PodcastEpisodeRef, PodcastId, - PodcastMeta, ProgressRecord, ProgressStatus, ShareRecord, SubscriptionRecord, effective_share, + EpisodeId, EpisodeMeta, EpisodeType, ListenSession, PodcastEpisodeRef, PodcastId, PodcastMeta, + ProgressRecord, ProgressStatus, SubscriptionRecord, effective_share, }; use sqlx::postgres::PgRow; use sqlx::{PgConnection, Postgres, QueryBuilder, Row}; @@ -441,21 +441,23 @@ impl PodcastStore { } /// The share register, if any. + #[cfg(test)] pub(crate) async fn share( &self, user_id: i64, podcast_id: &PodcastId, - ) -> Result> { + ) -> Result> { let mut conn = self.pool.acquire().await?; apply::load_share(&mut conn, user_id, podcast_id).await } /// The bounded, synced feed alias set of a podcast. + #[cfg(test)] pub(crate) async fn feed_aliases( &self, user_id: i64, podcast_id: &PodcastId, - ) -> Result> { + ) -> Result> { let mut conn = self.pool.acquire().await?; apply::load_aliases(&mut conn, user_id, podcast_id).await } @@ -597,6 +599,7 @@ impl PodcastStore { /// Position ordinary Play resumes from /// (`ProgressRecord::resume_position_ms`, 0 without progress). + #[cfg(test)] pub(crate) async fn resume_position_ms( &self, user_id: i64, @@ -646,6 +649,7 @@ impl PodcastStore { } /// Number of distinct completed sessions of an episode. + #[cfg(test)] pub(crate) async fn completed_listen_count( &self, user_id: i64, diff --git a/src/podcasts/refresh.rs b/src/podcasts/refresh.rs index 6cf6899..ef9afd4 100644 --- a/src/podcasts/refresh.rs +++ b/src/podcasts/refresh.rs @@ -420,6 +420,9 @@ impl PodcastStore { } /// Records the locally cached cover image of a podcast. + // Covers are not cached yet: the browser loads `image_url` from the + // publisher. The column and this setter stay for a later cache. + #[cfg(test)] pub(crate) async fn set_cover_cache( &self, user_id: i64, diff --git a/src/podcasts/runner.rs b/src/podcasts/runner.rs index 6f2f837..4e7767d 100644 --- a/src/podcasts/runner.rs +++ b/src/podcasts/runner.rs @@ -386,3 +386,77 @@ pub(crate) async fn refresh_now( let mut results = refresh_group(store, fetcher, &feed_url, vec![target], now_ms).await?; Ok(results.pop().map(|(_, result)| result).expect("one target")) } + +/// Result of [`subscribe_url`]. +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct UrlSubscription { + /// Podcast the URL resolved to (an existing one for a known URL or alias). + pub(crate) podcast_id: PodcastId, + /// The account was already subscribed; no subscription was written. + pub(crate) already_subscribed: bool, + /// Ingestion of the document fetched for the subscription. + pub(crate) refresh: RefreshOutcome, +} + +/// Why [`subscribe_url`] did not subscribe. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum SubscribeUrlError { + /// The feed could not be fetched or parsed; nothing was stored. + Fetch(FetchError), +} + +impl fmt::Display for SubscribeUrlError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Fetch(error) => write!(f, "the feed could not be loaded: {}", error.message), + } + } +} + +impl std::error::Error for SubscribeUrlError {} + +/// Subscribes an account to the feed at `url` (the "add RSS" action). +/// +/// The feed is fetched first: an address that is not public, a document +/// that is not a feed or a failed request subscribes nothing +/// ([`SubscribeUrlError`] inside the error). Then the subscription is +/// written with the channel metadata of the document +/// ([`PodcastStore::subscribe`]: a known URL or alias reuses the existing +/// podcast, its progress and history; an active subscription writes +/// nothing) and the document is ingested like a scheduled refresh, which +/// also stores the validators and the next check. +pub(crate) async fn subscribe_url( + store: &PodcastStore, + fetcher: &FeedFetcher, + user_id: i64, + url: &FeedUrl, + now_ms: i64, +) -> Result { + let document = match fetcher.fetch(url, &Validators::default()).await { + Ok(FeedFetch::Fetched(document)) => document, + Ok(FeedFetch::NotModified) => { + // Unconditional requests are never answered with 304 by a + // conforming server; refuse instead of subscribing blindly. + return Err(SubscribeUrlError::Fetch(FetchError { + kind: super::fetch::FetchErrorKind::Status(304), + message: "unexpected 304 Not Modified".to_string(), + retry_after: None, + }) + .into()); + } + Err(error) => return Err(SubscribeUrlError::Fetch(error).into()), + }; + let subscription = store + .subscribe(user_id, &document.feed.to_podcast_meta(url.clone())) + .await?; + let target = RefreshTarget { + user_id, + podcast_id: subscription.podcast_id.clone(), + }; + let refresh = ingest(store, &target, &document, now_ms).await?; + Ok(UrlSubscription { + podcast_id: subscription.podcast_id, + already_subscribed: subscription.already_subscribed, + refresh, + }) +} diff --git a/src/podcasts/test_server.rs b/src/podcasts/test_server.rs index 4f9c199..e4344da 100644 --- a/src/podcasts/test_server.rs +++ b/src/podcasts/test_server.rs @@ -139,10 +139,6 @@ impl TestServer { TestServer { port, state } } - pub(crate) fn port(&self) -> u16 { - self.port - } - /// `http://127.0.0.1:`. pub(crate) fn url(&self, path: &str) -> String { format!("http://127.0.0.1:{}{path}", self.port)