From 319f0bcba77b2e5192c0f7f21e0b23316221fc9e Mon Sep 17 00:00:00 2001 From: AB Date: Sun, 22 Nov 2026 17:11:37 +0200 Subject: [PATCH] Fixed podcast sync --- docs/administration.md | 16 +- docs/development.md | 6 + src/admin/v2.rs | 120 ++++++- src/federation/capabilities.rs | 2 +- src/federation/devices.rs | 397 ++++++++++------------- src/i18n/phrases.rs | 2 +- src/music/mod.rs | 23 ++ src/player/dto.rs | 1 + src/player/mod.rs | 29 +- src/podcasts/mod.rs | 31 +- src/podcasts/sync.rs | 448 ++++++++++++++++++++++++++ src/podcasts/sync_schema.sql | 6 + src/podcasts/tests.rs | 87 +++++ src/podcasts/web.rs | 49 ++- templates/player/podcast_scripts.html | 42 ++- templates/player/podcasts.html | 15 +- templates/player/scripts.html | 2 + templates/player/shell.html | 16 +- tests/podcast_sync_fixture.sql | 15 + tests/podcasts.test.cjs | 22 +- tests/unicode_search_fixture.sql | 14 + 21 files changed, 1067 insertions(+), 276 deletions(-) create mode 100644 src/podcasts/sync.rs create mode 100644 src/podcasts/sync_schema.sql create mode 100644 tests/podcast_sync_fixture.sql create mode 100644 tests/unicode_search_fixture.sql diff --git a/docs/administration.md b/docs/administration.md index bcad054..1b009a1 100644 --- a/docs/administration.md +++ b/docs/administration.md @@ -31,8 +31,20 @@ its publications, clear saved audio, or delete the podcast for every user. Audio retention is disabled by default. When enabled, its day count is measured from the download time; the periodic job removes old audio while keeping episode descriptions and listening progress. Removed audio is downloaded again only on an explicit save -or playback request. Podcast personal state is not replicated to trusted Frid devices -yet; device synchronization continues to advertise protocol v3. +or playback request. + +Connected devices use Frid device-sync v4 to exchange podcast subscriptions, share +decisions, listening progress and listening-session records. Adding or removing a +subscription on one connected device updates the others; unsubscribe and share-off +registers survive offline devices reconnecting. Older v3 clients keep music sync and +receive no unsupported podcast operations or snapshot sections. + +The main player search also finds accessible podcasts by words in their titles, +descriptions and authors. All query words must match; a private subscription is visible +only to its owner. Podcast search and the admin artist/release/track search use the +explicit ICU `und` collation `furumusic_unicode`, so case matching does not depend on +the database's initial locale (including databases created with the `C` locale). +PostgreSQL must include ICU support to apply migration `m_0055_podcast_sync_unicode_search`. ## Access and public sharing diff --git a/docs/development.md b/docs/development.md index dce48b2..b073bc3 100644 --- a/docs/development.md +++ b/docs/development.md @@ -62,10 +62,16 @@ retention and administrator actions: ```bash node --test tests/podcasts.test.cjs FURU_PODCAST_TEST_DATABASE_URL=postgresql://... cargo test podcasts -- --include-ignored +FURU_PODCAST_TEST_DATABASE_URL=postgresql://... cargo test admin_unicode_search -- --include-ignored ``` Use a disposable database. The PostgreSQL test creates and drops a unique schema; its audio files use a unique temporary directory. Normal podcast tests need no network. +The podcast fixture includes real Frid v4 subscription operations, snapshot exchange, +offline unsubscribe replay, legacy-peer filtering, and private Unicode search. The +admin fixture exercises list counts, all three catalogue lists and the track picker +with Cyrillic and Greek searches. Use a PostgreSQL build with ICU support; running +these fixtures on a database created with locale `C` covers the locale regression. The access regression suite uses a disposable migrated database and a local OIDC provider with signed tokens and PKCE: diff --git a/src/admin/v2.rs b/src/admin/v2.rs index 72a4ebc..ea8195d 100644 --- a/src/admin/v2.rs +++ b/src/admin/v2.rs @@ -3978,11 +3978,11 @@ async fn search_tracks( LEFT JOIN furumusic__release r ON r.id = t.release_id \ LEFT JOIN furumusic__track_artist ta ON ta.track_id = t.id AND ta.role = 'main' \ LEFT JOIN furumusic__artist a ON a.id = ta.artist_id \ - WHERE t.title ILIKE $1 OR COALESCE(r.title::text, '') ILIKE $1 OR COALESCE(a.name::text, '') ILIKE $1 \ + WHERE t.title COLLATE furumusic_unicode ILIKE $1 OR COALESCE(r.title::text, '') COLLATE furumusic_unicode ILIKE $1 OR COALESCE(a.name::text, '') COLLATE furumusic_unicode ILIKE $1 \ GROUP BY t.id, r.id, r.title \ ORDER BY CASE \ - WHEN LOWER(t.title::text) = LOWER($2) THEN 0 \ - WHEN t.title ILIKE $3 THEN 1 \ + WHEN LOWER(t.title::text COLLATE furumusic_unicode) = LOWER($2::text COLLATE furumusic_unicode) THEN 0 \ + WHEN t.title COLLATE furumusic_unicode ILIKE $3 THEN 1 \ ELSE 2 \ END, \ t.title_sort ASC, t.id ASC \ @@ -4306,9 +4306,9 @@ fn push_library_search_filter( }; match kind { "releases" => { - qb.push(" AND (r.title ILIKE "); + qb.push(" AND (r.title COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern.clone()); - qb.push(" OR a.name ILIKE "); + qb.push(" OR a.name COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern); qb.push(")"); } @@ -4322,16 +4322,16 @@ fn push_library_search_filter( qb.push(")"); } "tracks" => { - qb.push(" AND (t.title ILIKE "); + qb.push(" AND (t.title COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern.clone()); - qb.push(" OR r.title ILIKE "); + qb.push(" OR r.title COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern.clone()); - qb.push(" OR a.name ILIKE "); + qb.push(" OR a.name COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern); qb.push(")"); } _ => { - qb.push(" AND a.name ILIKE "); + qb.push(" AND a.name COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern); } } @@ -4356,7 +4356,7 @@ async fn load_artist_items( WHERE 1=1", ); if let Some(pattern) = search_pattern { - qb.push(" AND a.name ILIKE "); + qb.push(" AND a.name COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern); } qb.push(" GROUP BY a.id ORDER BY secondary_count DESC, a.name ASC LIMIT "); @@ -4390,9 +4390,9 @@ async fn load_release_items( WHERE 1=1", ); if let Some(pattern) = search_pattern { - qb.push(" AND (r.title ILIKE "); + qb.push(" AND (r.title COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern.clone()); - qb.push(" OR a.name ILIKE "); + qb.push(" OR a.name COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern); qb.push(")"); } @@ -4428,11 +4428,11 @@ async fn load_track_items( WHERE 1=1", ); if let Some(pattern) = search_pattern { - qb.push(" AND (t.title ILIKE "); + qb.push(" AND (t.title COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern.clone()); - qb.push(" OR r.title ILIKE "); + qb.push(" OR r.title COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern.clone()); - qb.push(" OR a.name ILIKE "); + qb.push(" OR a.name COLLATE furumusic_unicode ILIKE "); qb.push_bind(pattern); qb.push(")"); } @@ -4612,6 +4612,96 @@ fn clean_search(search: Option<&str>) -> Option { .map(|s| s.chars().take(120).collect()) } +#[cfg(test)] +mod unicode_search_tests { + use super::*; + #[tokio::test] + #[ignore = "requires FURU_PODCAST_TEST_DATABASE_URL pointing to a disposable PostgreSQL database"] + async fn admin_unicode_search_matches_counts_lists_and_track_picker() { + let url = std::env::var("FURU_PODCAST_TEST_DATABASE_URL").unwrap(); + let admin = PgPool::connect(&url).await.unwrap(); + let schema = format!("unicode_search_{}", uuid::Uuid::new_v4().simple()); + sqlx::query(&format!("CREATE SCHEMA {schema}")) + .execute(&admin) + .await + .unwrap(); + let search_path = format!("SET search_path TO {schema}"); + let pool = sqlx::postgres::PgPoolOptions::new() + .max_connections(2) + .after_connect(move |conn, _| { + let statement = search_path.clone(); + Box::pin(async move { + sqlx::query(&statement).execute(conn).await?; + Ok(()) + }) + }) + .connect(&url) + .await + .unwrap(); + sqlx::query( + "CREATE COLLATION furumusic_unicode(provider=icu,locale='und',deterministic=true)", + ) + .execute(&pool) + .await + .unwrap(); + for statement in include_str!("../../tests/unicode_search_fixture.sql") + .split(';') + .filter(|s| !s.trim().is_empty()) + { + sqlx::query(statement).execute(&pool).await.unwrap(); + } + for query in ["АЛИСА", "алиса", "АлИсА"] { + let pattern = Some(format!("%{query}%")); + for kind in ["artists", "releases", "tracks"] { + assert_eq!( + count_library(&pool, kind, pattern.clone()).await.unwrap(), + 1, + "{kind}: {query}" + ); + } + assert_eq!( + load_artist_items(&pool, pattern.clone(), 10, 0) + .await + .unwrap() + .len(), + 1 + ); + assert_eq!( + load_release_items(&pool, pattern.clone(), 10, 0) + .await + .unwrap() + .len(), + 1 + ); + assert_eq!( + load_track_items(&pool, pattern, 10, 0).await.unwrap().len(), + 1 + ); + } + for query in ["солнце", "СОЛНЦЕ", "ελληνικά", "ΕΛΛΗΝΙΚΆ", "трек", "ТРЕК"] + { + assert_eq!( + load_track_items(&pool, Some(format!("%{query}%")), 10, 0) + .await + .unwrap() + .len(), + 1, + "{query}" + ); + assert_eq!( + search_tracks(&pool, query, 10).await.unwrap().len(), + 1, + "picker: {query}" + ); + } + pool.close().await; + sqlx::query(&format!("DROP SCHEMA {schema} CASCADE")) + .execute(&admin) + .await + .unwrap(); + } +} + fn normalize_name(value: &str) -> String { value.trim().to_lowercase() } diff --git a/src/federation/capabilities.rs b/src/federation/capabilities.rs index df35a2a..b2e0738 100644 --- a/src/federation/capabilities.rs +++ b/src/federation/capabilities.rs @@ -15,7 +15,7 @@ fn local_manifest() -> CapabilityManifest { CapabilityManifest::frid("furumusic", env!("CARGO_PKG_VERSION")) // The web server does not expose federation Jam yet. .without_protocol(JAM_ID) - // Podcast discovery is supported, but personal device sync remains v3. + // Keep the advertised version aligned with the web device-sync adapter. .with_protocol(DEVICE_SYNC_ID, super::devices::PROTOCOL_VERSION) .with_protocol("audio", AUDIO_PROTOCOL_VERSION) .with_protocol( diff --git a/src/federation/devices.rs b/src/federation/devices.rs index c09081f..9723769 100644 --- a/src/federation/devices.rs +++ b/src/federation/devices.rs @@ -5,9 +5,7 @@ //! protocol as the TUI clients on `furumi/sync/1` and maps operations into //! user-scoped Postgres state. -use music_dht::playback::{ - Announcement, Checkpoint, CommandStamp, Config as PlaybackConfig, Engine, -}; +use music_dht::playback::{Announcement, Checkpoint, Config as PlaybackConfig, Engine}; use std::collections::BTreeMap; use std::str::FromStr; use std::sync::Arc; @@ -27,7 +25,7 @@ use super::{TransportStats, record_stream_transport}; pub const SYNC_ALPN: &[u8] = b"furumi/sync/2"; const CLIENT_VERSION: &str = env!("CARGO_PKG_VERSION"); -pub(super) const PROTOCOL_VERSION: u16 = 3; +pub(super) const PROTOCOL_VERSION: u16 = 4; const INVITE_TTL_MS: i64 = 10 * 60 * 1000; const PAIRING_WAIT_MS: i64 = 5 * 60 * 1000; const PAIRING_RETRY_DELAY: Duration = Duration::from_secs(1); @@ -125,220 +123,12 @@ struct DeviceProfileWire { updated_at_ms: i64, } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -struct SyncedFedTrack { - item_id: String, - owner: String, - title: String, - #[serde(default)] - artist_names: Vec, - #[serde(default)] - featured_artist_names: Vec, - year: Option, - duration_seconds: Option, - content_id: String, - release_title: Option, - track_number: Option, - disc_number: Option, -} - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -struct PlaybackTrack { - id: i64, - title: String, - track_number: Option, - disc_number: Option, - duration_seconds: f64, - #[serde(default)] - artist_names: Vec, - #[serde(default)] - featured_artist_names: Vec, - release_id: i64, - release_title: String, - release_year: Option, - #[serde(default)] - file_path: String, - content_id: Option, - audio_format: Option, - audio_bitrate: Option, - audio_sample_rate: Option, - audio_bit_depth: Option, - file_size_bytes: Option, - #[serde(default)] - play_count: i64, - #[serde(default)] - fed: Option, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] -#[serde(rename_all = "snake_case")] -enum PlaybackRepeat { - #[default] - Off, - One, - All, -} - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -struct PlaybackStateWire { - #[serde(default)] - queue: Vec, - #[serde(default)] - queue_pos: usize, - playing: bool, - paused: bool, - #[serde(default)] - idle_since_ms: Option, - position_secs: f64, - #[serde(default)] - volume: u8, - shuffle: bool, - repeat: PlaybackRepeat, -} - -type PlaybackSnapshot = music_dht::playback::Snapshot; -type PlaybackCommand = music_dht::playback::Command; - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SyncOpWire { - op_id: String, - origin_device_id: String, - seq: i64, - hlc_ms: i64, - payload: SyncOpPayload, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(tag = "kind", rename_all = "snake_case")] -enum SyncOpPayload { - TrackLikeSet { - content_id: String, - liked: bool, - #[serde(default)] - fed: Option, - }, - PlaylistCreated { - playlist_id: String, - title: String, - }, - PlaylistRenamed { - playlist_id: String, - title: String, - }, - PlaylistDeleted { - playlist_id: String, - }, - PlaylistTrackAdded { - playlist_id: String, - content_id: String, - position: i64, - #[serde(default)] - fed: Option, - }, - PlaylistTrackRemoved { - playlist_id: String, - content_id: String, - }, - DeviceProfileSet { - name: String, - client_version: String, - endpoint_ticket: String, - endpoint_id: String, - }, - DeviceTrusted { - target_device_id: String, - }, - DeviceRevoked { - target_device_id: String, - target_max_seq_seen: i64, - }, - PlaybackCommand { - target_device_id: String, - command: PlaybackCommand, - #[serde(default)] - authority: Option, - }, - ListenRecorded { - event: ListenEvent, - }, -} - -impl SyncOpPayload { - fn is_tombstone(&self) -> bool { - matches!( - self, - SyncOpPayload::TrackLikeSet { liked: false, .. } - | SyncOpPayload::PlaylistDeleted { .. } - | SyncOpPayload::PlaylistTrackRemoved { .. } - | SyncOpPayload::DeviceRevoked { .. } - ) - } -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -struct SyncSnapshot { - #[serde(default)] - likes: Vec, - #[serde(default)] - unlikes: Vec, - #[serde(default)] - playlists: Vec, - #[serde(default)] - deleted_playlists: Vec, - #[serde(default)] - removed_playlist_items: Vec, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotLike { - content_id: String, - hlc_ms: i64, - op_id: String, - #[serde(default)] - fed: Option, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotLikeTombstone { - content_id: String, - hlc_ms: i64, - op_id: String, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotPlaylist { - playlist_id: String, - title: String, - hlc_ms: i64, - op_id: String, - #[serde(default)] - items: Vec, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotPlaylistTombstone { - playlist_id: String, - hlc_ms: i64, - op_id: String, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotPlaylistItem { - content_id: String, - position: i64, - hlc_ms: i64, - op_id: String, - #[serde(default)] - fed: Option, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotPlaylistItemTombstone { - playlist_id: String, - content_id: String, - hlc_ms: i64, - op_id: String, -} +use music_dht::device_sync::{ + PlaybackCommand, PlaybackRepeat, PlaybackSnapshot, PlaybackStateWire, PlaybackTrack, + SnapshotLike, SnapshotLikeTombstone, SnapshotPlaylist, SnapshotPlaylistItem, + SnapshotPlaylistItemTombstone, SnapshotPlaylistTombstone, SyncOpPayload, SyncOpWire, + SyncSnapshot, SyncedFedTrack, +}; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] @@ -1180,8 +970,9 @@ async fn try_connect_invite( let devices = device_profiles(pool, user_id).await?; let vector = vector(pool, user_id).await?; let ops = ops_for_peer(pool, user_id, &invite.device_id).await?; - let snapshot = snapshot(pool, user_id).await?; + let snapshot = snapshot_for_peer(pool, user_id, &invite.device_id).await?; let playback = local_playback_snapshot(pool, Arc::clone(&hub), user_id, &identity).await; + let playback = playback_for_peer(pool, user_id, &invite.device_id, playback).await?; let mut stream = service.open_stream(peer, SYNC_ALPN).await?; record_stream_transport(&transport_stats, "device-sync", "outbound", "open", &stream); write_msg( @@ -1235,6 +1026,7 @@ async fn try_connect_invite( apply_device_profiles(pool, user_id, &devices).await?; apply_snapshot(pool, user_id, snapshot).await?; apply_ops(pool, Arc::clone(&hub), user_id, ops).await?; + crate::podcasts::sync::exchange_complete(pool, user_id, &invite.device_id).await?; record_local_op( pool, user_id, @@ -1459,6 +1251,7 @@ async fn handle_pair_request( } apply_snapshot(pool, user_id, incoming_snapshot).await?; apply_ops(pool, Arc::clone(&hub), user_id, ops).await?; + crate::podcasts::sync::exchange_complete(pool, user_id, &profile.device_id).await?; apply_device_profiles(pool, user_id, &requester_group_devices).await?; note_peer_vector(pool, user_id, &profile.device_id, &incoming_vector).await?; sqlx::query("UPDATE furumusic__fed_device_invite SET used_at_ms = $2 WHERE invite_id = $1") @@ -1476,8 +1269,9 @@ async fn handle_pair_request( let devices = device_profiles(pool, user_id).await?; let vector = vector(pool, user_id).await?; let ops = ops_for_peer(pool, user_id, &profile.device_id).await?; - let snapshot = snapshot(pool, user_id).await?; + let snapshot = snapshot_for_peer(pool, user_id, &profile.device_id).await?; let playback = local_playback_snapshot(pool, hub, user_id, &identity).await; + let playback = playback_for_peer(pool, user_id, &profile.device_id, playback).await?; write_msg( &mut stream, &WireMessage::PairResponse { @@ -1522,8 +1316,9 @@ async fn handle_hello( return write_sync_reject(stream, "sync group mismatch").await; } profile.endpoint_id = stream.peer_id.to_string(); - apply_device_profile(pool, user_id, &profile, false).await?; apply_device_profiles(pool, user_id, &devices).await?; + // A sender's own current report takes precedence over relayed profiles. + apply_device_profile(pool, user_id, &profile, false).await?; if let Some(playback) = playback { apply_playback_snapshot( Arc::clone(&hub), @@ -1536,6 +1331,7 @@ async fn handle_hello( } apply_snapshot(pool, user_id, incoming_snapshot).await?; apply_ops(pool, Arc::clone(&hub), user_id, ops).await?; + crate::podcasts::sync::exchange_complete(pool, user_id, &profile.device_id).await?; note_peer_vector(pool, user_id, &profile.device_id, &incoming_vector).await?; mark_seen( pool, @@ -1557,8 +1353,9 @@ async fn handle_hello( devices.push(own_profile); let vector = vector(pool, user_id).await?; let ops = ops_for_peer(pool, user_id, &profile.device_id).await?; - let snapshot = snapshot(pool, user_id).await?; + let snapshot = snapshot_for_peer(pool, user_id, &profile.device_id).await?; let playback = local_playback_snapshot(pool, hub, user_id, &identity).await; + let playback = playback_for_peer(pool, user_id, &profile.device_id, playback).await?; write_msg( &mut stream, &WireMessage::SyncResponse { @@ -1592,8 +1389,9 @@ async fn sync_device( let devices = device_profiles(pool, user_id).await?; let vector = vector(pool, user_id).await?; let ops = ops_for_peer(pool, user_id, &device.device_id).await?; - let snapshot = snapshot(pool, user_id).await?; + let snapshot = snapshot_for_peer(pool, user_id, &device.device_id).await?; let playback = local_playback_snapshot(pool, Arc::clone(&hub), user_id, &identity).await; + let playback = playback_for_peer(pool, user_id, &device.device_id, playback).await?; let mut stream = service.open_stream(peer, SYNC_ALPN).await?; record_stream_transport(&transport_stats, "device-sync", "outbound", "open", &stream); write_msg( @@ -1637,6 +1435,7 @@ async fn sync_device( } apply_snapshot(pool, user_id, snapshot).await?; apply_ops(pool, hub, user_id, ops).await?; + crate::podcasts::sync::exchange_complete(pool, user_id, &device.device_id).await?; note_peer_vector(pool, user_id, &device.device_id, &vector).await?; mark_seen( pool, @@ -2049,6 +1848,34 @@ async fn record_local_op( Ok(()) } +pub(crate) async fn record_podcast_op( + pool: &sqlx::PgPool, + user_id: i64, + payload: SyncOpPayload, +) -> Result<()> { + let identity = ensure_identity(pool, user_id, "").await?; + let mut tx = pool.begin().await?; + sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1,7512))") + .bind(format!("podcast-sync:{user_id}")) + .execute(&mut *tx) + .await?; + let row=sqlx::query("UPDATE furumusic__fed_device_identity SET local_seq=local_seq+1,last_hlc_ms=GREATEST($2,last_hlc_ms+1) WHERE user_id=$1 RETURNING local_seq,last_hlc_ms").bind(user_id).bind(now_ms()).fetch_one(&mut *tx).await?; + let seq: i64 = row.get("local_seq"); + let op = SyncOpWire { + op_id: format!("{}:{seq}", identity.device_id), + origin_device_id: identity.device_id, + seq, + hlc_ms: row.get("last_hlc_ms"), + payload, + }; + op.validate_podcast()?; + sqlx::query("INSERT INTO furumusic__fed_sync_ops(user_id,op_id,origin_device_id,seq,kind,payload_json,hlc_ms,received_at_ms,tombstone) VALUES($1,$2,$3,$4,$5,$6,$7,$8,false)").bind(user_id).bind(&op.op_id).bind(&op.origin_device_id).bind(seq).bind(payload_kind(&op.payload)).bind(serde_json::to_value(&op.payload)?).bind(op.hlc_ms).bind(now_ms()).execute(&mut *tx).await?; + sqlx::query("INSERT INTO furumusic__fed_sync_vector(user_id,device_id,max_seq) VALUES($1,$2,$3) ON CONFLICT(user_id,device_id) DO UPDATE SET max_seq=GREATEST(furumusic__fed_sync_vector.max_seq,EXCLUDED.max_seq)").bind(user_id).bind(&op.origin_device_id).bind(seq).execute(&mut *tx).await?; + crate::podcasts::sync::apply_op_tx(&mut tx, user_id, &op).await?; + tx.commit().await?; + Ok(()) +} + async fn apply_ops( pool: &sqlx::PgPool, hub: Arc, @@ -2078,7 +1905,7 @@ async fn apply_ops( .execute(pool) .await? .rows_affected(); - if inserted > 0 { + if inserted > 0 || op.payload.is_podcast() { let _ = apply_op(pool, Arc::clone(&hub), user_id, &op).await?; } sqlx::query( @@ -2127,6 +1954,9 @@ async fn apply_op( user_id: i64, op: &SyncOpWire, ) -> Result { + if op.payload.is_podcast() { + return crate::podcasts::sync::apply_op(pool, user_id, op).await; + } match &op.payload { SyncOpPayload::TrackLikeSet { content_id, @@ -2201,12 +2031,16 @@ async fn apply_op( client_version, endpoint_ticket, endpoint_id, + protocol_version, } => { let profile = DeviceProfileWire { device_id: op.origin_device_id.clone(), name: name.clone(), client_version: client_version.clone(), - protocol_version: PROTOCOL_VERSION, + protocol_version: music_dht::device_sync::profile_protocol_version( + *protocol_version, + Some(peer_protocol_version(pool, user_id, &op.origin_device_id).await?), + ), endpoint_id: endpoint_id.clone(), endpoint_ticket: endpoint_ticket.clone(), revoked: false, @@ -2240,6 +2074,7 @@ async fn apply_op( SyncOpPayload::ListenRecorded { event } => { apply_listen_event(pool, user_id, &op.origin_device_id, event).await } + _ => Ok(false), } } @@ -2605,6 +2440,8 @@ async fn apply_playlist_item_state( } async fn apply_snapshot(pool: &sqlx::PgPool, user_id: i64, snapshot: SyncSnapshot) -> Result<()> { + crate::podcasts::sync::apply_snapshot(pool, user_id, &snapshot).await?; + crate::podcasts::sync::refresh_received(pool.clone()); for like in snapshot.likes { apply_like_state( pool, @@ -2690,7 +2527,12 @@ async fn apply_device_profiles( devices: &[DeviceProfileWire], ) -> Result<()> { for profile in devices { - apply_device_profile(pool, user_id, profile, false).await?; + let mut relayed = profile.clone(); + relayed.protocol_version = music_dht::device_sync::relayed_protocol_version( + profile.protocol_version, + Some(peer_protocol_version(pool, user_id, &profile.device_id).await?), + ); + apply_device_profile(pool, user_id, &relayed, false).await?; } Ok(()) } @@ -2846,6 +2688,14 @@ async fn apply_playback_command( }; let authority = authority.as_ref(); let origin = &op.origin_device_id; + if peer_protocol_version(pool, user_id, origin).await? < 4 + && let Some(state) = hub.playback_state_json_for_commands(user_id) + { + let local = playback_state_from_browser_json(pool, state).await?; + if !music_dht::device_sync::playback_command_allowed_from_peer(&local, 3) { + return Ok(()); + } + } let op_id = &op.op_id; let identity = ensure_identity(pool, user_id, "").await?; let Some(authority) = authority else { @@ -3009,6 +2859,7 @@ pub async fn record_web_playback_command( ensure_web_playback_target(pool, user_id, target_device_id).await?; let next_state = browser_state_for_command(command, payload, current_state)?; let wire = playback_state_from_browser_json(pool, next_state.clone()).await?; + require_playback_peer(pool, user_id, target_device_id, &wire).await?; record_local_op( pool, user_id, @@ -3037,6 +2888,7 @@ pub async fn record_web_active_transfer( ) -> Result<()> { ensure_web_playback_target(pool, user_id, target_device_id).await?; let wire = playback_state_from_browser_json(pool, state).await?; + require_playback_peer(pool, user_id, target_device_id, &wire).await?; let target_name = web_playback_target_name(pool, user_id, target_device_id).await?; let identity = ensure_identity(pool, user_id, "").await?; with_playback_engine(pool, user_id, &identity, |engine| { @@ -3710,6 +3562,7 @@ async fn playback_state_from_browser_json( } } Ok(PlaybackStateWire { + playback_rate: Some(1.0), queue, queue_pos: state .get("index") @@ -3754,6 +3607,20 @@ async fn playback_track_from_track_json( track_id: i64, track: &serde_json::Value, ) -> Result { + if let Some(source) = track.get("podcast_source") { + let source: music_dht::device_sync::PlaybackSource = + serde_json::from_value(source.clone())?; + match source { + music_dht::device_sync::PlaybackSource::PodcastEpisode(episode) => { + episode.validate()?; + return Ok(PlaybackTrack::podcast_episode(*episode)); + } + music_dht::device_sync::PlaybackSource::Private => { + return Ok(PlaybackTrack::private_placeholder()); + } + music_dht::device_sync::PlaybackSource::Music => {} + } + } Ok(PlaybackTrack { id: track_id, title: track @@ -3819,6 +3686,7 @@ async fn playback_track_from_track_json( .and_then(serde_json::Value::as_i64), play_count: 0, fed: None, + source: None, }) } @@ -3842,6 +3710,12 @@ async fn resolve_playback_tracks( ) -> Result> { let mut out = Vec::new(); for (index, track) in tracks.iter().enumerate() { + if track.requires_podcast_support() { + let mut value = placeholder_playback_track(track, index, None); + value["podcast_source"] = serde_json::to_value(&track.source)?; + out.push(value); + continue; + } let content_id = track .content_id .as_deref() @@ -4476,6 +4350,7 @@ async fn ops_for_peer( user_id: i64, peer_device_id: &str, ) -> Result> { + let peer_version = peer_protocol_version(pool, user_id, peer_device_id).await?; let rows = sqlx::query( "SELECT o.op_id, o.origin_device_id, o.seq, o.hlc_ms, o.payload_json FROM furumusic__fed_sync_ops o @@ -4485,6 +4360,7 @@ async fn ops_for_peer( AND a.origin_device_id = o.origin_device_id WHERE o.user_id = $1 AND o.seq > COALESCE(a.max_seq, 0) + AND ($5 >= 4 OR o.kind NOT LIKE 'podcast_%') AND ( o.kind != 'playback_command' OR ( @@ -4499,9 +4375,11 @@ async fn ops_for_peer( .bind(peer_device_id) .bind(now_ms().saturating_sub(PLAYBACK_COMMAND_TTL_MS)) .bind(MAX_OPS_PER_BATCH) + .bind(peer_version as i32) .fetch_all(pool) .await?; - rows.into_iter() + let ops: Vec = rows + .into_iter() .map(|row| { let payload_json: serde_json::Value = row.get("payload_json"); Ok(SyncOpWire { @@ -4512,7 +4390,58 @@ async fn ops_for_peer( payload: serde_json::from_value(payload_json)?, }) }) - .collect() + .collect::>>()?; + Ok(ops + .into_iter() + .filter(|op| music_dht::device_sync::op_allowed_for_peer(&op.payload, peer_version)) + .collect()) +} + +async fn peer_protocol_version(pool: &sqlx::PgPool, user_id: i64, device_id: &str) -> Result { + let version: Option = sqlx::query_scalar( + "SELECT protocol_version FROM furumusic__fed_device WHERE user_id=$1 AND device_id=$2", + ) + .bind(user_id) + .bind(device_id) + .fetch_optional(pool) + .await?; + Ok(version.unwrap_or(3) as u16) +} +async fn snapshot_for_peer( + pool: &sqlx::PgPool, + user_id: i64, + device_id: &str, +) -> Result { + Ok(snapshot(pool, user_id) + .await? + .for_peer(peer_protocol_version(pool, user_id, device_id).await?)) +} + +async fn playback_for_peer( + pool: &sqlx::PgPool, + user_id: i64, + device_id: &str, + playback: Option, +) -> Result> { + let version = peer_protocol_version(pool, user_id, device_id).await?; + Ok(playback.filter(|snapshot| { + music_dht::device_sync::playback_allowed_for_peer(&snapshot.state, version) + })) +} + +async fn require_playback_peer( + pool: &sqlx::PgPool, + user_id: i64, + device_id: &str, + state: &PlaybackStateWire, +) -> Result<()> { + if !music_dht::device_sync::playback_allowed_for_peer( + state, + peer_protocol_version(pool, user_id, device_id).await?, + ) { + anyhow::bail!("update the connected player to play podcast queues"); + } + Ok(()) } async fn snapshot(pool: &sqlx::PgPool, user_id: i64) -> Result { @@ -4601,13 +4530,16 @@ async fn snapshot(pool: &sqlx::PgPool, user_id: i64) -> Result { op_id: row.get("op_id"), }) .collect(); - Ok(SyncSnapshot { + let mut result = SyncSnapshot { likes, unlikes, playlists, deleted_playlists, removed_playlist_items, - }) + ..SyncSnapshot::default() + }; + crate::podcasts::sync::append_snapshot(pool, user_id, &mut result).await?; + Ok(result) } async fn snapshot_playlist_items( @@ -4852,6 +4784,11 @@ fn payload_kind(payload: &SyncOpPayload) -> &'static str { SyncOpPayload::DeviceRevoked { .. } => "device_revoked", SyncOpPayload::PlaybackCommand { .. } => "playback_command", SyncOpPayload::ListenRecorded { .. } => "listen_recorded", + SyncOpPayload::PodcastSubscriptionSet { .. } => "podcast_subscription_set", + SyncOpPayload::PodcastShareSet { .. } => "podcast_share_set", + SyncOpPayload::PodcastFeedAliasesAdded { .. } => "podcast_feed_aliases_added", + SyncOpPayload::PodcastProgressSet { .. } => "podcast_progress_set", + SyncOpPayload::PodcastListenRecorded { .. } => "podcast_listen_recorded", } } diff --git a/src/i18n/phrases.rs b/src/i18n/phrases.rs index 777e2d1..95f6bee 100644 --- a/src/i18n/phrases.rs +++ b/src/i18n/phrases.rs @@ -305,7 +305,7 @@ translations! { player_likes_count: "likes" , "лайков"; player_likes_playlist: "Likes" , "Лайки"; player_listened: "listened" , "прослушано"; - player_search_placeholder: "Search artists, releases, tracks..." , "Поиск артистов, релизов, треков..."; + player_search_placeholder: "Search artists, releases, tracks, podcasts..." , "Поиск артистов, релизов, треков, подкастов..."; player_search_similar_to: "Search similar to:" , "Поиск похожих на:"; player_find_similar: "Find similar tracks" , "Найти похожие треки"; player_similarity_failed: "Similarity search failed" , "Не удалось найти похожие треки"; diff --git a/src/music/mod.rs b/src/music/mod.rs index 3f778c4..da4c922 100644 --- a/src/music/mod.rs +++ b/src/music/mod.rs @@ -2865,6 +2865,28 @@ pub mod db_migrations { const OPERATIONS: &'static [Operation] = &[Operation::custom(create_podcasts).build()]; } + #[cot::db::migrations::migration_op] + async fn create_podcast_sync(ctx: migrations::MigrationContext<'_>) -> cot::db::Result<()> { + for statement in include_str!("../podcasts/sync_schema.sql").split(';') { + if !statement.trim().is_empty() { + ctx.db.raw(statement).await?; + } + } + Ok(()) + } + #[derive(Debug, Copy, Clone)] + pub struct M0055PodcastSyncAndUnicodeSearch; + impl migrations::Migration for M0055PodcastSyncAndUnicodeSearch { + const APP_NAME: &'static str = "furumusic"; + const MIGRATION_NAME: &'static str = "m_0055_podcast_sync_unicode_search"; + const DEPENDENCIES: &'static [migrations::MigrationDependency] = + &[migrations::MigrationDependency::migration( + "furumusic", + "m_0054_create_podcasts", + )]; + const OPERATIONS: &'static [Operation] = &[Operation::custom(create_podcast_sync).build()]; + } + pub const MIGRATIONS: &[&SyncDynMigration] = &[ &M0006CreateMediaFile, &M0007CreateArtist, @@ -2909,5 +2931,6 @@ pub mod db_migrations { &M0051AddListenStatisticsIndexes, &M0052AddImportPlaylistDestinations, &M0054CreatePodcasts, + &M0055PodcastSyncAndUnicodeSearch, ]; } diff --git a/src/player/dto.rs b/src/player/dto.rs index 3c68d40..a106a46 100644 --- a/src/player/dto.rs +++ b/src/player/dto.rs @@ -332,6 +332,7 @@ pub(super) struct ShareDetail { #[derive(Debug, Serialize, JsonSchema)] pub(super) struct SearchResults { + pub(super) podcasts: Vec, pub(super) artists: Vec, pub(super) releases: Vec, pub(super) tracks: Vec, diff --git a/src/player/mod.rs b/src/player/mod.rs index 2b0c478..4796997 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -1046,7 +1046,18 @@ impl PlayerDeviceHub { .playback_state_by_user .get(&playback_user_id) .cloned() - .map(|playback_state| playback_state_at(playback_state, now)) + .map(|playback_state| { + let mut playback_state = playback_state_at(playback_state, now); + if playback_user_id != user_id { + for track in &mut playback_state.tracks { + redact_podcast_track(track); + } + if let Some(track) = &mut playback_state.track { + redact_podcast_track(track); + } + } + playback_state + }) } fn jam_dtos_locked( @@ -1150,6 +1161,16 @@ fn current_millis() -> i64 { chrono::Utc::now().timestamp_millis() } +fn redact_podcast_track(track: &mut serde_json::Value) { + if track.get("podcast_source").is_none() + && track.get("podcast").and_then(serde_json::Value::as_bool) != Some(true) + { + return; + } + let id = track.get("id").cloned().unwrap_or(serde_json::json!(0)); + *track = serde_json::json!({"id":id,"title":"Private item","artists":[],"featured_artists":[],"release_id":0,"release_title":"","cover_url":null,"stream_url":"","duration_seconds":0,"unavailable":true,"podcast_source":{"kind":"private"}}); +} + fn fed_virtual_device_id(device_id: &str) -> String { format!("{FED_DEVICE_PREFIX}{device_id}") } @@ -6657,13 +6678,14 @@ async fn search_handler( pool: &sqlx::PgPool, query: cot::request::extractors::UrlQuery, ) -> cot::Result { - let Some(_user) = auth::get_request_user(&auth_ctx, &session, &db).await else { + let Some(user) = auth::get_request_user(&auth_ctx, &session, &db).await else { return Ok(json_error(StatusCode::UNAUTHORIZED, "not authenticated")); }; let q = query.0.q.trim().to_lowercase(); if q.is_empty() { return Json(SearchResults { + podcasts: Vec::new(), artists: Vec::new(), releases: Vec::new(), tracks: Vec::new(), @@ -6968,6 +6990,9 @@ async fn search_handler( .collect(); Json(SearchResults { + podcasts: crate::podcasts::search(pool, user.id, &q, limit) + .await + .map_err(|e| cot::Error::internal(e.to_string()))?, artists, releases, tracks, diff --git a/src/podcasts/mod.rs b/src/podcasts/mod.rs index 9469c57..4fe61f2 100644 --- a/src/podcasts/mod.rs +++ b/src/podcasts/mod.rs @@ -1,4 +1,5 @@ //! Server-owned podcast catalogue and cache; subscriptions and progress are personal. +pub(crate) mod sync; mod web; pub use web::PodcastApp; @@ -21,6 +22,30 @@ use tokio::io::AsyncWriteExt; const MAX_FEED_BYTES: usize = 16 * 1024 * 1024; const MAX_AUDIO_BYTES: u64 = 2 * 1024 * 1024 * 1024; +pub(crate) async fn search( + pool: &PgPool, + user: i64, + query: &str, + limit: i64, +) -> Result> { + let words: Vec = query + .split_whitespace() + .take(20) + .map(|word| { + format!( + "%{}%", + word.replace('\\', "\\\\") + .replace('%', "\\%") + .replace('_', "\\_") + ) + }) + .collect(); + if words.is_empty() { + return Ok(Vec::new()); + } + Ok(sqlx::query_scalar("SELECT jsonb_build_object('id',p.id,'url',p.origin_url,'metadata',p.metadata,'subscribed',EXISTS(SELECT 1 FROM furumusic__podcast_subscription s WHERE s.user_id=$1 AND s.podcast_id=p.id)) FROM furumusic__podcast p WHERE EXISTS(SELECT 1 FROM furumusic__podcast_subscription s WHERE s.podcast_id=p.id AND (s.user_id=$1 OR s.shared)) AND NOT EXISTS(SELECT 1 FROM unnest($2::text[]) AS term WHERE concat_ws(' ',p.metadata->>'title',p.metadata->>'description',p.metadata->>'author') COLLATE furumusic_unicode NOT ILIKE term) ORDER BY p.metadata->>'title',p.id LIMIT $3").bind(user).bind(words).bind(limit.clamp(1,100)).fetch_all(pool).await?) +} + fn public_address(ip: IpAddr) -> bool { match ip { IpAddr::V4(ip) => { @@ -244,6 +269,7 @@ async fn refresh_inner(pool: &PgPool, id: &str) -> Result { let meta = episode.to_meta(&podcast_id, episode_id.clone()); let inserted: bool = sqlx::query_scalar("INSERT INTO furumusic__podcast_episode(id,podcast_id,metadata) VALUES($1,$2,$3) ON CONFLICT(id) DO UPDATE SET metadata=EXCLUDED.metadata RETURNING (xmax=0)").bind(episode_id.as_str()).bind(id).bind(serde_json::to_value(meta)?).fetch_one(&mut *tx).await?; added += usize::from(inserted); + sync::restore_episode_progress(&mut tx, episode_id.as_str()).await?; } for alias in [ Some(final_url.clone()), @@ -342,9 +368,12 @@ pub async fn reconcile(pool: &PgPool, overlay: &PodcastDht) -> Result<()> { // never publish its snapshot after a completed share-off/unsubscribe pass. static GATE: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); let _guard = GATE.lock().await; - let rows = sqlx::query("SELECT p.*,s.confirmed_urls FROM furumusic__podcast p JOIN furumusic__podcast_subscription s ON s.podcast_id=p.id WHERE s.shared ORDER BY p.id").fetch_all(pool).await?; + let rows = sqlx::query("SELECT p.*,s.confirmed_urls,s.user_id FROM furumusic__podcast p JOIN furumusic__podcast_subscription s ON s.podcast_id=p.id WHERE s.shared ORDER BY p.id").fetch_all(pool).await?; let mut states = std::collections::BTreeMap::new(); for row in rows { + if !sync::publication_current(pool, row.get("user_id")).await? { + continue; + } let id: String = row.get("id"); let meta: PodcastMeta = serde_json::from_value(row.get("metadata"))?; let origin = FeedUrl::parse(row.get::<&str, _>("origin_url"))?; diff --git a/src/podcasts/sync.rs b/src/podcasts/sync.rs new file mode 100644 index 0000000..3592a4e --- /dev/null +++ b/src/podcasts/sync.rs @@ -0,0 +1,448 @@ +//! Durable Frid podcast registers. Projections are user-scoped; revocations +//! remain here after a subscription is removed from the visible catalogue. +use super::*; +use music_dht::device_sync::{SyncOpPayload, SyncOpWire, SyncSnapshot}; +use music_dht::podcast::{ + ListenSession, ProgressRecord, ProgressStatus, effective_share, merge_listen_session, + merge_progress, merge_share, merge_subscription, next_podcast_generation, +}; +use serde::{Serialize, de::DeserializeOwned}; +use sqlx::{Postgres, Transaction}; + +fn current_users() -> &'static std::sync::Mutex> { + static USERS: std::sync::OnceLock>> = + std::sync::OnceLock::new(); + USERS.get_or_init(Default::default) +} +pub async fn publication_current(pool: &PgPool, user: i64) -> Result { + if current_users() + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .contains(&user) + { + return Ok(true); + } + let peers:bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM furumusic__fed_device d WHERE user_id=$1 AND trusted_at_ms IS NOT NULL AND revoked_at_ms IS NULL AND protocol_version>=4 AND device_id<>(SELECT device_id FROM furumusic__fed_device_identity WHERE user_id=$1))").bind(user).fetch_one(pool).await?; + Ok(!peers) +} +pub async fn exchange_complete(pool: &PgPool, user: i64, peer: &str) -> Result<()> { + let version: Option = sqlx::query_scalar( + "SELECT protocol_version FROM furumusic__fed_device WHERE user_id=$1 AND device_id=$2", + ) + .bind(user) + .bind(peer) + .fetch_optional(pool) + .await?; + if version.is_some_and(|v| v >= 4) { + current_users() + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .insert(user); + refresh_received(pool.clone()); + tokio::spawn(async { + let _ = crate::federation::handle().sync_podcasts().await; + }); + } + Ok(()) +} + +async fn get( + tx: &mut Transaction<'_, Postgres>, + user: i64, + kind: &str, + id: &str, +) -> Result> { + let value: Option = sqlx::query_scalar("SELECT value FROM furumusic__podcast_register WHERE user_id=$1 AND kind=$2 AND entity_id=$3").bind(user).bind(kind).bind(id).fetch_optional(&mut **tx).await?; + Ok(value.map(serde_json::from_value).transpose()?) +} +async fn put( + tx: &mut Transaction<'_, Postgres>, + user: i64, + kind: &str, + id: &str, + value: &T, +) -> Result<()> { + sqlx::query("INSERT INTO furumusic__podcast_register(user_id,kind,entity_id,value) VALUES($1,$2,$3,$4) ON CONFLICT(user_id,kind,entity_id) DO UPDATE SET value=EXCLUDED.value").bind(user).bind(kind).bind(id).bind(serde_json::to_value(value)?).execute(&mut **tx).await?; + Ok(()) +} +async fn begin(pool: &PgPool, user: i64) -> Result> { + let mut tx = pool.begin().await?; + sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1,7512))") + .bind(format!("podcast-sync:{user}")) + .execute(&mut *tx) + .await?; + Ok(tx) +} + +async fn project_subscription( + tx: &mut Transaction<'_, Postgres>, + user: i64, + id: &str, +) -> Result<()> { + let Some(sub) = get::(tx, user, "subscription", id).await? else { + return Ok(()); + }; + let share = get::(tx, user, "share", id).await?; + let origin = sub + .feed_urls() + .into_iter() + .find(|url| PodcastId::from_feed_url(url) == sub.podcast_id) + .unwrap_or(sub.podcast.feed_url.clone()); + sqlx::query("INSERT INTO furumusic__podcast(id,origin_url,feed_url,metadata) VALUES($1,$2,$3,$4) ON CONFLICT(id) DO UPDATE SET origin_url=EXCLUDED.origin_url").bind(id).bind(origin.as_str()).bind(sub.podcast.feed_url.as_str()).bind(json!(sub.podcast)).execute(&mut **tx).await?; + for alias in sub.feed_urls() { + sqlx::query("INSERT INTO furumusic__podcast_alias(url,podcast_id) VALUES($1,$2) ON CONFLICT DO NOTHING").bind(alias.as_str()).bind(id).execute(&mut **tx).await?; + } + if sub.subscribed { + let shared = effective_share(Some(&sub), share.as_ref()); + sqlx::query("INSERT INTO furumusic__podcast_subscription(user_id,podcast_id,shared) VALUES($1,$2,$3) ON CONFLICT(user_id,podcast_id) DO UPDATE SET shared=EXCLUDED.shared,confirmed_urls=CASE WHEN EXCLUDED.shared THEN furumusic__podcast_subscription.confirmed_urls ELSE NULL END").bind(user).bind(id).bind(shared).execute(&mut **tx).await?; + } else { + sqlx::query( + "DELETE FROM furumusic__podcast_subscription WHERE user_id=$1 AND podcast_id=$2", + ) + .bind(user) + .bind(id) + .execute(&mut **tx) + .await?; + } + Ok(()) +} + +async fn merge_sub( + tx: &mut Transaction<'_, Postgres>, + user: i64, + incoming: SubscriptionRecord, +) -> Result<()> { + incoming.validate()?; + let id = incoming.podcast_id.as_str(); + let mut merged = match get::(tx, user, "subscription", id).await? { + Some(current) => merge_subscription(¤t, &incoming)?, + None => incoming.clone(), + }; + if let Some(aliases) = get::>(tx, user, "aliases", id).await? { + merged.add_feed_aliases(&aliases); + } + put(tx, user, "subscription", id, &merged).await?; + project_subscription(tx, user, id).await +} +async fn merge_shared( + tx: &mut Transaction<'_, Postgres>, + user: i64, + incoming: ShareRecord, + local: bool, +) -> Result<()> { + let id = incoming.podcast_id.as_str(); + let current = get::(tx, user, "share", id).await?; + let merged = match ¤t { + Some(current) => merge_share(current, &incoming)?, + None => incoming.clone(), + }; + if !local && current.as_ref().map(|r| r.op_id.as_str()) != Some(merged.op_id.as_str()) { + sqlx::query("UPDATE furumusic__podcast_subscription SET confirmed_urls=NULL WHERE user_id=$1 AND podcast_id=$2").bind(user).bind(id).execute(&mut **tx).await?; + } + put(tx, user, "share", id, &merged).await?; + project_subscription(tx, user, id).await +} +async fn merge_resume( + tx: &mut Transaction<'_, Postgres>, + user: i64, + incoming: ProgressRecord, +) -> Result<()> { + incoming.validate()?; + let id = incoming.episode_id.as_str(); + let merged = match get::(tx, user, "progress", id).await? { + Some(current) => merge_progress(¤t, &incoming)?, + None => incoming.clone(), + }; + put(tx, user, "progress", id, &merged).await?; + project_resume(tx, user, &merged).await +} +async fn project_resume( + tx: &mut Transaction<'_, Postgres>, + user: i64, + record: &ProgressRecord, +) -> Result<()> { + sqlx::query("INSERT INTO furumusic__podcast_progress(user_id,episode_id,position_ms,completed,generation,checkpoint) SELECT $1,id,$3,$4,$5,0 FROM furumusic__podcast_episode WHERE id=$2 ON CONFLICT(user_id,episode_id) DO UPDATE SET position_ms=EXCLUDED.position_ms,completed=EXCLUDED.completed,generation=EXCLUDED.generation").bind(user).bind(record.episode_id.as_str()).bind(record.position_ms).bind(record.status==ProgressStatus::Completed).bind(record.generation.min(i64::MAX as u64) as i64).execute(&mut **tx).await?; + Ok(()) +} +async fn merge_session( + tx: &mut Transaction<'_, Postgres>, + user: i64, + incoming: ListenSession, +) -> Result<()> { + incoming.validate()?; + let id = &incoming.session_id; + let merged = match get::(tx, user, "session", id).await? { + Some(current) => merge_listen_session(¤t, &incoming)?, + None => incoming.clone(), + }; + put(tx, user, "session", id, &merged).await +} + +/// Applying the same operation again is safe, including after a crash between +/// persisting its log entry and updating the projection. +pub async fn apply_op(pool: &PgPool, user: i64, op: &SyncOpWire) -> Result { + let mut tx = begin(pool, user).await?; + apply_op_tx(&mut tx, user, op).await?; + tx.commit().await?; + Ok(true) +} + +pub(crate) async fn apply_op_tx( + tx: &mut Transaction<'_, Postgres>, + user: i64, + op: &SyncOpWire, +) -> Result { + op.validate_podcast()?; + let reference = match &op.payload { + SyncOpPayload::PodcastProgressSet { episode, .. } + | SyncOpPayload::PodcastListenRecorded { episode, .. } => episode.as_ref(), + _ => None, + }; + if let Some(reference) = reference { + materialize_reference(tx, reference).await?; + } + if let Some(sub) = op.podcast_subscription() { + merge_sub(tx, user, sub).await?; + } else if let Some(share) = op.podcast_share() { + let local:bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM furumusic__fed_device_identity WHERE user_id=$1 AND device_id=$2)").bind(user).bind(&op.origin_device_id).fetch_one(&mut **tx).await?; + merge_shared(tx, user, share, local).await?; + } else if let Some((id, aliases)) = op.podcast_feed_aliases() { + let current = get::>(tx, user, "aliases", id.as_str()) + .await? + .unwrap_or_default(); + let merged = music_dht::podcast::merge_feed_aliases(id, ¤t, aliases); + put(tx, user, "aliases", id.as_str(), &merged).await?; + if let Some(mut sub) = + get::(tx, user, "subscription", id.as_str()).await? + { + sub.add_feed_aliases(aliases); + put(tx, user, "subscription", id.as_str(), &sub).await?; + project_subscription(tx, user, id.as_str()).await?; + } + } else if let Some(progress) = op.podcast_progress() { + merge_resume(tx, user, progress).await?; + } else if let Some(session) = op.podcast_listen_session() { + merge_session(tx, user, session.clone()).await?; + } + Ok(true) +} + +async fn materialize_reference( + tx: &mut Transaction<'_, Postgres>, + reference: &music_dht::podcast::PodcastEpisodeRef, +) -> Result<()> { + reference.validate()?; + let podcast = PodcastMeta { + feed_url: reference.feed_url.clone(), + title: reference.podcast_title.clone(), + author: None, + description: None, + link: None, + image_url: reference.image_url.clone(), + }; + sqlx::query("INSERT INTO furumusic__podcast(id,origin_url,feed_url,metadata) VALUES($1,$2,$2,$3) ON CONFLICT DO NOTHING").bind(reference.podcast_id.as_str()).bind(reference.feed_url.as_str()).bind(json!(podcast)).execute(&mut **tx).await?; + let episode = EpisodeMeta { + episode_id: reference.episode_id.clone(), + podcast_id: reference.podcast_id.clone(), + guid: None, + title: reference.episode_title.clone(), + description: None, + season: reference.season, + episode_number: reference.episode_number, + episode_type: music_dht::podcast::EpisodeType::Full, + published_at_ms: reference.published_at_ms, + duration_ms: reference.duration_ms, + audio_url: reference.audio_url.clone(), + mime: reference.mime.clone(), + byte_len: None, + image_url: reference.image_url.clone(), + link: None, + }; + sqlx::query("INSERT INTO furumusic__podcast_episode(id,podcast_id,metadata) VALUES($1,$2,$3) ON CONFLICT DO NOTHING").bind(reference.episode_id.as_str()).bind(reference.podcast_id.as_str()).bind(json!(episode)).execute(&mut **tx).await?; + Ok(()) +} + +pub async fn apply_snapshot(pool: &PgPool, user: i64, snapshot: &SyncSnapshot) -> Result<()> { + if !snapshot.podcast_state_within_limits() { + bail!("podcast snapshot exceeds contract limits"); + } + let mut tx = begin(pool, user).await?; + for sub in &snapshot.podcast_subscriptions { + merge_sub(&mut tx, user, sub.clone()).await?; + } + for share in &snapshot.podcast_shares { + merge_shared(&mut tx, user, share.clone(), false).await?; + } + for progress in &snapshot.podcast_progress { + merge_resume(&mut tx, user, progress.clone()).await?; + } + for session in &snapshot.podcast_listen_sessions { + merge_session(&mut tx, user, session.clone()).await?; + } + tx.commit().await?; + Ok(()) +} + +pub async fn append_snapshot(pool: &PgPool, user: i64, snapshot: &mut SyncSnapshot) -> Result<()> { + seed_user(pool, user).await?; + let rows = sqlx::query( + "SELECT kind,value FROM furumusic__podcast_register WHERE user_id=$1 ORDER BY entity_id", + ) + .bind(user) + .fetch_all(pool) + .await?; + for row in rows { + let value: Value = row.get("value"); + match row.get::<&str, _>("kind") { + "subscription" => snapshot + .podcast_subscriptions + .push(serde_json::from_value(value)?), + "share" => snapshot.podcast_shares.push(serde_json::from_value(value)?), + "progress" => snapshot + .podcast_progress + .push(serde_json::from_value(value)?), + "session" => snapshot + .podcast_listen_sessions + .push(serde_json::from_value(value)?), + _ => {} + } + } + snapshot.bound_podcast_state(); + Ok(()) +} + +pub async fn local_subscription( + pool: &PgPool, + user: i64, + id: &str, + subscribed: bool, +) -> Result<()> { + let mut tx = begin(pool, user).await?; + let sub = get::(&mut tx, user, "subscription", id).await?; + let share = get::(&mut tx, user, "share", id).await?; + let generation = next_podcast_generation(sub.as_ref(), share.as_ref()); + let row = sqlx::query("SELECT metadata,origin_url FROM furumusic__podcast WHERE id=$1") + .bind(id) + .fetch_one(&mut *tx) + .await?; + let podcast: PodcastMeta = serde_json::from_value(row.get("metadata"))?; + let origin = FeedUrl::parse(row.get::<&str, _>("origin_url"))?; + let mut aliases = sub.as_ref().map(|s| s.feed_urls()).unwrap_or_default(); + aliases.push(origin); + let podcast_id: PodcastId = serde_json::from_value(json!(id))?; + let aliases = music_dht::podcast::merge_feed_aliases(&podcast_id, &[], &aliases); + tx.commit().await?; + crate::federation::devices::record_podcast_op( + pool, + user, + SyncOpPayload::PodcastSubscriptionSet { + podcast_id, + subscribed, + podcast, + feed_aliases: aliases, + generation, + }, + ) + .await?; + if !subscribed { + local_share(pool, user, id, false).await?; + } + Ok(()) +} +pub async fn local_share(pool: &PgPool, user: i64, id: &str, shared: bool) -> Result<()> { + let mut tx = begin(pool, user).await?; + let sub = get::(&mut tx, user, "subscription", id).await?; + let share = get::(&mut tx, user, "share", id).await?; + let generation = next_podcast_generation(sub.as_ref(), share.as_ref()); + tx.commit().await?; + crate::federation::devices::record_podcast_op( + pool, + user, + SyncOpPayload::PodcastShareSet { + podcast_id: serde_json::from_value(json!(id))?, + shared, + generation, + }, + ) + .await +} +pub async fn seed_user(pool: &PgPool, user: i64) -> Result<()> { + let rows=sqlx::query("SELECT s.podcast_id,s.shared FROM furumusic__podcast_subscription s WHERE user_id=$1 AND NOT EXISTS(SELECT 1 FROM furumusic__podcast_register r WHERE r.user_id=s.user_id AND r.entity_id=s.podcast_id AND r.kind='subscription')").bind(user).fetch_all(pool).await?; + for row in rows { + let id: String = row.get("podcast_id"); + let shared: bool = row.get("shared"); + local_subscription(pool, user, &id, true).await?; + if shared { + local_share(pool, user, &id, true).await?; + } + } + Ok(()) +} + +pub async fn ensure_subscribed(pool: &PgPool, user: i64, id: &str) -> Result<()> { + let active:bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM furumusic__podcast_register WHERE user_id=$1 AND kind='subscription' AND entity_id=$2 AND value->>'subscribed'='true')").bind(user).bind(id).fetch_one(pool).await?; + if !active { + local_subscription(pool, user, id, true).await?; + } + Ok(()) +} + +pub async fn local_progress( + pool: &PgPool, + user: i64, + id: &str, + generation: u64, + position: i64, + completed: bool, +) -> Result<()> { + let row=sqlx::query("SELECT g.*,e.metadata AS episode,p.metadata AS podcast FROM furumusic__podcast_progress g JOIN furumusic__podcast_episode e ON e.id=g.episode_id JOIN furumusic__podcast p ON p.id=e.podcast_id WHERE g.user_id=$1 AND g.episode_id=$2").bind(user).bind(id).fetch_one(pool).await?; + let episode: EpisodeMeta = serde_json::from_value(row.get("episode"))?; + let podcast: PodcastMeta = serde_json::from_value(row.get("podcast"))?; + crate::federation::devices::record_podcast_op( + pool, + user, + SyncOpPayload::PodcastProgressSet { + episode_id: episode.episode_id.clone(), + podcast_id: episode.podcast_id.clone(), + position_ms: position, + status: if completed { + ProgressStatus::Completed + } else if position > 0 { + ProgressStatus::InProgress + } else { + ProgressStatus::NotStarted + }, + generation, + session_id: format!("web:{user}:{id}:{generation}"), + episode: Some(episode.to_ref(&podcast)), + }, + ) + .await +} + +pub async fn restore_episode_progress(tx: &mut Transaction<'_, Postgres>, id: &str) -> Result<()> { + let rows=sqlx::query("SELECT user_id,value FROM furumusic__podcast_register WHERE kind='progress' AND entity_id=$1").bind(id).fetch_all(&mut **tx).await?; + for row in rows { + project_resume( + tx, + row.get("user_id"), + &serde_json::from_value(row.get("value"))?, + ) + .await?; + } + Ok(()) +} + +pub fn refresh_received(pool: PgPool) { + static WORKER: tokio::sync::Semaphore = tokio::sync::Semaphore::const_new(1); + let Ok(permit) = WORKER.try_acquire() else { + return; + }; + tokio::spawn(async move { + let _permit = permit; + let ids:Vec=sqlx::query_scalar("SELECT DISTINCT p.id FROM furumusic__podcast p JOIN furumusic__podcast_subscription s ON s.podcast_id=p.id WHERE p.checked_at IS NULL LIMIT 20").fetch_all(&pool).await.unwrap_or_default(); + for id in ids { + let _ = refresh(&pool, &id).await; + } + }); +} diff --git a/src/podcasts/sync_schema.sql b/src/podcasts/sync_schema.sql new file mode 100644 index 0000000..22b8fa3 --- /dev/null +++ b/src/podcasts/sync_schema.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS furumusic__podcast_register ( + user_id BIGINT NOT NULL REFERENCES furumusic__user(id) ON DELETE CASCADE, + kind TEXT NOT NULL, entity_id TEXT NOT NULL, value JSONB NOT NULL, + PRIMARY KEY(user_id,kind,entity_id) +); +CREATE COLLATION IF NOT EXISTS furumusic_unicode (provider=icu, locale='und', deterministic=true); diff --git a/src/podcasts/tests.rs b/src/podcasts/tests.rs index 0783baf..bff7971 100644 --- a/src/podcasts/tests.rs +++ b/src/podcasts/tests.rs @@ -82,6 +82,14 @@ async fn postgres_multiuser_progress_retention_and_admin() { .execute(&pool) .await .unwrap(); + for source in [ + include_str!("sync_schema.sql"), + include_str!("../../tests/podcast_sync_fixture.sql"), + ] { + for statement in source.split(';').filter(|s| !s.trim().is_empty()) { + sqlx::query(statement).execute(&pool).await.unwrap(); + } + } let origin = FeedUrl::parse("https://example.test/feed.xml").unwrap(); let podcast = PodcastId::from_feed_url(&origin); let feed=parse_feed_with_base(br#"FixtureEpisodeone"#,Some(origin.as_str())).unwrap(); @@ -273,6 +281,85 @@ async fn postgres_multiuser_progress_retention_and_admin() { .unwrap() .is_empty() ); + // An actual v4 wire operation, as emitted by the TUI, creates the web + // subscription without HTTP fetching or a second manual subscription. + let url = FeedUrl::parse("https://example.test/tui-feed").unwrap(); + let id = PodcastId::from_feed_url(&url); + let incoming:music_dht::device_sync::SyncOpWire=serde_json::from_value(json!({"op_id":"tui_test:1","origin_device_id":"tui_test","seq":1,"hlc_ms":42,"payload":{"kind":"podcast_subscription_set","podcast_id":id,"subscribed":true,"podcast":{"feed_url":url,"title":"ТАЙНЫ Космоса","description":"История МУЗЫКИ и Ελληνικά"},"feed_aliases":[url],"generation":1}})).unwrap(); + sync::apply_op(&pool, 1, &incoming).await.unwrap(); + assert_eq!( + list(&pool, 1, false).await.unwrap()["mine"][0]["id"], + json!(id) + ); + assert_eq!( + super::super::search(&pool, 1, "тайны музыка", 10) + .await + .unwrap() + .len(), + 0 + ); + for query in [ + "тайны космоса", + "ТАЙНЫ КОСМОСА", + "история музыки", + "ελληνικά", + "ΕΛΛΗΝΙΚΆ", + ] { + assert_eq!( + super::super::search(&pool, 1, query, 10) + .await + .unwrap() + .len(), + 1, + "{query}" + ); + } + assert!( + super::super::search(&pool, 2, "тайны", 10) + .await + .unwrap() + .is_empty() + ); + let mut before = music_dht::device_sync::SyncSnapshot::default(); + sync::append_snapshot(&pool, 1, &mut before).await.unwrap(); + assert!(!before.for_peer(3).has_podcast_state()); + // Simulate the same person's next connected device using another local + // user id, since local database user ids are not part of the wire contract. + sync::apply_snapshot(&pool, 2, &before).await.unwrap(); + assert_eq!( + list(&pool, 2, false).await.unwrap()["mine"][0]["id"], + json!(id) + ); + sync::local_subscription(&pool, 1, id.as_str(), false) + .await + .unwrap(); + let mut after = music_dht::device_sync::SyncSnapshot::default(); + sync::append_snapshot(&pool, 1, &mut after).await.unwrap(); + sync::apply_snapshot(&pool, 2, &after).await.unwrap(); + sync::apply_snapshot(&pool, 2, &before).await.unwrap(); + sync::apply_op(&pool, 1, &incoming).await.unwrap(); + assert!( + list(&pool, 1, false).await.unwrap()["mine"] + .as_array() + .unwrap() + .is_empty() + ); + assert!( + list(&pool, 2, false).await.unwrap()["mine"] + .as_array() + .unwrap() + .is_empty() + ); + let wire:music_dht::device_sync::SyncOpWire=serde_json::from_value(sqlx::query_scalar::<_,Value>("SELECT jsonb_build_object('op_id',op_id,'origin_device_id',origin_device_id,'seq',seq,'hlc_ms',hlc_ms,'payload',payload_json) FROM furumusic__fed_sync_ops WHERE user_id=1 AND kind='podcast_subscription_set' ORDER BY seq DESC LIMIT 1").fetch_one(&pool).await.unwrap()).unwrap(); + assert!(!wire.podcast_subscription().unwrap().subscribed); + assert!(!music_dht::device_sync::op_allowed_for_peer( + &wire.payload, + 3 + )); + assert!(music_dht::device_sync::op_allowed_for_peer( + &wire.payload, + 4 + )); pool.close().await; sqlx::query(&format!("DROP SCHEMA {schema} CASCADE")) .execute(&admin_pool) diff --git a/src/podcasts/web.rs b/src/podcasts/web.rs index 448f0a4..c7a3dd2 100644 --- a/src/podcasts/web.rs +++ b/src/podcasts/web.rs @@ -107,6 +107,8 @@ async fn action( "admin_list" => return list(&pool, user, true).await, "add" => { let id = subscribe(&pool, user, &body.url).await?; + sync::ensure_subscribed(&pool, user, &id).await?; + sync::seed_user(&pool, user).await?; let checked = refresh(&pool, &id).await; tokio::spawn(async move { let _ = download_pending(&pool, &config, Some(&id)).await; @@ -116,6 +118,8 @@ async fn action( ); } "remove" => { + require_subscription(&pool, user, &body.id).await?; + sync::local_subscription(&pool, user, &body.id, false).await?; sqlx::query( "DELETE FROM furumusic__podcast_subscription WHERE user_id=$1 AND podcast_id=$2", ) @@ -126,6 +130,7 @@ async fn action( } "share" => { require_subscription(&pool, user, &body.id).await?; + sync::seed_user(&pool, user).await?; let row = sqlx::query("SELECT origin_url,metadata FROM furumusic__podcast WHERE id=$1") .bind(&body.id) .fetch_one(&pool) @@ -147,6 +152,7 @@ async fn action( return Ok(json!({"confirmation_required":true,"urls":urls})); } sqlx::query("UPDATE furumusic__podcast_subscription SET shared=$3,confirmed_urls=$4 WHERE user_id=$1 AND podcast_id=$2").bind(user).bind(&body.id).bind(body.shared).bind(if body.shared {Some(urls)} else {None}).execute(&pool).await?; + sync::local_share(&pool, user, &body.id, body.shared).await?; } "episodes" => { require_subscription(&pool, user, &body.id).await?; @@ -187,15 +193,36 @@ async fn action( } else { sqlx::query_scalar("UPDATE furumusic__podcast_progress SET position_ms=$3,completed=$4,checkpoint=$6,updated_at=now() WHERE user_id=$1 AND episode_id=$2 AND generation=$5 AND checkpoint<=$6 RETURNING generation").bind(user).bind(&body.id).bind(body.position_ms.max(0)).bind(completed).bind(body.generation).bind(body.sequence).fetch_optional(&pool).await? }; + if let Some(generation) = generation { + sync::local_progress( + &pool, + user, + &body.id, + generation.max(0) as u64, + body.position_ms.max(0), + completed, + ) + .await?; + } return Ok(json!({"ok":true,"generation":generation})); } "search" => { if body.query.chars().count() < 2 || body.query.len() > 200 { bail!("enter 2–200 characters"); } - let hits = crate::federation::handle() - .search_podcasts(&body.query) - .await?; + let mut hits = super::search(&pool, user, &body.query, 100).await?; + if let Ok(Ok(Value::Array(remote))) = tokio::time::timeout( + Duration::from_secs(10), + crate::federation::handle().search_podcasts(&body.query), + ) + .await + { + for hit in remote { + if !hits.iter().any(|local| local["id"] == hit["id"]) { + hits.push(hit); + } + } + } return Ok(json!({"hits":hits})); } "admin_settings" => { @@ -211,9 +238,25 @@ async fn action( .await?; } "admin_unpublish" => { + let owners:Vec=sqlx::query_scalar("SELECT user_id FROM furumusic__podcast_subscription WHERE podcast_id=$1 AND shared").bind(&body.id).fetch_all(&pool).await?; + for owner in owners { + sync::seed_user(&pool, owner).await?; + sync::local_share(&pool, owner, &body.id, false).await?; + } sqlx::query("UPDATE furumusic__podcast_subscription SET shared=false,confirmed_urls=NULL WHERE podcast_id=$1").bind(&body.id).execute(&pool).await?; } "admin_clear" | "admin_delete" => { + if body.action == "admin_delete" { + let owners: Vec = sqlx::query_scalar( + "SELECT user_id FROM furumusic__podcast_subscription WHERE podcast_id=$1", + ) + .bind(&body.id) + .fetch_all(&pool) + .await?; + for owner in owners { + sync::local_subscription(&pool, owner, &body.id, false).await?; + } + } let mut tx = pool.begin().await?; let locked: bool = sqlx::query_scalar("SELECT pg_try_advisory_xact_lock(hashtextextended($1,7421))") diff --git a/templates/player/podcast_scripts.html b/templates/player/podcast_scripts.html index e4433ef..39d5f24 100644 --- a/templates/player/podcast_scripts.html +++ b/templates/player/podcast_scripts.html @@ -5,6 +5,8 @@ add: ['Add', 'Добавить'], mine: ['My podcasts', 'Мои подкасты'], public: ['Public podcasts', 'Публичные подкасты'], + results: ['Podcast search results','Результаты поиска подкастов'], + noMatches: ['No matching podcasts.','Подходящих подкастов нет.'], empty: ['Add an RSS or Atom feed.', 'Добавьте RSS или Atom ленту.'], check: ['Check episodes', 'Проверить выпуски'], checkAll: ['Check all', 'Проверить все'], @@ -13,6 +15,7 @@ remove: ['Unsubscribe', 'Отписаться'], subscribe: ['Subscribe', 'Подписаться'], play: ['Play', 'Слушать'], + open: ['Open', 'Открыть'], resume: ['Resume', 'Продолжить'], download: ['Save on server', 'Сохранить на сервере'], saved: ['Saved on server', 'Сохранено на сервере'], @@ -28,7 +31,7 @@ description: ['Description', 'Описание'], noEpisodes: ['No episodes in this filter.', 'В этом фильтре нет выпусков.'], noPublic: ['No public podcasts yet.', 'Публичных подкастов пока нет.'], - searchNetwork: ['Search Frid podcasts', 'Поиск подкастов в Frid'], + searchNetwork: ['Search podcasts', 'Поиск подкастов'], search: ['Search', 'Найти'], working: ['Working…', 'Выполняется…'], done: ['Done', 'Готово'], @@ -58,6 +61,7 @@ mine: [], public: [], hits: [], + searched: false, episodes: [], hasMore: false, selected: null, @@ -105,9 +109,11 @@ this.public = data.public; }, enter() { + if (location.hash !== '#podcasts') + Alpine.store('library')._beginNavigation('#podcasts'); Alpine.store('library').view = 'podcasts'; Alpine.store('mobile')?.closeLibrary(); - this.task(async () => { + const loading = this.task(async () => { await this.load(); this.message = ''; }); @@ -116,6 +122,7 @@ if (Alpine.store('library').view === 'podcasts' && !this.busy) this.update().catch(() => {}); }, 5000); + return loading; }, async update() { await this.load(); @@ -133,11 +140,7 @@ const old = this.episodes; this.episodes = append ? [...old, ...data.episodes] - : [ - ...new Map( - [...data.episodes, ...old].map((e) => [e.id, e]), - ).values(), - ]; + : [...new Map([...data.episodes, ...old].map((e) => [e.id, e])).values()]; if (append || old.length <= 200) this.hasMore = !!data.has_more; }, more() { @@ -157,6 +160,21 @@ this.message = ''; }); }, + openResult(pod) { + return this.task(async () => { + if (!pod.subscribed) await this.api('add', { url: pod.url }); + await this.load(); + this.selected = this.mine.find((p) => p.id === pod.id) || pod; + this.episodes = []; + this.hasMore = false; + this.filter = ''; + this.season = ''; + Alpine.store('library')._beginNavigation('#podcasts'); + Alpine.store('library').view = 'podcasts'; + await this.loadEpisodes(); + this.message = ''; + }); + }, add(url) { this.task(async () => { const result = await this.api('add', { url: url || this.url }); @@ -232,14 +250,18 @@ }); }, search() { - this.task(async () => { - const data = await this.api('search', { query: this.query }); + const query=this.query.trim(); + return this.task(async () => { + const data = await this.api('search', { query }); + if(this.query.trim()!==query){this.message='';return;} this.hits = data.hits; + this.searched=true; this.message = this.text('done'); }); }, + resetSearch(){this.searched=false;this.hits=[];}, publicCards() { - return [...new Map([...this.public, ...this.hits].map((p) => [p.id, p])).values()]; + return [...new Map((this.searched?this.hits:this.public).map((p) => [p.id, p])).values()]; }, seasons() { return [ diff --git a/templates/player/podcasts.html b/templates/player/podcasts.html index 2befaba..496257e 100644 --- a/templates/player/podcasts.html +++ b/templates/player/podcasts.html @@ -1,7 +1,7 @@ -

+

{ if (view === 'artists' && !id) { if (this.view !== 'artists' || this.artistsPage === 0) this.goArtists(options); else if (options.restoreScroll) this._restoreScrollPosition(hash); + } else if (view === 'podcasts') { + Alpine.store('podcasts').enter(); } else if (view === 'uploads' && !id) { if (this.view !== 'my_uploads' || this.artistsPage === 0) this.goMyUploads(options); else if (options.restoreScroll) this._restoreScrollPosition(hash); diff --git a/templates/player/shell.html b/templates/player/shell.html index d8fc1ec..f1608bd 100644 --- a/templates/player/shell.html +++ b/templates/player/shell.html @@ -440,13 +440,27 @@