Recognize a found podcast the account follows at its new address
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3a1c50e78e
commit
678c7b8211
4 files changed
+173
-49
No files matched your search
+31
-24
@@ -29,7 +29,7 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::discovery::Discovery;
|
||||
use super::fetch::{FeedFetch, FeedFetcher, FetchErrorKind};
|
||||
use super::fetch::{FeedFetcher, FetchErrorKind};
|
||||
use super::runner::{self, SubscribeUrlError};
|
||||
use super::search;
|
||||
use super::{
|
||||
@@ -727,7 +727,9 @@ fn found_podcast(body: Value) -> ApiResult<(PodcastId, FeedUrl)> {
|
||||
|
||||
/// Seasons and episodes of a found podcast before subscribing: `{
|
||||
/// "podcast_id", "feed_url" }` from the search hit. The feed is read
|
||||
/// through the SSRF-guarded fetcher and nothing is stored.
|
||||
/// through the SSRF-guarded fetcher and nothing is stored. `subscribed`
|
||||
/// also holds when the account follows the feed under the address it
|
||||
/// moved to (a permanent redirect or the feed's `itunes:new-feed-url`).
|
||||
pub(crate) async fn preview_found(
|
||||
store: &PodcastStore,
|
||||
fetcher: &FeedFetcher,
|
||||
@@ -735,17 +737,9 @@ pub(crate) async fn preview_found(
|
||||
body: Value,
|
||||
) -> ApiResult<search::PodcastPreview> {
|
||||
let (podcast_id, feed_url) = found_podcast(body)?;
|
||||
let document = match fetcher.fetch(&feed_url, &Default::default()).await {
|
||||
Ok(FeedFetch::Fetched(document)) => document,
|
||||
Ok(FeedFetch::NotModified) => {
|
||||
return Err(ApiError::new(
|
||||
502,
|
||||
"the feed could not be loaded: unexpected 304 Not Modified",
|
||||
));
|
||||
}
|
||||
Err(error) => return Err(anyhow::Error::from(SubscribeUrlError::Fetch(error)).into()),
|
||||
};
|
||||
let known = search::known_podcast(store, user_id, &podcast_id, &feed_url).await?;
|
||||
let document = runner::fetch_to_subscribe(fetcher, &feed_url).await?;
|
||||
let urls = runner::document_urls(&feed_url, &document);
|
||||
let known = search::known_podcast(store, user_id, &podcast_id, &urls).await?;
|
||||
Ok(search::preview(
|
||||
&podcast_id,
|
||||
&feed_url,
|
||||
@@ -759,11 +753,12 @@ pub(crate) async fn preview_found(
|
||||
/// search hit.
|
||||
///
|
||||
/// The subscription is the account's own and private, made like "add RSS"
|
||||
/// with the origin URL (fetch first, then `runner::subscribe_url`): its id
|
||||
/// is the card's id, or the account's existing podcast when the URL is a
|
||||
/// known feed URL or alias (progress and history continue). Redirects and
|
||||
/// `itunes:new-feed-url` of the feed itself become aliases as usual. When
|
||||
/// the account is already subscribed nothing is fetched or written.
|
||||
/// with the origin URL (fetch first, then `runner::subscribe_fetched`): its
|
||||
/// id is the card's id, or the account's existing podcast when the URL, or
|
||||
/// the address the feed moved to, is a known feed URL or alias (progress
|
||||
/// and history continue). Redirects and `itunes:new-feed-url` of the feed
|
||||
/// itself become aliases as usual. When the account is already subscribed
|
||||
/// (by id or a known URL, before or after the fetch) nothing is written.
|
||||
pub(crate) async fn subscribe_found(
|
||||
store: &PodcastStore,
|
||||
fetcher: &FeedFetcher,
|
||||
@@ -772,16 +767,28 @@ pub(crate) async fn subscribe_found(
|
||||
now_ms: i64,
|
||||
) -> ApiResult<SubscribeResponse> {
|
||||
let (podcast_id, feed_url) = found_podcast(body)?;
|
||||
if let Some(known) = search::known_podcast(store, user_id, &podcast_id, &feed_url).await?
|
||||
&& known.subscribed
|
||||
{
|
||||
return Ok(SubscribeResponse {
|
||||
let already = async |known: PodcastSummary| -> ApiResult<SubscribeResponse> {
|
||||
Ok(SubscribeResponse {
|
||||
podcast: podcast_view(store, user_id, &known.podcast_id).await?,
|
||||
already_subscribed: true,
|
||||
new_episodes: 0,
|
||||
});
|
||||
})
|
||||
};
|
||||
if let Some(known) =
|
||||
search::known_podcast(store, user_id, &podcast_id, std::slice::from_ref(&feed_url)).await?
|
||||
&& known.subscribed
|
||||
{
|
||||
return already(known).await;
|
||||
}
|
||||
let subscription = runner::subscribe_url(store, fetcher, user_id, &feed_url, now_ms).await?;
|
||||
let document = runner::fetch_to_subscribe(fetcher, &feed_url).await?;
|
||||
let urls = runner::document_urls(&feed_url, &document);
|
||||
if let Some(known) = search::known_podcast(store, user_id, &podcast_id, &urls).await?
|
||||
&& known.subscribed
|
||||
{
|
||||
return already(known).await;
|
||||
}
|
||||
let subscription =
|
||||
runner::subscribe_fetched(store, user_id, &feed_url, &document, now_ms).await?;
|
||||
Ok(SubscribeResponse {
|
||||
podcast: podcast_view(store, user_id, &subscription.podcast_id).await?,
|
||||
already_subscribed: subscription.already_subscribed,
|
||||
|
||||
+71
-16
@@ -440,14 +440,10 @@ 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.
|
||||
/// The feed is fetched first ([`fetch_to_subscribe`]): an address that is
|
||||
/// not public, a document that is not a feed or a failed request
|
||||
/// subscribes nothing ([`SubscribeUrlError`] inside the error). Then
|
||||
/// [`subscribe_fetched`] writes the subscription.
|
||||
pub(crate) async fn subscribe_url(
|
||||
store: &PodcastStore,
|
||||
fetcher: &FeedFetcher,
|
||||
@@ -455,28 +451,87 @@ pub(crate) async fn subscribe_url(
|
||||
url: &FeedUrl,
|
||||
now_ms: i64,
|
||||
) -> Result<UrlSubscription> {
|
||||
let document = match fetcher.fetch(url, &Validators::default()).await {
|
||||
Ok(FeedFetch::Fetched(document)) => document,
|
||||
let document = fetch_to_subscribe(fetcher, url).await?;
|
||||
subscribe_fetched(store, user_id, url, &document, now_ms).await
|
||||
}
|
||||
|
||||
/// Fetches the feed at `url` unconditionally for a new subscription or a
|
||||
/// preview; a failure is a [`SubscribeUrlError`].
|
||||
pub(crate) async fn fetch_to_subscribe(
|
||||
fetcher: &FeedFetcher,
|
||||
url: &FeedUrl,
|
||||
) -> Result<Box<FetchedFeed>> {
|
||||
match fetcher.fetch(url, &Validators::default()).await {
|
||||
Ok(FeedFetch::Fetched(document)) => Ok(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 {
|
||||
Err(SubscribeUrlError::Fetch(FetchError {
|
||||
kind: super::fetch::FetchErrorKind::Status(304),
|
||||
message: "unexpected 304 Not Modified".to_string(),
|
||||
retry_after: None,
|
||||
})
|
||||
.into());
|
||||
.into())
|
||||
}
|
||||
Err(error) => return Err(SubscribeUrlError::Fetch(error).into()),
|
||||
};
|
||||
Err(error) => Err(SubscribeUrlError::Fetch(error).into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The URLs a document fetched from `requested` shows for the same
|
||||
/// podcast: `requested`, the end of a chain of permanent redirects and the
|
||||
/// feed's own `itunes:new-feed-url` (the same URLs a refresh learns as
|
||||
/// aliases), without duplicates.
|
||||
pub(crate) fn document_urls(requested: &FeedUrl, document: &FetchedFeed) -> Vec<FeedUrl> {
|
||||
let mut urls = vec![requested.clone()];
|
||||
if document.moved_permanently {
|
||||
urls.push(document.fetched_from.clone());
|
||||
}
|
||||
if let Some(new_url) = document
|
||||
.feed
|
||||
.new_feed_url
|
||||
.as_deref()
|
||||
.and_then(|url| FeedUrl::parse(url).ok())
|
||||
{
|
||||
urls.push(new_url);
|
||||
}
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
urls.retain(|url| seen.insert(url.clone()));
|
||||
urls
|
||||
}
|
||||
|
||||
/// Writes the subscription for a document fetched from `url` and ingests
|
||||
/// it like a scheduled refresh (which also stores the validators and the
|
||||
/// next check).
|
||||
///
|
||||
/// The podcast is the account's podcast for the first of
|
||||
/// [`document_urls`] it knows (its id, a known feed URL or alias), so a
|
||||
/// feed the account already follows under its newer address is not
|
||||
/// subscribed a second time; otherwise a new podcast with the id of `url`.
|
||||
/// [`PodcastStore::subscribe`] reuses the existing podcast with its
|
||||
/// progress and history, and writes nothing for an active subscription.
|
||||
pub(crate) async fn subscribe_fetched(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
url: &FeedUrl,
|
||||
document: &FetchedFeed,
|
||||
now_ms: i64,
|
||||
) -> Result<UrlSubscription> {
|
||||
let mut subscribe_as = url.clone();
|
||||
for candidate in document_urls(url, document) {
|
||||
let podcast_id = store.resolve_podcast_id(user_id, &candidate).await?;
|
||||
if store.podcast(user_id, &podcast_id).await?.is_some() {
|
||||
subscribe_as = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let subscription = store
|
||||
.subscribe(user_id, &document.feed.to_podcast_meta(url.clone()))
|
||||
.subscribe(user_id, &document.feed.to_podcast_meta(subscribe_as))
|
||||
.await?;
|
||||
let target = RefreshTarget {
|
||||
user_id,
|
||||
podcast_id: subscription.podcast_id.clone(),
|
||||
};
|
||||
let refresh = ingest(store, &target, &document, now_ms).await?;
|
||||
let refresh = ingest(store, &target, document, now_ms).await?;
|
||||
Ok(UrlSubscription {
|
||||
podcast_id: subscription.podcast_id,
|
||||
already_subscribed: subscription.already_subscribed,
|
||||
|
||||
+20
-8
@@ -110,22 +110,28 @@ pub(crate) fn ranked_hits(
|
||||
}
|
||||
|
||||
/// The account's podcast for a found podcast: the podcast with that id, or
|
||||
/// the podcast its origin URL resolves to through the account's known feed
|
||||
/// URLs and aliases.
|
||||
/// 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`).
|
||||
pub(crate) async fn known_podcast(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
feed_url: &FeedUrl,
|
||||
feed_urls: &[FeedUrl],
|
||||
) -> Result<Option<PodcastSummary>> {
|
||||
if let Some(summary) = store.podcast(user_id, podcast_id).await? {
|
||||
return Ok(Some(summary));
|
||||
}
|
||||
let resolved = store.resolve_podcast_id(user_id, feed_url).await?;
|
||||
if resolved == *podcast_id {
|
||||
return Ok(None);
|
||||
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));
|
||||
}
|
||||
}
|
||||
store.podcast(user_id, &resolved).await
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Views of the podcast hits of one search for `user_id`, in rank order.
|
||||
@@ -138,7 +144,13 @@ pub(crate) async fn hit_views(
|
||||
) -> Result<Vec<PodcastHitView>> {
|
||||
let mut views = Vec::new();
|
||||
for (rank, exact_match, hit) in ranked_hits(query, music, podcasts) {
|
||||
let known = known_podcast(store, user_id, &hit.podcast_id, &hit.feed_url).await?;
|
||||
let known = known_podcast(
|
||||
store,
|
||||
user_id,
|
||||
&hit.podcast_id,
|
||||
std::slice::from_ref(&hit.feed_url),
|
||||
)
|
||||
.await?;
|
||||
let card = hit.card;
|
||||
views.push(PodcastHitView {
|
||||
hit_type: "podcast",
|
||||
|
||||
@@ -500,5 +500,55 @@ async fn postgres_subscribing_from_a_card_keeps_the_card_id() {
|
||||
.is_none()
|
||||
);
|
||||
|
||||
cleanup(&pool, [user, other]).await;
|
||||
// The reverse: an account that follows the feed only under its new
|
||||
// address finds the card of the old one. The preview fetches the old
|
||||
// URL, sees the move and marks it as already subscribed; subscribing
|
||||
// writes nothing and creates no second podcast (nor does "add RSS").
|
||||
let third = random_user();
|
||||
let direct = runner::subscribe_url(&store, &fetcher, third, &url(&server.url("/new.xml")), now)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(direct.podcast_id, new.podcast_id);
|
||||
let marked = super::search::hit_views(&store, third, "moved show", &[], vec![old.clone()])
|
||||
.await
|
||||
.unwrap();
|
||||
// The search list cannot know without fetching the feed.
|
||||
assert!(!marked[0].subscribed);
|
||||
let preview = api::preview_found(&store, &fetcher, third, body(&old))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(preview.moved && preview.subscribed);
|
||||
assert_eq!(
|
||||
preview.local_podcast_id.as_deref(),
|
||||
Some(new.podcast_id.as_str())
|
||||
);
|
||||
let log = log_len(&pool, third).await;
|
||||
let again = api::subscribe_found(&store, &fetcher, third, body(&old), now)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(again.already_subscribed);
|
||||
assert_eq!(again.podcast.podcast_id, new.podcast_id.to_string());
|
||||
let added = runner::subscribe_url(&store, &fetcher, third, &url(&server.url("/old.xml")), now)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(added.already_subscribed);
|
||||
assert_eq!(added.podcast_id, new.podcast_id);
|
||||
assert_eq!(log_len(&pool, third).await, log);
|
||||
assert!(
|
||||
store
|
||||
.podcast(third, &old.podcast_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
let subscriptions: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM furumusic__podcast_subscription WHERE user_id = $1",
|
||||
)
|
||||
.bind(third)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(subscriptions, 1);
|
||||
|
||||
cleanup(&pool, [user, other, third]).await;
|
||||
}
|
||||
Reference in new issue
Block a user