555 lines
20 KiB
Rust
555 lines
20 KiB
Rust
//! Public podcasts in the federated search against PostgreSQL: the search
|
|
//! events (music unchanged, podcast hits added), the "already subscribed"
|
|
//! marker and subscribing from a card with the card's id.
|
|
//!
|
|
//! Ignored by default, like the other store tests: set
|
|
//! `FURU_DEVICE_SYNC_TEST_DATABASE_URL` to a disposable database. Feeds come
|
|
//! from a local test HTTP server.
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
use music_dht::device_sync::{SyncOpPayload, SyncOpWire, random_hex};
|
|
use music_dht::podcast::{FeedUrl, PodcastId, PodcastMeta};
|
|
use music_dht::podcast_dht::{PodcastCard, PodcastCardSpec, PodcastSearchHit};
|
|
use music_dht::{ItemId, ItemKind, LibraryItem, NetworkId, SecretKey, normalize_name};
|
|
use serde_json::{Value, json};
|
|
|
|
use super::api;
|
|
use super::postgres_tests::{cleanup, local_fetcher, random_user, test_pool};
|
|
use super::test_server::{Route, TestServer};
|
|
use super::*;
|
|
use crate::federation::client::{music_search_events, podcast_events};
|
|
|
|
fn url(value: &str) -> FeedUrl {
|
|
FeedUrl::parse(value).unwrap()
|
|
}
|
|
|
|
/// A search hit as `PodcastDht::search` returns it, for the origin URL
|
|
/// `feed` (published by one device).
|
|
fn hit(feed: &str, title: &str) -> PodcastSearchHit {
|
|
let feed_url = url(feed);
|
|
let podcast_id = PodcastId::from_feed_url(&feed_url);
|
|
let publisher = SecretKey::from_bytes(&[9; 32]).public();
|
|
let card = PodcastCard::from_spec(
|
|
PodcastCardSpec {
|
|
podcast_id: podcast_id.clone(),
|
|
feed_url: feed_url.clone(),
|
|
id_feed_url: None,
|
|
title: title.into(),
|
|
author: Some("Host".into()),
|
|
description: Some("<b>Not</b> markup".into()),
|
|
image_url: Some("https://cdn.example/art.jpg".into()),
|
|
link: Some("https://show.example/".into()),
|
|
language: Some("en".into()),
|
|
categories: vec!["Technology".into()],
|
|
episode_count: Some(12),
|
|
latest_episode_at_ms: Some(1_700_000_000_000),
|
|
},
|
|
NetworkId::from_name("search-tests"),
|
|
publisher,
|
|
1,
|
|
1_700_000_000_000,
|
|
);
|
|
PodcastSearchHit {
|
|
podcast_id,
|
|
feed_url,
|
|
card,
|
|
publishers: vec![publisher],
|
|
claimed_feed_urls: Vec::new(),
|
|
}
|
|
}
|
|
|
|
fn item(kind: ItemKind, name: &str, content_id: Option<&str>) -> LibraryItem {
|
|
LibraryItem {
|
|
id: ItemId::from_bytes(*blake3::hash(name.as_bytes()).as_bytes()),
|
|
owner: SecretKey::from_bytes(&[5; 32]).public(),
|
|
kind,
|
|
name: name.into(),
|
|
normalized_name: normalize_name(name),
|
|
artist_names: vec!["Night Owls".into()],
|
|
featured_artist_names: Vec::new(),
|
|
year: Some(2024),
|
|
release_type: None,
|
|
release_title: (kind == ItemKind::Track).then(|| "Night Radio".into()),
|
|
track_number: Some(1),
|
|
disc_number: None,
|
|
duration_seconds: Some(180.0),
|
|
content_id: content_id.map(str::to_string),
|
|
revision: 1,
|
|
deleted: false,
|
|
updated_at_ms: 1,
|
|
}
|
|
}
|
|
|
|
fn keys(value: &Value) -> BTreeSet<String> {
|
|
value.as_object().unwrap().keys().cloned().collect()
|
|
}
|
|
|
|
fn set(names: &[&str]) -> BTreeSet<String> {
|
|
names.iter().map(|name| name.to_string()).collect()
|
|
}
|
|
|
|
fn meta(feed: &str, title: &str) -> PodcastMeta {
|
|
PodcastMeta {
|
|
feed_url: url(feed),
|
|
title: title.into(),
|
|
author: None,
|
|
description: None,
|
|
link: None,
|
|
image_url: None,
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
|
async fn postgres_federated_search_keeps_music_events_and_adds_podcast_hits() {
|
|
let pool = test_pool().await;
|
|
let store = PodcastStore::new(pool.clone());
|
|
let (user, other) = (random_user(), random_user());
|
|
cleanup(&pool, [user, other]).await;
|
|
let tag = random_hex(6);
|
|
let content_id = format!("b3:{}", blake3::hash(tag.as_bytes()).to_hex());
|
|
|
|
// Music: the contract of the existing events is unchanged; only the
|
|
// `type` marker is added.
|
|
let music = vec![
|
|
item(ItemKind::Track, "Night Radio Theme", Some(&content_id)),
|
|
item(ItemKind::Artist, "Night Owls", None),
|
|
item(ItemKind::Release, "Night Radio", None),
|
|
];
|
|
let own = SecretKey::from_bytes(&[1; 32]).public();
|
|
let music_events = music_search_events(&pool, "search-1", &music, own)
|
|
.await
|
|
.unwrap();
|
|
let music_json: Vec<Value> = music_events
|
|
.iter()
|
|
.map(|event| serde_json::to_value(event).unwrap())
|
|
.collect();
|
|
let kinds: Vec<&str> = music_json
|
|
.iter()
|
|
.map(|event| event["kind"].as_str().unwrap())
|
|
.collect();
|
|
assert_eq!(
|
|
kinds,
|
|
vec![
|
|
"federation.track",
|
|
"federation.artist",
|
|
"federation.release"
|
|
]
|
|
);
|
|
for (index, event) in music_json.iter().enumerate() {
|
|
assert_eq!(
|
|
keys(event),
|
|
set(&[
|
|
"search_id",
|
|
"sequence",
|
|
"kind",
|
|
"type",
|
|
"peer",
|
|
"entity_key",
|
|
"entity"
|
|
])
|
|
);
|
|
assert_eq!(event["type"], "music");
|
|
assert_eq!(event["search_id"], "search-1");
|
|
assert_eq!(event["sequence"], index as u64 + 1);
|
|
}
|
|
let track = &music_json[0];
|
|
assert_eq!(track["entity_key"], json!({ "content_id": content_id }));
|
|
assert_eq!(
|
|
keys(&track["entity"]),
|
|
set(&["key", "metadata", "availability"])
|
|
);
|
|
assert_eq!(track["entity"]["metadata"]["title"], "Night Radio Theme");
|
|
assert_eq!(track["entity"]["availability"]["state"], "federated");
|
|
assert_eq!(track["peer"], music[0].owner.to_string());
|
|
assert_eq!(
|
|
keys(&music_json[1]["entity"]),
|
|
set(&["key", "name", "image_url", "peers"])
|
|
);
|
|
assert_eq!(
|
|
keys(&music_json[2]["entity"]),
|
|
set(&["key", "title", "artists", "year", "cover_url", "sources"])
|
|
);
|
|
|
|
// The account has podcast A, and podcast C through an alias.
|
|
let feed_a = format!("https://podcasts.example/a-{tag}.xml");
|
|
let feed_b = format!("https://podcasts.example/b-{tag}.xml");
|
|
let feed_c = format!("https://podcasts.example/c-{tag}.xml");
|
|
let alias_c = format!("https://moved.example/c-{tag}.xml");
|
|
let id_a = store
|
|
.subscribe(user, &meta(&feed_a, "Night Radio"))
|
|
.await
|
|
.unwrap()
|
|
.podcast_id;
|
|
let id_c = store
|
|
.subscribe(user, &meta(&feed_c, "Cellar Talks"))
|
|
.await
|
|
.unwrap()
|
|
.podcast_id;
|
|
let aliases = SyncOpWire {
|
|
op_id: format!("tui-{tag}:1"),
|
|
origin_device_id: format!("tui-{tag}"),
|
|
seq: 1,
|
|
hlc_ms: 9_000_000_000_000,
|
|
payload: SyncOpPayload::PodcastFeedAliasesAdded {
|
|
podcast_id: id_c.clone(),
|
|
feed_aliases: vec![url(&feed_c), url(&alias_c)],
|
|
},
|
|
};
|
|
store.apply_remote_op(user, &aliases).await.unwrap();
|
|
|
|
// Someone published a card for the alias URL: another podcast id, but
|
|
// the account already has it.
|
|
let hits = vec![
|
|
hit(&feed_b, "Night Radio Stories"),
|
|
hit(&alias_c, "Night Radio Cellar"),
|
|
hit(&feed_a, "Night Radio"),
|
|
];
|
|
let first = music_events.len() as u64 + 1;
|
|
let events = podcast_events(
|
|
&pool,
|
|
user,
|
|
"search-1",
|
|
"night radio",
|
|
&music,
|
|
first,
|
|
hits.clone(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let json: Vec<Value> = events
|
|
.iter()
|
|
.map(|event| serde_json::to_value(event).unwrap())
|
|
.collect();
|
|
assert_eq!(json.len(), 3);
|
|
for (index, event) in json.iter().enumerate() {
|
|
assert_eq!(event["kind"], "federation.podcast");
|
|
assert_eq!(event["type"], "podcast");
|
|
assert_eq!(event["peer"], Value::Null);
|
|
assert_eq!(event["sequence"], first + index as u64);
|
|
assert_eq!(event["entity"]["type"], "podcast");
|
|
assert_eq!(event["entity"]["kind"], "podcast");
|
|
assert_eq!(
|
|
event["entity_key"],
|
|
json!({ "podcast_id": event["entity"]["podcast_id"] })
|
|
);
|
|
// The publisher's personal state never appears: no registers,
|
|
// devices or publisher keys, only the card's public projection.
|
|
assert_eq!(
|
|
keys(&event["entity"]),
|
|
set(&[
|
|
"type",
|
|
"kind",
|
|
"podcast_id",
|
|
"feed_url",
|
|
"title",
|
|
"author",
|
|
"description",
|
|
"image_url",
|
|
"link",
|
|
"language",
|
|
"categories",
|
|
"episode_count",
|
|
"latest_episode_at_ms",
|
|
"publisher_count",
|
|
"rank",
|
|
"exact_match",
|
|
"subscribed",
|
|
"local_podcast_id",
|
|
])
|
|
);
|
|
}
|
|
// The exact title leads (after the exact music release), then the rest
|
|
// in the overlay's order.
|
|
let entity = |index: usize| &json[index]["entity"];
|
|
assert_eq!(entity(0)["podcast_id"], id_a.to_string());
|
|
assert_eq!(entity(0)["exact_match"], true);
|
|
assert_eq!(entity(0)["rank"], 1);
|
|
assert_eq!(entity(0)["subscribed"], true);
|
|
assert_eq!(entity(0)["local_podcast_id"], id_a.to_string());
|
|
assert_eq!(entity(0)["feed_url"], feed_a);
|
|
assert_eq!(entity(0)["description"], "<b>Not</b> markup");
|
|
assert_eq!(entity(0)["publisher_count"], 1);
|
|
assert_eq!(entity(1)["podcast_id"], hits[0].podcast_id.to_string());
|
|
assert_eq!(entity(1)["subscribed"], false);
|
|
assert_eq!(entity(1)["local_podcast_id"], Value::Null);
|
|
assert_eq!(entity(2)["podcast_id"], hits[1].podcast_id.to_string());
|
|
assert_ne!(hits[1].podcast_id, id_c);
|
|
assert_eq!(entity(2)["subscribed"], true, "known through the alias");
|
|
assert_eq!(entity(2)["local_podcast_id"], id_c.to_string());
|
|
|
|
// The marker belongs to the searching account.
|
|
let theirs = podcast_events(&pool, other, "search-2", "night radio", &music, 1, hits)
|
|
.await
|
|
.unwrap();
|
|
assert!(
|
|
theirs
|
|
.iter()
|
|
.all(|event| event.entity["subscribed"] == false
|
|
&& event.entity["local_podcast_id"].is_null())
|
|
);
|
|
|
|
// Unsubscribed: known, but no longer marked as subscribed.
|
|
api::unsubscribe(&store, user, &id_a).await.unwrap();
|
|
let events = podcast_events(
|
|
&pool,
|
|
user,
|
|
"search-3",
|
|
"night radio",
|
|
&[],
|
|
1,
|
|
vec![hit(&feed_a, "Night Radio")],
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(events[0].entity["subscribed"], false);
|
|
assert_eq!(events[0].entity["local_podcast_id"], id_a.to_string());
|
|
assert_eq!(events[0].entity["rank"], 0);
|
|
|
|
sqlx::query("DELETE FROM furumusic__track_ref WHERE content_id = $1")
|
|
.bind(&content_id)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
cleanup(&pool, [user, other]).await;
|
|
}
|
|
|
|
fn rss(title: &str, items: &str) -> String {
|
|
format!(
|
|
r#"<?xml version="1.0" encoding="UTF-8"?>
|
|
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
|
|
<channel>
|
|
<title>{title}</title>
|
|
<link>https://show.example/</link>
|
|
<description>Found in the federation</description>
|
|
{items}
|
|
</channel>
|
|
</rss>"#
|
|
)
|
|
}
|
|
|
|
const ITEMS: &str = r#"
|
|
<item><title>Pilot</title><guid>s1e1</guid><itunes:season>1</itunes:season>
|
|
<itunes:episode>1</itunes:episode><pubDate>Mon, 01 Jan 2024 10:00:00 GMT</pubDate>
|
|
<enclosure url="/audio/s1e1.mp3" type="audio/mpeg" length="1000"/></item>
|
|
<item><title>Second</title><guid>s1e2</guid><itunes:season>1</itunes:season>
|
|
<itunes:episode>2</itunes:episode><pubDate>Mon, 08 Jan 2024 10:00:00 GMT</pubDate>
|
|
<enclosure url="/audio/s1e2.mp3" type="audio/mpeg" length="1000"/></item>
|
|
<item><title>Bonus</title><guid>bonus</guid>
|
|
<enclosure url="/audio/bonus.mp3" type="audio/mpeg" length="1000"/></item>"#;
|
|
|
|
async fn log_len(pool: &sqlx::PgPool, user: i64) -> i64 {
|
|
sqlx::query_scalar("SELECT COUNT(*) FROM furumusic__fed_sync_ops WHERE user_id = $1")
|
|
.bind(user)
|
|
.fetch_one(pool)
|
|
.await
|
|
.unwrap()
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
|
async fn postgres_subscribing_from_a_card_keeps_the_card_id() {
|
|
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();
|
|
server.set(
|
|
"/show.xml",
|
|
Route::new(rss("Found Show", ITEMS), "application/rss+xml"),
|
|
);
|
|
server.set("/old.xml", Route::redirect(301, "/new.xml"));
|
|
server.set(
|
|
"/new.xml",
|
|
Route::new(rss("Moved Show", ITEMS), "application/rss+xml"),
|
|
);
|
|
let fetcher = local_fetcher();
|
|
let now = super::now_ms();
|
|
let show = hit(&server.url("/show.xml"), "Found Show");
|
|
let body = |hit: &PodcastSearchHit| json!({ "podcast_id": hit.podcast_id.to_string(), "feed_url": hit.feed_url.to_string() });
|
|
|
|
// Invalid requests fetch nothing.
|
|
let other_id = PodcastId::from_feed_url(&url("https://podcasts.example/other.xml"));
|
|
for (request, status) in [
|
|
(json!({}), 400),
|
|
(
|
|
json!({ "podcast_id": "x", "feed_url": show.feed_url.to_string() }),
|
|
400,
|
|
),
|
|
(
|
|
json!({ "podcast_id": show.podcast_id.to_string(), "feed_url": "ftp://x/y" }),
|
|
400,
|
|
),
|
|
// A URL the id does not come from (a claimed move, another podcast).
|
|
(
|
|
json!({ "podcast_id": other_id.to_string(), "feed_url": show.feed_url.to_string() }),
|
|
422,
|
|
),
|
|
] {
|
|
let error = api::preview_found(&store, &fetcher, user, request.clone())
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(error.status, status, "{request}: {}", error.message);
|
|
let error = api::subscribe_found(&store, &fetcher, user, request.clone(), now)
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(error.status, status, "{request}: {}", error.message);
|
|
}
|
|
assert_eq!(server.requests("/show.xml").len(), 0);
|
|
// The production policy never reaches the local server.
|
|
let refused = api::preview_found(&store, &FeedFetcher::new().unwrap(), user, body(&show))
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(refused.status, 422, "{}", refused.message);
|
|
|
|
// The preview reads the feed and stores nothing.
|
|
let preview = api::preview_found(&store, &fetcher, user, body(&show))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(preview.title, "Found Show");
|
|
assert_eq!(preview.podcast_id, show.podcast_id.to_string());
|
|
assert_eq!(preview.episode_count, 3);
|
|
let seasons: Vec<_> = preview
|
|
.seasons
|
|
.iter()
|
|
.map(|season| (season.season, season.episode_count))
|
|
.collect();
|
|
assert_eq!(seasons, vec![(Some(1), 2), (None, 1)]);
|
|
assert_eq!(preview.episodes[0].title, "Second");
|
|
assert!(!preview.subscribed && !preview.moved);
|
|
assert_eq!(server.requests("/show.xml").len(), 1);
|
|
assert!(
|
|
store
|
|
.podcast(user, &show.podcast_id)
|
|
.await
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
assert_eq!(log_len(&pool, user).await, 0);
|
|
|
|
// Subscribing: the account's own private subscription with the card's id.
|
|
let subscribed = api::subscribe_found(&store, &fetcher, user, body(&show), now)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(subscribed.podcast.podcast_id, show.podcast_id.to_string());
|
|
assert!(!subscribed.already_subscribed);
|
|
assert_eq!(subscribed.new_episodes, 3);
|
|
assert!(subscribed.podcast.subscribed);
|
|
assert!(
|
|
!subscribed.podcast.shared,
|
|
"a subscription from a card is private"
|
|
);
|
|
assert_eq!(server.requests("/show.xml").len(), 2);
|
|
let marked = super::search::hit_views(&store, user, "found show", &[], vec![show.clone()])
|
|
.await
|
|
.unwrap();
|
|
assert!(marked[0].subscribed);
|
|
|
|
// Again: already subscribed, nothing fetched or written.
|
|
let log = log_len(&pool, user).await;
|
|
let again = api::subscribe_found(&store, &fetcher, user, body(&show), now)
|
|
.await
|
|
.unwrap();
|
|
assert!(again.already_subscribed);
|
|
assert_eq!(again.podcast.podcast_id, show.podcast_id.to_string());
|
|
assert_eq!(server.requests("/show.xml").len(), 2);
|
|
assert_eq!(log_len(&pool, user).await, log);
|
|
let preview = api::preview_found(&store, &fetcher, user, body(&show))
|
|
.await
|
|
.unwrap();
|
|
assert!(preview.subscribed);
|
|
assert_eq!(
|
|
preview.local_podcast_id.as_deref(),
|
|
Some(show.podcast_id.as_str())
|
|
);
|
|
|
|
// Another account subscribing from the same card gets its own podcast
|
|
// with the same id; the first account's state is untouched.
|
|
let theirs = api::subscribe_found(&store, &fetcher, other, body(&show), now)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(theirs.podcast.podcast_id, show.podcast_id.to_string());
|
|
assert!(!theirs.already_subscribed);
|
|
|
|
// A card whose origin URL moved permanently: the id stays the card's,
|
|
// the new URL becomes an alias and is fetched from now on.
|
|
let old = hit(&server.url("/old.xml"), "Moved Show");
|
|
let moved = api::subscribe_found(&store, &fetcher, user, body(&old), now)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(moved.podcast.podcast_id, old.podcast_id.to_string());
|
|
assert_eq!(moved.podcast.feed_url, server.url("/new.xml"));
|
|
let aliases = store.feed_aliases(user, &old.podcast_id).await.unwrap();
|
|
assert!(aliases.contains(&url(&server.url("/new.xml"))));
|
|
// Someone's card for the new URL is the same podcast for this account.
|
|
let new = hit(&server.url("/new.xml"), "Moved Show");
|
|
assert_ne!(new.podcast_id, old.podcast_id);
|
|
let requests = server.requests("/new.xml").len();
|
|
let again = api::subscribe_found(&store, &fetcher, user, body(&new), now)
|
|
.await
|
|
.unwrap();
|
|
assert!(again.already_subscribed);
|
|
assert_eq!(again.podcast.podcast_id, old.podcast_id.to_string());
|
|
assert_eq!(server.requests("/new.xml").len(), requests);
|
|
assert!(
|
|
store
|
|
.podcast(user, &new.podcast_id)
|
|
.await
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
|
|
// 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;
|
|
}
|