diff --git a/docs/development.md b/docs/development.md index 3d556d0..4dfae0e 100644 --- a/docs/development.md +++ b/docs/development.md @@ -305,8 +305,9 @@ Search and subscription from a card (`src/podcasts/search.rs`): fetches the origin URL, then `runner::subscribe_fetched` writes a private subscription with the card's id, or the account's existing podcast for a known URL of `runner::document_urls` (the origin, the end - of a permanent redirect chain, the feed's `itunes:new-feed-url`; - `search::known_podcast`; also used by "add RSS"). The document is + of a permanent redirect chain, the feed's `itunes:new-feed-url`; a + subscribed podcast first, `search::known_podcast`; also used by "add + RSS"). The document is ingested only into a new podcast or when it came from the existing podcast's own fetch URL (requested there, or a permanent redirect ended there); otherwise the podcast is subscribed again with its stored diff --git a/src/podcasts/search.rs b/src/podcasts/search.rs index c9568c8..76d42aa 100644 --- a/src/podcasts/search.rs +++ b/src/podcasts/search.rs @@ -113,25 +113,35 @@ pub(crate) fn ranked_hits( /// the podcast one of `feed_urls` resolves to through the account's known /// feed URLs and aliases (the card's origin URL; after a fetch also where /// the feed moved, see `runner::document_urls`). +/// +/// A podcast the account is subscribed to comes first, so an old, +/// unsubscribed podcast of the same feed (followed once under its former +/// address) never hides the current subscription; otherwise the first +/// known one, in the order id, then `feed_urls`. pub(crate) async fn known_podcast( store: &PodcastStore, user_id: i64, podcast_id: &PodcastId, feed_urls: &[FeedUrl], ) -> Result> { - if let Some(summary) = store.podcast(user_id, podcast_id).await? { - return Ok(Some(summary)); - } + let mut ids = vec![podcast_id.clone()]; for feed_url in feed_urls { let resolved = store.resolve_podcast_id(user_id, feed_url).await?; - if resolved == *podcast_id { - continue; - } - if let Some(summary) = store.podcast(user_id, &resolved).await? { - return Ok(Some(summary)); + if !ids.contains(&resolved) { + ids.push(resolved); } } - Ok(None) + let mut first = None; + for id in ids { + let Some(summary) = store.podcast(user_id, &id).await? else { + continue; + }; + if summary.subscribed { + return Ok(Some(summary)); + } + first.get_or_insert(summary); + } + Ok(first) } /// Views of the podcast hits of one search for `user_id`, in rank order. diff --git a/src/podcasts/search_tests.rs b/src/podcasts/search_tests.rs index f40c898..249f2bc 100644 --- a/src/podcasts/search_tests.rs +++ b/src/podcasts/search_tests.rs @@ -724,3 +724,75 @@ async fn postgres_a_feed_claiming_a_followed_address_never_rewrites_that_podcast cleanup(&pool, [user]).await; } + +#[tokio::test] +#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"] +async fn postgres_a_current_subscription_wins_over_an_old_unsubscribed_podcast() { + let pool = test_pool().await; + let store = PodcastStore::new(pool.clone()); + let user = random_user(); + cleanup(&pool, [user]).await; + let server = TestServer::start(); + server.set( + "/old.xml", + Route::new(rss("Moving Show", ITEMS), "application/rss+xml"), + ); + server.set( + "/new.xml", + Route::new(rss("Moving Show", ITEMS), "application/rss+xml"), + ); + let fetcher = local_fetcher(); + let now = super::now_ms(); + let old = hit(&server.url("/old.xml"), "Moving Show"); + let new_id = PodcastId::from_feed_url(&url(&server.url("/new.xml"))); + + // Followed once under the old address, then unsubscribed; the feed + // moved, and the account follows it under the new address only. + runner::subscribe_url(&store, &fetcher, user, &old.feed_url, now) + .await + .unwrap(); + store.unsubscribe(user, &old.podcast_id).await.unwrap(); + server.set("/old.xml", Route::redirect(301, "/new.xml")); + let current = runner::subscribe_url(&store, &fetcher, user, &url(&server.url("/new.xml")), now) + .await + .unwrap(); + assert_eq!(current.podcast_id, new_id); + assert!(!current.already_subscribed); + + let known = super::search::known_podcast( + &store, + user, + &old.podcast_id, + &[old.feed_url.clone(), url(&server.url("/new.xml"))], + ) + .await + .unwrap() + .unwrap(); + assert_eq!(known.podcast_id, new_id); + assert!(known.subscribed); + + let body = + json!({ "podcast_id": old.podcast_id.to_string(), "feed_url": old.feed_url.to_string() }); + let preview = api::preview_found(&store, &fetcher, user, body.clone()) + .await + .unwrap(); + assert!(preview.subscribed); + assert_eq!(preview.local_podcast_id.as_deref(), Some(new_id.as_str())); + let log = log_len(&pool, user).await; + let found = api::subscribe_found(&store, &fetcher, user, body, now) + .await + .unwrap(); + assert!(found.already_subscribed); + assert_eq!(found.podcast.podcast_id, new_id.to_string()); + let added = runner::subscribe_url(&store, &fetcher, user, &old.feed_url, now) + .await + .unwrap(); + assert!(added.already_subscribed); + assert_eq!(added.podcast_id, new_id); + assert_eq!(log_len(&pool, user).await, log); + assert_eq!(active_subscriptions(&pool, user).await, 1); + let old_podcast = store.podcast(user, &old.podcast_id).await.unwrap().unwrap(); + assert!(!old_podcast.subscribed); + + cleanup(&pool, [user]).await; +}