forked from Furumi/furumi-web
Compare commits
27
Commits
No files matched your search
@@ -1,5 +1,52 @@
|
||||
# Changelog
|
||||
|
||||
## v0.15.0 — 2026-11-19
|
||||
|
||||
- Find podcasts in the federated search: cards shared by federation members
|
||||
appear in the search results as **Podcasts**, typed apart from music
|
||||
(`federation.podcast` events, `"type": "podcast"`; music events are
|
||||
unchanged and now carry `"type": "music"`), with **Already subscribed**
|
||||
when you follow the podcast, also under another feed address. A card
|
||||
opens with its metadata and the seasons and episodes read from the feed,
|
||||
browsable by season (`POST /api/podcasts/discovery/preview`), and
|
||||
**Subscribe**
|
||||
(`POST /api/podcasts/discovery/subscribe`) creates your own private
|
||||
subscription from the card's original feed address with the card's
|
||||
podcast id, or your existing podcast when you already follow the feed
|
||||
under the address it moved to (also for **Add RSS**); the publishing
|
||||
member is never contacted. A failed podcast search keeps the music
|
||||
results and says so. Card texts and
|
||||
links are untrusted: shown as text, links and artwork only as checked
|
||||
`http(s)` (artwork `https`) addresses.
|
||||
- Share a podcast in the federation: a **Share in the federation** toggle on
|
||||
the podcast page publishes a public card of the podcast (title, author,
|
||||
description, artwork, feed URL, episode count; never progress, history,
|
||||
devices or the account) through the server's federation node on the
|
||||
separate `podcast_dht` overlay of `music-dht` 0.6. Sharing is the
|
||||
account's synchronized decision; turning it off or unsubscribing on any
|
||||
device revokes it, and a late state of another device cannot publish it
|
||||
again. After a restart the server publishes only after its first
|
||||
device-sync round, so sharing turned off on a TUI while the server was
|
||||
stopped is not announced again. A feed URL that looks private (token or key in the address, a
|
||||
member-feed host) needs a confirmation of the exact address first; the
|
||||
confirmation is local to the server (`m_0058`,
|
||||
`furumusic__podcast_share_confirmation`) and asked again for a new share
|
||||
decision or a moved feed. Accounts sharing the same podcast share one
|
||||
card, published while any of them shares it; at most 192 podcasts per
|
||||
server, shared fairly between the sharing accounts (an account within
|
||||
its part never loses a published card to other accounts). The podcast
|
||||
page says whether this server published the card, waits for a
|
||||
confirmation, holds it back at the limit, or cannot publish because
|
||||
federation is off or discovery is unavailable; the list marks
|
||||
**Sharing on**. New endpoint `POST /api/podcasts/feeds/{id}/share`
|
||||
(`{"shared": bool, "confirm"?: {...}}`, 409 when a confirmation is
|
||||
needed); `GET /api/podcasts/feeds/{id}` reports the share state.
|
||||
- The federation node serves the podcast-discovery overlay
|
||||
(`furumi-fd/podcast-dht/1`, cache `podcast-dht.sqlite3` in the federation
|
||||
data directory), advertises `podcast_dht` in its capability manifest only
|
||||
while it runs, and probes peers' manifests so the overlay skips peers
|
||||
without it. If the overlay cannot start, only discovery is disabled.
|
||||
|
||||
## v0.14.0 — 2026-11-19
|
||||
|
||||
- Podcasts in the web player: a Podcasts section to add RSS feeds, browse
|
||||
|
||||
Generated
+3
-3
@@ -1843,7 +1843,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "federation-net"
|
||||
version = "0.3.1"
|
||||
source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fcontract#65d6ca2f14a1324dc129b90694da83ceb55c60dc"
|
||||
source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fdiscovery#9c82f6d91691a66d0a559caba06e13a1293f06be"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"data-encoding",
|
||||
@@ -1987,7 +1987,7 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
|
||||
|
||||
[[package]]
|
||||
name = "furumusic"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-stream",
|
||||
@@ -3900,7 +3900,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "music-dht"
|
||||
version = "0.6.0"
|
||||
source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fcontract#65d6ca2f14a1324dc129b90694da83ceb55c60dc"
|
||||
source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fdiscovery#9c82f6d91691a66d0a559caba06e13a1293f06be"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"blake3",
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "furumusic"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
edition = "2024"
|
||||
description = "Reusable web-app boilerplate: auth, OIDC/SSO, admin panel, user management, i18n, PostgreSQL"
|
||||
|
||||
@@ -61,5 +61,5 @@ librqbit = { version = "8.1.1", features = ["disable-upload"] }
|
||||
music-dht = { version = "0.6.0", features = ["podcast-feed"] }
|
||||
|
||||
[patch.crates-io]
|
||||
# Unpublished frid (MR-F1); remove once music-dht 0.6.0 is on crates.io.
|
||||
music-dht = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/contract" }
|
||||
# Unpublished frid (MR-F1, MR-F3); remove once music-dht 0.6.0 is on crates.io.
|
||||
music-dht = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/discovery" }
|
||||
@@ -23,7 +23,9 @@ the Furumi federated network.
|
||||
- **Follow podcasts.** Subscribe to RSS feeds, browse seasons and episodes, and
|
||||
pick up where you stopped, also on your other Furumi players. Episodes stream
|
||||
from the publisher and are never imported into the library; subscriptions
|
||||
and listening progress stay private to your own devices.
|
||||
and listening progress stay private to your own devices. Share a podcast
|
||||
in the federation on purpose, and find podcasts others shared in the
|
||||
federated search.
|
||||
- **Discover more.** Find similar tracks and connect Last.fm for scrobbling.
|
||||
- **Connect your players.** Pair trusted Furumi devices to synchronize playlists,
|
||||
likes, playback state, and playback controls.
|
||||
|
||||
+122
-5
@@ -112,11 +112,128 @@ for unplayed, started and played episodes, and the recent listening history.
|
||||
Privacy: a new subscription is private. Subscriptions, progress and listening
|
||||
history are synchronized only between your own paired Furumi players (trusted
|
||||
devices), never published to the federation, and Jam guests cannot see your
|
||||
episodes. Publishing a podcast for discovery is not available in the web
|
||||
player yet. Episodes are not stored on the server or shared with peers: the
|
||||
browser streams the audio and loads the artwork directly from the publisher,
|
||||
so the publisher sees your browser's address. With the audio proxy (below)
|
||||
the publisher sees the server instead.
|
||||
episodes. Only podcasts you share explicitly are published (see below).
|
||||
Episodes are not stored on the server or shared with peers: the browser
|
||||
streams the audio and loads the artwork directly from the publisher, so the
|
||||
publisher sees your browser's address. With the audio proxy (below) the
|
||||
publisher sees the server instead.
|
||||
|
||||
### Sharing a podcast in the federation
|
||||
|
||||
**Share in the federation** on a podcast page publishes a public card of the
|
||||
podcast so that members of the federation network outside your own devices
|
||||
can find it and subscribe. The card holds only the podcast's public
|
||||
metadata: title, author, description, artwork and site links, the feed URL,
|
||||
the number of episodes and the newest publication date. It never contains
|
||||
your progress, history, listening counts, device names or your account.
|
||||
**Stop sharing** or **Unsubscribe** revokes it; your progress is kept.
|
||||
|
||||
- Sharing is your decision and is synchronized with your other Furumi
|
||||
players, like the subscription. A podcast shared on another player is
|
||||
shared here too, and turning it off anywhere turns it off everywhere. A
|
||||
new subscription, including subscribing again, is always private.
|
||||
- If the feed URL looks like it may contain private data (an access token or
|
||||
key in the address, a host of personal subscriber feeds, a long random
|
||||
part), the web player shows a confirmation dialog with the exact address
|
||||
before publishing it. Such feeds are often personal: publishing one can
|
||||
let others use your access. The confirmation belongs to this server and to
|
||||
this one decision; it is asked again when you share again, when the feed
|
||||
moves to another address, and when sharing was turned on on another
|
||||
player.
|
||||
- The **server** publishes the card with its federation identity (the same
|
||||
node key that publishes its music library), not with an identity of your
|
||||
account. When several accounts of this server share the same podcast,
|
||||
the server publishes one card for it, built from one of these accounts'
|
||||
copies of the feed, and keeps it while any of them still shares it. The
|
||||
account whose copy the published card shows keeps providing it while it
|
||||
shares (also after a server restart, recognised by the card's text), so
|
||||
another account sharing the podcast later does not replace the card's
|
||||
text; a new card preferably uses a copy the server downloaded itself
|
||||
from the podcast's original address. If that account's copy changes
|
||||
while the server restarts, the card may be rebuilt from another sharing
|
||||
account's copy. Other members see that this server shares the
|
||||
podcast, not who. Podcasts shared by a deactivated account are no longer
|
||||
published (within five minutes).
|
||||
- The podcast page says what this server does with the card: published,
|
||||
publishing, waiting for your confirmation, held back by the server's
|
||||
limit, or not possible because federation is off or public discovery is
|
||||
unavailable. It shows only this server's publication: a Furumi player
|
||||
such as the terminal player publishes its own card with its own identity
|
||||
when you share there, so the card may be public through that player
|
||||
while this server still waits for your confirmation. The podcast list
|
||||
marks a podcast with **Sharing on** when your decision is to share it.
|
||||
- Publishing needs federation to be enabled on the server (Settings →
|
||||
Federation). Without it, the share decision is kept and the card is
|
||||
published once federation runs.
|
||||
- A server publishes at most 192 podcasts, shared fairly between the
|
||||
accounts that share podcasts: every account gets its turn in rotation,
|
||||
so an account whose shared podcasts fit in its part of the limit (192
|
||||
divided by the number of sharing accounts) never loses a published card
|
||||
because other accounts share many podcasts. When the limit is reached,
|
||||
only podcasts of accounts beyond their part wait (newest shares first;
|
||||
cards already published are kept before new ones), and the podcast page
|
||||
says that the card is held back. Members' caches keep at most 256
|
||||
records per server, including revocations of the last 24 hours, so a
|
||||
new card also waits while recent revocations fill that room, and no
|
||||
published card is given up for a card the federation would not store.
|
||||
- A podcast whose stored data cannot be read (for example after a damaged
|
||||
import) is treated as not shared, and its card is revoked; the other
|
||||
podcasts are still published and revoked as usual.
|
||||
- Revoking is eventually consistent: the server publishes a signed
|
||||
revocation at once, but a federation peer that missed it may show the old
|
||||
card for up to 12 hours. Subscriptions other members made from the card
|
||||
stay theirs.
|
||||
|
||||
The server joins the separate podcast-discovery overlay
|
||||
(`furumi-fd/podcast-dht/1`) of the federation network and keeps its routing
|
||||
contacts and the cards it holds for others in `podcast-dht.sqlite3` in the
|
||||
federation data directory (a cache that can be deleted while the server is
|
||||
stopped). If the overlay cannot start, the server log says "public podcast
|
||||
discovery disabled" and everything else keeps working. The admin
|
||||
federation status (`/admin/v2/api/federation`) reports `podcast_discovery`
|
||||
and `podcast_discovery_peers`.
|
||||
|
||||
### Finding podcasts in the federation
|
||||
|
||||
The search box searches the federation too. Podcasts that members shared
|
||||
appear under **Federation → Podcasts**, marked **Podcast** and kept apart
|
||||
from music; podcasts you already follow are marked **Already subscribed**
|
||||
(also when you follow the same feed under an older address it moved
|
||||
from). If you follow the feed only under the newer address of a card's
|
||||
moved feed, the result list cannot tell yet; opening the card reads the
|
||||
feed, sees the move and shows **Already subscribed** with **Open**, and
|
||||
neither **Subscribe** nor **Add RSS** with the old address creates a
|
||||
second subscription. A podcast you follow is changed only by its own
|
||||
address: when a feed found under another address only claims to have
|
||||
moved to yours (or is an old copy of it), nothing of that feed is written
|
||||
into your podcast; if you had unsubscribed, it is subscribed again with
|
||||
its own data and refreshed from its own address.
|
||||
|
||||
- Opening a card shows its title, author, description, artwork and the feed
|
||||
URL, then loads the seasons and episodes from the feed itself through the
|
||||
server: the newest 50 episodes, and the newest 50 of one season when you
|
||||
pick its chip (pick it again for all seasons). Nothing is stored until
|
||||
you subscribe. Closing the card, or picking another season, cancels a
|
||||
loading preview.
|
||||
- **Subscribe** adds your own private subscription: the server downloads the
|
||||
feed from the card's address, as for **Add RSS**, and you can then play,
|
||||
share or unsubscribe it like any other. The member who published the card
|
||||
is never contacted and does not need to stay online; their card is only a
|
||||
pointer to the public feed. The subscription keeps the podcast's identity,
|
||||
so your other Furumi players recognise it, and nobody else sees it.
|
||||
- Card texts and links come from other members and may be wrong. The
|
||||
player shows them as plain text, opens only web links, and loads artwork
|
||||
directly from its host only over HTTPS (like the artwork of subscribed
|
||||
podcasts). Only the original feed address of a card is used: an address a
|
||||
member claims the feed moved to is ignored unless the feed itself
|
||||
redirects there.
|
||||
- Search results show only cards; no member's subscriptions, progress or
|
||||
account are ever visible. A card disappears from search results within
|
||||
12 hours after it is revoked.
|
||||
- Podcast search needs federation on this server. When public discovery is
|
||||
off or unavailable, the search shows music only. When the podcast search
|
||||
fails or times out, the music results are still shown, with a note that
|
||||
the podcast search did not answer.
|
||||
|
||||
### Podcast feeds and audio
|
||||
|
||||
|
||||
@@ -204,6 +204,10 @@ node --test tests/player_queue_sources.test.cjs tests/player_podcasts.test.cjs
|
||||
# the migration test creates and drops a database next to it:
|
||||
FURU_DEVICE_SYNC_TEST_DATABASE_URL=postgresql://... \
|
||||
cargo test postgres_tests -- --ignored
|
||||
# Sharing, aggregation and a loopback publication from the web node to
|
||||
# another iroh node (temporary directories):
|
||||
FURU_DEVICE_SYNC_TEST_DATABASE_URL=postgresql://... \
|
||||
cargo test discovery -- --include-ignored
|
||||
FURU_METADATA_FIX_TEST_DATABASE_URL=postgresql://... \
|
||||
cargo test undo_accepts_playback_archives_written_before_the_typed_queue -- --ignored
|
||||
```
|
||||
@@ -212,6 +216,119 @@ FURU_METADATA_FIX_TEST_DATABASE_URL=postgresql://... \
|
||||
0.12.x byte for byte; `fixtures/v3_hello.golden.json` is the v3 shape. Rewrite
|
||||
it only for a deliberate change, with `FURUMUSIC_BLESS_GOLDEN=1`.
|
||||
|
||||
### Public podcast discovery
|
||||
|
||||
`src/podcasts/discovery.rs` decides what the server node publishes on the
|
||||
`music_dht::podcast_dht` overlay; `src/federation/podcast_discovery.rs` runs
|
||||
the overlay on the federation node (ALPN `furumi-fd/podcast-dht/1`, cache
|
||||
`podcast-dht.sqlite3` in the federation data directory, `serve`,
|
||||
`maintenance`, the reconciler and a capability probe that calls
|
||||
`note_peer_capabilities`). The capability manifest advertises `podcast_dht`
|
||||
only while the overlay runs.
|
||||
|
||||
- The server's node key is the only publisher. Each account's
|
||||
`AccountShare` is built like the frid integration pattern for SQLite
|
||||
clients: the merged subscription register (with its aliases) and share
|
||||
register, and a card from `PodcastCardSpec::from_subscription` with the
|
||||
account's catalog fetch URL as `feed_url`, the origin URL as `id_feed_url`
|
||||
and the feed's episode count and newest date.
|
||||
- `load_shared` reads active accounts only (`furumusic__user.is_active`).
|
||||
An `(account, podcast)` whose stored data does not decode (a URL, the
|
||||
register JSON, an id) is left out and counted (`unreadable`): only a
|
||||
failing connection or query skips the round, so revocation stays
|
||||
fail-safe for each podcast on its own.
|
||||
- `aggregate_share_states` keeps the accounts for which `effective_share`
|
||||
holds and whose card has no privacy warnings or a matching confirmation,
|
||||
and builds one `PodcastShareState` per podcast with
|
||||
`privacy_confirmed = true`. Representative: no warnings, then the
|
||||
incumbent (the previous round's representative, kept in memory, or after
|
||||
a restart an account whose card text equals the active card's text from
|
||||
`PodcastDht::published`), then `AccountShare::trusted_meta`
|
||||
(`meta_from_feed` and the catalog URL is the origin; an account can point
|
||||
its synced catalog URL anywhere before the server fetches it), then the
|
||||
smallest account id.
|
||||
- At most 192 podcasts (replicas keep 256 records per publisher, tombstones
|
||||
included), allocated round-robin over the sharing accounts (`allocate`):
|
||||
each account's queue holds its podcasts the node already publishes
|
||||
first, then by its own share time; in every round each account takes its
|
||||
next podcast not taken yet, published ones first inside a round. An
|
||||
account's share times therefore only order its own podcasts. A podcast
|
||||
without a live record of the node (an active card or an unexpired
|
||||
tombstone, `Incumbents::live_keys`) needs a new replica key; at most
|
||||
`256 - live keys` of them are taken per round (`Incumbents::key_headroom`),
|
||||
the others wait in `over_limit`, so a rebalance never displaces more
|
||||
cards than replicas accept new ones for.
|
||||
`PodcastDht::sync_shares` publishes the result and tombstones everything
|
||||
else. The podcasts left out (`over_limit`) and those `sync_shares`
|
||||
reported as failed are kept in memory for `ShareView.held`.
|
||||
- `furumusic__podcast_share_confirmation` (`m_0058`) is the client-held
|
||||
`privacy_confirmed`: per account and podcast, bound to the share
|
||||
register's `op_id` and the exact card URLs. Never synced.
|
||||
- The reconciler runs on `discovery::request_reconcile()` (after local share,
|
||||
subscribe and unsubscribe actions, received subscription/share/alias
|
||||
operations and snapshot sections, refreshes of shared podcasts; 2 s
|
||||
debounce) and every five minutes. Connection and query errors skip a
|
||||
round. Only counts are logged.
|
||||
- Its first round in a federation run waits for the first device-sync round
|
||||
(`devices::sync_loop` reports it through a `watch` channel once every
|
||||
exchange of the first complete device listing has answered, failed or
|
||||
timed out; at most `FIRST_DEVICE_SYNC_WAIT`, 45 s). The overlay withholds
|
||||
the cards stored before the restart until the first `sync_shares`, which
|
||||
re-sends the cards still shared, so a share turned off on a TUI while the
|
||||
server was stopped is not announced again. A device that cannot be
|
||||
reached at start delivers its decision only when it syncs.
|
||||
- `ShareView` (in `GET /api/podcasts/feeds/{id}` and the share response):
|
||||
`discovery` is `running`, `unavailable` (the node runs, the overlay did
|
||||
not start; `Federation::podcast_discovery_unavailable`) or `off`;
|
||||
`published` is this server node's own active card only; `held` is
|
||||
`over_limit` or `failed` after the last round. An unreadable overlay
|
||||
cache degrades to `published: false` instead of failing the page.
|
||||
- `POST /api/podcasts/feeds/{id}/share` requires a JSON body
|
||||
(`{"shared": bool, "confirm"?: {"feed_url", "id_feed_url"}}`); a card with
|
||||
warnings is shared only with `confirm` naming exactly its current URLs
|
||||
(otherwise 409 `needs_confirmation` with the warnings, nothing written).
|
||||
|
||||
Search and subscription from a card (`src/podcasts/search.rs`):
|
||||
|
||||
- `GET /api/player/federation/search/events` starts `PodcastDht::search`
|
||||
(`Federation::spawn_podcast_search`, at most 20 hits, 20 s) next to the
|
||||
music DHT search. Music events stream first and are unchanged
|
||||
(`federation.track|artist|release`); every event now carries `"type":
|
||||
"music"` or `"type": "podcast"`. Podcast hits follow as
|
||||
`federation.podcast` events with a `PodcastHitView` entity: the card's
|
||||
public fields, the origin `feed_url` (never `claimed_feed_urls`),
|
||||
`publisher_count`, `rank` and `exact_match` from `merge_search_results`
|
||||
with the music items, and the searching account's `subscribed` /
|
||||
`local_podcast_id` (by id, or a known feed URL or alias through
|
||||
`resolve_podcast_id`). `search.completed` adds `podcast_discovery`
|
||||
(`running`, `off`, `failed`); a failed podcast search, or a failure to
|
||||
prepare its hits for the account (`Federation::podcast_search_events`
|
||||
never fails), only drops the podcast hits and reports `failed`.
|
||||
- `POST /api/podcasts/discovery/preview` and `/discovery/subscribe` take
|
||||
`{"podcast_id", "feed_url"}` from a hit and refuse a URL the id does not
|
||||
hash from (422). The preview (optional `"season"`: a number as a
|
||||
string, `"none"` or `"all"`) fetches the feed through the SSRF-guarded
|
||||
fetcher and returns the channel, season counts, `listed_count` and the
|
||||
newest 50 episodes of the season, without storing anything; subscribe
|
||||
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`; 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
|
||||
metadata and nothing of the document is written. Nothing is written when
|
||||
the account is already subscribed, by id or known URL, before or after
|
||||
the fetch. Both share
|
||||
the "add RSS" request limits; the browser aborts a preview it no longer
|
||||
shows (`AbortController`), so a closed card frees its slot once the
|
||||
server sees the connection close.
|
||||
- The browser keeps only expected, typed card fields (`podcasts.foundHit`):
|
||||
ids must look like podcast ids, URLs must be absolute `http(s)` without
|
||||
credentials (artwork: `https` only), and texts render with `x-text`.
|
||||
|
||||
## Contributing
|
||||
|
||||
Bug reports, design discussions, and patches are welcome. Before submitting a
|
||||
|
||||
+115
-14
@@ -1,43 +1,64 @@
|
||||
//! Informational publication of the protocol versions exposed by this peer.
|
||||
//! Informational publication of the protocol versions exposed by this peer,
|
||||
//! and observation of the peers' manifests for the podcast-discovery overlay.
|
||||
|
||||
use std::time::Duration;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use anyhow::Result;
|
||||
use music_dht::StreamAcceptor;
|
||||
use anyhow::{Context, Result};
|
||||
use futures_util::StreamExt;
|
||||
use music_dht::capabilities::{
|
||||
CAPABILITIES_PROTOCOL_VERSION, CapabilityManifest, CapabilityMessage, JAM_ID, SIMILARITY_ID,
|
||||
read_message, write_message,
|
||||
CAPABILITIES_ALPN, CAPABILITIES_PROTOCOL_VERSION, CapabilityManifest, CapabilityMessage,
|
||||
JAM_ID, SIMILARITY_ID, read_message, write_message,
|
||||
};
|
||||
use music_dht::podcast_dht::PodcastDht;
|
||||
use music_dht::{EndpointId, MusicDhtService, StreamAcceptor};
|
||||
|
||||
use super::serve::AUDIO_PROTOCOL_VERSION;
|
||||
|
||||
fn local_manifest() -> CapabilityManifest {
|
||||
CapabilityManifest::frid("furumusic", env!("CARGO_PKG_VERSION"))
|
||||
/// How often known peers are considered for a manifest probe.
|
||||
const PROBE_INTERVAL: Duration = Duration::from_secs(5 * 60);
|
||||
/// A peer's manifest is probed again after this long.
|
||||
const REPROBE_AFTER: Duration = Duration::from_secs(60 * 60);
|
||||
/// Peers probed per round and in parallel.
|
||||
const PROBES_PER_ROUND: usize = 64;
|
||||
const PROBE_CONCURRENCY: usize = 8;
|
||||
const PROBE_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
/// The manifest of this node. The public podcast-discovery overlay is
|
||||
/// advertised only while the node serves it (`podcast_dht`).
|
||||
fn local_manifest(podcast_dht: bool) -> CapabilityManifest {
|
||||
let manifest = CapabilityManifest::frid("furumusic", env!("CARGO_PKG_VERSION"))
|
||||
// The web server does not expose federation Jam yet.
|
||||
.without_protocol(JAM_ID)
|
||||
.with_protocol("audio", AUDIO_PROTOCOL_VERSION)
|
||||
.with_protocol(
|
||||
SIMILARITY_ID,
|
||||
music_dht::similarity::SIMILARITY_PROTOCOL_VERSION,
|
||||
)
|
||||
);
|
||||
if podcast_dht {
|
||||
manifest.with_podcast_dht()
|
||||
} else {
|
||||
manifest
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn serve(mut acceptor: StreamAcceptor) {
|
||||
pub async fn serve(mut acceptor: StreamAcceptor, podcast_dht: bool) {
|
||||
while let Some(stream) = acceptor.accept().await {
|
||||
tokio::spawn(async move {
|
||||
if let Err(error) = serve_one(stream).await {
|
||||
if let Err(error) = serve_one(stream, podcast_dht).await {
|
||||
tracing::debug!("capability stream failed: {error:#}");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve_one(mut stream: music_dht::ByteStream) -> Result<()> {
|
||||
async fn serve_one(mut stream: music_dht::ByteStream, podcast_dht: bool) -> Result<()> {
|
||||
let response = match read_message(&mut stream).await? {
|
||||
CapabilityMessage::Get {
|
||||
version: CAPABILITIES_PROTOCOL_VERSION,
|
||||
} => CapabilityMessage::Manifest {
|
||||
manifest: local_manifest(),
|
||||
manifest: local_manifest(podcast_dht),
|
||||
},
|
||||
CapabilityMessage::Get { version } => CapabilityMessage::Error {
|
||||
message: format!("unsupported capability protocol {version}"),
|
||||
@@ -52,13 +73,93 @@ async fn serve_one(mut stream: music_dht::ByteStream) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Probes the manifests of known peers and tells the podcast overlay which
|
||||
/// of them lack it ([`PodcastDht::note_peer_capabilities`]), so bootstrap
|
||||
/// stops dialing them. A peer that does not answer is left alone: the
|
||||
/// overlay only learns contacts that answer on its own protocol anyway.
|
||||
pub async fn probe_loop(service: Arc<MusicDhtService>, dht: Arc<PodcastDht>) {
|
||||
let mut probed: HashMap<EndpointId, Instant> = HashMap::new();
|
||||
let mut interval = tokio::time::interval(PROBE_INTERVAL);
|
||||
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
interval.tick().await;
|
||||
let own = service.endpoint_id();
|
||||
probed.retain(|_, at| at.elapsed() < REPROBE_AFTER);
|
||||
let peers: Vec<EndpointId> = service
|
||||
.connected_peers()
|
||||
.into_iter()
|
||||
.chain(
|
||||
service
|
||||
.known_peers()
|
||||
.into_iter()
|
||||
.map(|contact| contact.peer_id),
|
||||
)
|
||||
.filter(|peer| *peer != own && !probed.contains_key(peer))
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
.take(PROBES_PER_ROUND)
|
||||
.collect();
|
||||
let results = futures_util::stream::iter(peers.into_iter().map(|peer| {
|
||||
let service = Arc::clone(&service);
|
||||
async move { (peer, probe_peer(&service, peer).await) }
|
||||
}))
|
||||
.buffer_unordered(PROBE_CONCURRENCY)
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
for (peer, result) in results {
|
||||
probed.insert(peer, Instant::now());
|
||||
match result {
|
||||
Ok(manifest) => {
|
||||
if let Err(error) = dht.note_peer_capabilities(peer, &manifest).await {
|
||||
tracing::debug!(%peer, "podcast overlay capability note failed: {error}");
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::trace!(%peer, "peer capability probe unavailable: {error:#}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn probe_peer(service: &MusicDhtService, peer: EndpointId) -> Result<CapabilityManifest> {
|
||||
let mut stream =
|
||||
tokio::time::timeout(PROBE_TIMEOUT, service.open_stream(peer, CAPABILITIES_ALPN))
|
||||
.await
|
||||
.context("capability stream timed out")??;
|
||||
write_message(
|
||||
&mut stream,
|
||||
&CapabilityMessage::Get {
|
||||
version: CAPABILITIES_PROTOCOL_VERSION,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
stream.send.finish()?;
|
||||
let response = tokio::time::timeout(PROBE_TIMEOUT, read_message(&mut stream))
|
||||
.await
|
||||
.context("capability request timed out")??;
|
||||
let CapabilityMessage::Manifest { manifest } = response else {
|
||||
anyhow::bail!("peer did not return a capability manifest");
|
||||
};
|
||||
manifest.validate()?;
|
||||
Ok(manifest)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_podcast_overlay_is_advertised_only_while_it_runs() {
|
||||
assert!(!local_manifest(false).supports_podcast_dht());
|
||||
let manifest = local_manifest(true);
|
||||
assert!(manifest.supports_podcast_dht());
|
||||
manifest.validate().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manifest_describes_only_supported_player_protocols() {
|
||||
let manifest = local_manifest();
|
||||
let manifest = local_manifest(false);
|
||||
assert_eq!(manifest.application, "furumusic");
|
||||
assert_eq!(
|
||||
manifest.protocols.get("audio"),
|
||||
|
||||
+326
-151
@@ -9,6 +9,7 @@ use anyhow::{Context, Result};
|
||||
use music_dht::{ItemKind, LibraryItem, normalize_content_id};
|
||||
use serde::Serialize;
|
||||
use serde_json::{Value, json};
|
||||
use sqlx::PgPool;
|
||||
use sqlx::Row as _;
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
@@ -94,6 +95,9 @@ pub struct SearchEvent {
|
||||
pub search_id: String,
|
||||
pub sequence: u64,
|
||||
pub kind: &'static str,
|
||||
/// `music` or `podcast` (`music_dht::podcast_dht::FederatedSearchHit`).
|
||||
#[serde(rename = "type")]
|
||||
pub hit_type: &'static str,
|
||||
pub peer: Option<String>,
|
||||
pub entity_key: Value,
|
||||
pub entity: Value,
|
||||
@@ -218,7 +222,12 @@ impl Federation {
|
||||
/// Performs one bounded DHT search and returns entity upserts. The HTTP
|
||||
/// layer streams each upsert independently; catalog fan-out can append
|
||||
/// events to the same contract without changing the browser model.
|
||||
pub async fn search_events(&self, search_id: &str, query: &str) -> Result<Vec<SearchEvent>> {
|
||||
///
|
||||
/// Music only (`federation.track|artist|release`, marked `"type":
|
||||
/// "music"`). Public podcasts are searched next to it
|
||||
/// ([`Self::spawn_podcast_search`]) and follow as `federation.podcast`
|
||||
/// events ([`Self::podcast_search_events`]).
|
||||
pub async fn search_events(&self, search_id: &str, query: &str) -> Result<MusicSearch> {
|
||||
let query = query.trim();
|
||||
anyhow::ensure!(!query.is_empty(), "search query is empty");
|
||||
anyhow::ensure!(query.chars().count() <= 200, "search query is too long");
|
||||
@@ -233,13 +242,10 @@ impl Federation {
|
||||
"federated search started"
|
||||
);
|
||||
let own = service.endpoint_id();
|
||||
let result = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(20),
|
||||
service.search_network(query),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("federated search timed out after 20 seconds"))?
|
||||
.map_err(|err| anyhow::anyhow!("federated search failed: {err}"))?;
|
||||
let result = tokio::time::timeout(SEARCH_TIMEOUT, service.search_network(query))
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("federated search timed out after 20 seconds"))?
|
||||
.map_err(|err| anyhow::anyhow!("federated search failed: {err}"))?;
|
||||
tracing::info!(
|
||||
search_id,
|
||||
query,
|
||||
@@ -249,154 +255,14 @@ impl Federation {
|
||||
elapsed_ms = started.elapsed().as_millis() as u64,
|
||||
"federated DHT search finished"
|
||||
);
|
||||
let all_items: Vec<LibraryItem> = result
|
||||
let items: Vec<LibraryItem> = result
|
||||
.local_results
|
||||
.into_iter()
|
||||
.chain(result.network_results)
|
||||
.collect();
|
||||
|
||||
let pool = self.pool().await?;
|
||||
let mut by_content: HashMap<String, TrackDto> = HashMap::new();
|
||||
for item in all_items.iter().filter(|item| item.kind == ItemKind::Track) {
|
||||
let Some(content_id) = item.content_id.as_deref().and_then(normalize_content_id) else {
|
||||
// A globally usable track reference must be verifiable.
|
||||
continue;
|
||||
};
|
||||
let local = local_availability(&pool, &content_id).await?;
|
||||
let source = FederationSourceDto {
|
||||
owner: item.owner.to_string(),
|
||||
item_id: hex(item.id.as_bytes()),
|
||||
};
|
||||
let entry = by_content.entry(content_id.clone()).or_insert_with(|| {
|
||||
track_from_item(content_id.clone(), item, local, item.owner == own)
|
||||
});
|
||||
if !entry.availability.federation.iter().any(|candidate| {
|
||||
candidate.owner == source.owner && candidate.item_id == source.item_id
|
||||
}) {
|
||||
entry.availability.federation.push(source);
|
||||
}
|
||||
if entry.availability.local.is_some() {
|
||||
entry.availability.state = "local";
|
||||
}
|
||||
}
|
||||
|
||||
let mut tracks: Vec<_> = by_content.into_values().collect();
|
||||
tracks.sort_by(|left, right| {
|
||||
left.metadata
|
||||
.title
|
||||
.to_lowercase()
|
||||
.cmp(&right.metadata.title.to_lowercase())
|
||||
});
|
||||
|
||||
let mut events = Vec::with_capacity(all_items.len());
|
||||
for (index, track) in tracks.into_iter().enumerate() {
|
||||
persist_track_ref(&pool, &track).await?;
|
||||
let peer = track
|
||||
.availability
|
||||
.federation
|
||||
.first()
|
||||
.map(|source| source.owner.clone());
|
||||
events.push(SearchEvent {
|
||||
search_id: search_id.to_owned(),
|
||||
sequence: index as u64 + 1,
|
||||
kind: "federation.track",
|
||||
peer,
|
||||
entity_key: serde_json::to_value(&track.key)?,
|
||||
entity: serde_json::to_value(track)?,
|
||||
});
|
||||
}
|
||||
let mut artist_peers: HashMap<String, (String, Vec<String>)> = HashMap::new();
|
||||
let mut releases: HashMap<String, Value> = HashMap::new();
|
||||
for item in &all_items {
|
||||
match item.kind {
|
||||
ItemKind::Artist => {
|
||||
let key = music_dht::normalize_name(&item.name);
|
||||
let entry = artist_peers
|
||||
.entry(key)
|
||||
.or_insert_with(|| (item.name.clone(), Vec::new()));
|
||||
let owner = item.owner.to_string();
|
||||
if !entry.1.contains(&owner) {
|
||||
entry.1.push(owner);
|
||||
}
|
||||
}
|
||||
ItemKind::Release => {
|
||||
let artist_keys: Vec<String> = item
|
||||
.artist_names
|
||||
.iter()
|
||||
.map(|name| music_dht::normalize_name(name))
|
||||
.collect();
|
||||
let normalized_title = music_dht::normalize_name(&item.name);
|
||||
let cover_url = all_items
|
||||
.iter()
|
||||
.find(|track| {
|
||||
track.kind == ItemKind::Track
|
||||
&& track.release_title.as_deref().is_some_and(|title| {
|
||||
music_dht::normalize_name(title) == normalized_title
|
||||
})
|
||||
&& track.year == item.year
|
||||
})
|
||||
.map(|track| {
|
||||
format!(
|
||||
"/api/player/federation/tracks/artwork?owner={}&item_id={}",
|
||||
track.owner,
|
||||
hex(track.id.as_bytes())
|
||||
)
|
||||
});
|
||||
let key = format!(
|
||||
"{}|{}|{}",
|
||||
normalized_title,
|
||||
artist_keys.join(","),
|
||||
item.year.map_or_else(String::new, |year| year.to_string())
|
||||
);
|
||||
releases.entry(key.clone()).or_insert_with(|| {
|
||||
json!({
|
||||
"key": {
|
||||
"normalized_title": music_dht::normalize_name(&item.name),
|
||||
"primary_artists": artist_keys,
|
||||
"release_type": null,
|
||||
"year": item.year,
|
||||
},
|
||||
"title": item.name,
|
||||
"artists": item.artist_names,
|
||||
"year": item.year,
|
||||
"cover_url": cover_url,
|
||||
"sources": [{
|
||||
"owner": item.owner.to_string(),
|
||||
"item_id": hex(item.id.as_bytes()),
|
||||
}],
|
||||
})
|
||||
});
|
||||
}
|
||||
ItemKind::Track => {}
|
||||
}
|
||||
}
|
||||
for (key, (name, peers)) in artist_peers {
|
||||
let sequence = events.len() as u64 + 1;
|
||||
events.push(SearchEvent {
|
||||
search_id: search_id.to_owned(),
|
||||
sequence,
|
||||
kind: "federation.artist",
|
||||
peer: peers.first().cloned(),
|
||||
entity_key: json!({ "normalized_name": key }),
|
||||
entity: json!({
|
||||
"key": { "normalized_name": key },
|
||||
"name": name,
|
||||
"image_url": null,
|
||||
"peers": peers,
|
||||
}),
|
||||
});
|
||||
}
|
||||
for (key, release) in releases {
|
||||
let sequence = events.len() as u64 + 1;
|
||||
events.push(SearchEvent {
|
||||
search_id: search_id.to_owned(),
|
||||
sequence,
|
||||
kind: "federation.release",
|
||||
peer: None,
|
||||
entity_key: json!({ "composite": key }),
|
||||
entity: release,
|
||||
});
|
||||
}
|
||||
let events = music_search_events(&pool, search_id, &items, own).await?;
|
||||
tracing::info!(
|
||||
search_id,
|
||||
query,
|
||||
@@ -404,8 +270,317 @@ impl Federation {
|
||||
elapsed_ms = started.elapsed().as_millis() as u64,
|
||||
"federated search response ready"
|
||||
);
|
||||
Ok(events)
|
||||
Ok(MusicSearch { events, items })
|
||||
}
|
||||
|
||||
/// Starts the public podcast search of `query` on the discovery
|
||||
/// overlay, if it runs, next to the music search.
|
||||
pub fn spawn_podcast_search(&self, query: &str) -> tokio::task::JoinHandle<PodcastSearch> {
|
||||
let discovery = self.podcast_discovery();
|
||||
let query = query.trim().to_owned();
|
||||
tokio::spawn(async move { search_podcasts(discovery.as_deref(), &query).await })
|
||||
}
|
||||
|
||||
/// The `federation.podcast` events of a search for `user_id`, numbered
|
||||
/// after the music events (see [`podcast_events`]), and the
|
||||
/// `podcast_discovery` state for `search.completed`.
|
||||
///
|
||||
/// Never fails: when the hits cannot be prepared for the account (a
|
||||
/// database error) they are dropped and the state is `failed`, so the
|
||||
/// music results stand and the browser can say the podcast search
|
||||
/// failed.
|
||||
pub async fn podcast_search_events(
|
||||
&self,
|
||||
search_id: &str,
|
||||
query: &str,
|
||||
user_id: i64,
|
||||
music: &MusicSearch,
|
||||
podcasts: PodcastSearch,
|
||||
) -> (&'static str, Vec<SearchEvent>) {
|
||||
let state = podcasts.state();
|
||||
let PodcastSearch::Found(hits) = podcasts else {
|
||||
return (state, Vec::new());
|
||||
};
|
||||
if hits.is_empty() {
|
||||
return (state, Vec::new());
|
||||
}
|
||||
let events = async {
|
||||
let pool = self.pool().await?;
|
||||
let first_sequence = music.events.len() as u64 + 1;
|
||||
podcast_events(
|
||||
&pool,
|
||||
user_id,
|
||||
search_id,
|
||||
query.trim(),
|
||||
&music.items,
|
||||
first_sequence,
|
||||
hits,
|
||||
)
|
||||
.await
|
||||
};
|
||||
match events.await {
|
||||
Ok(events) => {
|
||||
tracing::info!(
|
||||
search_id,
|
||||
podcasts = events.len(),
|
||||
"public podcast hits ready"
|
||||
);
|
||||
(state, events)
|
||||
}
|
||||
Err(error) => {
|
||||
// Only the error: never the query or the hits.
|
||||
tracing::warn!(
|
||||
search_id,
|
||||
"public podcast hits could not be prepared: {error:#}"
|
||||
);
|
||||
("failed", Vec::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Deadline of the music DHT and the podcast overlay searches.
|
||||
const SEARCH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(20);
|
||||
|
||||
/// Music results of a federated search: the events to stream and the items
|
||||
/// (podcast hits are ranked among them).
|
||||
#[derive(Debug)]
|
||||
pub struct MusicSearch {
|
||||
pub events: Vec<SearchEvent>,
|
||||
pub items: Vec<LibraryItem>,
|
||||
}
|
||||
|
||||
/// Outcome of the public podcast search of one federated search.
|
||||
#[derive(Debug)]
|
||||
pub enum PodcastSearch {
|
||||
/// Public discovery does not run on this server.
|
||||
Off,
|
||||
/// The overlay search failed or timed out (music results still count).
|
||||
Failed,
|
||||
/// Hits, possibly none.
|
||||
Found(Vec<music_dht::podcast_dht::PodcastSearchHit>),
|
||||
}
|
||||
|
||||
impl PodcastSearch {
|
||||
/// `running`, `off` or `failed` (reported in `search.completed`).
|
||||
pub fn state(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Off => "off",
|
||||
Self::Failed => "failed",
|
||||
Self::Found(_) => "running",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn search_podcasts(
|
||||
discovery: Option<&music_dht::podcast_dht::PodcastDht>,
|
||||
query: &str,
|
||||
) -> PodcastSearch {
|
||||
let Some(dht) = discovery else {
|
||||
return PodcastSearch::Off;
|
||||
};
|
||||
let limit = crate::podcasts::search::PODCAST_SEARCH_LIMIT;
|
||||
match tokio::time::timeout(SEARCH_TIMEOUT, dht.search(query, limit)).await {
|
||||
Ok(Ok(hits)) => PodcastSearch::Found(hits),
|
||||
Ok(Err(error)) => {
|
||||
tracing::info!("public podcast search failed: {error}");
|
||||
PodcastSearch::Failed
|
||||
}
|
||||
Err(_) => {
|
||||
tracing::info!("public podcast search timed out");
|
||||
PodcastSearch::Failed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One `federation.podcast` event per public podcast hit, for `user_id`
|
||||
/// (`subscribed`, `local_podcast_id`), ordered by the hit's place in
|
||||
/// `merge_search_results` with the music items and numbered from
|
||||
/// `first_sequence`.
|
||||
pub(crate) async fn podcast_events(
|
||||
pool: &PgPool,
|
||||
user_id: i64,
|
||||
search_id: &str,
|
||||
query: &str,
|
||||
music: &[LibraryItem],
|
||||
first_sequence: u64,
|
||||
hits: Vec<music_dht::podcast_dht::PodcastSearchHit>,
|
||||
) -> Result<Vec<SearchEvent>> {
|
||||
let store = crate::podcasts::PodcastStore::new(pool.clone());
|
||||
let views = crate::podcasts::search::hit_views(&store, user_id, query, music, hits).await?;
|
||||
views
|
||||
.into_iter()
|
||||
.zip(first_sequence..)
|
||||
.map(|(view, sequence)| {
|
||||
Ok(SearchEvent {
|
||||
search_id: search_id.to_owned(),
|
||||
sequence,
|
||||
kind: "federation.podcast",
|
||||
hit_type: "podcast",
|
||||
// Subscribing never contacts a publisher: no peer to name.
|
||||
peer: None,
|
||||
entity_key: json!({ "podcast_id": view.podcast_id }),
|
||||
entity: serde_json::to_value(view)?,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Music events of a search (tracks, then artists and releases).
|
||||
pub(crate) async fn music_search_events(
|
||||
pool: &PgPool,
|
||||
search_id: &str,
|
||||
all_items: &[LibraryItem],
|
||||
own: music_dht::EndpointId,
|
||||
) -> Result<Vec<SearchEvent>> {
|
||||
let mut by_content: HashMap<String, TrackDto> = HashMap::new();
|
||||
for item in all_items.iter().filter(|item| item.kind == ItemKind::Track) {
|
||||
let Some(content_id) = item.content_id.as_deref().and_then(normalize_content_id) else {
|
||||
// A globally usable track reference must be verifiable.
|
||||
continue;
|
||||
};
|
||||
let local = local_availability(pool, &content_id).await?;
|
||||
let source = FederationSourceDto {
|
||||
owner: item.owner.to_string(),
|
||||
item_id: hex(item.id.as_bytes()),
|
||||
};
|
||||
let entry = by_content
|
||||
.entry(content_id.clone())
|
||||
.or_insert_with(|| track_from_item(content_id.clone(), item, local, item.owner == own));
|
||||
if !entry
|
||||
.availability
|
||||
.federation
|
||||
.iter()
|
||||
.any(|candidate| candidate.owner == source.owner && candidate.item_id == source.item_id)
|
||||
{
|
||||
entry.availability.federation.push(source);
|
||||
}
|
||||
if entry.availability.local.is_some() {
|
||||
entry.availability.state = "local";
|
||||
}
|
||||
}
|
||||
|
||||
let mut tracks: Vec<_> = by_content.into_values().collect();
|
||||
tracks.sort_by(|left, right| {
|
||||
left.metadata
|
||||
.title
|
||||
.to_lowercase()
|
||||
.cmp(&right.metadata.title.to_lowercase())
|
||||
});
|
||||
|
||||
let mut events = Vec::with_capacity(all_items.len());
|
||||
for (index, track) in tracks.into_iter().enumerate() {
|
||||
persist_track_ref(pool, &track).await?;
|
||||
let peer = track
|
||||
.availability
|
||||
.federation
|
||||
.first()
|
||||
.map(|source| source.owner.clone());
|
||||
events.push(SearchEvent {
|
||||
search_id: search_id.to_owned(),
|
||||
sequence: index as u64 + 1,
|
||||
kind: "federation.track",
|
||||
hit_type: "music",
|
||||
peer,
|
||||
entity_key: serde_json::to_value(&track.key)?,
|
||||
entity: serde_json::to_value(track)?,
|
||||
});
|
||||
}
|
||||
let mut artist_peers: HashMap<String, (String, Vec<String>)> = HashMap::new();
|
||||
let mut releases: HashMap<String, Value> = HashMap::new();
|
||||
for item in all_items {
|
||||
match item.kind {
|
||||
ItemKind::Artist => {
|
||||
let key = music_dht::normalize_name(&item.name);
|
||||
let entry = artist_peers
|
||||
.entry(key)
|
||||
.or_insert_with(|| (item.name.clone(), Vec::new()));
|
||||
let owner = item.owner.to_string();
|
||||
if !entry.1.contains(&owner) {
|
||||
entry.1.push(owner);
|
||||
}
|
||||
}
|
||||
ItemKind::Release => {
|
||||
let artist_keys: Vec<String> = item
|
||||
.artist_names
|
||||
.iter()
|
||||
.map(|name| music_dht::normalize_name(name))
|
||||
.collect();
|
||||
let normalized_title = music_dht::normalize_name(&item.name);
|
||||
let cover_url = all_items
|
||||
.iter()
|
||||
.find(|track| {
|
||||
track.kind == ItemKind::Track
|
||||
&& track.release_title.as_deref().is_some_and(|title| {
|
||||
music_dht::normalize_name(title) == normalized_title
|
||||
})
|
||||
&& track.year == item.year
|
||||
})
|
||||
.map(|track| {
|
||||
format!(
|
||||
"/api/player/federation/tracks/artwork?owner={}&item_id={}",
|
||||
track.owner,
|
||||
hex(track.id.as_bytes())
|
||||
)
|
||||
});
|
||||
let key = format!(
|
||||
"{}|{}|{}",
|
||||
normalized_title,
|
||||
artist_keys.join(","),
|
||||
item.year.map_or_else(String::new, |year| year.to_string())
|
||||
);
|
||||
releases.entry(key.clone()).or_insert_with(|| {
|
||||
json!({
|
||||
"key": {
|
||||
"normalized_title": music_dht::normalize_name(&item.name),
|
||||
"primary_artists": artist_keys,
|
||||
"release_type": null,
|
||||
"year": item.year,
|
||||
},
|
||||
"title": item.name,
|
||||
"artists": item.artist_names,
|
||||
"year": item.year,
|
||||
"cover_url": cover_url,
|
||||
"sources": [{
|
||||
"owner": item.owner.to_string(),
|
||||
"item_id": hex(item.id.as_bytes()),
|
||||
}],
|
||||
})
|
||||
});
|
||||
}
|
||||
ItemKind::Track => {}
|
||||
}
|
||||
}
|
||||
for (key, (name, peers)) in artist_peers {
|
||||
let sequence = events.len() as u64 + 1;
|
||||
events.push(SearchEvent {
|
||||
search_id: search_id.to_owned(),
|
||||
sequence,
|
||||
kind: "federation.artist",
|
||||
hit_type: "music",
|
||||
peer: peers.first().cloned(),
|
||||
entity_key: json!({ "normalized_name": key }),
|
||||
entity: json!({
|
||||
"key": { "normalized_name": key },
|
||||
"name": name,
|
||||
"image_url": null,
|
||||
"peers": peers,
|
||||
}),
|
||||
});
|
||||
}
|
||||
for (key, release) in releases {
|
||||
let sequence = events.len() as u64 + 1;
|
||||
events.push(SearchEvent {
|
||||
search_id: search_id.to_owned(),
|
||||
sequence,
|
||||
kind: "federation.release",
|
||||
hit_type: "music",
|
||||
peer: None,
|
||||
entity_key: json!({ "composite": key }),
|
||||
entity: release,
|
||||
});
|
||||
}
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
async fn fetch_artist_catalog(
|
||||
|
||||
@@ -800,16 +800,25 @@ pub async fn serve_peers(
|
||||
}
|
||||
}
|
||||
|
||||
/// Polls every trusted device of every account every
|
||||
/// [`DEVICE_SYNC_INTERVAL`] (one exchange per device at a time).
|
||||
///
|
||||
/// `first_round` turns `true` once the first complete listing of the
|
||||
/// devices has been polled: every exchange in flight then has answered,
|
||||
/// failed or timed out (30 s). The podcast publication reconciler waits for
|
||||
/// it (see [`crate::podcasts::discovery::run`]).
|
||||
pub async fn sync_loop(
|
||||
pool: sqlx::PgPool,
|
||||
service: Arc<MusicDhtService>,
|
||||
hub: Arc<PlayerDeviceHub>,
|
||||
transport_stats: Arc<TransportStats>,
|
||||
first_round: tokio::sync::watch::Sender<bool>,
|
||||
) {
|
||||
let mut interval = tokio::time::interval(DEVICE_SYNC_INTERVAL);
|
||||
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let mut polls = tokio::task::JoinSet::new();
|
||||
let mut active = std::collections::HashMap::new();
|
||||
let mut first_round = FirstSyncRound::new(first_round);
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = interval.tick() => {
|
||||
@@ -819,10 +828,15 @@ pub async fn sync_loop(
|
||||
Ok(users) => users,
|
||||
Err(error) => { tracing::warn!("device poll listing failed: {error:#}"); continue; }
|
||||
};
|
||||
let mut listed_all = true;
|
||||
for user_id in users {
|
||||
let devices = match active_remote_devices(&pool, user_id).await {
|
||||
Ok(devices) => devices,
|
||||
Err(error) => { let _ = set_last_error(&pool, user_id, Some(&format!("{error:#}"))).await; continue; }
|
||||
Err(error) => {
|
||||
listed_all = false;
|
||||
let _ = set_last_error(&pool, user_id, Some(&format!("{error:#}"))).await;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
for device in devices {
|
||||
let key = (user_id, device.device_id.clone());
|
||||
@@ -838,12 +852,17 @@ pub async fn sync_loop(
|
||||
active.insert(handle.id(), key);
|
||||
}
|
||||
}
|
||||
if listed_all {
|
||||
// Includes exchanges still running from an earlier tick.
|
||||
first_round.started(active.keys().copied());
|
||||
}
|
||||
}
|
||||
Some(completed) = polls.join_next_with_id(), if !polls.is_empty() => {
|
||||
let (task, result) = match completed {
|
||||
Ok((task, result)) => (task, result),
|
||||
Err(error) => (error.id(), Err(anyhow::Error::from(error))),
|
||||
};
|
||||
first_round.finished(&task);
|
||||
if let Some((user_id, device_id)) = active.remove(&task)
|
||||
&& let Err(error) = result {
|
||||
tracing::debug!(device = %device_id, "web fed device sync failed: {error:#}");
|
||||
@@ -854,6 +873,50 @@ pub async fn sync_loop(
|
||||
}
|
||||
}
|
||||
|
||||
/// Tracks the first device-sync round of a federation run for
|
||||
/// [`sync_loop`]: the exchanges started by the first complete listing of the
|
||||
/// trusted devices (and those still running then). The watch turns `true`
|
||||
/// when the last of them ends, or at once when there are none, and stays
|
||||
/// `true`.
|
||||
pub(crate) struct FirstSyncRound<K> {
|
||||
pending: Option<std::collections::HashSet<K>>,
|
||||
done: tokio::sync::watch::Sender<bool>,
|
||||
}
|
||||
|
||||
impl<K: Eq + std::hash::Hash> FirstSyncRound<K> {
|
||||
pub(crate) fn new(done: tokio::sync::watch::Sender<bool>) -> Self {
|
||||
Self {
|
||||
pending: None,
|
||||
done,
|
||||
}
|
||||
}
|
||||
|
||||
/// The first complete listing started (or found running) `polls`;
|
||||
/// later listings are ignored.
|
||||
pub(crate) fn started(&mut self, polls: impl IntoIterator<Item = K>) {
|
||||
if self.pending.is_some() || *self.done.borrow() {
|
||||
return;
|
||||
}
|
||||
let pending: std::collections::HashSet<K> = polls.into_iter().collect();
|
||||
if pending.is_empty() {
|
||||
self.done.send_replace(true);
|
||||
} else {
|
||||
self.pending = Some(pending);
|
||||
}
|
||||
}
|
||||
|
||||
/// An exchange ended (answered, failed or timed out).
|
||||
pub(crate) fn finished(&mut self, poll: &K) {
|
||||
if let Some(pending) = &mut self.pending
|
||||
&& pending.remove(poll)
|
||||
&& pending.is_empty()
|
||||
{
|
||||
self.pending = None;
|
||||
self.done.send_replace(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn sync_once(
|
||||
pool: &sqlx::PgPool,
|
||||
service: Arc<MusicDhtService>,
|
||||
@@ -5081,6 +5144,8 @@ fn short_id(value: &str) -> String {
|
||||
value.chars().take(10).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod first_round_tests;
|
||||
#[cfg(test)]
|
||||
mod gating_tests;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//! The first device-sync round that the podcast publication reconciler
|
||||
//! waits for ([`FirstSyncRound`]).
|
||||
|
||||
use super::FirstSyncRound;
|
||||
|
||||
#[test]
|
||||
fn the_first_round_ends_when_its_last_exchange_ends() {
|
||||
let (done, watch) = tokio::sync::watch::channel(false);
|
||||
let mut round = FirstSyncRound::new(done);
|
||||
// An exchange that ends before the first complete listing is not
|
||||
// waited for.
|
||||
round.finished(&7);
|
||||
assert!(!*watch.borrow());
|
||||
round.started([1, 2]);
|
||||
// A later listing (with exchanges it started) changes nothing.
|
||||
round.started([3]);
|
||||
round.finished(&1);
|
||||
round.finished(&3);
|
||||
assert!(!*watch.borrow());
|
||||
round.finished(&2);
|
||||
assert!(*watch.borrow());
|
||||
// Stays done.
|
||||
round.started([4]);
|
||||
round.finished(&4);
|
||||
assert!(*watch.borrow());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_trusted_devices_the_first_round_ends_at_once() {
|
||||
let (done, watch) = tokio::sync::watch::channel(false);
|
||||
let mut round = FirstSyncRound::<u32>::new(done);
|
||||
round.started([]);
|
||||
assert!(*watch.borrow());
|
||||
}
|
||||
+74
-14
@@ -9,6 +9,11 @@
|
||||
//! for catalog metadata and, when enabled, compatible similarity embeddings;
|
||||
//! local playback and the local library remain independent of the network.
|
||||
//!
|
||||
//! The node also runs the public podcast-discovery overlay
|
||||
//! ([`podcast_discovery`], `music_dht::podcast_dht`): it publishes cards of
|
||||
//! the podcasts that accounts explicitly shared (`crate::podcasts::discovery`)
|
||||
//! under the server's node identity.
|
||||
//!
|
||||
//! Settings are the regular admin config entries (`federation_enabled`,
|
||||
//! `federation_network_id`, `federation_save_on_listen`) and apply on the fly —
|
||||
//! saving the settings starts, stops or re-joins the node without a server
|
||||
@@ -17,6 +22,7 @@
|
||||
mod capabilities;
|
||||
pub mod client;
|
||||
pub mod devices;
|
||||
pub(crate) mod podcast_discovery;
|
||||
mod receive;
|
||||
mod serve;
|
||||
mod similarity;
|
||||
@@ -30,6 +36,7 @@ use std::time::{Duration, Instant};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use music_dht::capabilities::CAPABILITIES_ALPN;
|
||||
use music_dht::podcast_dht::{PODCAST_DHT_ALPN, PodcastDht};
|
||||
use music_dht::similarity_dht::SimilarityDht;
|
||||
use music_dht::similarity_lsh::SIMILARITY_DHT_ALPN;
|
||||
use music_dht::{
|
||||
@@ -60,6 +67,8 @@ const TRANSPORT_SAMPLE_LIMIT: usize = 16;
|
||||
struct Running {
|
||||
service: Arc<MusicDhtService>,
|
||||
similarity_dht: Arc<SimilarityDht>,
|
||||
/// Public podcast discovery; `None` when the overlay failed to start.
|
||||
podcast_dht: Option<Arc<PodcastDht>>,
|
||||
network_name: String,
|
||||
tasks: Vec<tokio::task::JoinHandle<()>>,
|
||||
}
|
||||
@@ -290,6 +299,11 @@ pub struct Federation {
|
||||
download_locks: std::sync::Mutex<HashMap<String, Arc<tokio::sync::Mutex<()>>>>,
|
||||
pool: tokio::sync::OnceCell<PgPool>,
|
||||
running: tokio::sync::Mutex<Option<Running>>,
|
||||
/// The running podcast-discovery overlay, readable without waiting for
|
||||
/// `running` (held while the node starts).
|
||||
podcast_dht: std::sync::Mutex<Option<Arc<PodcastDht>>>,
|
||||
/// The node runs, but the podcast-discovery overlay failed to start.
|
||||
podcast_discovery_failed: AtomicBool,
|
||||
sync_lock: tokio::sync::Mutex<()>,
|
||||
supervisor_started: AtomicBool,
|
||||
recovery_count: AtomicU64,
|
||||
@@ -325,6 +339,8 @@ pub fn handle() -> Arc<Federation> {
|
||||
download_locks: std::sync::Mutex::new(Default::default()),
|
||||
pool: tokio::sync::OnceCell::new(),
|
||||
running: tokio::sync::Mutex::new(None),
|
||||
podcast_dht: std::sync::Mutex::new(None),
|
||||
podcast_discovery_failed: AtomicBool::new(false),
|
||||
sync_lock: tokio::sync::Mutex::new(()),
|
||||
supervisor_started: AtomicBool::new(false),
|
||||
recovery_count: AtomicU64::new(0),
|
||||
@@ -474,6 +490,9 @@ impl Federation {
|
||||
} else if recovery_only {
|
||||
return Ok(false);
|
||||
}
|
||||
*lock(&self.podcast_dht) = None;
|
||||
self.podcast_discovery_failed
|
||||
.store(false, Ordering::Relaxed);
|
||||
stop_running(guard.take()).await;
|
||||
} else if recovery_only {
|
||||
tracing::warn!(network = %network_name, "retrying stopped federation service");
|
||||
@@ -498,6 +517,7 @@ impl Federation {
|
||||
.schema_independent_stream_protocol(SIMILARITY_ALPN)
|
||||
.schema_independent_stream_protocol(SIMILARITY_DHT_ALPN)
|
||||
.schema_independent_stream_protocol(CAPABILITIES_ALPN)
|
||||
.schema_independent_stream_protocol(PODCAST_DHT_ALPN)
|
||||
.build()
|
||||
.map_err(|err| anyhow::anyhow!("invalid federation config: {err}"))?;
|
||||
let (service, mut events) =
|
||||
@@ -530,6 +550,17 @@ impl Federation {
|
||||
Arc::clone(&similarity_manager),
|
||||
));
|
||||
|
||||
// Public podcast discovery: the server node publishes the podcasts
|
||||
// accounts explicitly shared (see `crate::podcasts::discovery`),
|
||||
// after the first device-sync round of this run.
|
||||
let (first_device_sync_done, first_device_sync) = tokio::sync::watch::channel(false);
|
||||
let podcast_discovery =
|
||||
podcast_discovery::start(&service, &self.data_dir, pool.clone(), first_device_sync)
|
||||
.await;
|
||||
let podcast_dht = podcast_discovery
|
||||
.as_ref()
|
||||
.map(|discovery| Arc::clone(&discovery.dht));
|
||||
|
||||
// Drain DHT events into the log; the channel is bounded.
|
||||
let event_task = tokio::spawn(async move {
|
||||
while let Some(event) = events.recv().await {
|
||||
@@ -584,11 +615,15 @@ impl Federation {
|
||||
Arc::clone(&service),
|
||||
device_hub,
|
||||
Arc::clone(&self.transport_stats),
|
||||
first_device_sync_done,
|
||||
));
|
||||
let capabilities_acceptor = service
|
||||
.stream_acceptor(CAPABILITIES_ALPN)
|
||||
.map_err(|err| anyhow::anyhow!("failed to take the capabilities acceptor: {err}"))?;
|
||||
let capabilities_task = tokio::spawn(capabilities::serve(capabilities_acceptor));
|
||||
let capabilities_task = tokio::spawn(capabilities::serve(
|
||||
capabilities_acceptor,
|
||||
podcast_dht.is_some(),
|
||||
));
|
||||
let similarity_acceptor = service
|
||||
.stream_acceptor(SIMILARITY_ALPN)
|
||||
.map_err(|err| anyhow::anyhow!("failed to take the similarity acceptor: {err}"))?;
|
||||
@@ -599,23 +634,31 @@ impl Federation {
|
||||
Arc::clone(&self.transport_stats),
|
||||
));
|
||||
|
||||
let mut tasks = vec![
|
||||
event_task,
|
||||
sync_task,
|
||||
audio_task,
|
||||
catalog_task,
|
||||
device_task,
|
||||
device_sync_task,
|
||||
capabilities_task,
|
||||
similarity_task,
|
||||
similarity_dht_serve_task,
|
||||
similarity_dht_maintenance_task,
|
||||
similarity_dht_sync_task,
|
||||
];
|
||||
if let Some(discovery) = podcast_discovery {
|
||||
tasks.extend(discovery.tasks);
|
||||
}
|
||||
*lock(&self.podcast_dht) = podcast_dht.clone();
|
||||
self.podcast_discovery_failed
|
||||
.store(podcast_dht.is_none(), Ordering::Relaxed);
|
||||
*guard = Some(Running {
|
||||
service,
|
||||
similarity_dht,
|
||||
podcast_dht,
|
||||
network_name,
|
||||
tasks: vec![
|
||||
event_task,
|
||||
sync_task,
|
||||
audio_task,
|
||||
catalog_task,
|
||||
device_task,
|
||||
device_sync_task,
|
||||
capabilities_task,
|
||||
similarity_task,
|
||||
similarity_dht_serve_task,
|
||||
similarity_dht_maintenance_task,
|
||||
similarity_dht_sync_task,
|
||||
],
|
||||
tasks,
|
||||
});
|
||||
self.set_error(None);
|
||||
drop(guard);
|
||||
@@ -659,6 +702,9 @@ impl Federation {
|
||||
|
||||
async fn stop(&self) {
|
||||
let mut guard = self.running.lock().await;
|
||||
*lock(&self.podcast_dht) = None;
|
||||
self.podcast_discovery_failed
|
||||
.store(false, Ordering::Relaxed);
|
||||
stop_running(guard.take()).await;
|
||||
}
|
||||
|
||||
@@ -685,6 +731,18 @@ impl Federation {
|
||||
.context("federation is not running")
|
||||
}
|
||||
|
||||
/// The public podcast-discovery overlay, while the node runs it. Does
|
||||
/// not wait for a node that is starting or stopping.
|
||||
pub fn podcast_discovery(&self) -> Option<Arc<PodcastDht>> {
|
||||
lock(&self.podcast_dht).clone()
|
||||
}
|
||||
|
||||
/// Whether the node runs but its podcast-discovery overlay could not
|
||||
/// start (`podcast_discovery()` is then `None` too).
|
||||
pub fn podcast_discovery_unavailable(&self) -> bool {
|
||||
self.podcast_discovery_failed.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
async fn spawn_sync_soon(self: &Arc<Self>) {
|
||||
if let Ok(service) = self.service().await {
|
||||
let fed = Arc::clone(self);
|
||||
@@ -1018,6 +1076,8 @@ impl Federation {
|
||||
"last_rendezvous_error": health.last_rendezvous_error,
|
||||
"recovery_count": self.recovery_count.load(Ordering::Relaxed),
|
||||
"similarity_routing_peers": running.similarity_dht.known_peers(),
|
||||
"podcast_discovery": running.podcast_dht.is_some(),
|
||||
"podcast_discovery_peers": running.podcast_dht.as_ref().map(|dht| dht.known_peers()),
|
||||
"published_items": published,
|
||||
"transport": transport,
|
||||
})
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
//! Runs the public podcast-discovery overlay (`music_dht::podcast_dht`) on
|
||||
//! the web server's federation node.
|
||||
//!
|
||||
//! The overlay shares the node's iroh endpoint and identity with the music
|
||||
//! DHT, like `similarity_dht`: its ALPN is registered as a
|
||||
//! schema-independent stream protocol, its routing contacts and replica
|
||||
//! cache live in `podcast-dht.sqlite3` in the federation data directory (a
|
||||
//! replaceable cache, not durable state), and the capability manifest
|
||||
//! advertises `podcast_dht` only while it runs. What the node publishes is
|
||||
//! decided by [`crate::podcasts::discovery`] for all accounts.
|
||||
//!
|
||||
//! Failing to open the overlay disables public discovery and nothing else:
|
||||
//! the federation node, the music DHT and private podcast sync keep running.
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use music_dht::MusicDhtService;
|
||||
use music_dht::podcast_dht::{PODCAST_DHT_ALPN, PodcastDht};
|
||||
use sqlx::PgPool;
|
||||
use tokio::sync::watch;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
/// Cache file of the overlay in the federation data directory.
|
||||
pub(crate) const CACHE_FILE: &str = "podcast-dht.sqlite3";
|
||||
|
||||
/// The running overlay and its tasks (serving, maintenance, publication
|
||||
/// reconciler, capability probes).
|
||||
pub(crate) struct PodcastDiscovery {
|
||||
pub(crate) dht: Arc<PodcastDht>,
|
||||
pub(crate) tasks: Vec<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
/// Opens the overlay on `service` (whose config registered
|
||||
/// [`PODCAST_DHT_ALPN`]) and starts its tasks. Returns `None`, with a
|
||||
/// warning, when the cache cannot be opened or the acceptor is unavailable.
|
||||
///
|
||||
/// `first_device_sync` turns `true` after the first device-sync round of
|
||||
/// this federation run; the publication reconciler waits for it.
|
||||
pub(crate) async fn start(
|
||||
service: &Arc<MusicDhtService>,
|
||||
data_dir: &Path,
|
||||
pool: PgPool,
|
||||
first_device_sync: watch::Receiver<bool>,
|
||||
) -> Option<PodcastDiscovery> {
|
||||
let (dht, serve_task) = open(service, data_dir).await?;
|
||||
let tasks = vec![
|
||||
serve_task,
|
||||
tokio::spawn(Arc::clone(&dht).maintenance()),
|
||||
tokio::spawn(crate::podcasts::discovery::run(
|
||||
pool,
|
||||
Arc::clone(&dht),
|
||||
first_device_sync,
|
||||
)),
|
||||
tokio::spawn(super::capabilities::probe_loop(
|
||||
Arc::clone(service),
|
||||
Arc::clone(&dht),
|
||||
)),
|
||||
];
|
||||
tracing::info!("public podcast discovery started");
|
||||
Some(PodcastDiscovery { dht, tasks })
|
||||
}
|
||||
|
||||
/// Opens the overlay cache and serves overlay requests; the rest of
|
||||
/// [`start`] (maintenance, reconciler, probes) is not started.
|
||||
pub(crate) async fn open(
|
||||
service: &Arc<MusicDhtService>,
|
||||
data_dir: &Path,
|
||||
) -> Option<(Arc<PodcastDht>, JoinHandle<()>)> {
|
||||
let dht = match PodcastDht::open(Arc::clone(service), data_dir.join(CACHE_FILE)).await {
|
||||
Ok(dht) => dht,
|
||||
Err(error) => {
|
||||
tracing::warn!("public podcast discovery disabled: cannot open its cache: {error}");
|
||||
// Refuse overlay streams instead of queueing them.
|
||||
drop(service.stream_acceptor(PODCAST_DHT_ALPN));
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let acceptor = match service.stream_acceptor(PODCAST_DHT_ALPN) {
|
||||
Ok(acceptor) => acceptor,
|
||||
Err(error) => {
|
||||
tracing::warn!("public podcast discovery disabled: {error}");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let serve_task = tokio::spawn(Arc::clone(&dht).serve(acceptor));
|
||||
Some((dht, serve_task))
|
||||
}
|
||||
@@ -409,6 +409,29 @@ translations! {
|
||||
player_podcast_unsubscribe: "Unsubscribe", "Отписаться";
|
||||
player_podcast_unsubscribe_confirm: "Unsubscribe from “{title}”? The feed will no longer be checked. Your progress and listening history are kept: subscribing again continues where you stopped.", "Отписаться от «{title}»? Лента больше не будет проверяться. Прогресс и история прослушиваний сохранятся: при повторной подписке вы продолжите с того же места.";
|
||||
player_podcast_subscribe: "Subscribe", "Подписаться";
|
||||
player_podcast_share: "Share in the federation", "Поделиться в федерации";
|
||||
player_podcast_unshare: "Stop sharing", "Перестать делиться";
|
||||
player_podcast_sharing: "Saving…", "Сохраняем…";
|
||||
player_podcast_shared_badge: "Sharing on", "Публикация включена";
|
||||
player_podcast_share_private: "Private: only your own devices see this subscription. Sharing publishes the podcast card (title, description, artwork, feed URL) from this server so that other federation members can find it and subscribe. Your progress, history and account are never published.", "Приватно: подписку видят только ваши устройства. «Поделиться» публикует карточку подкаста (название, описание, обложку, адрес RSS-ленты) от имени этого сервера, чтобы другие участники федерации могли найти его и подписаться. Ваш прогресс, история и аккаунт не публикуются.";
|
||||
player_podcast_share_published: "Published by this server: other federation members can find this podcast and subscribe. Your progress, history and account stay private.", "Опубликовано этим сервером: участники федерации могут найти этот подкаст и подписаться. Ваш прогресс, история и аккаунт остаются приватными.";
|
||||
player_podcast_share_publishing: "Shared: the server is publishing the card…", "Включено: сервер публикует карточку…";
|
||||
player_podcast_share_discovery_off: "Shared: this server publishes the card once federation is enabled on it.", "Включено: этот сервер опубликует карточку, когда на нём включат федерацию.";
|
||||
player_podcast_share_discovery_unavailable: "Shared, but this server cannot publish the card: public podcast discovery failed to start (see the federation status).", "Включено, но этот сервер не может опубликовать карточку: публичный поиск подкастов не запустился (см. статус федерации).";
|
||||
player_podcast_share_over_limit: "Shared, but not published: this server already publishes as many podcasts as it can, and your part of them is used. The card is published when a place frees up.", "Включено, но не опубликовано: сервер уже публикует столько подкастов, сколько может, и ваша доля занята. Карточка появится, когда освободится место.";
|
||||
player_podcast_share_failed: "Shared, but this server could not publish the card. It tries again regularly.", "Включено, но этот сервер не смог опубликовать карточку. Он регулярно пробует снова.";
|
||||
player_podcast_share_pending: "Sharing is on, but the feed URL may contain private data. This server does not publish it until you confirm here; your other players where you already confirmed may publish it themselves.", "Публикация включена, но адрес ленты может содержать личные данные. Этот сервер не опубликует его, пока вы не подтвердите здесь; ваши другие плееры, где вы уже подтвердили, могут публиковать его сами.";
|
||||
player_podcast_share_review: "Review and publish", "Проверить и опубликовать";
|
||||
player_podcast_share_unavailable: "This podcast cannot be published", "Этот подкаст нельзя опубликовать";
|
||||
player_podcast_share_dialog_title: "Publish this feed URL?", "Опубликовать адрес ленты?";
|
||||
player_podcast_share_dialog_text: "The podcast card is visible to everyone in the federation, and anyone can subscribe with its feed URL. This URL may contain private data:", "Карточку подкаста увидят все участники федерации, и любой сможет подписаться по адресу ленты. Похоже, этот адрес содержит личные данные:";
|
||||
player_podcast_share_dialog_hint: "If this is a personal feed (for example, a paid or member subscription), do not publish it: others could use your access.", "Если это личная лента (например, платная подписка или лента для участников), не публикуйте её: другие смогут воспользоваться вашим доступом.";
|
||||
player_podcast_share_dialog_confirm: "Publish anyway", "Всё равно опубликовать";
|
||||
player_podcast_warning_secret_query: "the query parameter «{name}» looks like an access key", "параметр «{name}» похож на ключ доступа";
|
||||
player_podcast_warning_private_host: "{host} usually serves personal subscriber feeds", "{host} обычно отдаёт личные ленты подписчиков";
|
||||
player_podcast_warning_entropy_query: "the value of the parameter «{name}» looks like a secret token", "значение параметра «{name}» похоже на секретный токен";
|
||||
player_podcast_warning_entropy_path: "a part of the address looks like a secret token", "часть адреса похожа на секретный токен";
|
||||
player_podcast_warning_other: "the address looks like a personal feed", "адрес похож на личную ленту";
|
||||
player_podcast_all_seasons: "All seasons", "Все сезоны";
|
||||
player_podcast_season: "Season", "Сезон";
|
||||
player_podcast_no_season: "No season", "Без сезона";
|
||||
@@ -440,6 +463,20 @@ translations! {
|
||||
player_podcast_loading: "Preparing the episode…", "Готовим выпуск…";
|
||||
player_podcast_feed: "Feed", "Лента";
|
||||
player_podcast_request_failed: "Request failed", "Запрос не выполнен";
|
||||
player_podcast_found_title: "Podcasts", "Подкасты";
|
||||
player_podcast_type: "Podcast", "Подкаст";
|
||||
player_podcast_found_failed: "The podcast search did not answer this time; music results are complete.", "Поиск подкастов сейчас не ответил; результаты по музыке полные.";
|
||||
player_podcast_found_subscribed: "Already subscribed", "Уже подписан";
|
||||
player_podcast_found_open: "Open", "Открыть";
|
||||
player_podcast_found_subscribing: "Subscribing…", "Подписываемся…";
|
||||
player_podcast_found_done: "Subscribed. The subscription is private: only your devices see it.", "Вы подписались. Подписка приватная: её видят только ваши устройства.";
|
||||
player_podcast_found_note: "This card was published by a federation member; its title, description and artwork are their claims. Subscribing reads the episodes from the feed below, never from the member, and your subscription stays private.", "Эту карточку опубликовал участник федерации; название, описание и обложка — его данные. При подписке выпуски загружаются из ленты по адресу ниже, а не у участника, и ваша подписка остаётся приватной.";
|
||||
player_podcast_found_feed: "Feed URL", "Адрес ленты";
|
||||
player_podcast_found_loading: "Loading seasons and episodes from the feed…", "Загружаем сезоны и выпуски из ленты…";
|
||||
player_podcast_found_retry: "Try again", "Повторить";
|
||||
player_podcast_found_latest: "Latest episode", "Последний выпуск";
|
||||
player_podcast_found_more: "and {count} more", "и ещё {count}";
|
||||
player_podcast_found_moved: "The feed has moved; subscribing follows it.", "Лента переехала; подписка последует за ней.";
|
||||
player_shuffle: "Shuffle" , "Перемешать";
|
||||
player_repeat: "Repeat" , "Повтор";
|
||||
player_volume: "Volume" , "Громкость";
|
||||
|
||||
@@ -3012,6 +3012,46 @@ pub mod db_migrations {
|
||||
.build()];
|
||||
}
|
||||
|
||||
// -- M0058: local privacy confirmations of shared podcasts -------------
|
||||
|
||||
// Client-held `privacy_confirmed` of public podcast discovery (MR-W3);
|
||||
// see `crate::podcasts::discovery`. Never synced.
|
||||
#[cot::db::migrations::migration_op]
|
||||
async fn create_podcast_share_confirmation(
|
||||
ctx: migrations::MigrationContext<'_>,
|
||||
) -> cot::db::Result<()> {
|
||||
for sql in crate::podcasts::schema::CREATE_SHARE_CONFIRMATION {
|
||||
ctx.db.raw(sql).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cot::db::migrations::migration_op]
|
||||
async fn drop_podcast_share_confirmation(
|
||||
ctx: migrations::MigrationContext<'_>,
|
||||
) -> cot::db::Result<()> {
|
||||
for sql in crate::podcasts::schema::DROP_SHARE_CONFIRMATION {
|
||||
ctx.db.raw(sql).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct M0058CreatePodcastShareConfirmation;
|
||||
impl migrations::Migration for M0058CreatePodcastShareConfirmation {
|
||||
const APP_NAME: &'static str = "furumusic";
|
||||
const MIGRATION_NAME: &'static str = "m_0058_create_podcast_share_confirmation";
|
||||
const DEPENDENCIES: &'static [migrations::MigrationDependency] =
|
||||
&[migrations::MigrationDependency::migration(
|
||||
"furumusic",
|
||||
"m_0057_import_podcast_relay",
|
||||
)];
|
||||
const OPERATIONS: &'static [Operation] =
|
||||
&[Operation::custom(create_podcast_share_confirmation)
|
||||
.backwards(drop_podcast_share_confirmation)
|
||||
.build()];
|
||||
}
|
||||
|
||||
pub const MIGRATIONS: &[&SyncDynMigration] = &[
|
||||
&M0006CreateMediaFile,
|
||||
&M0007CreateArtist,
|
||||
@@ -3059,5 +3099,6 @@ pub mod db_migrations {
|
||||
&M0055CreateFedPodcastRelay,
|
||||
&M0056CreatePodcastStore,
|
||||
&M0057ImportPodcastRelay,
|
||||
&M0058CreatePodcastShareConfirmation,
|
||||
];
|
||||
}
|
||||
+32
-8
@@ -7472,7 +7472,7 @@ async fn federation_search_events_handler(
|
||||
db: Database,
|
||||
query: cot::request::extractors::UrlQuery<SearchQuery>,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
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_owned();
|
||||
@@ -7482,6 +7482,7 @@ async fn federation_search_events_handler(
|
||||
|
||||
let search_id = uuid::Uuid::new_v4().to_string();
|
||||
let body_search_id = search_id.clone();
|
||||
let user_id = user.id;
|
||||
let body = Body::streaming(async_stream::try_stream! {
|
||||
let started = serde_json::json!({
|
||||
"search_id": body_search_id,
|
||||
@@ -7492,25 +7493,40 @@ async fn federation_search_events_handler(
|
||||
});
|
||||
yield Bytes::from(format!("event: search.started\ndata: {started}\n\n"));
|
||||
|
||||
match crate::federation::handle().search_events(&body_search_id, &q).await {
|
||||
Ok(events) => {
|
||||
let federation = crate::federation::handle();
|
||||
// Public podcasts are searched next to the music DHT; their events
|
||||
// follow the music events.
|
||||
let podcasts = federation.spawn_podcast_search(&q);
|
||||
match federation.search_events(&body_search_id, &q).await {
|
||||
Ok(music) => {
|
||||
let mut last_sequence = 0;
|
||||
for event in events {
|
||||
for event in &music.events {
|
||||
last_sequence = event.sequence;
|
||||
let data = serde_json::to_string(&event)
|
||||
.map_err(|err| cot::Error::internal(err.to_string()))?;
|
||||
yield Bytes::from(format!("event: {}\ndata: {data}\n\n", event.kind));
|
||||
yield search_event_frame(event)?;
|
||||
}
|
||||
let podcasts = podcasts
|
||||
.await
|
||||
.unwrap_or(crate::federation::client::PodcastSearch::Failed);
|
||||
// A podcast search or ranking failure only drops the
|
||||
// podcast hits: `search.completed` reports it.
|
||||
let (podcast_discovery, podcast_events) = federation
|
||||
.podcast_search_events(&body_search_id, &q, user_id, &music, podcasts)
|
||||
.await;
|
||||
for event in &podcast_events {
|
||||
last_sequence = event.sequence;
|
||||
yield search_event_frame(event)?;
|
||||
}
|
||||
let completed = serde_json::json!({
|
||||
"search_id": body_search_id,
|
||||
"sequence": last_sequence + 1,
|
||||
"kind": "search.completed",
|
||||
"entity_key": null,
|
||||
"entity": { "partial": false },
|
||||
"entity": { "partial": false, "podcast_discovery": podcast_discovery },
|
||||
});
|
||||
yield Bytes::from(format!("event: search.completed\ndata: {completed}\n\n"));
|
||||
}
|
||||
Err(err) => {
|
||||
podcasts.abort();
|
||||
let failed = serde_json::json!({
|
||||
"search_id": body_search_id,
|
||||
"sequence": 1,
|
||||
@@ -7531,6 +7547,14 @@ async fn federation_search_events_handler(
|
||||
.map_err(|err| cot::Error::internal(err.to_string()))
|
||||
}
|
||||
|
||||
/// One server-sent event of a federated search: `event: <kind>` and the
|
||||
/// event as JSON.
|
||||
fn search_event_frame(event: &crate::federation::client::SearchEvent) -> cot::Result<Bytes> {
|
||||
let data = serde_json::to_string(event).map_err(|err| cot::Error::internal(err.to_string()))?;
|
||||
let kind = event.kind;
|
||||
Ok(Bytes::from(format!("event: {kind}\ndata: {data}\n\n")))
|
||||
}
|
||||
|
||||
async fn federation_artist_events_handler(
|
||||
auth_ctx: auth::AuthContext,
|
||||
session: Session,
|
||||
|
||||
+92
-4
@@ -246,6 +246,9 @@ impl PodcastStore {
|
||||
})
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
// A new subscription is private, but it supersedes an earlier share
|
||||
// decision of a former subscription.
|
||||
super::discovery::request_reconcile();
|
||||
Ok(SubscribeOutcome {
|
||||
podcast_id,
|
||||
already_subscribed: false,
|
||||
@@ -294,7 +297,9 @@ impl PodcastStore {
|
||||
.await?;
|
||||
let revoke_payload = share_payload(&mut tx, user_id, podcast_id, false).await?;
|
||||
let revoke = emit(&mut tx, user_id, &device_id, |_| revoke_payload.clone()).await?;
|
||||
super::discovery::clear_confirmation(&mut tx, user_id, podcast_id).await?;
|
||||
tx.commit().await?;
|
||||
super::discovery::request_reconcile();
|
||||
Ok(vec![unsubscribe, revoke])
|
||||
}
|
||||
|
||||
@@ -304,9 +309,10 @@ impl PodcastStore {
|
||||
/// is already effectively shared (`effective_share`). Revoking is a no-op
|
||||
/// when no share decision says `true`. The write uses the next podcast
|
||||
/// generation, so it orders after the subscription it observed.
|
||||
// The publication toggle is part of public discovery (MR-W3); until
|
||||
// then only the tests call it.
|
||||
#[cfg_attr(not(test), allow(dead_code))]
|
||||
///
|
||||
/// Revoking also forgets the account's privacy confirmation, so sharing
|
||||
/// again asks again. Publishing and revoking the card is the
|
||||
/// reconciler's job (`super::discovery`), which this requests.
|
||||
pub(crate) async fn set_shared(
|
||||
&self,
|
||||
user_id: i64,
|
||||
@@ -332,10 +338,93 @@ impl PodcastStore {
|
||||
}
|
||||
let payload = share_payload(&mut tx, user_id, podcast_id, shared).await?;
|
||||
let op = emit(&mut tx, user_id, &device_id, |_| payload.clone()).await?;
|
||||
if !shared {
|
||||
super::discovery::clear_confirmation(&mut tx, user_id, podcast_id).await?;
|
||||
}
|
||||
tx.commit().await?;
|
||||
super::discovery::request_reconcile();
|
||||
Ok(vec![op])
|
||||
}
|
||||
|
||||
/// Shares a podcast whose card has privacy warnings, with the
|
||||
/// account's confirmation of exactly `feed_url` / `id_feed_url`.
|
||||
///
|
||||
/// In one transaction: the card the account would publish now is
|
||||
/// rebuilt and compared with the confirmed URLs (a feed that moved
|
||||
/// meanwhile returns `Ok(None)` and writes nothing, so the user is asked
|
||||
/// about the new URLs); the share decision is written like
|
||||
/// [`PodcastStore::set_shared`] when the podcast is not already
|
||||
/// effectively shared; the confirmation is stored for the resulting
|
||||
/// share decision (`ShareRecord::op_id`). Returns the emitted operations.
|
||||
pub(crate) async fn share_confirmed(
|
||||
&self,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
feed_url: &FeedUrl,
|
||||
id_feed_url: Option<&FeedUrl>,
|
||||
now_ms: i64,
|
||||
) -> Result<Option<Vec<SyncOpWire>>> {
|
||||
let device_id = devices::local_device_id(&self.pool, user_id).await?;
|
||||
let mut tx = self.begin_write(user_id).await?;
|
||||
let Some(account) =
|
||||
super::discovery::load_account_share(&mut tx, user_id, podcast_id).await?
|
||||
else {
|
||||
return Err(PodcastStoreError::NotSubscribed(podcast_id.clone()).into());
|
||||
};
|
||||
if !account.subscription.subscribed {
|
||||
return Err(PodcastStoreError::NotSubscribed(podcast_id.clone()).into());
|
||||
}
|
||||
let card = account.card.as_ref().map_err(invalid)?;
|
||||
if card.feed_url != *feed_url || card.id_feed_url.as_ref() != id_feed_url {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut ops = Vec::new();
|
||||
let share_op_id = if account.is_shared() {
|
||||
account
|
||||
.share
|
||||
.as_ref()
|
||||
.map(|share| share.op_id.clone())
|
||||
.expect("an effective share has a share register")
|
||||
} else {
|
||||
let payload = share_payload(&mut tx, user_id, podcast_id, true).await?;
|
||||
let op = emit(&mut tx, user_id, &device_id, |_| payload.clone()).await?;
|
||||
let op_id = op.op_id.clone();
|
||||
ops.push(op);
|
||||
op_id
|
||||
};
|
||||
let confirmation = super::discovery::ShareConfirmation {
|
||||
share_op_id,
|
||||
feed_url: card.feed_url.clone(),
|
||||
id_feed_url: card.id_feed_url.clone(),
|
||||
};
|
||||
super::discovery::store_confirmation(&mut tx, user_id, podcast_id, &confirmation, now_ms)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
super::discovery::request_reconcile();
|
||||
Ok(Some(ops))
|
||||
}
|
||||
|
||||
/// Forgets the account's privacy confirmation of a podcast.
|
||||
pub(crate) async fn clear_share_confirmation(
|
||||
&self,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
) -> Result<()> {
|
||||
let mut conn = self.pool.acquire().await?;
|
||||
super::discovery::clear_confirmation(&mut conn, user_id, podcast_id).await
|
||||
}
|
||||
|
||||
/// One account's publication view of a podcast (registers, card,
|
||||
/// confirmation); `None` without a subscription register.
|
||||
pub(crate) async fn account_share(
|
||||
&self,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
) -> Result<Option<super::discovery::AccountShare>> {
|
||||
let mut conn = self.pool.acquire().await?;
|
||||
super::discovery::load_account_share(&mut conn, user_id, podcast_id).await
|
||||
}
|
||||
|
||||
async fn explicit_progress(
|
||||
&self,
|
||||
user_id: i64,
|
||||
@@ -678,7 +767,6 @@ impl PodcastStore {
|
||||
/// Resolves a feed URL to a podcast id: the existing podcast when the
|
||||
/// URL is a known feed URL or alias (`resolve_podcast_id`), otherwise
|
||||
/// the id a first subscription through this URL would derive.
|
||||
#[cfg(test)]
|
||||
pub(crate) async fn resolve_podcast_id(
|
||||
&self,
|
||||
user_id: i64,
|
||||
|
||||
+354
-2
@@ -24,11 +24,14 @@ use music_dht::podcast::{
|
||||
EpisodeId, EpisodeType, FeedUrl, ListenSession, PodcastEpisodeRef, PodcastId, PodcastMeta,
|
||||
ProgressRecord, ProgressStatus, reaches_completion,
|
||||
};
|
||||
use music_dht::podcast_dht::FeedUrlPrivacyWarning;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::discovery::Discovery;
|
||||
use super::fetch::{FeedFetcher, FetchErrorKind};
|
||||
use super::runner::{self, SubscribeUrlError};
|
||||
use super::search;
|
||||
use super::{
|
||||
CheckpointOutcome, EpisodeListItem, EpisodeQuery, EpisodeSort, FetchState, PlayedFilter,
|
||||
PodcastListFilter, PodcastStore, PodcastStoreError, PodcastSummary, ProgressSession,
|
||||
@@ -162,7 +165,8 @@ pub(crate) struct PodcastView {
|
||||
pub(crate) link: Option<String>,
|
||||
pub(crate) image_url: Option<String>,
|
||||
pub(crate) subscribed: bool,
|
||||
/// `effective_share` (publication is MR-W3; shown read-only).
|
||||
/// The account's synced share decision applies (`effective_share`).
|
||||
/// Whether the card is actually published is [`ShareView`].
|
||||
pub(crate) shared: bool,
|
||||
pub(crate) episode_count: u64,
|
||||
pub(crate) unplayed_count: u64,
|
||||
@@ -376,18 +380,23 @@ pub(crate) struct HistoryView {
|
||||
#[derive(Debug, Serialize)]
|
||||
pub(crate) struct PodcastDetail {
|
||||
pub(crate) podcast: PodcastView,
|
||||
/// Public discovery state of the podcast for this account.
|
||||
pub(crate) share: ShareView,
|
||||
pub(crate) seasons: Vec<SeasonView>,
|
||||
/// Newest listening sessions of this podcast, from every device.
|
||||
pub(crate) history: Vec<HistoryView>,
|
||||
}
|
||||
|
||||
/// One podcast with its season groups.
|
||||
/// One podcast with its season groups and its public discovery state
|
||||
/// (`discovery`: whether the server's overlay runs).
|
||||
pub(crate) async fn podcast_detail(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
discovery: Discovery<'_>,
|
||||
) -> ApiResult<PodcastDetail> {
|
||||
let podcast = podcast_view(store, user_id, podcast_id).await?;
|
||||
let share = share_view(store, user_id, podcast_id, discovery).await?;
|
||||
let seasons = store
|
||||
.seasons(user_id, podcast_id)
|
||||
.await?
|
||||
@@ -409,6 +418,7 @@ pub(crate) async fn podcast_detail(
|
||||
.collect();
|
||||
Ok(PodcastDetail {
|
||||
podcast,
|
||||
share,
|
||||
seasons,
|
||||
history,
|
||||
})
|
||||
@@ -476,6 +486,348 @@ pub(crate) async fn unsubscribe(
|
||||
podcast_view(store, user_id, podcast_id).await
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sharing (public discovery)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Public discovery state of one podcast for the signed-in account.
|
||||
///
|
||||
/// Sharing is the account's synced decision; the card is published by the
|
||||
/// server's federation node (see [`super::discovery`]) and contains only
|
||||
/// the podcast's public metadata.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub(crate) struct ShareView {
|
||||
/// The account's share decision applies (`effective_share`).
|
||||
pub(crate) shared: bool,
|
||||
/// Shared, but the card's privacy warnings are not confirmed by this
|
||||
/// account for the current decision and URLs: not published for it.
|
||||
pub(crate) pending_confirmation: bool,
|
||||
/// Whether the account can share the podcast now (subscribed and the
|
||||
/// card can be built).
|
||||
pub(crate) can_share: bool,
|
||||
/// Why the card cannot be built, if it cannot.
|
||||
pub(crate) card_error: Option<String>,
|
||||
/// Why the card's feed URLs may be private (`PodcastCardSpec::
|
||||
/// privacy_warnings`); enabling sharing must be confirmed when present.
|
||||
pub(crate) warnings: Vec<FeedUrlPrivacyWarning>,
|
||||
/// Feed URL the card would publish.
|
||||
pub(crate) feed_url: Option<String>,
|
||||
/// Origin URL the podcast id comes from, when the feed moved.
|
||||
pub(crate) id_feed_url: Option<String>,
|
||||
/// `running` when the server's node runs public discovery,
|
||||
/// `unavailable` when the node runs but the overlay failed to start,
|
||||
/// `off` when federation is not running.
|
||||
pub(crate) discovery: &'static str,
|
||||
/// This server's node currently publishes a card of this podcast and
|
||||
/// this account shares it. Only this server's own publication: another
|
||||
/// player of the account (a TUI) publishes its own card from its own
|
||||
/// node, which this flag does not show.
|
||||
pub(crate) published: bool,
|
||||
/// Why the server does not publish a shared, publishable podcast after
|
||||
/// its last round: `over_limit` (beyond this account's part of the
|
||||
/// server's publication budget) or `failed` (the card could not be
|
||||
/// signed or stored). `null` otherwise.
|
||||
pub(crate) held: Option<&'static str>,
|
||||
}
|
||||
|
||||
/// The account's [`ShareView`] of a podcast.
|
||||
pub(crate) async fn share_view(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
discovery: Discovery<'_>,
|
||||
) -> ApiResult<ShareView> {
|
||||
let account = store.account_share(user_id, podcast_id).await?;
|
||||
let mut view = ShareView {
|
||||
shared: false,
|
||||
pending_confirmation: false,
|
||||
can_share: false,
|
||||
card_error: None,
|
||||
warnings: Vec::new(),
|
||||
feed_url: None,
|
||||
id_feed_url: None,
|
||||
discovery: discovery.state(),
|
||||
published: false,
|
||||
held: None,
|
||||
};
|
||||
let Some(account) = account else {
|
||||
return Ok(view);
|
||||
};
|
||||
view.shared = account.is_shared();
|
||||
view.pending_confirmation = account.pending_confirmation();
|
||||
view.warnings = account.warnings();
|
||||
match &account.card {
|
||||
Ok(card) => {
|
||||
view.can_share = account.subscription.subscribed;
|
||||
view.feed_url = Some(card.feed_url.to_string());
|
||||
view.id_feed_url = card.id_feed_url.as_ref().map(ToString::to_string);
|
||||
}
|
||||
Err(error) => view.card_error = Some(error.clone()),
|
||||
}
|
||||
if let Some(dht) = discovery.dht()
|
||||
&& account.publishable()
|
||||
{
|
||||
// The overlay's cache is not essential to the podcast page: when it
|
||||
// cannot be read, the card shows as not published yet.
|
||||
view.published = match dht.published().await {
|
||||
Ok(publications) => publications
|
||||
.iter()
|
||||
.any(|publication| publication.active && publication.podcast_id == *podcast_id),
|
||||
Err(error) => {
|
||||
tracing::warn!("podcast publications unavailable: {error}");
|
||||
false
|
||||
}
|
||||
};
|
||||
if !view.published {
|
||||
view.held = super::discovery::held_reason(podcast_id);
|
||||
}
|
||||
}
|
||||
Ok(view)
|
||||
}
|
||||
|
||||
/// URLs the user confirmed in the privacy dialog.
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub(crate) struct ShareConfirmRequest {
|
||||
pub(crate) feed_url: String,
|
||||
#[serde(default)]
|
||||
pub(crate) id_feed_url: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
struct ShareRequest {
|
||||
shared: bool,
|
||||
/// Confirmation of the card's URLs despite its privacy warnings.
|
||||
#[serde(default)]
|
||||
confirm: Option<ShareConfirmRequest>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub(crate) struct ShareResponse {
|
||||
pub(crate) podcast: PodcastView,
|
||||
pub(crate) share: ShareView,
|
||||
}
|
||||
|
||||
/// Result of [`set_share`].
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum ShareOutcome {
|
||||
/// The decision is stored (or was already in place).
|
||||
Done(Box<ShareResponse>),
|
||||
/// The card's feed URLs look private: nothing was written; ask the user
|
||||
/// and send the shown URLs back as `confirm` (HTTP 409).
|
||||
NeedsConfirmation(ShareView),
|
||||
}
|
||||
|
||||
/// "Share in the federation": `{ "shared": bool, "confirm"?: {feed_url,
|
||||
/// id_feed_url} }`.
|
||||
///
|
||||
/// * Off: writes `shared = false` (no-op when not shared) and forgets the
|
||||
/// confirmation; the reconciler revokes the card unless another account
|
||||
/// still shares the podcast.
|
||||
/// * On: needs an active subscription (409). When the card has privacy
|
||||
/// warnings, `confirm` must name exactly the card's current URLs;
|
||||
/// otherwise nothing is written and [`ShareOutcome::NeedsConfirmation`]
|
||||
/// carries the warnings. A podcast already shared from another device
|
||||
/// but pending this account's confirmation is confirmed the same way.
|
||||
pub(crate) async fn set_share(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
body: Value,
|
||||
discovery: Discovery<'_>,
|
||||
now_ms: i64,
|
||||
) -> ApiResult<ShareOutcome> {
|
||||
let request: ShareRequest = decode(body)?;
|
||||
if store.podcast(user_id, podcast_id).await?.is_none() {
|
||||
return Err(ApiError::not_found("unknown podcast"));
|
||||
}
|
||||
if request.shared {
|
||||
let Some(account) = store.account_share(user_id, podcast_id).await? else {
|
||||
return Err(
|
||||
anyhow::Error::from(PodcastStoreError::NotSubscribed(podcast_id.clone())).into(),
|
||||
);
|
||||
};
|
||||
if !account.subscription.subscribed {
|
||||
return Err(
|
||||
anyhow::Error::from(PodcastStoreError::NotSubscribed(podcast_id.clone())).into(),
|
||||
);
|
||||
}
|
||||
if let Err(error) = &account.card {
|
||||
return Err(ApiError::new(
|
||||
422,
|
||||
format!("this podcast cannot be published: {error}"),
|
||||
));
|
||||
}
|
||||
if account.warnings().is_empty() {
|
||||
store.set_shared(user_id, podcast_id, true).await?;
|
||||
} else {
|
||||
let confirmed = match &request.confirm {
|
||||
Some(confirm) => {
|
||||
let parse = |url: &str| {
|
||||
FeedUrl::parse(url)
|
||||
.map_err(|_| ApiError::bad_request("invalid confirmed feed URL"))
|
||||
};
|
||||
let feed_url = parse(&confirm.feed_url)?;
|
||||
let id_feed_url = confirm.id_feed_url.as_deref().map(parse).transpose()?;
|
||||
store
|
||||
.share_confirmed(
|
||||
user_id,
|
||||
podcast_id,
|
||||
&feed_url,
|
||||
id_feed_url.as_ref(),
|
||||
now_ms,
|
||||
)
|
||||
.await?
|
||||
.is_some()
|
||||
}
|
||||
None => false,
|
||||
};
|
||||
if !confirmed {
|
||||
let view = share_view(store, user_id, podcast_id, discovery).await?;
|
||||
return Ok(ShareOutcome::NeedsConfirmation(view));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
store.set_shared(user_id, podcast_id, false).await?;
|
||||
store.clear_share_confirmation(user_id, podcast_id).await?;
|
||||
}
|
||||
Ok(ShareOutcome::Done(Box::new(ShareResponse {
|
||||
podcast: podcast_view(store, user_id, podcast_id).await?,
|
||||
share: share_view(store, user_id, podcast_id, discovery).await?,
|
||||
})))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Podcasts found in the federation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// A podcast from a public search hit: its id and the origin feed URL
|
||||
/// (`PodcastSearchHit::feed_url`).
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
struct FoundPodcastRequest {
|
||||
podcast_id: String,
|
||||
feed_url: String,
|
||||
}
|
||||
|
||||
/// The season a preview lists: `{"season": "2"}`, `"none"` (episodes
|
||||
/// without a season) or absent / `"all"`.
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
struct PreviewSeasonRequest {
|
||||
#[serde(default)]
|
||||
season: Option<String>,
|
||||
}
|
||||
|
||||
fn preview_season(body: &Value) -> ApiResult<SeasonFilter> {
|
||||
let request: PreviewSeasonRequest = decode(body.clone())?;
|
||||
Ok(
|
||||
match request
|
||||
.season
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|v| !v.is_empty())
|
||||
{
|
||||
None | Some("all") => SeasonFilter::Any,
|
||||
Some("none") => SeasonFilter::NoSeason,
|
||||
Some(value) => SeasonFilter::Season(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| ApiError::bad_request("invalid season"))?,
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// The found podcast of a request. The id must hash from the URL, like a
|
||||
/// valid card's origin URL: a claimed moved URL or a URL of another podcast
|
||||
/// is refused (422), so the subscription gets the card's id.
|
||||
fn found_podcast(body: Value) -> ApiResult<(PodcastId, FeedUrl)> {
|
||||
let request: FoundPodcastRequest = decode(body)?;
|
||||
let podcast_id = parse_podcast_id(&request.podcast_id)?;
|
||||
let feed_url = FeedUrl::parse(&request.feed_url)
|
||||
.map_err(|error| ApiError::bad_request(format!("invalid feed URL: {error}")))?;
|
||||
if PodcastId::from_feed_url(&feed_url) != podcast_id {
|
||||
return Err(ApiError::new(
|
||||
422,
|
||||
"the feed URL is not the URL this podcast id comes from",
|
||||
));
|
||||
}
|
||||
Ok((podcast_id, feed_url))
|
||||
}
|
||||
|
||||
/// Seasons and episodes of a found podcast before subscribing: `{
|
||||
/// "podcast_id", "feed_url" }` from the search hit, optionally `"season"`
|
||||
/// to list the episodes of one season (`"none"`: without a season). The
|
||||
/// feed is read 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,
|
||||
user_id: i64,
|
||||
body: Value,
|
||||
) -> ApiResult<search::PodcastPreview> {
|
||||
let season = preview_season(&body)?;
|
||||
let (podcast_id, feed_url) = found_podcast(body)?;
|
||||
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,
|
||||
document.moved_permanently,
|
||||
&document.feed,
|
||||
known.as_ref(),
|
||||
season,
|
||||
))
|
||||
}
|
||||
|
||||
/// "Subscribe" on a found podcast: `{ "podcast_id", "feed_url" }` from the
|
||||
/// search hit.
|
||||
///
|
||||
/// The subscription is the account's own and private, made like "add RSS"
|
||||
/// 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,
|
||||
user_id: i64,
|
||||
body: Value,
|
||||
now_ms: i64,
|
||||
) -> ApiResult<SubscribeResponse> {
|
||||
let (podcast_id, feed_url) = found_podcast(body)?;
|
||||
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 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,
|
||||
new_episodes: subscription.refresh.new_episodes,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Episodes
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -648,6 +648,18 @@ pub(crate) async fn apply_op(
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether an applied operation can change what is published
|
||||
/// (`super::discovery`): subscription and share registers, and the alias set
|
||||
/// the card's origin URL comes from.
|
||||
fn affects_publication(payload: &SyncOpPayload) -> bool {
|
||||
matches!(
|
||||
payload,
|
||||
SyncOpPayload::PodcastSubscriptionSet { .. }
|
||||
| SyncOpPayload::PodcastShareSet { .. }
|
||||
| SyncOpPayload::PodcastFeedAliasesAdded { .. }
|
||||
)
|
||||
}
|
||||
|
||||
fn record_outcome(stats: &mut SnapshotApplyStats, result: Result<bool>) -> Result<()> {
|
||||
match result {
|
||||
Ok(true) => stats.changed += 1,
|
||||
@@ -682,6 +694,9 @@ impl PodcastStore {
|
||||
let mut tx = self.begin_write(user_id).await?;
|
||||
let outcome = apply_op(&mut tx, user_id, op).await?;
|
||||
tx.commit().await?;
|
||||
if outcome == ApplyOutcome::Changed && affects_publication(&op.payload) {
|
||||
super::discovery::request_reconcile();
|
||||
}
|
||||
Ok(outcome)
|
||||
}
|
||||
|
||||
@@ -737,6 +752,11 @@ impl PodcastStore {
|
||||
record_outcome(&mut stats, result)?;
|
||||
}
|
||||
tx.commit().await?;
|
||||
if stats.changed > 0
|
||||
&& !(snapshot.podcast_subscriptions.is_empty() && snapshot.podcast_shares.is_empty())
|
||||
{
|
||||
super::discovery::request_reconcile();
|
||||
}
|
||||
Ok(stats)
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,800 @@
|
||||
//! Public podcast discovery against PostgreSQL: the share toggle and its
|
||||
//! privacy confirmation, the aggregation of several accounts, the
|
||||
//! publication from the web's federation node to another loopback node and
|
||||
//! the first publication after a restart.
|
||||
//!
|
||||
//! Ignored by default, like the other store tests: set
|
||||
//! `FURU_DEVICE_SYNC_TEST_DATABASE_URL` to a disposable database. The
|
||||
//! loopback test starts two iroh nodes in temporary directories (the same
|
||||
//! setup as frid's `tests/podcast_dht.rs`).
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use music_dht::device_sync::{SyncOpPayload, SyncOpWire, random_hex};
|
||||
use music_dht::podcast::{FeedUrl, PodcastId, PodcastMeta};
|
||||
use music_dht::podcast_dht::{PODCAST_DHT_ALPN, PodcastDht};
|
||||
use music_dht::{MusicDhtConfig, MusicDhtService, NetworkId};
|
||||
use serde_json::json;
|
||||
|
||||
use super::api::{self, ShareOutcome};
|
||||
use super::discovery::{self, Discovery, aggregate_share_states, load_shared};
|
||||
use super::postgres_tests::{cleanup, random_user, test_pool};
|
||||
use super::*;
|
||||
|
||||
fn url(value: &str) -> FeedUrl {
|
||||
FeedUrl::parse(value).unwrap()
|
||||
}
|
||||
|
||||
fn meta(feed: &str, title: &str) -> PodcastMeta {
|
||||
PodcastMeta {
|
||||
feed_url: url(feed),
|
||||
title: title.into(),
|
||||
author: Some("Host".into()),
|
||||
description: Some("A public show".into()),
|
||||
link: None,
|
||||
image_url: None,
|
||||
}
|
||||
}
|
||||
|
||||
async fn subscribe(store: &PodcastStore, user: i64, feed: &str, title: &str) -> PodcastId {
|
||||
store
|
||||
.subscribe(user, &meta(feed, title))
|
||||
.await
|
||||
.unwrap()
|
||||
.podcast_id
|
||||
}
|
||||
|
||||
fn share_op(
|
||||
origin: &str,
|
||||
seq: i64,
|
||||
podcast_id: &PodcastId,
|
||||
shared: bool,
|
||||
generation: u64,
|
||||
) -> SyncOpWire {
|
||||
SyncOpWire {
|
||||
op_id: format!("{origin}:{seq}"),
|
||||
origin_device_id: origin.into(),
|
||||
seq,
|
||||
hlc_ms: 9_000_000_000_000 + seq,
|
||||
payload: SyncOpPayload::PodcastShareSet {
|
||||
podcast_id: podcast_id.clone(),
|
||||
shared,
|
||||
generation,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn share(
|
||||
store: &PodcastStore,
|
||||
user: i64,
|
||||
podcast_id: &PodcastId,
|
||||
body: serde_json::Value,
|
||||
) -> Result<ShareOutcome, api::ApiError> {
|
||||
api::set_share(store, user, podcast_id, body, Discovery::Off, now_ms()).await
|
||||
}
|
||||
|
||||
fn done(outcome: Result<ShareOutcome, api::ApiError>) -> Box<api::ShareResponse> {
|
||||
match outcome.unwrap() {
|
||||
ShareOutcome::Done(response) => response,
|
||||
ShareOutcome::NeedsConfirmation(view) => panic!("unexpected confirmation: {view:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn needs_confirmation(outcome: Result<ShareOutcome, api::ApiError>) -> api::ShareView {
|
||||
match outcome.unwrap() {
|
||||
ShareOutcome::NeedsConfirmation(view) => view,
|
||||
ShareOutcome::Done(response) => panic!("shared without confirmation: {:?}", response.share),
|
||||
}
|
||||
}
|
||||
|
||||
fn confirm_body(view: &api::ShareView) -> serde_json::Value {
|
||||
json!({
|
||||
"shared": true,
|
||||
"confirm": { "feed_url": view.feed_url, "id_feed_url": view.id_feed_url },
|
||||
})
|
||||
}
|
||||
|
||||
async fn published_ids(pool: &sqlx::PgPool, users: &[i64]) -> Vec<(PodcastId, i64)> {
|
||||
let aggregation = aggregate_share_states(
|
||||
load_shared(pool, Some(users)).await.unwrap().shares,
|
||||
&Default::default(),
|
||||
);
|
||||
aggregation.representatives.into_iter().collect()
|
||||
}
|
||||
|
||||
/// Active accounts (only they publish); random ids like the store tests.
|
||||
async fn accounts<const N: usize>(pool: &sqlx::PgPool) -> [i64; N] {
|
||||
let users: [i64; N] = std::array::from_fn(|_| random_user());
|
||||
for user in users {
|
||||
sqlx::query(
|
||||
"INSERT INTO furumusic__user (id, username, role, is_active)
|
||||
VALUES ($1, $2, 'user', TRUE)",
|
||||
)
|
||||
.bind(user)
|
||||
.bind(format!("podcast-discovery-test-{user}"))
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
users
|
||||
}
|
||||
|
||||
async fn remove_accounts(pool: &sqlx::PgPool, users: &[i64]) {
|
||||
cleanup(pool, users.iter().copied()).await;
|
||||
sqlx::query("DELETE FROM furumusic__user WHERE id = ANY($1)")
|
||||
.bind(users)
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
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_share_toggle_asks_before_publishing_a_private_looking_feed() {
|
||||
let pool = test_pool().await;
|
||||
let store = PodcastStore::new(pool.clone());
|
||||
let [user, other] = accounts(&pool).await;
|
||||
let secret = format!(
|
||||
"https://members.example/feed.xml?token=8f3a9c2e7d1b4a6f{}",
|
||||
random_hex(4)
|
||||
);
|
||||
let podcast_id = subscribe(&store, user, &secret, "Members Only").await;
|
||||
|
||||
// Private by default; the dialog needs the warnings and the URL.
|
||||
let view = api::share_view(&store, user, &podcast_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!view.shared && !view.pending_confirmation && view.can_share);
|
||||
assert_eq!(view.discovery, "off");
|
||||
assert!(!view.warnings.is_empty());
|
||||
assert_eq!(view.feed_url.as_deref(), Some(secret.as_str()));
|
||||
|
||||
// Enabling without confirming writes nothing.
|
||||
let before = log_len(&pool, user).await;
|
||||
let asked = needs_confirmation(share(&store, user, &podcast_id, json!({"shared": true})).await);
|
||||
assert!(!asked.warnings.is_empty());
|
||||
assert!(store.share(user, &podcast_id).await.unwrap().is_none());
|
||||
// Confirming other URLs than the card's writes nothing either.
|
||||
let wrong =
|
||||
json!({"shared": true, "confirm": {"feed_url": "https://members.example/feed.xml"}});
|
||||
needs_confirmation(share(&store, user, &podcast_id, wrong).await);
|
||||
assert_eq!(log_len(&pool, user).await, before);
|
||||
assert!(published_ids(&pool, &[user]).await.is_empty());
|
||||
|
||||
// Confirming exactly the shown URLs shares and publishes.
|
||||
let response = done(share(&store, user, &podcast_id, confirm_body(&asked)).await);
|
||||
assert!(response.podcast.shared);
|
||||
assert!(response.share.shared && !response.share.pending_confirmation);
|
||||
assert_eq!(log_len(&pool, user).await, before + 1);
|
||||
assert_eq!(
|
||||
published_ids(&pool, &[user]).await,
|
||||
vec![(podcast_id.clone(), user)]
|
||||
);
|
||||
|
||||
// Another account cannot see or change this account's podcast.
|
||||
let error = share(&store, other, &podcast_id, json!({"shared": false}))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.status, 404);
|
||||
// Malformed requests.
|
||||
let error = share(&store, user, &podcast_id, json!({"share": true}))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.status, 400);
|
||||
|
||||
// Turning sharing off revokes and forgets the confirmation: sharing
|
||||
// again asks again.
|
||||
let response = done(share(&store, user, &podcast_id, json!({"shared": false})).await);
|
||||
assert!(!response.share.shared);
|
||||
assert!(published_ids(&pool, &[user]).await.is_empty());
|
||||
let confirmations: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM furumusic__podcast_share_confirmation WHERE user_id = $1",
|
||||
)
|
||||
.bind(user)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(confirmations, 0);
|
||||
needs_confirmation(share(&store, user, &podcast_id, json!({"shared": true})).await);
|
||||
|
||||
// Shared on another trusted device (which asked its own user): the web
|
||||
// still asks this account before its server publishes the URL.
|
||||
let remote = share_op("tui_a", 1, &podcast_id, true, 100);
|
||||
assert_eq!(
|
||||
store.apply_remote_op(user, &remote).await.unwrap(),
|
||||
ApplyOutcome::Changed
|
||||
);
|
||||
let view = api::share_view(&store, user, &podcast_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(view.shared && view.pending_confirmation);
|
||||
assert!(published_ids(&pool, &[user]).await.is_empty());
|
||||
let before = log_len(&pool, user).await;
|
||||
let response = done(share(&store, user, &podcast_id, confirm_body(&view)).await);
|
||||
assert!(response.share.shared && !response.share.pending_confirmation);
|
||||
// Confirming an existing decision emits no operation.
|
||||
assert_eq!(log_len(&pool, user).await, before);
|
||||
assert_eq!(published_ids(&pool, &[user]).await.len(), 1);
|
||||
|
||||
// A feed that moves to another URL that looks private asks again.
|
||||
let moved = format!(
|
||||
"https://members.example/v2/feed.xml?key=9a8b7c6d5e4f3a2b{}",
|
||||
random_hex(4)
|
||||
);
|
||||
sqlx::query(
|
||||
"UPDATE furumusic__podcast SET feed_url = $3 WHERE user_id = $1 AND podcast_id = $2",
|
||||
)
|
||||
.bind(user)
|
||||
.bind(podcast_id.as_str())
|
||||
.bind(&moved)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let view = api::share_view(&store, user, &podcast_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(view.shared && view.pending_confirmation);
|
||||
assert_eq!(view.feed_url.as_deref(), Some(moved.as_str()));
|
||||
assert_eq!(view.id_feed_url.as_deref(), Some(secret.as_str()));
|
||||
assert!(published_ids(&pool, &[user]).await.is_empty());
|
||||
|
||||
// Unsubscribing revokes and forgets; the podcast is private again.
|
||||
api::unsubscribe(&store, user, &podcast_id).await.unwrap();
|
||||
let view = api::share_view(&store, user, &podcast_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!view.shared && !view.can_share);
|
||||
let error = share(&store, user, &podcast_id, json!({"shared": true}))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.status, 409);
|
||||
|
||||
remove_accounts(&pool, &[user, other]).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
||||
async fn postgres_shared_podcasts_aggregate_across_accounts() {
|
||||
let pool = test_pool().await;
|
||||
let store = PodcastStore::new(pool.clone());
|
||||
let users = accounts::<3>(&pool).await;
|
||||
let [a, b, c] = users;
|
||||
let feed = format!("https://podcasts.example/shared-{}.xml", random_hex(4));
|
||||
let private = format!("https://podcasts.example/private-{}.xml", random_hex(4));
|
||||
let podcast_id = subscribe(&store, a, &feed, "Shared Show").await;
|
||||
assert_eq!(subscribe(&store, b, &feed, "Shared Show").await, podcast_id);
|
||||
assert_eq!(subscribe(&store, c, &feed, "Shared Show").await, podcast_id);
|
||||
let private_id = subscribe(&store, a, &private, "Private Show").await;
|
||||
|
||||
// Nothing shared: nothing published, not even the private podcast.
|
||||
assert!(published_ids(&pool, &users).await.is_empty());
|
||||
|
||||
done(share(&store, a, &podcast_id, json!({"shared": true})).await);
|
||||
done(share(&store, b, &podcast_id, json!({"shared": true})).await);
|
||||
let published = published_ids(&pool, &users).await;
|
||||
assert_eq!(published, vec![(podcast_id.clone(), a.min(b))]);
|
||||
let aggregation = aggregate_share_states(
|
||||
load_shared(&pool, Some(&users)).await.unwrap().shares,
|
||||
&Default::default(),
|
||||
);
|
||||
let state = &aggregation.states[0];
|
||||
assert!(state.is_shared() && state.privacy_confirmed);
|
||||
let card = state.card.as_ref().unwrap();
|
||||
assert_eq!(card.feed_url, url(&feed));
|
||||
assert_eq!(card.title, "Shared Show");
|
||||
|
||||
// A deactivated account publishes nothing; the other one still shares.
|
||||
let set_active = async |user: i64, active: bool| {
|
||||
sqlx::query("UPDATE furumusic__user SET is_active = $2 WHERE id = $1")
|
||||
.bind(user)
|
||||
.bind(active)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
};
|
||||
set_active(a.min(b), false).await;
|
||||
assert_eq!(
|
||||
published_ids(&pool, &users).await,
|
||||
vec![(podcast_id.clone(), a.max(b))]
|
||||
);
|
||||
set_active(a.max(b), false).await;
|
||||
assert!(published_ids(&pool, &users).await.is_empty());
|
||||
set_active(a, true).await;
|
||||
set_active(b, true).await;
|
||||
|
||||
// One account stops sharing: the other still shares the same card.
|
||||
done(share(&store, a, &podcast_id, json!({"shared": false})).await);
|
||||
assert_eq!(
|
||||
published_ids(&pool, &users).await,
|
||||
vec![(podcast_id.clone(), b)]
|
||||
);
|
||||
|
||||
// A stale `shared = true` of A (delivered late from an old device log)
|
||||
// cannot republish for A.
|
||||
let stale = share_op("old_device", 1, &podcast_id, true, 1);
|
||||
assert_eq!(
|
||||
store.apply_remote_op(a, &stale).await.unwrap(),
|
||||
ApplyOutcome::Unchanged
|
||||
);
|
||||
let view = api::share_view(&store, a, &podcast_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!view.shared);
|
||||
|
||||
// The last sharing account unsubscribes: the card is revoked, and
|
||||
// subscribing again is private.
|
||||
api::unsubscribe(&store, b, &podcast_id).await.unwrap();
|
||||
assert!(published_ids(&pool, &users).await.is_empty());
|
||||
api::resubscribe(&store, b, &podcast_id).await.unwrap();
|
||||
assert!(published_ids(&pool, &users).await.is_empty());
|
||||
// Share decisions of C never existed; the private podcast of A stays
|
||||
// private throughout.
|
||||
let view = api::share_view(&store, a, &private_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!view.shared);
|
||||
|
||||
remove_accounts(&pool, &users).await;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Loopback: publish from the web node, find from another node
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
struct Node {
|
||||
service: Arc<MusicDhtService>,
|
||||
dht: Arc<PodcastDht>,
|
||||
tasks: Vec<tokio::task::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
async fn stop(self) {
|
||||
for task in &self.tasks {
|
||||
task.abort();
|
||||
}
|
||||
self.service.shutdown().await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
async fn start_node(directory: &std::path::Path, network: NetworkId) -> Node {
|
||||
std::fs::create_dir_all(directory).unwrap();
|
||||
let config = MusicDhtConfig::builder()
|
||||
.data_dir(directory)
|
||||
.network_id(network)
|
||||
.request_timeout(Duration::from_secs(2))
|
||||
.lookup_timeout(Duration::from_secs(5))
|
||||
.transport_timeout(Duration::from_secs(5))
|
||||
.dial_timeout(Duration::from_secs(2))
|
||||
.schema_independent_stream_protocol(PODCAST_DHT_ALPN)
|
||||
.build()
|
||||
.unwrap();
|
||||
let (service, mut events) = MusicDhtService::start(config).await.unwrap();
|
||||
let service = Arc::new(service);
|
||||
let events = tokio::spawn(async move { while events.recv().await.is_some() {} });
|
||||
// The web's own wiring: the overlay on the node's endpoint, its cache
|
||||
// in the federation data directory.
|
||||
let (dht, serve) = crate::federation::podcast_discovery::open(&service, directory)
|
||||
.await
|
||||
.expect("overlay opens");
|
||||
assert!(
|
||||
directory
|
||||
.join(crate::federation::podcast_discovery::CACHE_FILE)
|
||||
.exists()
|
||||
);
|
||||
Node {
|
||||
service,
|
||||
dht,
|
||||
tasks: vec![events, serve],
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_for<T>(what: &str, mut check: impl AsyncFnMut() -> Option<T>) -> T {
|
||||
let started = std::time::Instant::now();
|
||||
loop {
|
||||
if let Some(value) = check().await {
|
||||
return value;
|
||||
}
|
||||
assert!(
|
||||
started.elapsed() < Duration::from_secs(90),
|
||||
"timed out waiting for {what}"
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(300)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
||||
async fn postgres_web_node_publishes_shared_podcasts_to_another_node() {
|
||||
tokio::time::timeout(Duration::from_secs(240), loopback_scenario())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn loopback_scenario() {
|
||||
let pool = test_pool().await;
|
||||
let store = PodcastStore::new(pool.clone());
|
||||
let users = accounts::<2>(&pool).await;
|
||||
let [u1, u2] = users;
|
||||
// An account of the stranger's side (another server in real life) that
|
||||
// subscribes from the card it found.
|
||||
let finder = random_user();
|
||||
let tag = random_hex(6);
|
||||
let directory = std::env::temp_dir().join(format!("furumusic-podcast-dht-{tag}"));
|
||||
let network = NetworkId::from_name(&format!("furumusic-podcast-loopback-{tag}"));
|
||||
let web = start_node(&directory.join("web"), network).await;
|
||||
let stranger = start_node(&directory.join("stranger"), network).await;
|
||||
|
||||
// The stranger reaches the web node through the music-DHT transport
|
||||
// only: no device-sync pairing, another identity.
|
||||
meet(&web, &stranger).await;
|
||||
|
||||
// Two accounts share one podcast; one of them also has a private one.
|
||||
let title = format!("Loopback Radio {tag}");
|
||||
let hidden = format!("Hidden Garden {tag}");
|
||||
let server = super::test_server::TestServer::start();
|
||||
server.set(
|
||||
"/loopback.xml",
|
||||
super::test_server::Route::new(
|
||||
format!(
|
||||
r#"<?xml version="1.0"?><rss version="2.0"><channel><title>{title}</title>
|
||||
<item><title>First</title><guid>1</guid><pubDate>Mon, 01 Jan 2024 10:00:00 GMT</pubDate>
|
||||
<enclosure url="/audio/1.mp3" type="audio/mpeg"/></item></channel></rss>"#
|
||||
),
|
||||
"application/rss+xml",
|
||||
),
|
||||
);
|
||||
let feed = server.url("/loopback.xml");
|
||||
let podcast_id = subscribe(&store, u1, &feed, &title).await;
|
||||
subscribe(&store, u2, &feed, &title).await;
|
||||
subscribe(
|
||||
&store,
|
||||
u1,
|
||||
&format!("https://podcasts.example/hidden-{tag}.xml"),
|
||||
&hidden,
|
||||
)
|
||||
.await;
|
||||
// Sharing while the stranger already looks: nothing before.
|
||||
assert!(stranger.dht.search(&title, 10).await.unwrap().is_empty());
|
||||
done(share(&store, u1, &podcast_id, json!({"shared": true})).await);
|
||||
done(share(&store, u2, &podcast_id, json!({"shared": true})).await);
|
||||
|
||||
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.aggregation.states.len(), 1, "one card per podcast");
|
||||
assert_eq!(report.stats.published, 1);
|
||||
assert!(report.stats.held_back.is_empty() && report.stats.failed.is_empty());
|
||||
let view = api::share_view(&store, u1, &podcast_id, Discovery::Running(&web.dht))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(view.published && view.held.is_none());
|
||||
assert_eq!(view.discovery, "running");
|
||||
|
||||
let hit = wait_for("the shared card", async || {
|
||||
stranger
|
||||
.dht
|
||||
.search(&title, 10)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.find(|hit| hit.podcast_id == podcast_id)
|
||||
})
|
||||
.await;
|
||||
assert_eq!(hit.feed_url, url(&feed));
|
||||
assert_eq!(hit.card.title, title);
|
||||
assert_eq!(hit.publishers, vec![web.service.endpoint_id()]);
|
||||
// The private podcast is not discoverable, by text or by id.
|
||||
assert!(stranger.dht.search(&hidden, 10).await.unwrap().is_empty());
|
||||
|
||||
// The finder sees the card in the federated search, looks at its
|
||||
// episodes and subscribes: its own private subscription with the
|
||||
// card's id, fetched from the feed (never from the publisher).
|
||||
let views = super::search::hit_views(&store, finder, &title, &[], vec![hit.clone()])
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(views.len(), 1);
|
||||
assert!(!views[0].subscribed && views[0].exact_match);
|
||||
let found = json!({ "podcast_id": views[0].podcast_id, "feed_url": views[0].feed_url });
|
||||
let fetcher = super::postgres_tests::local_fetcher();
|
||||
let preview = api::preview_found(&store, &fetcher, finder, found.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(preview.episodes.len(), 1);
|
||||
let subscribed = api::subscribe_found(&store, &fetcher, finder, found, now_ms())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(subscribed.podcast.podcast_id, podcast_id.to_string());
|
||||
assert!(subscribed.podcast.subscribed && !subscribed.podcast.shared);
|
||||
|
||||
// Idempotent; one account stopping keeps the card of the other.
|
||||
done(share(&store, u1, &podcast_id, json!({"shared": false})).await);
|
||||
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
(
|
||||
report.stats.published,
|
||||
report.stats.unchanged,
|
||||
report.stats.unpublished
|
||||
),
|
||||
(0, 1, 0)
|
||||
);
|
||||
|
||||
// The last account unsubscribes: the card is revoked with a tombstone.
|
||||
api::unsubscribe(&store, u2, &podcast_id).await.unwrap();
|
||||
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.stats.unpublished, 1);
|
||||
wait_for("the revocation", async || {
|
||||
stranger
|
||||
.dht
|
||||
.lookup_podcast(&podcast_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
.then_some(())
|
||||
})
|
||||
.await;
|
||||
assert!(
|
||||
stranger
|
||||
.dht
|
||||
.search(&title, 10)
|
||||
.await
|
||||
.unwrap()
|
||||
.iter()
|
||||
.all(|hit| hit.podcast_id != podcast_id)
|
||||
);
|
||||
let view = api::share_view(&store, u2, &podcast_id, Discovery::Running(&web.dht))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!view.shared && !view.published);
|
||||
// The revocation does not touch the finder's subscription.
|
||||
let kept = store.podcast(finder, &podcast_id).await.unwrap().unwrap();
|
||||
assert!(kept.subscribed && !kept.shared);
|
||||
cleanup(&pool, [finder]).await;
|
||||
|
||||
web.stop().await;
|
||||
stranger.stop().await;
|
||||
remove_accounts(&pool, &users).await;
|
||||
let _ = std::fs::remove_dir_all(&directory);
|
||||
}
|
||||
|
||||
/// Connects `stranger` to `web` and fills both overlay routing tables.
|
||||
async fn meet(web: &Node, stranger: &Node) {
|
||||
stranger
|
||||
.service
|
||||
.connect(web.service.ticket().await.unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
wait_for("music DHT contacts", async || {
|
||||
(!web.service.known_peers().is_empty() && !stranger.service.known_peers().is_empty())
|
||||
.then_some(())
|
||||
})
|
||||
.await;
|
||||
web.dht.bootstrap().await.unwrap();
|
||||
stranger.dht.bootstrap().await.unwrap();
|
||||
wait_for("overlay contacts", async || {
|
||||
(web.dht.known_peers() >= 1 && stranger.dht.known_peers() >= 1).then_some(())
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
||||
async fn postgres_a_restarted_server_publishes_only_after_the_first_device_sync() {
|
||||
tokio::time::timeout(Duration::from_secs(240), restart_scenario())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Interop scenario 7c with the web server as the device that was stopped:
|
||||
/// a TUI of the account turned sharing off meanwhile, and the server comes
|
||||
/// back with the old "shared" rows.
|
||||
async fn restart_scenario() {
|
||||
let pool = test_pool().await;
|
||||
let store = PodcastStore::new(pool.clone());
|
||||
let users = accounts::<1>(&pool).await;
|
||||
let [user] = users;
|
||||
let tag = random_hex(6);
|
||||
let directory = std::env::temp_dir().join(format!("furumusic-podcast-restart-{tag}"));
|
||||
let network = NetworkId::from_name(&format!("furumusic-podcast-restart-{tag}"));
|
||||
|
||||
let kept_title = format!("Kept Show {tag}");
|
||||
let revoked_title = format!("Revoked Show {tag}");
|
||||
let kept = subscribe(
|
||||
&store,
|
||||
user,
|
||||
&format!("https://podcasts.example/kept-{tag}.xml"),
|
||||
&kept_title,
|
||||
)
|
||||
.await;
|
||||
let revoked = subscribe(
|
||||
&store,
|
||||
user,
|
||||
&format!("https://podcasts.example/revoked-{tag}.xml"),
|
||||
&revoked_title,
|
||||
)
|
||||
.await;
|
||||
done(share(&store, user, &kept, json!({"shared": true})).await);
|
||||
done(share(&store, user, &revoked, json!({"shared": true})).await);
|
||||
let web = start_node(&directory.join("web"), network).await;
|
||||
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.stats.published, 2);
|
||||
web.stop().await;
|
||||
|
||||
// The server restarts from the same directory (same key, same overlay
|
||||
// cache) and meets a stranger before its first device-sync round.
|
||||
let web = start_node(&directory.join("web"), network).await;
|
||||
let stranger = start_node(&directory.join("stranger"), network).await;
|
||||
meet(&web, &stranger).await;
|
||||
let (first_sync_done, first_sync) = tokio::sync::watch::channel(false);
|
||||
let reconciler = tokio::spawn(discovery::run_rounds(
|
||||
pool.clone(),
|
||||
Arc::clone(&web.dht),
|
||||
first_sync,
|
||||
Duration::from_secs(600),
|
||||
Some(users.to_vec()),
|
||||
));
|
||||
// Time for a round: nothing is announced from the stored decisions.
|
||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||
for podcast_id in [&kept, &revoked] {
|
||||
assert!(
|
||||
stranger
|
||||
.dht
|
||||
.lookup_podcast(podcast_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none(),
|
||||
"a card was announced before the first device-sync round"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
stranger
|
||||
.dht
|
||||
.search(&revoked_title, 10)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
// The first device-sync round brings the share-off the TUI made while
|
||||
// the server was stopped.
|
||||
let share_off = share_op("tui_while_stopped", 1, &revoked, false, 100);
|
||||
assert_eq!(
|
||||
store.apply_remote_op(user, &share_off).await.unwrap(),
|
||||
ApplyOutcome::Changed
|
||||
);
|
||||
first_sync_done.send_replace(true);
|
||||
|
||||
let hit = wait_for("the card still shared", async || {
|
||||
stranger.dht.lookup_podcast(&kept).await.unwrap()
|
||||
})
|
||||
.await;
|
||||
assert_eq!(hit.card.title, kept_title);
|
||||
assert_eq!(hit.publishers, vec![web.service.endpoint_id()]);
|
||||
assert!(
|
||||
stranger
|
||||
.dht
|
||||
.lookup_podcast(&revoked)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
stranger
|
||||
.dht
|
||||
.search(&revoked_title, 10)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
let revoked_record = web
|
||||
.dht
|
||||
.published()
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.find(|publication| publication.podcast_id == revoked)
|
||||
.unwrap();
|
||||
assert!(!revoked_record.active, "the revoked card is tombstoned");
|
||||
|
||||
reconciler.abort();
|
||||
web.stop().await;
|
||||
stranger.stop().await;
|
||||
remove_accounts(&pool, &users).await;
|
||||
let _ = std::fs::remove_dir_all(&directory);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
||||
async fn postgres_an_unreadable_row_does_not_stop_other_revocations() {
|
||||
tokio::time::timeout(Duration::from_secs(120), unreadable_row_scenario())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn unreadable_row_scenario() {
|
||||
let pool = test_pool().await;
|
||||
let store = PodcastStore::new(pool.clone());
|
||||
let users = accounts::<2>(&pool).await;
|
||||
let [a, b] = users;
|
||||
let tag = random_hex(6);
|
||||
let directory = std::env::temp_dir().join(format!("furumusic-podcast-unreadable-{tag}"));
|
||||
let network = NetworkId::from_name(&format!("furumusic-podcast-unreadable-{tag}"));
|
||||
let web = start_node(&directory.join("web"), network).await;
|
||||
|
||||
let a_feed = format!("https://podcasts.example/a-{tag}.xml");
|
||||
let b_feed = format!("https://podcasts.example/b-{tag}.xml");
|
||||
let a_id = subscribe(&store, a, &a_feed, "A Show").await;
|
||||
let b_id = subscribe(&store, b, &b_feed, "B Show").await;
|
||||
done(share(&store, a, &a_id, json!({"shared": true})).await);
|
||||
done(share(&store, b, &b_id, json!({"shared": true})).await);
|
||||
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.stats.published, 2);
|
||||
let active = async || {
|
||||
web.dht
|
||||
.published()
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|publication| publication.active)
|
||||
.map(|publication| publication.podcast_id)
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(active().await.len(), 2);
|
||||
|
||||
// B's stored alias no longer decodes (an imported row, a stricter
|
||||
// parser after an upgrade); A turns sharing off.
|
||||
sqlx::query(
|
||||
"INSERT INTO furumusic__podcast_feed_alias (user_id, podcast_id, feed_url)
|
||||
VALUES ($1, $2, 'not a feed url')",
|
||||
)
|
||||
.bind(b)
|
||||
.bind(b_id.as_str())
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
done(share(&store, a, &a_id, json!({"shared": false})).await);
|
||||
|
||||
// The round still runs: A's card is revoked, and B's card (which can no
|
||||
// longer be read) fails closed instead of being republished forever.
|
||||
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.aggregation.unreadable, 1);
|
||||
assert!(report.aggregation.states.is_empty());
|
||||
assert_eq!(report.stats.unpublished, 2);
|
||||
assert!(active().await.is_empty());
|
||||
|
||||
// Repaired: B's card is published again; A's stays revoked.
|
||||
sqlx::query(
|
||||
"DELETE FROM furumusic__podcast_feed_alias WHERE user_id = $1 AND feed_url = 'not a feed url'",
|
||||
)
|
||||
.bind(b)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.aggregation.unreadable, 0);
|
||||
assert_eq!(active().await, vec![b_id.clone()]);
|
||||
|
||||
web.stop().await;
|
||||
remove_accounts(&pool, &users).await;
|
||||
let _ = std::fs::remove_dir_all(&directory);
|
||||
}
|
||||
+157
-2
@@ -12,7 +12,16 @@
|
||||
//! * `GET /feeds/{podcast_id}` — one podcast and its season groups.
|
||||
//! * `GET /feeds/{podcast_id}/episodes?season=&played=&q=&sort=&limit=&offset=`
|
||||
//! * `POST /feeds/{podcast_id}/subscribe`, `POST /feeds/{podcast_id}/unsubscribe`
|
||||
//! (unsubscribing keeps progress and history).
|
||||
//! (unsubscribing keeps progress and history and revokes sharing).
|
||||
//! * `POST /feeds/{podcast_id}/share` `{shared, confirm?}` — "share in the
|
||||
//! federation" on or off; 409 with `needs_confirmation` when the feed URLs
|
||||
//! look private and the request does not confirm exactly them.
|
||||
//! * `POST /discovery/preview` `{podcast_id, feed_url}` — seasons and
|
||||
//! episodes of a podcast found in the public search, read from its feed
|
||||
//! (nothing stored).
|
||||
//! * `POST /discovery/subscribe` `{podcast_id, feed_url}` — subscribe
|
||||
//! (private) to a found podcast with its origin feed URL; the id must hash
|
||||
//! from the URL (422 otherwise).
|
||||
//! * `POST /episodes/{episode_id}/restart|played|unplayed` — explicit
|
||||
//! progress actions.
|
||||
//! * `POST /{podcast_id}/refresh` — manual feed refresh of a subscribed
|
||||
@@ -51,6 +60,7 @@ use music_dht::podcast::{EpisodeId, PodcastId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::api::{self, ApiError, EpisodeAction, EpisodeListParams};
|
||||
use super::discovery::Discovery;
|
||||
use super::fetch::FeedFetcher;
|
||||
use super::limits::{self, ConcurrencyLimit, Permit};
|
||||
use super::proxy::AudioProxy;
|
||||
@@ -508,6 +518,57 @@ async fn subscribe_url_handler(
|
||||
api_response(api::subscribe_url(&store, fetcher, user_id, body, now_ms()).await)
|
||||
}
|
||||
|
||||
/// What a `/discovery/...` route does with a found podcast.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum FoundRoute {
|
||||
Preview,
|
||||
Subscribe,
|
||||
}
|
||||
|
||||
/// A podcast from a public search hit: read its feed for a preview, or
|
||||
/// subscribe. Both fetch the feed, so they share the feed request limits.
|
||||
async fn found_handler(
|
||||
state: State,
|
||||
route: FoundRoute,
|
||||
auth_ctx: auth::AuthContext,
|
||||
session: Session,
|
||||
db: Database,
|
||||
request: cot::request::Request,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
let Some(user_id) = signed_in(&auth_ctx, &session, &db).await else {
|
||||
return Ok(unauthorized());
|
||||
};
|
||||
let body = match json_body(request).await {
|
||||
Ok(body) => body,
|
||||
Err(error) => return Ok(api_error(error)),
|
||||
};
|
||||
let Some(_permit) = state.feed_fetch(user_id) else {
|
||||
return Ok(too_busy(FEEDS_BUSY));
|
||||
};
|
||||
let store = state.store().await?;
|
||||
let fetcher = state.fetcher().await?;
|
||||
match route {
|
||||
FoundRoute::Preview => {
|
||||
api_response(api::preview_found(&store, fetcher, user_id, body).await)
|
||||
}
|
||||
FoundRoute::Subscribe => {
|
||||
api_response(api::subscribe_found(&store, fetcher, user_id, body, now_ms()).await)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether public discovery runs on this server's federation node.
|
||||
fn discovery_state<'a>(
|
||||
federation: &crate::federation::Federation,
|
||||
dht: Option<&'a music_dht::podcast_dht::PodcastDht>,
|
||||
) -> Discovery<'a> {
|
||||
match dht {
|
||||
Some(dht) => Discovery::Running(dht),
|
||||
None if federation.podcast_discovery_unavailable() => Discovery::Unavailable,
|
||||
None => Discovery::Off,
|
||||
}
|
||||
}
|
||||
|
||||
/// What a `/feeds/{podcast_id}...` route does.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum PodcastRoute {
|
||||
@@ -534,7 +595,10 @@ async fn podcast_handler(
|
||||
let store = state.store().await?;
|
||||
match route {
|
||||
PodcastRoute::Detail => {
|
||||
api_response(api::podcast_detail(&store, user_id, &podcast_id).await)
|
||||
let federation = crate::federation::handle();
|
||||
let dht = federation.podcast_discovery();
|
||||
let discovery = discovery_state(&federation, dht.as_deref());
|
||||
api_response(api::podcast_detail(&store, user_id, &podcast_id, discovery).await)
|
||||
}
|
||||
PodcastRoute::Subscribe => {
|
||||
api_response(api::resubscribe(&store, user_id, &podcast_id).await)
|
||||
@@ -545,6 +609,64 @@ async fn podcast_handler(
|
||||
}
|
||||
}
|
||||
|
||||
async fn share_handler(
|
||||
state: State,
|
||||
auth_ctx: auth::AuthContext,
|
||||
session: Session,
|
||||
db: Database,
|
||||
path: Path<PodcastPath>,
|
||||
request: cot::request::Request,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
let Some(user_id) = signed_in(&auth_ctx, &session, &db).await else {
|
||||
return Ok(unauthorized());
|
||||
};
|
||||
let podcast_id = match api::parse_podcast_id(&path.0.podcast_id) {
|
||||
Ok(id) => id,
|
||||
Err(error) => return Ok(api_error(error)),
|
||||
};
|
||||
// A JSON body is required (415 otherwise): the toggle cannot be sent by
|
||||
// a cross-origin form or `text/plain` request without a CORS preflight.
|
||||
let body = match json_body(request).await {
|
||||
Ok(serde_json::Value::Object(body)) if !body.is_empty() => serde_json::Value::Object(body),
|
||||
Ok(_) => {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"expected {\"shared\": bool}",
|
||||
));
|
||||
}
|
||||
Err(error) => return Ok(api_error(error)),
|
||||
};
|
||||
let store = state.store().await?;
|
||||
let federation = crate::federation::handle();
|
||||
let dht = federation.podcast_discovery();
|
||||
match api::set_share(
|
||||
&store,
|
||||
user_id,
|
||||
&podcast_id,
|
||||
body,
|
||||
discovery_state(&federation, dht.as_deref()),
|
||||
now_ms(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(api::ShareOutcome::Done(response)) => api_response(Ok(*response)),
|
||||
Ok(api::ShareOutcome::NeedsConfirmation(share)) => {
|
||||
let body = serde_json::json!({
|
||||
"error": "the feed URL may contain private data; confirm publishing it",
|
||||
"needs_confirmation": true,
|
||||
"share": share,
|
||||
});
|
||||
Ok(cot::http::Response::builder()
|
||||
.status(StatusCode::CONFLICT)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.header(CACHE_CONTROL, "private, no-store")
|
||||
.body(Body::fixed(body.to_string()))
|
||||
.map_err(internal)?)
|
||||
}
|
||||
Err(error) => Ok(api_error(error)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn episodes_handler(
|
||||
state: State,
|
||||
auth_ctx: auth::AuthContext,
|
||||
@@ -632,6 +754,16 @@ impl App for PodcastsApp {
|
||||
let list_state = self.state.clone();
|
||||
let subscribe_state = self.state.clone();
|
||||
let episodes_state = self.state.clone();
|
||||
let share_state = self.state.clone();
|
||||
let found_route = |route: FoundRoute| {
|
||||
let state = self.state.clone();
|
||||
move |auth_ctx: auth::AuthContext,
|
||||
session: Session,
|
||||
db: Database,
|
||||
request: cot::request::Request| {
|
||||
found_handler(state.clone(), route, auth_ctx, session, db, request)
|
||||
}
|
||||
};
|
||||
let podcast_route = |route: PodcastRoute| {
|
||||
let state = self.state.clone();
|
||||
move |auth_ctx: auth::AuthContext,
|
||||
@@ -680,6 +812,16 @@ impl App for PodcastsApp {
|
||||
),
|
||||
"podcasts_feeds",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/discovery/preview",
|
||||
post(found_route(FoundRoute::Preview)),
|
||||
"podcasts_discovery_preview",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/discovery/subscribe",
|
||||
post(found_route(FoundRoute::Subscribe)),
|
||||
"podcasts_discovery_subscribe",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/feeds/{podcast_id}/episodes",
|
||||
get(
|
||||
@@ -703,6 +845,19 @@ impl App for PodcastsApp {
|
||||
post(podcast_route(PodcastRoute::Unsubscribe)),
|
||||
"podcasts_unsubscribe",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/feeds/{podcast_id}/share",
|
||||
post(
|
||||
move |auth_ctx: auth::AuthContext,
|
||||
session: Session,
|
||||
db: Database,
|
||||
path: Path<PodcastPath>,
|
||||
request: cot::request::Request| {
|
||||
share_handler(share_state.clone(), auth_ctx, session, db, path, request)
|
||||
},
|
||||
),
|
||||
"podcasts_share",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/feeds/{podcast_id}",
|
||||
get(podcast_route(PodcastRoute::Detail)),
|
||||
|
||||
+16
-2
@@ -20,6 +20,7 @@
|
||||
//! | `furumusic__podcast_progress` | `episode_id` | progress register | yes |
|
||||
//! | `furumusic__podcast_listen_session` | `session_id` | grow-only set of sessions | yes |
|
||||
//! | `furumusic__podcast_fetch_state` | `podcast_id` | ETag, Last-Modified, check times, errors, cover cache | **never** |
|
||||
//! | `furumusic__podcast_share_confirmation` | `podcast_id` | privacy confirmation of a shared card (share decision, card URLs) | **never** |
|
||||
//!
|
||||
//! # One apply path
|
||||
//!
|
||||
@@ -54,12 +55,20 @@
|
||||
//! `/api/podcasts` endpoints. Audio is streamed from the publisher, never
|
||||
//! stored.
|
||||
//! * [`api`] — what the endpoints of the podcast UI and the web player do
|
||||
//! (subscribe by URL, lists, explicit progress actions, playback sessions
|
||||
//! and progress reports), independent of HTTP.
|
||||
//! (subscribe by URL, lists, explicit progress actions, sharing, playback
|
||||
//! sessions and progress reports), independent of HTTP.
|
||||
//! * [`discovery`] — public discovery: which podcasts the server node
|
||||
//! publishes to `music_dht::podcast_dht` for the accounts that explicitly
|
||||
//! shared them, the local privacy confirmations and the reconciler that
|
||||
//! calls `PodcastDht::sync_shares`.
|
||||
//! * [`search`] — public podcasts found by the federated search, as seen by
|
||||
//! the searching account ("already subscribed"), and the preview of a
|
||||
//! found podcast's feed before subscribing.
|
||||
|
||||
mod actions;
|
||||
pub(crate) mod api;
|
||||
mod apply;
|
||||
pub(crate) mod discovery;
|
||||
mod fetch;
|
||||
pub(crate) mod http;
|
||||
mod limits;
|
||||
@@ -69,7 +78,10 @@ mod queries;
|
||||
mod refresh;
|
||||
pub(crate) mod runner;
|
||||
pub(crate) mod schema;
|
||||
pub(crate) mod search;
|
||||
|
||||
#[cfg(test)]
|
||||
mod discovery_tests;
|
||||
#[cfg(test)]
|
||||
mod fetch_tests;
|
||||
#[cfg(test)]
|
||||
@@ -79,6 +91,8 @@ pub(crate) mod postgres_tests;
|
||||
#[cfg(test)]
|
||||
mod proxy_tests;
|
||||
#[cfg(test)]
|
||||
mod search_tests;
|
||||
#[cfg(test)]
|
||||
pub(crate) mod test_server;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
@@ -16,6 +16,7 @@ use music_dht::podcast::{
|
||||
ProgressStatus, ShareRecord, SubscriptionRecord,
|
||||
};
|
||||
|
||||
use super::discovery::Discovery;
|
||||
use super::*;
|
||||
use crate::player::queue_items::test_episode;
|
||||
|
||||
@@ -60,13 +61,14 @@ pub(crate) async fn test_pool() -> sqlx::PgPool {
|
||||
sqlx::PgPool::connect(&url).await.unwrap()
|
||||
}
|
||||
|
||||
fn random_user() -> i64 {
|
||||
pub(crate) fn random_user() -> i64 {
|
||||
let bytes = blake3::hash(random_hex(8).as_bytes());
|
||||
9_000_000_000
|
||||
+ i64::from(u32::from_le_bytes(bytes.as_bytes()[..4].try_into().unwrap()) % 1_000_000_000)
|
||||
}
|
||||
|
||||
const ACCOUNT_TABLES: &[&str] = &[
|
||||
pub(crate) const ACCOUNT_TABLES: &[&str] = &[
|
||||
"furumusic__podcast_share_confirmation",
|
||||
"furumusic__podcast_fetch_state",
|
||||
"furumusic__podcast_listen_session",
|
||||
"furumusic__podcast_progress",
|
||||
@@ -85,7 +87,7 @@ const ACCOUNT_TABLES: &[&str] = &[
|
||||
"furumusic__fed_state_playlist_item",
|
||||
];
|
||||
|
||||
async fn cleanup(pool: &sqlx::PgPool, users: impl IntoIterator<Item = i64>) {
|
||||
pub(crate) async fn cleanup(pool: &sqlx::PgPool, users: impl IntoIterator<Item = i64>) {
|
||||
let users: Vec<i64> = users.into_iter().collect();
|
||||
for table in ACCOUNT_TABLES {
|
||||
sqlx::query(&format!("DELETE FROM {table} WHERE user_id = ANY($1)"))
|
||||
@@ -1254,7 +1256,11 @@ async fn postgres_podcast_migrations_import_the_relay_state() {
|
||||
let url = format!("{base}/{name}");
|
||||
|
||||
// A 0.13.0 database: every migration up to the relay table.
|
||||
let new_migrations = ["m_0056_create_podcast_store", "m_0057_import_podcast_relay"];
|
||||
let new_migrations = [
|
||||
"m_0056_create_podcast_store",
|
||||
"m_0057_import_podcast_relay",
|
||||
"m_0058_create_podcast_share_confirmation",
|
||||
];
|
||||
let old: Vec<_> = all_migrations()
|
||||
.into_iter()
|
||||
.filter(|migration| {
|
||||
@@ -1595,7 +1601,7 @@ fn refresh_item(guid: &str) -> String {
|
||||
|
||||
/// A fetcher that may reach the local test server (loopback is allowed
|
||||
/// only by the test policy).
|
||||
fn local_fetcher() -> FeedFetcher {
|
||||
pub(crate) fn local_fetcher() -> FeedFetcher {
|
||||
FeedFetcher::with_http(
|
||||
net::SafeHttp::with_lookup(
|
||||
fetch::FEED_HTTP,
|
||||
@@ -2056,13 +2062,13 @@ async fn postgres_podcast_api_subscribes_plays_and_keeps_progress() {
|
||||
.is_empty()
|
||||
);
|
||||
assert_eq!(
|
||||
api::podcast_detail(&store, other, &podcast_id)
|
||||
api::podcast_detail(&store, other, &podcast_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap_err()
|
||||
.status,
|
||||
404
|
||||
);
|
||||
let detail = api::podcast_detail(&store, user, &podcast_id)
|
||||
let detail = api::podcast_detail(&store, user, &podcast_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap();
|
||||
let seasons: Vec<Option<u32>> = detail.seasons.iter().map(|season| season.season).collect();
|
||||
@@ -2247,7 +2253,7 @@ async fn postgres_podcast_api_subscribes_plays_and_keeps_progress() {
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!((replay.position_ms, replay.status), (0, "completed"));
|
||||
let history = api::podcast_detail(&store, user, &podcast_id)
|
||||
let history = api::podcast_detail(&store, user, &podcast_id, Discovery::Off)
|
||||
.await
|
||||
.unwrap()
|
||||
.history;
|
||||
|
||||
@@ -292,7 +292,19 @@ impl PodcastStore {
|
||||
}
|
||||
outcome.learned_aliases = learned;
|
||||
}
|
||||
// The card of a shared podcast carries the channel metadata, the
|
||||
// fetch URL and the episode counters of the feed.
|
||||
let shared: Option<bool> = sqlx::query_scalar(
|
||||
"SELECT shared FROM furumusic__podcast_share WHERE user_id = $1 AND podcast_id = $2",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(podcast_id.as_str())
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
if shared == Some(true) {
|
||||
super::discovery::request_reconcile();
|
||||
}
|
||||
outcome.feed_url = Some(fetch_url);
|
||||
Ok(outcome)
|
||||
}
|
||||
|
||||
+99
-17
@@ -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,114 @@ 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 and moves to), without duplicates.
|
||||
///
|
||||
/// Only the first two are where the document came from. The
|
||||
/// `itunes:new-feed-url` is a claim of the document: following the feed
|
||||
/// moves to it anyway (its first refresh fetches from there), so it may
|
||||
/// name the account's podcast for the feed, but the document is never that
|
||||
/// podcast's content ([`subscribe_fetched`] does not ingest it there).
|
||||
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
|
||||
}
|
||||
|
||||
/// Whether a document fetched from `requested` is the content of a podcast
|
||||
/// fetched from `feed_url`: the request went to `feed_url` itself (what a
|
||||
/// refresh does), or a permanent redirect chain ended there.
|
||||
fn fetched_from_feed(requested: &FeedUrl, document: &FetchedFeed, feed_url: &FeedUrl) -> bool {
|
||||
requested == feed_url || (document.moved_permanently && document.fetched_from == *feed_url)
|
||||
}
|
||||
|
||||
/// Writes the subscription for a document fetched from `url`.
|
||||
///
|
||||
/// * The podcast is the account's podcast for the feed
|
||||
/// ([`super::search::known_podcast`] over [`document_urls`]: a subscribed
|
||||
/// one first), so a feed the account already follows under another
|
||||
/// address is not subscribed a second time; otherwise a new podcast with
|
||||
/// the id of `url`.
|
||||
/// * The document is ingested like a scheduled refresh (which also stores
|
||||
/// the validators and the next check) only for a new podcast, or when it
|
||||
/// came from the existing podcast's own fetch URL
|
||||
/// ([`fetched_from_feed`]). Otherwise (the match came through another
|
||||
/// alias or the feed's `itunes:new-feed-url` claim) it may be a stale
|
||||
/// copy or another feed altogether: the existing podcast is subscribed
|
||||
/// again with its stored channel metadata, like "subscribe again", and
|
||||
/// the refresh job fetches its own URL. An active subscription is never
|
||||
/// written.
|
||||
///
|
||||
/// [`PodcastStore::subscribe`] reuses the existing podcast with its
|
||||
/// progress and history.
|
||||
pub(crate) async fn subscribe_fetched(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
url: &FeedUrl,
|
||||
document: &FetchedFeed,
|
||||
now_ms: i64,
|
||||
) -> Result<UrlSubscription> {
|
||||
let requested_id = store.resolve_podcast_id(user_id, url).await?;
|
||||
let urls = document_urls(url, document);
|
||||
let existing = super::search::known_podcast(store, user_id, &requested_id, &urls).await?;
|
||||
let meta = match &existing {
|
||||
Some(known) if !fetched_from_feed(url, document, &known.meta.feed_url) => {
|
||||
let subscription = store.subscribe(user_id, &known.meta).await?;
|
||||
return Ok(UrlSubscription {
|
||||
podcast_id: subscription.podcast_id,
|
||||
already_subscribed: subscription.already_subscribed,
|
||||
refresh: RefreshOutcome::default(),
|
||||
});
|
||||
}
|
||||
Some(known) => document.feed.to_podcast_meta(known.meta.feed_url.clone()),
|
||||
None => document.feed.to_podcast_meta(url.clone()),
|
||||
};
|
||||
let subscription = store
|
||||
.subscribe(user_id, &document.feed.to_podcast_meta(url.clone()))
|
||||
.await?;
|
||||
let subscription = store.subscribe(user_id, &meta).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,
|
||||
|
||||
+36
-7
@@ -1,9 +1,10 @@
|
||||
//! PostgreSQL schema of the podcast store and the import of the relay state.
|
||||
//!
|
||||
//! The migrations themselves are declared in `crate::music::db_migrations`
|
||||
//! (`m_0056_create_podcast_store`, `m_0057_import_podcast_relay`), next to
|
||||
//! the rest of the `furumusic` chain; this module holds their SQL so the
|
||||
//! store and its tests share one definition.
|
||||
//! (`m_0056_create_podcast_store`, `m_0057_import_podcast_relay`,
|
||||
//! `m_0058_create_podcast_share_confirmation`), next to the rest of the
|
||||
//! `furumusic` chain; this module holds their SQL so the store and its tests
|
||||
//! share one definition.
|
||||
//!
|
||||
//! The tables mirror the SQLite reference store of `furumi-library`
|
||||
//! (`PODCASTS.md`, "SQLite reference store") with a `user_id` column in
|
||||
@@ -174,6 +175,31 @@ pub(crate) const DROP_PODCAST_STORE: &[&str] = &[
|
||||
"DROP TABLE IF EXISTS furumusic__podcast",
|
||||
];
|
||||
|
||||
/// Local privacy confirmations of shared podcasts (`m_0058`).
|
||||
///
|
||||
/// `PodcastShareState::privacy_confirmed` is client-held state (frid
|
||||
/// `PODCASTS.md`, "Publication"): it is never synced and never part of a
|
||||
/// snapshot. A row records that the account confirmed publishing exactly
|
||||
/// `feed_url` / `id_feed_url` for exactly the share decision `share_op_id`
|
||||
/// despite `PodcastCardSpec::privacy_warnings`. A new share decision (on any
|
||||
/// device) or a feed move to other URLs makes the row stale, so the user is
|
||||
/// asked again (see `crate::podcasts::discovery`).
|
||||
pub(crate) const CREATE_SHARE_CONFIRMATION: &[&str] = &[
|
||||
"CREATE TABLE IF NOT EXISTS furumusic__podcast_share_confirmation (
|
||||
user_id BIGINT NOT NULL,
|
||||
podcast_id TEXT NOT NULL,
|
||||
share_op_id TEXT NOT NULL,
|
||||
feed_url TEXT NOT NULL,
|
||||
id_feed_url TEXT,
|
||||
confirmed_at_ms BIGINT NOT NULL,
|
||||
PRIMARY KEY (user_id, podcast_id)
|
||||
)",
|
||||
];
|
||||
|
||||
/// Backwards operation of [`CREATE_SHARE_CONFIRMATION`].
|
||||
pub(crate) const DROP_SHARE_CONFIRMATION: &[&str] =
|
||||
&["DROP TABLE IF EXISTS furumusic__podcast_share_confirmation"];
|
||||
|
||||
/// Placeholder in [`IMPORT_PODCAST_RELAY`] for the account filter: the
|
||||
/// migration imports every account (`TRUE`), tests narrow it to one.
|
||||
pub(crate) const USER_FILTER: &str = "{user_filter}";
|
||||
@@ -318,7 +344,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn every_store_table_is_dropped_backwards() {
|
||||
for sql in CREATE_PODCAST_STORE {
|
||||
let drops: Vec<&str> = DROP_PODCAST_STORE
|
||||
.iter()
|
||||
.chain(DROP_SHARE_CONFIRMATION)
|
||||
.copied()
|
||||
.collect();
|
||||
for sql in CREATE_PODCAST_STORE.iter().chain(CREATE_SHARE_CONFIRMATION) {
|
||||
let Some(rest) = sql
|
||||
.strip_prefix("CREATE TABLE IF NOT EXISTS ")
|
||||
.or_else(|| sql.strip_prefix("CREATE OR REPLACE VIEW "))
|
||||
@@ -327,9 +358,7 @@ mod tests {
|
||||
};
|
||||
let name = rest.split_whitespace().next().unwrap();
|
||||
assert!(
|
||||
DROP_PODCAST_STORE
|
||||
.iter()
|
||||
.any(|drop| drop.ends_with(&format!(" {name}"))),
|
||||
drops.iter().any(|drop| drop.ends_with(&format!(" {name}"))),
|
||||
"{name}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,474 @@
|
||||
//! Public podcasts found by the federated search, seen by one account.
|
||||
//!
|
||||
//! The federated search (`/api/player/federation/search/events`) asks the
|
||||
//! discovery overlay (`PodcastDht::search`) next to the music DHT and merges
|
||||
//! both with `merge_search_results`: exact title matches first, music before
|
||||
//! podcasts inside a tier. This module turns the podcast hits into
|
||||
//! [`PodcastHitView`]s for the account that searched:
|
||||
//!
|
||||
//! * only the hit's `feed_url` (the origin URL the id hashes from) is
|
||||
//! offered; `claimed_feed_urls` are unverified publisher claims and are
|
||||
//! not passed on;
|
||||
//! * every text and URL of a card is a claim of some publisher. Artwork and
|
||||
//! web-page links are kept only as absolute `http(s)` URLs; the browser
|
||||
//! renders all text as text;
|
||||
//! * `subscribed` / `local_podcast_id` mark a podcast the account already
|
||||
//! has, by id or through a known feed URL or alias (`resolve_podcast_id`).
|
||||
//!
|
||||
//! [`preview`] reads the found podcast's feed (through the SSRF-guarded
|
||||
//! fetcher, nothing stored) so the user can look at its seasons and
|
||||
//! episodes before subscribing; subscribing uses the origin URL, so the new
|
||||
//! private subscription gets the card's id (see `api::subscribe_found`).
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use anyhow::Result;
|
||||
use music_dht::podcast::feed::ParsedFeed;
|
||||
use music_dht::podcast::{FeedUrl, PodcastId};
|
||||
use music_dht::podcast_dht::{FederatedSearchHit, PodcastSearchHit, merge_search_results};
|
||||
use music_dht::{LibraryItem, normalize_name};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{PodcastStore, PodcastSummary, SeasonFilter};
|
||||
|
||||
/// Podcast hits asked from the overlay per search.
|
||||
pub(crate) const PODCAST_SEARCH_LIMIT: usize = 20;
|
||||
/// Episodes listed in a preview (newest first), per season filter.
|
||||
pub(crate) const PREVIEW_EPISODES: usize = 50;
|
||||
/// Longest URL kept from a card or a feed.
|
||||
const MAX_URL_BYTES: usize = 2048;
|
||||
|
||||
/// One public podcast in the federated search results.
|
||||
///
|
||||
/// Serialized with `"type": "podcast"` (music hits are `"type": "music"`).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
pub(crate) struct PodcastHitView {
|
||||
#[serde(rename = "type")]
|
||||
pub(crate) hit_type: &'static str,
|
||||
/// Always `podcast` (music hits: `artist`, `release`, `track`).
|
||||
pub(crate) kind: &'static str,
|
||||
pub(crate) podcast_id: String,
|
||||
/// The origin feed URL: the only URL to subscribe with.
|
||||
pub(crate) feed_url: String,
|
||||
pub(crate) title: String,
|
||||
pub(crate) author: Option<String>,
|
||||
pub(crate) description: Option<String>,
|
||||
/// Absolute `http(s)` artwork URL, if the card has a valid one.
|
||||
pub(crate) image_url: Option<String>,
|
||||
/// Absolute `http(s)` web page, if the card has a valid one.
|
||||
pub(crate) link: Option<String>,
|
||||
pub(crate) language: Option<String>,
|
||||
pub(crate) categories: Vec<String>,
|
||||
pub(crate) episode_count: Option<u32>,
|
||||
pub(crate) latest_episode_at_ms: Option<i64>,
|
||||
/// Distinct publisher devices (not subscribers, not a trust signal).
|
||||
pub(crate) publisher_count: usize,
|
||||
/// Position in the merged music and podcast results.
|
||||
pub(crate) rank: usize,
|
||||
/// The normalized title equals the normalized query.
|
||||
pub(crate) exact_match: bool,
|
||||
/// The account has an active subscription to this podcast.
|
||||
pub(crate) subscribed: bool,
|
||||
/// The account's podcast for this hit (its id, or the podcast a known
|
||||
/// feed URL or alias resolves to), when the account knows it.
|
||||
pub(crate) local_podcast_id: Option<String>,
|
||||
}
|
||||
|
||||
/// An absolute `http(s)` URL without credentials, at most 2 048 bytes.
|
||||
pub(crate) fn http_url(value: Option<&str>) -> Option<String> {
|
||||
let value = value?.trim();
|
||||
if value.is_empty() || value.len() > MAX_URL_BYTES {
|
||||
return None;
|
||||
}
|
||||
let url = reqwest::Url::parse(value).ok()?;
|
||||
(matches!(url.scheme(), "http" | "https")
|
||||
&& url.host_str().is_some()
|
||||
&& url.username().is_empty()
|
||||
&& url.password().is_none())
|
||||
.then(|| value.to_string())
|
||||
}
|
||||
|
||||
/// Podcast hits in their merged order with the music results:
|
||||
/// `(rank, exact_match, hit)`.
|
||||
pub(crate) fn ranked_hits(
|
||||
query: &str,
|
||||
music: &[LibraryItem],
|
||||
podcasts: Vec<PodcastSearchHit>,
|
||||
) -> Vec<(usize, bool, PodcastSearchHit)> {
|
||||
let normalized = normalize_name(query);
|
||||
merge_search_results(query, music.to_vec(), podcasts)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(rank, hit)| match hit {
|
||||
FederatedSearchHit::Podcast(hit) => {
|
||||
let exact = !normalized.is_empty() && normalize_name(&hit.card.title) == normalized;
|
||||
Some((rank, exact, hit))
|
||||
}
|
||||
FederatedSearchHit::Music(_) => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The account's podcast for a found podcast: the podcast with that id, or
|
||||
/// 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<Option<PodcastSummary>> {
|
||||
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 !ids.contains(&resolved) {
|
||||
ids.push(resolved);
|
||||
}
|
||||
}
|
||||
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.
|
||||
pub(crate) async fn hit_views(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
query: &str,
|
||||
music: &[LibraryItem],
|
||||
podcasts: Vec<PodcastSearchHit>,
|
||||
) -> 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,
|
||||
std::slice::from_ref(&hit.feed_url),
|
||||
)
|
||||
.await?;
|
||||
let card = hit.card;
|
||||
views.push(PodcastHitView {
|
||||
hit_type: "podcast",
|
||||
kind: "podcast",
|
||||
podcast_id: hit.podcast_id.to_string(),
|
||||
feed_url: hit.feed_url.to_string(),
|
||||
title: card.title,
|
||||
author: card.author,
|
||||
description: card.description,
|
||||
image_url: http_url(card.image_url.as_deref()),
|
||||
link: http_url(card.link.as_deref()),
|
||||
language: card.language,
|
||||
categories: card.categories,
|
||||
episode_count: card.episode_count,
|
||||
latest_episode_at_ms: card.latest_episode_at_ms,
|
||||
publisher_count: hit.publishers.len(),
|
||||
rank,
|
||||
exact_match,
|
||||
subscribed: known.as_ref().is_some_and(|summary| summary.subscribed),
|
||||
local_podcast_id: known.map(|summary| summary.podcast_id.to_string()),
|
||||
});
|
||||
}
|
||||
Ok(views)
|
||||
}
|
||||
|
||||
/// One season group of a preview (`season: null`: episodes without one).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
pub(crate) struct PreviewSeason {
|
||||
pub(crate) season: Option<u32>,
|
||||
pub(crate) episode_count: usize,
|
||||
}
|
||||
|
||||
/// One episode of a preview.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
pub(crate) struct PreviewEpisode {
|
||||
pub(crate) title: String,
|
||||
pub(crate) published_at_ms: Option<i64>,
|
||||
pub(crate) duration_ms: Option<i64>,
|
||||
pub(crate) season: Option<u32>,
|
||||
pub(crate) episode_number: Option<u32>,
|
||||
}
|
||||
|
||||
/// A found podcast read from its feed: channel, seasons and the newest
|
||||
/// episodes. Nothing of it is stored.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
pub(crate) struct PodcastPreview {
|
||||
pub(crate) podcast_id: String,
|
||||
/// The URL that was read (the card's origin URL).
|
||||
pub(crate) feed_url: String,
|
||||
/// The feed answered with a permanent redirect chain to another URL.
|
||||
pub(crate) moved: bool,
|
||||
pub(crate) title: String,
|
||||
pub(crate) author: Option<String>,
|
||||
pub(crate) description: Option<String>,
|
||||
pub(crate) image_url: Option<String>,
|
||||
pub(crate) link: Option<String>,
|
||||
pub(crate) episode_count: usize,
|
||||
pub(crate) seasons: Vec<PreviewSeason>,
|
||||
/// Episodes of the requested season (all episodes without a filter).
|
||||
pub(crate) listed_count: usize,
|
||||
/// The newest of them, at most [`PREVIEW_EPISODES`].
|
||||
pub(crate) episodes: Vec<PreviewEpisode>,
|
||||
pub(crate) subscribed: bool,
|
||||
pub(crate) local_podcast_id: Option<String>,
|
||||
}
|
||||
|
||||
/// The preview of a parsed feed (`known`: the account's podcast for it),
|
||||
/// listing the episodes of `season`.
|
||||
pub(crate) fn preview(
|
||||
podcast_id: &PodcastId,
|
||||
feed_url: &FeedUrl,
|
||||
moved: bool,
|
||||
feed: &ParsedFeed,
|
||||
known: Option<&PodcastSummary>,
|
||||
season: SeasonFilter,
|
||||
) -> PodcastPreview {
|
||||
let mut seasons: BTreeMap<Option<u32>, usize> = BTreeMap::new();
|
||||
for episode in &feed.episodes {
|
||||
*seasons.entry(episode.season).or_default() += 1;
|
||||
}
|
||||
let mut episodes: Vec<&_> = feed
|
||||
.episodes
|
||||
.iter()
|
||||
.filter(|episode| match season {
|
||||
SeasonFilter::Any => true,
|
||||
SeasonFilter::Season(number) => episode.season == Some(number),
|
||||
SeasonFilter::NoSeason => episode.season.is_none(),
|
||||
})
|
||||
.collect();
|
||||
let listed_count = episodes.len();
|
||||
// Newest first; undated episodes keep their document order at the end.
|
||||
episodes.sort_by_key(|episode| std::cmp::Reverse(episode.published_at_ms));
|
||||
PodcastPreview {
|
||||
podcast_id: podcast_id.to_string(),
|
||||
feed_url: feed_url.to_string(),
|
||||
moved,
|
||||
title: feed.title.clone(),
|
||||
author: feed.author.clone(),
|
||||
description: feed.description.clone(),
|
||||
image_url: http_url(feed.image_url.as_deref()),
|
||||
link: http_url(feed.link.as_deref()),
|
||||
episode_count: feed.episodes.len(),
|
||||
// Numbered seasons first, then the episodes without a season.
|
||||
seasons: seasons
|
||||
.iter()
|
||||
.filter(|(season, _)| season.is_some())
|
||||
.chain(seasons.iter().filter(|(season, _)| season.is_none()))
|
||||
.map(|(season, count)| PreviewSeason {
|
||||
season: *season,
|
||||
episode_count: *count,
|
||||
})
|
||||
.collect(),
|
||||
listed_count,
|
||||
episodes: episodes
|
||||
.into_iter()
|
||||
.take(PREVIEW_EPISODES)
|
||||
.map(|episode| PreviewEpisode {
|
||||
title: episode.title.clone(),
|
||||
published_at_ms: episode.published_at_ms,
|
||||
duration_ms: episode.duration_ms,
|
||||
season: episode.season,
|
||||
episode_number: episode.episode_number,
|
||||
})
|
||||
.collect(),
|
||||
subscribed: known.is_some_and(|summary| summary.subscribed),
|
||||
local_podcast_id: known.map(|summary| summary.podcast_id.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use music_dht::podcast_dht::{PodcastCard, PodcastCardSpec};
|
||||
use music_dht::{ItemId, ItemKind, NetworkId, SecretKey};
|
||||
|
||||
fn hit(path: &str, title: &str) -> PodcastSearchHit {
|
||||
let feed_url = FeedUrl::parse(&format!("https://feeds.example/{path}")).unwrap();
|
||||
let podcast_id = PodcastId::from_feed_url(&feed_url);
|
||||
let publisher = SecretKey::from_bytes(&[7; 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: None,
|
||||
description: None,
|
||||
image_url: None,
|
||||
link: None,
|
||||
language: None,
|
||||
categories: Vec::new(),
|
||||
episode_count: None,
|
||||
latest_episode_at_ms: None,
|
||||
},
|
||||
NetworkId::from_name("search-test"),
|
||||
publisher,
|
||||
1,
|
||||
1_700_000_000_000,
|
||||
);
|
||||
PodcastSearchHit {
|
||||
podcast_id,
|
||||
feed_url,
|
||||
card,
|
||||
publishers: vec![publisher],
|
||||
claimed_feed_urls: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn music(name: &str) -> LibraryItem {
|
||||
LibraryItem {
|
||||
id: ItemId::from_bytes([1; 32]),
|
||||
owner: SecretKey::from_bytes(&[5; 32]).public(),
|
||||
kind: ItemKind::Track,
|
||||
name: name.into(),
|
||||
normalized_name: normalize_name(name),
|
||||
artist_names: Vec::new(),
|
||||
featured_artist_names: Vec::new(),
|
||||
year: None,
|
||||
release_type: None,
|
||||
release_title: None,
|
||||
track_number: None,
|
||||
disc_number: None,
|
||||
duration_seconds: None,
|
||||
content_id: None,
|
||||
revision: 1,
|
||||
deleted: false,
|
||||
updated_at_ms: 1,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_hits_take_their_place_among_music_results() {
|
||||
let ranked = ranked_hits(
|
||||
"Night Radio",
|
||||
&[music("Night Radio Theme"), music("Night Radio")],
|
||||
vec![
|
||||
hit("a.xml", "Night Radio"),
|
||||
hit("b.xml", "Night Radio Talks"),
|
||||
],
|
||||
);
|
||||
let ranks: Vec<_> = ranked
|
||||
.iter()
|
||||
.map(|(rank, exact, hit)| (*rank, *exact, hit.card.title.as_str()))
|
||||
.collect();
|
||||
// Exact matches lead (music first inside a tier), then the rest.
|
||||
assert_eq!(
|
||||
ranks,
|
||||
vec![(1, true, "Night Radio"), (3, false, "Night Radio Talks")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_absolute_http_urls_without_credentials_are_kept() {
|
||||
for (value, kept) in [
|
||||
("https://cdn.example/art.jpg", true),
|
||||
("http://example.org/show", true),
|
||||
("javascript:alert(1)", false),
|
||||
("data:image/png;base64,AAAA", false),
|
||||
("//cdn.example/art.jpg", false),
|
||||
("https://user:secret@cdn.example/art.jpg", false),
|
||||
("file:///etc/passwd", false),
|
||||
("", false),
|
||||
] {
|
||||
assert_eq!(http_url(Some(value)).is_some(), kept, "{value}");
|
||||
}
|
||||
let long = format!("https://cdn.example/{}", "a".repeat(MAX_URL_BYTES));
|
||||
assert_eq!(http_url(Some(&long)), None);
|
||||
assert_eq!(http_url(None), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn podcast_hits_that_cannot_be_prepared_are_reported_as_failed() {
|
||||
use crate::federation::client::{MusicSearch, PodcastSearch};
|
||||
// The federation handle of a unit test has no database: ranking the
|
||||
// hits for the account fails. The search still completes.
|
||||
let federation = crate::federation::handle();
|
||||
let music = MusicSearch {
|
||||
events: Vec::new(),
|
||||
items: vec![music("Night Radio Theme")],
|
||||
};
|
||||
let (state, events) = federation
|
||||
.podcast_search_events(
|
||||
"search",
|
||||
"night radio",
|
||||
1,
|
||||
&music,
|
||||
PodcastSearch::Found(vec![hit("a.xml", "Night Radio")]),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(state, "failed");
|
||||
assert!(events.is_empty());
|
||||
for (search, expected) in [
|
||||
(PodcastSearch::Found(Vec::new()), "running"),
|
||||
(PodcastSearch::Off, "off"),
|
||||
(PodcastSearch::Failed, "failed"),
|
||||
] {
|
||||
let (state, events) = federation
|
||||
.podcast_search_events("search", "night radio", 1, &music, search)
|
||||
.await;
|
||||
assert_eq!(state, expected);
|
||||
assert!(events.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_preview_lists_seasons_and_the_newest_episodes() {
|
||||
let xml = r#"<?xml version="1.0"?>
|
||||
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"><channel>
|
||||
<title>Preview Show</title><link>javascript:alert(1)</link>
|
||||
<item><title>Old</title><guid>1</guid><itunes:season>1</itunes:season>
|
||||
<pubDate>Mon, 01 Jan 2024 10:00:00 GMT</pubDate>
|
||||
<enclosure url="https://cdn.example/1.mp3" type="audio/mpeg"/></item>
|
||||
<item><title>Bonus</title><guid>2</guid>
|
||||
<enclosure url="https://cdn.example/2.mp3" type="audio/mpeg"/></item>
|
||||
<item><title>New</title><guid>3</guid><itunes:season>2</itunes:season>
|
||||
<pubDate>Mon, 01 Jul 2024 10:00:00 GMT</pubDate>
|
||||
<enclosure url="https://cdn.example/3.mp3" type="audio/mpeg"/></item>
|
||||
</channel></rss>"#;
|
||||
let url = FeedUrl::parse("https://feeds.example/preview.xml").unwrap();
|
||||
let feed =
|
||||
music_dht::podcast::feed::parse_feed_with_base(xml.as_bytes(), Some(url.as_str()))
|
||||
.unwrap();
|
||||
let id = PodcastId::from_feed_url(&url);
|
||||
let preview_of = |season| preview(&id, &url, false, &feed, None, season);
|
||||
let preview = preview_of(SeasonFilter::Any);
|
||||
assert_eq!(preview.title, "Preview Show");
|
||||
assert_eq!(preview.link, None, "only http(s) links");
|
||||
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), 1), (Some(2), 1), (None, 1)]);
|
||||
let titles: Vec<_> = preview.episodes.iter().map(|e| e.title.as_str()).collect();
|
||||
assert_eq!(titles, vec!["New", "Old", "Bonus"]);
|
||||
assert_eq!(preview.listed_count, 3);
|
||||
assert!(!preview.subscribed && preview.local_podcast_id.is_none());
|
||||
|
||||
// One season (older seasons stay reachable), or the episodes
|
||||
// without a season; the season groups stay complete.
|
||||
let first = preview_of(SeasonFilter::Season(1));
|
||||
let titles: Vec<_> = first.episodes.iter().map(|e| e.title.as_str()).collect();
|
||||
assert_eq!(titles, vec!["Old"]);
|
||||
assert_eq!((first.listed_count, first.seasons.len()), (1, 3));
|
||||
let unnumbered = preview_of(SeasonFilter::NoSeason);
|
||||
assert_eq!(unnumbered.episodes[0].title, "Bonus");
|
||||
assert_eq!(unnumbered.listed_count, 1);
|
||||
assert!(preview_of(SeasonFilter::Season(9)).episodes.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,798 @@
|
||||
//! 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())
|
||||
);
|
||||
// One season at a time; a bad season is refused before any fetch.
|
||||
let mut seasonal = body(&show);
|
||||
seasonal["season"] = json!("1");
|
||||
let first = api::preview_found(&store, &fetcher, user, seasonal)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(first.listed_count, 2);
|
||||
assert!(
|
||||
first
|
||||
.episodes
|
||||
.iter()
|
||||
.all(|episode| episode.season == Some(1))
|
||||
);
|
||||
let mut unnumbered = body(&show);
|
||||
unnumbered["season"] = json!("none");
|
||||
let none = api::preview_found(&store, &fetcher, user, unnumbered)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(none.listed_count, 1);
|
||||
assert!(none.episodes.iter().all(|episode| episode.season.is_none()));
|
||||
let requests = server.requests("/show.xml").len();
|
||||
let mut bad = body(&show);
|
||||
bad["season"] = json!("first");
|
||||
let error = api::preview_found(&store, &fetcher, user, bad)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.status, 400);
|
||||
assert_eq!(server.requests("/show.xml").len(), requests);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
/// The catalog row and the episodes of one podcast of the account:
|
||||
/// `(title, description, meta_from_feed, [(episode title, in_feed)])`.
|
||||
async fn podcast_snapshot(
|
||||
pool: &sqlx::PgPool,
|
||||
user: i64,
|
||||
podcast_id: &PodcastId,
|
||||
) -> (String, Option<String>, bool, Vec<(String, bool)>) {
|
||||
let (title, description, meta_from_feed): (String, Option<String>, bool) = sqlx::query_as(
|
||||
"SELECT title, description, meta_from_feed FROM furumusic__podcast
|
||||
WHERE user_id = $1 AND podcast_id = $2",
|
||||
)
|
||||
.bind(user)
|
||||
.bind(podcast_id.as_str())
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let episodes: Vec<(String, bool)> = sqlx::query_as(
|
||||
"SELECT title, in_feed FROM furumusic__podcast_episode
|
||||
WHERE user_id = $1 ORDER BY title",
|
||||
)
|
||||
.bind(user)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
(title, description, meta_from_feed, episodes)
|
||||
}
|
||||
|
||||
async fn active_subscriptions(pool: &sqlx::PgPool, user: i64) -> i64 {
|
||||
sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM furumusic__podcast_subscription
|
||||
WHERE user_id = $1 AND subscribed",
|
||||
)
|
||||
.bind(user)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
||||
async fn postgres_a_feed_claiming_a_followed_address_never_rewrites_that_podcast() {
|
||||
let pool = test_pool().await;
|
||||
let store = PodcastStore::new(pool.clone());
|
||||
let user = random_user();
|
||||
cleanup(&pool, [user]).await;
|
||||
let server = TestServer::start();
|
||||
let real = server.url("/real.xml");
|
||||
server.set(
|
||||
"/real.xml",
|
||||
Route::new(rss("Real Show", ITEMS), "application/rss+xml"),
|
||||
);
|
||||
// The old address serves a frozen copy that names the new address; a
|
||||
// feed of somebody else claims to have moved there too.
|
||||
let claim = |title: &str, guid: &str| {
|
||||
rss(
|
||||
title,
|
||||
&format!(
|
||||
r#"<itunes:new-feed-url>{real}</itunes:new-feed-url>
|
||||
<item><title>{title} item</title><guid>{guid}</guid>
|
||||
<enclosure url="/audio/{guid}.mp3" type="audio/mpeg" length="1000"/></item>"#
|
||||
),
|
||||
)
|
||||
};
|
||||
server.set(
|
||||
"/stub.xml",
|
||||
Route::new(claim("Stale Copy", "stale"), "application/rss+xml"),
|
||||
);
|
||||
server.set(
|
||||
"/evil.xml",
|
||||
Route::new(claim("Injected", "evil"), "application/rss+xml"),
|
||||
);
|
||||
let fetcher = local_fetcher();
|
||||
let now = super::now_ms();
|
||||
let real_id = PodcastId::from_feed_url(&url(&real));
|
||||
let followed = runner::subscribe_url(&store, &fetcher, user, &url(&real), now)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(followed.podcast_id, real_id);
|
||||
let before = podcast_snapshot(&pool, user, &real_id).await;
|
||||
assert_eq!(before.0, "Real Show");
|
||||
assert_eq!(before.3.len(), 3);
|
||||
assert!(before.3.iter().all(|(_, in_feed)| *in_feed));
|
||||
let register_before = store.subscription(user, &real_id).await.unwrap().unwrap();
|
||||
let log = log_len(&pool, user).await;
|
||||
|
||||
// "Add RSS" of the old address and "Subscribe" on a card of the other
|
||||
// feed: the account already follows the feed they name, nothing of
|
||||
// theirs is written into it.
|
||||
for added in [
|
||||
runner::subscribe_url(&store, &fetcher, user, &url(&server.url("/stub.xml")), now)
|
||||
.await
|
||||
.unwrap(),
|
||||
runner::subscribe_url(&store, &fetcher, user, &url(&server.url("/evil.xml")), now)
|
||||
.await
|
||||
.unwrap(),
|
||||
] {
|
||||
assert!(added.already_subscribed);
|
||||
assert_eq!(added.podcast_id, real_id);
|
||||
assert_eq!(added.refresh.new_episodes, 0);
|
||||
}
|
||||
let evil = hit(&server.url("/evil.xml"), "Injected");
|
||||
let body =
|
||||
json!({ "podcast_id": evil.podcast_id.to_string(), "feed_url": evil.feed_url.to_string() });
|
||||
let found = api::subscribe_found(&store, &fetcher, user, body.clone(), now)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(found.already_subscribed);
|
||||
assert_eq!(found.podcast.podcast_id, real_id.to_string());
|
||||
assert_eq!(podcast_snapshot(&pool, user, &real_id).await, before);
|
||||
assert_eq!(log_len(&pool, user).await, log);
|
||||
assert_eq!(active_subscriptions(&pool, user).await, 1);
|
||||
|
||||
// Unsubscribed, the same claim subscribes the account's podcast again
|
||||
// with its own stored metadata; the claiming document is not ingested
|
||||
// (the refresh job fetches the podcast's own address).
|
||||
store.unsubscribe(user, &real_id).await.unwrap();
|
||||
let again = runner::subscribe_url(&store, &fetcher, user, &url(&server.url("/evil.xml")), now)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!again.already_subscribed);
|
||||
assert_eq!(again.podcast_id, real_id);
|
||||
assert_eq!(again.refresh.new_episodes, 0);
|
||||
assert_eq!(podcast_snapshot(&pool, user, &real_id).await, before);
|
||||
let register = store.subscription(user, &real_id).await.unwrap().unwrap();
|
||||
assert!(register.subscribed);
|
||||
assert_eq!(register.podcast, register_before.podcast);
|
||||
assert!(
|
||||
store
|
||||
.podcast(user, &evil.podcast_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
|
||||
// Its own address is still ingested as before (a refresh).
|
||||
let own = runner::subscribe_url(&store, &fetcher, user, &url(&real), now)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(own.already_subscribed);
|
||||
assert_eq!(own.refresh.unchanged_episodes, 3);
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -48,6 +48,114 @@
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Podcast: confirm publishing a feed URL that looks private -->
|
||||
<template x-if="$store.podcasts.shareDialog">
|
||||
<div class="modal-overlay" @click.self="$store.podcasts.cancelShare()" @keydown.escape.window="$store.podcasts.cancelShare()">
|
||||
<div class="modal-box podcast-share-dialog" role="dialog" aria-modal="true" aria-labelledby="podcast-share-dialog-title">
|
||||
<h3 id="podcast-share-dialog-title">{{ t.player_podcast_share_dialog_title }}</h3>
|
||||
<p class="podcast-share-dialog-title" x-text="$store.podcasts.shareDialog.title"></p>
|
||||
<p>{{ t.player_podcast_share_dialog_text }}</p>
|
||||
<ul class="podcast-share-warnings">
|
||||
<template x-for="(warning, index) in $store.podcasts.shareDialog.warnings" :key="index">
|
||||
<li x-text="$store.podcasts.warningText(warning)"></li>
|
||||
</template>
|
||||
</ul>
|
||||
<div class="podcast-share-url" x-text="$store.podcasts.shareDialog.feed_url"></div>
|
||||
<div class="podcast-share-url" x-show="$store.podcasts.shareDialog.id_feed_url" x-text="$store.podcasts.shareDialog.id_feed_url"></div>
|
||||
<p class="podcast-share-hint">{{ t.player_podcast_share_dialog_hint }}</p>
|
||||
<div class="modal-footer">
|
||||
<button class="modal-btn modal-btn-ghost" x-init="$nextTick(() => $el.focus())" @click="$store.podcasts.cancelShare()">{{ t.player_cancel }}</button>
|
||||
<button class="modal-btn modal-btn-danger" @click="$store.podcasts.confirmShare()">{{ t.player_podcast_share_dialog_confirm }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Podcast found in the federation: card, preview from its feed, subscribe -->
|
||||
<template x-if="$store.podcasts.found">
|
||||
<div class="modal-overlay" @click.self="$store.podcasts.closeFound()" @keydown.escape.window="$store.podcasts.closeFound()">
|
||||
<div class="modal-box podcast-found-dialog" role="dialog" aria-modal="true" aria-labelledby="podcast-found-title">
|
||||
<div class="podcast-found-head">
|
||||
<div class="podcast-found-cover">
|
||||
<template x-if="$store.podcasts.found.hit.image_url || $store.podcasts.found.preview?.image_url">
|
||||
<img :src="$store.podcasts.found.hit.image_url || $store.podcasts.found.preview?.image_url" alt="" aria-hidden="true" referrerpolicy="no-referrer">
|
||||
</template>
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
|
||||
<rect x="9" y="2" width="6" height="12" rx="3"/><path d="M5 10a7 7 0 0014 0M12 17v5M8 22h8"/>
|
||||
</svg>
|
||||
</div>
|
||||
<div class="podcast-found-heading">
|
||||
<div class="podcast-found-badges">
|
||||
<span class="podcast-type-badge">{{ t.player_podcast_type }}</span>
|
||||
<span class="podcast-subscribed-badge" x-show="$store.podcasts.found.subscribed">{{ t.player_podcast_found_subscribed }}</span>
|
||||
</div>
|
||||
<h3 id="podcast-found-title" x-text="$store.podcasts.found.hit.title"></h3>
|
||||
<div class="podcast-found-author" x-show="$store.podcasts.found.hit.author" x-text="$store.podcasts.found.hit.author"></div>
|
||||
<div class="podcast-found-meta" x-text="$store.podcasts.foundMeta($store.podcasts.found.hit)"></div>
|
||||
<a class="podcast-found-link" x-show="$store.podcasts.found.hit.link"
|
||||
:href="$store.podcasts.found.hit.link" target="_blank" rel="noopener noreferrer nofollow"
|
||||
x-text="$store.podcasts.found.hit.link"></a>
|
||||
</div>
|
||||
</div>
|
||||
<p class="podcast-found-description" x-show="$store.podcasts.found.hit.description" x-text="$store.podcasts.found.hit.description"></p>
|
||||
<div class="podcast-found-label">{{ t.player_podcast_found_feed }}</div>
|
||||
<div class="podcast-share-url" x-text="$store.podcasts.found.hit.feed_url"></div>
|
||||
<p class="podcast-share-hint">{{ t.player_podcast_found_note }}</p>
|
||||
|
||||
<div class="podcast-found-preview">
|
||||
<template x-if="$store.podcasts.found.previewLoading">
|
||||
<div class="podcast-found-status">{{ t.player_podcast_found_loading }}</div>
|
||||
</template>
|
||||
<template x-if="$store.podcasts.found.previewError">
|
||||
<div class="podcast-found-status error">
|
||||
<span x-text="$store.podcasts.found.previewError"></span>
|
||||
<button class="modal-btn modal-btn-ghost" @click="$store.podcasts.loadFoundPreview()">{{ t.player_podcast_found_retry }}</button>
|
||||
</div>
|
||||
</template>
|
||||
<template x-if="$store.podcasts.found.preview">
|
||||
<div>
|
||||
<div class="podcast-found-status" x-show="$store.podcasts.found.preview.moved">{{ t.player_podcast_found_moved }}</div>
|
||||
<div class="podcast-chips podcast-found-seasons" role="group" aria-label="{{ t.player_podcast_season }}">
|
||||
<button class="podcast-chip" type="button"
|
||||
:class="{ active: $store.podcasts.found.season === 'all' }"
|
||||
@click="$store.podcasts.selectFoundSeason('all')">{{ t.player_podcast_all_seasons }}</button>
|
||||
<template x-for="season in $store.podcasts.found.preview.seasons" :key="$store.podcasts.seasonValue(season)">
|
||||
<button class="podcast-chip" type="button"
|
||||
:class="{ active: $store.podcasts.found.season === $store.podcasts.seasonValue(season) }"
|
||||
@click="$store.podcasts.selectFoundSeason($store.podcasts.seasonValue(season))"
|
||||
x-text="`${$store.podcasts.seasonLabel(season)} · ${season.episode_count}`"></button>
|
||||
</template>
|
||||
</div>
|
||||
<ul class="podcast-found-episodes">
|
||||
<template x-for="(episode, index) in $store.podcasts.found.preview.episodes" :key="index">
|
||||
<li>
|
||||
<div class="podcast-found-episode-title" x-text="episode.title"></div>
|
||||
<div class="podcast-found-episode-meta" x-text="$store.podcasts.episodeMeta(episode)"></div>
|
||||
</li>
|
||||
</template>
|
||||
</ul>
|
||||
<div class="podcast-found-status" x-show="$store.podcasts.foundMore($store.podcasts.found.preview)" x-text="$store.podcasts.foundMore($store.podcasts.found.preview)"></div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div class="podcast-found-status error" x-show="$store.podcasts.found.error" x-text="$store.podcasts.found.error"></div>
|
||||
<div class="podcast-found-status" x-show="$store.podcasts.found.message" x-text="$store.podcasts.found.message"></div>
|
||||
<div class="modal-footer">
|
||||
<button class="modal-btn modal-btn-ghost" @click="$store.podcasts.closeFound()">{{ t.player_close }}</button>
|
||||
<template x-if="$store.podcasts.found.subscribed">
|
||||
<button class="modal-btn modal-btn-primary" @click="$store.podcasts.openFoundPodcast()">{{ t.player_podcast_found_open }}</button>
|
||||
</template>
|
||||
<template x-if="!$store.podcasts.found.subscribed">
|
||||
<button class="modal-btn modal-btn-primary" :disabled="$store.podcasts.found.subscribing"
|
||||
@click="$store.podcasts.subscribeFound()"
|
||||
x-text="$store.podcasts.foundButtonLabel()"></button>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Create / Rename Playlist Modal -->
|
||||
<template x-if="$store.playlists.modal">
|
||||
<div class="modal-overlay" @click.self="$store.playlists.modal = null">
|
||||
|
||||
@@ -75,6 +75,20 @@ const T = {
|
||||
podcastFetchError: "{{ t.player_podcast_fetch_error }}",
|
||||
podcastNextCheck: "{{ t.player_podcast_next_check }}",
|
||||
podcastUnsubscribeConfirm: "{{ t.player_podcast_unsubscribe_confirm }}",
|
||||
podcastSharePrivate: "{{ t.player_podcast_share_private }}",
|
||||
podcastSharePublished: "{{ t.player_podcast_share_published }}",
|
||||
podcastSharePublishing: "{{ t.player_podcast_share_publishing }}",
|
||||
podcastShareDiscoveryOff: "{{ t.player_podcast_share_discovery_off }}",
|
||||
podcastShareDiscoveryUnavailable: "{{ t.player_podcast_share_discovery_unavailable }}",
|
||||
podcastShareOverLimit: "{{ t.player_podcast_share_over_limit }}",
|
||||
podcastShareFailed: "{{ t.player_podcast_share_failed }}",
|
||||
podcastSharePending: "{{ t.player_podcast_share_pending }}",
|
||||
podcastShareUnavailable: "{{ t.player_podcast_share_unavailable }}",
|
||||
podcastWarningSecretQuery: "{{ t.player_podcast_warning_secret_query }}",
|
||||
podcastWarningPrivateHost: "{{ t.player_podcast_warning_private_host }}",
|
||||
podcastWarningEntropyQuery: "{{ t.player_podcast_warning_entropy_query }}",
|
||||
podcastWarningEntropyPath: "{{ t.player_podcast_warning_entropy_path }}",
|
||||
podcastWarningOther: "{{ t.player_podcast_warning_other }}",
|
||||
podcastSeason: "{{ t.player_podcast_season }}",
|
||||
podcastNoSeason: "{{ t.player_podcast_no_season }}",
|
||||
podcastLeft: "{{ t.player_podcast_left }}",
|
||||
@@ -83,6 +97,12 @@ const T = {
|
||||
podcastSourceFailed: "{{ t.player_podcast_source_failed }}",
|
||||
podcastCannotSeek: "{{ t.player_podcast_cannot_seek }}",
|
||||
podcastRequestFailed: "{{ t.player_podcast_request_failed }}",
|
||||
podcastType: "{{ t.player_podcast_type }}",
|
||||
podcastFoundLatest: "{{ t.player_podcast_found_latest }}",
|
||||
podcastFoundMore: "{{ t.player_podcast_found_more }}",
|
||||
podcastFoundDone: "{{ t.player_podcast_found_done }}",
|
||||
podcastFoundSubscribing: "{{ t.player_podcast_found_subscribing }}",
|
||||
podcastSubscribe: "{{ t.player_podcast_subscribe }}",
|
||||
podcastEpisodes: "{{ t.player_podcast_episodes }}",
|
||||
podcastUnplayed: "{{ t.player_podcast_unplayed }}",
|
||||
podcastSeasons: "{{ t.player_podcast_seasons }}",
|
||||
@@ -3677,6 +3697,14 @@ document.addEventListener('alpine:init', () => {
|
||||
refreshing: false,
|
||||
refreshMessage: '',
|
||||
actionError: '',
|
||||
share: null,
|
||||
sharing: false,
|
||||
shareError: '',
|
||||
shareDialog: null,
|
||||
found: null,
|
||||
_foundToken: 0,
|
||||
_foundAbort: null,
|
||||
_foundPreviewSeq: 0,
|
||||
busy: {},
|
||||
expanded: {},
|
||||
_searchTimer: null,
|
||||
@@ -3761,9 +3789,12 @@ document.addEventListener('alpine:init', () => {
|
||||
this.sort = 'newest';
|
||||
this.query = '';
|
||||
this.expanded = {};
|
||||
this.share = null;
|
||||
this.shareDialog = null;
|
||||
}
|
||||
this.refreshMessage = '';
|
||||
this.actionError = '';
|
||||
this.shareError = '';
|
||||
this.detailError = '';
|
||||
this._enterView('podcast_detail', '#podcast?id=' + encodeURIComponent(podcastId), options);
|
||||
this.loadDetail(podcastId);
|
||||
@@ -3777,6 +3808,7 @@ document.addEventListener('alpine:init', () => {
|
||||
const data = await this._json(`/api/podcasts/feeds/${podcastId}`);
|
||||
if (token !== this._detailToken) return;
|
||||
this.current = data.podcast;
|
||||
this.share = data.share || null;
|
||||
this.seasons = Array.isArray(data.seasons) ? data.seasons : [];
|
||||
this.history = Array.isArray(data.history) ? data.history : [];
|
||||
this.detailError = '';
|
||||
@@ -3849,6 +3881,8 @@ document.addEventListener('alpine:init', () => {
|
||||
try {
|
||||
this.current = await this._json(`/api/podcasts/feeds/${podcast.podcast_id}/unsubscribe`, { method: 'POST' });
|
||||
this.loadList();
|
||||
// Unsubscribing also revokes sharing.
|
||||
this.loadDetail(podcast.podcast_id);
|
||||
} catch (error) {
|
||||
this.actionError = error.message;
|
||||
}
|
||||
@@ -3861,11 +3895,309 @@ document.addEventListener('alpine:init', () => {
|
||||
try {
|
||||
this.current = await this._json(`/api/podcasts/feeds/${podcast.podcast_id}/subscribe`, { method: 'POST' });
|
||||
this.loadList();
|
||||
// A new subscription is private until shared again.
|
||||
this.loadDetail(podcast.podcast_id);
|
||||
} catch (error) {
|
||||
this.actionError = error.message;
|
||||
}
|
||||
},
|
||||
|
||||
// Sharing in the federation: the account's synced decision; the
|
||||
// server's node publishes the card. A feed URL that looks private
|
||||
// (token in the query, member-feed host...) is published only after
|
||||
// the user confirms exactly the URLs shown in the dialog.
|
||||
toggleShare() {
|
||||
const share = this.share;
|
||||
if (!this.current || !share || this.sharing) return;
|
||||
if (share.shared) return this._setShare(false);
|
||||
if (Array.isArray(share.warnings) && share.warnings.length) return this.reviewShare();
|
||||
return this._setShare(true);
|
||||
},
|
||||
|
||||
reviewShare(share = this.share) {
|
||||
const podcast = this.current;
|
||||
if (!podcast || !share || !share.feed_url) return;
|
||||
this.shareDialog = {
|
||||
podcastId: podcast.podcast_id,
|
||||
title: podcast.title,
|
||||
warnings: Array.isArray(share.warnings) ? share.warnings : [],
|
||||
feed_url: share.feed_url,
|
||||
id_feed_url: share.id_feed_url || null,
|
||||
};
|
||||
},
|
||||
|
||||
cancelShare() {
|
||||
this.shareDialog = null;
|
||||
},
|
||||
|
||||
confirmShare() {
|
||||
const dialog = this.shareDialog;
|
||||
this.shareDialog = null;
|
||||
if (!dialog || this.current?.podcast_id !== dialog.podcastId) return;
|
||||
return this._setShare(true, { feed_url: dialog.feed_url, id_feed_url: dialog.id_feed_url });
|
||||
},
|
||||
|
||||
async _setShare(shared, confirm = null) {
|
||||
const podcastId = this.current?.podcast_id;
|
||||
if (!podcastId || this.sharing) return;
|
||||
this.sharing = true;
|
||||
this.shareError = '';
|
||||
try {
|
||||
const body = { shared };
|
||||
if (confirm) body.confirm = confirm;
|
||||
const res = await fetch(`/api/podcasts/feeds/${podcastId}/share`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
let data = null;
|
||||
try { data = await res.json(); } catch {}
|
||||
if (this.current?.podcast_id !== podcastId) return;
|
||||
if (res.status === 409 && data?.needs_confirmation && data.share) {
|
||||
// The URLs changed (or were never confirmed): ask about
|
||||
// what the server would publish now.
|
||||
this.share = data.share;
|
||||
this.reviewShare(data.share);
|
||||
} else if (!res.ok) {
|
||||
throw new Error(data?.error || T.podcastRequestFailed);
|
||||
} else {
|
||||
if (data?.podcast) this.current = data.podcast;
|
||||
this.share = data?.share || this.share;
|
||||
this.loadList();
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.current?.podcast_id === podcastId) this.shareError = error.message;
|
||||
} finally {
|
||||
this.sharing = false;
|
||||
}
|
||||
},
|
||||
|
||||
shareStatus() {
|
||||
const share = this.share;
|
||||
if (!share) return '';
|
||||
if (!share.shared) return share.card_error ? T.podcastShareUnavailable : T.podcastSharePrivate;
|
||||
if (share.pending_confirmation) return T.podcastSharePending;
|
||||
if (share.discovery === 'unavailable') return T.podcastShareDiscoveryUnavailable;
|
||||
if (share.discovery !== 'running') return T.podcastShareDiscoveryOff;
|
||||
if (share.published) return T.podcastSharePublished;
|
||||
// Not published by this server after its last round, and why.
|
||||
if (share.held === 'over_limit') return T.podcastShareOverLimit;
|
||||
if (share.held === 'failed') return T.podcastShareFailed;
|
||||
return T.podcastSharePublishing;
|
||||
},
|
||||
|
||||
warningText(warning) {
|
||||
const name = warning?.name || '';
|
||||
switch (warning?.reason) {
|
||||
case 'secret_query_parameter': return T.podcastWarningSecretQuery.replace('{name}', name);
|
||||
case 'private_feed_host': return T.podcastWarningPrivateHost.replace('{host}', warning.host || '');
|
||||
case 'high_entropy_query_value': return T.podcastWarningEntropyQuery.replace('{name}', name);
|
||||
case 'high_entropy_path_segment': return T.podcastWarningEntropyPath;
|
||||
default: return T.podcastWarningOther;
|
||||
}
|
||||
},
|
||||
|
||||
// Public podcasts found by the federated search. A card is another
|
||||
// member's claim: only expected fields of expected types are kept,
|
||||
// URLs must be absolute http(s) (artwork: https only), and every
|
||||
// text is rendered with x-text.
|
||||
safeHttpUrl(value) {
|
||||
if (typeof value !== 'string' || !value || value.length > 2048) return '';
|
||||
let url;
|
||||
try { url = new URL(value); } catch { return ''; }
|
||||
if (url.protocol !== 'http:' && url.protocol !== 'https:') return '';
|
||||
if (url.username || url.password || !url.hostname) return '';
|
||||
return value;
|
||||
},
|
||||
|
||||
safeImageUrl(value) {
|
||||
const url = this.safeHttpUrl(value);
|
||||
return url && url.startsWith('https:') ? url : '';
|
||||
},
|
||||
|
||||
foundHit(entity) {
|
||||
if (!entity || typeof entity !== 'object' || entity.type !== 'podcast') return null;
|
||||
const text = (value, max) => (typeof value === 'string' ? value.slice(0, max) : '');
|
||||
const count = value => (Number.isInteger(value) && value >= 0 ? value : null);
|
||||
const podcastId = text(entity.podcast_id, 200);
|
||||
const feedUrl = this.safeHttpUrl(entity.feed_url);
|
||||
const title = text(entity.title, 300).trim();
|
||||
if (!/^pod1:[a-z2-7]{52}$/.test(podcastId) || !feedUrl || !title) return null;
|
||||
const localId = text(entity.local_podcast_id, 200);
|
||||
return {
|
||||
type: 'podcast',
|
||||
podcast_id: podcastId,
|
||||
feed_url: feedUrl,
|
||||
title,
|
||||
author: text(entity.author, 300),
|
||||
description: text(entity.description, 1000),
|
||||
image_url: this.safeImageUrl(entity.image_url),
|
||||
link: this.safeHttpUrl(entity.link),
|
||||
language: text(entity.language, 35),
|
||||
categories: Array.isArray(entity.categories)
|
||||
? entity.categories.filter(item => typeof item === 'string').slice(0, 8).map(item => item.slice(0, 64))
|
||||
: [],
|
||||
episode_count: count(entity.episode_count),
|
||||
latest_episode_at_ms: count(entity.latest_episode_at_ms),
|
||||
publisher_count: count(entity.publisher_count) || 0,
|
||||
rank: count(entity.rank) ?? Number.MAX_SAFE_INTEGER,
|
||||
exact_match: entity.exact_match === true,
|
||||
subscribed: entity.subscribed === true,
|
||||
local_podcast_id: /^pod1:[a-z2-7]{52}$/.test(localId) ? localId : null,
|
||||
};
|
||||
},
|
||||
|
||||
foundMeta(hit) {
|
||||
if (!hit) return '';
|
||||
const parts = [];
|
||||
if (hit.episode_count != null) parts.push(`${hit.episode_count} ${T.podcastEpisodes}`);
|
||||
if (hit.latest_episode_at_ms) parts.push(`${T.podcastFoundLatest}: ${this.formatDate(hit.latest_episode_at_ms)}`);
|
||||
if (hit.language) parts.push(hit.language);
|
||||
if (hit.categories?.length) parts.push(hit.categories.join(', '));
|
||||
return parts.join(' · ');
|
||||
},
|
||||
|
||||
foundSubtitle(hit) {
|
||||
if (!hit) return '';
|
||||
return [hit.author, hit.episode_count != null ? `${hit.episode_count} ${T.podcastEpisodes}` : '']
|
||||
.filter(Boolean)
|
||||
.join(' · ');
|
||||
},
|
||||
|
||||
foundMore(preview) {
|
||||
const listed = Number.isInteger(preview?.listed_count) ? preview.listed_count : Number(preview?.episode_count || 0);
|
||||
const more = listed - (preview?.episodes?.length || 0);
|
||||
return more > 0 ? T.podcastFoundMore.replace('{count}', String(more)) : '';
|
||||
},
|
||||
|
||||
openFound(hit) {
|
||||
if (GUEST || !hit?.podcast_id || !hit.feed_url) return;
|
||||
this._abortFoundPreview();
|
||||
this._foundToken += 1;
|
||||
this.found = {
|
||||
hit,
|
||||
season: 'all',
|
||||
preview: null,
|
||||
previewLoading: false,
|
||||
previewError: '',
|
||||
subscribing: false,
|
||||
error: '',
|
||||
message: '',
|
||||
subscribed: hit.subscribed === true,
|
||||
localPodcastId: hit.local_podcast_id || null,
|
||||
};
|
||||
this.loadFoundPreview();
|
||||
},
|
||||
|
||||
closeFound() {
|
||||
// An abandoned preview is cancelled: the server stops reading
|
||||
// the feed and frees the account's feed-request slot.
|
||||
this._abortFoundPreview();
|
||||
this._foundToken += 1;
|
||||
this.found = null;
|
||||
},
|
||||
|
||||
_abortFoundPreview() {
|
||||
if (this._foundAbort) {
|
||||
try { this._foundAbort.abort(); } catch {}
|
||||
}
|
||||
this._foundAbort = null;
|
||||
},
|
||||
|
||||
_foundBody(hit, extra = {}) {
|
||||
// The card's id and origin URL, exactly as found: the server
|
||||
// checks that the id comes from this URL.
|
||||
return {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ podcast_id: hit.podcast_id, feed_url: hit.feed_url, ...extra }),
|
||||
};
|
||||
},
|
||||
|
||||
selectFoundSeason(value) {
|
||||
const found = this.found;
|
||||
if (!found || typeof value !== 'string') return;
|
||||
// The same chip again shows every season.
|
||||
const season = found.season === value ? 'all' : value;
|
||||
this.found = { ...found, season };
|
||||
this.loadFoundPreview();
|
||||
},
|
||||
|
||||
async loadFoundPreview() {
|
||||
const found = this.found;
|
||||
if (!found) return;
|
||||
// A newer request (another season, Try again) replaces the
|
||||
// running one.
|
||||
this._abortFoundPreview();
|
||||
const controller = typeof AbortController === 'function' ? new AbortController() : null;
|
||||
this._foundAbort = controller;
|
||||
this._foundPreviewSeq += 1;
|
||||
const seq = this._foundPreviewSeq;
|
||||
const token = this._foundToken;
|
||||
const current = () => token === this._foundToken && seq === this._foundPreviewSeq;
|
||||
this.found = { ...found, previewLoading: true, previewError: '' };
|
||||
const season = found.season && found.season !== 'all' ? { season: found.season } : {};
|
||||
try {
|
||||
const preview = await this._json('/api/podcasts/discovery/preview', {
|
||||
...this._foundBody(found.hit, season),
|
||||
...(controller ? { signal: controller.signal } : {}),
|
||||
});
|
||||
if (!current()) return;
|
||||
this._foundAbort = null;
|
||||
this.found = {
|
||||
...this.found,
|
||||
preview: {
|
||||
...preview,
|
||||
image_url: this.safeImageUrl(preview?.image_url),
|
||||
link: this.safeHttpUrl(preview?.link),
|
||||
seasons: Array.isArray(preview?.seasons) ? preview.seasons : [],
|
||||
episodes: Array.isArray(preview?.episodes) ? preview.episodes : [],
|
||||
},
|
||||
previewLoading: false,
|
||||
subscribed: this.found.subscribed || preview?.subscribed === true,
|
||||
localPodcastId: this.found.localPodcastId || preview?.local_podcast_id || null,
|
||||
};
|
||||
} catch (error) {
|
||||
if (!current()) return;
|
||||
this._foundAbort = null;
|
||||
this.found = { ...this.found, previewLoading: false, previewError: error.message };
|
||||
}
|
||||
},
|
||||
|
||||
async subscribeFound() {
|
||||
const found = this.found;
|
||||
if (!found || found.subscribing || found.subscribed) return;
|
||||
const token = this._foundToken;
|
||||
this.found = { ...found, subscribing: true, error: '', message: '' };
|
||||
try {
|
||||
const data = await this._json('/api/podcasts/discovery/subscribe', this._foundBody(found.hit));
|
||||
const podcastId = data?.podcast?.podcast_id || found.hit.podcast_id;
|
||||
Alpine.store('library')?.markFoundPodcastSubscribed?.(found.hit.podcast_id, podcastId);
|
||||
this.loadList();
|
||||
if (token !== this._foundToken) return;
|
||||
this.found = {
|
||||
...this.found,
|
||||
subscribing: false,
|
||||
subscribed: true,
|
||||
localPodcastId: podcastId,
|
||||
message: data?.already_subscribed ? T.podcastAlready : T.podcastFoundDone,
|
||||
};
|
||||
} catch (error) {
|
||||
if (token !== this._foundToken) return;
|
||||
this.found = { ...this.found, subscribing: false, error: error.message };
|
||||
}
|
||||
},
|
||||
|
||||
foundButtonLabel() {
|
||||
return this.found?.subscribing ? T.podcastFoundSubscribing : T.podcastSubscribe;
|
||||
},
|
||||
|
||||
openFoundPodcast() {
|
||||
const podcastId = this.found?.localPodcastId || this.found?.hit?.podcast_id;
|
||||
this.closeFound();
|
||||
if (podcastId) this.open(podcastId);
|
||||
},
|
||||
|
||||
jamBlocked() {
|
||||
// A Jam guest controls the host's queue: episodes are private.
|
||||
return !!Alpine.store('devices')?.isControllingRemoteJam?.();
|
||||
@@ -4059,7 +4391,7 @@ document.addEventListener('alpine:init', () => {
|
||||
similaritySearchLabel: '',
|
||||
similaritySearchError: '',
|
||||
similaritySearchStats: { loading: false, tracks: 0, artists: 0, peers: 0, elapsed_ms: 0 },
|
||||
federationSearch: { loading: false, error: '', artists: [], releases: [], tracks: [] },
|
||||
federationSearch: { loading: false, error: '', artists: [], releases: [], tracks: [], podcasts: [], podcastDiscovery: '' },
|
||||
artistFederation: { loading: false, error: '', releases: [], tracks: [] },
|
||||
federationPreparing: {},
|
||||
federationFailures: {},
|
||||
@@ -4999,7 +5331,7 @@ document.addEventListener('alpine:init', () => {
|
||||
this.similaritySearchError = '';
|
||||
this.searchLoading = true;
|
||||
this.searchResults = null;
|
||||
this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [] };
|
||||
this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [], podcasts: [], podcastDiscovery: '' };
|
||||
this.similaritySearchStats = { loading: true, tracks: 0, artists: 0, peers: 0, elapsed_ms: 0 };
|
||||
Alpine.store('info').close();
|
||||
try {
|
||||
@@ -5141,12 +5473,12 @@ document.addEventListener('alpine:init', () => {
|
||||
this._federationSearchSource.close();
|
||||
this._federationSearchSource = null;
|
||||
}
|
||||
this.federationSearch = { loading: false, error: '', artists: [], releases: [], tracks: [] };
|
||||
this.federationSearch = { loading: false, error: '', artists: [], releases: [], tracks: [], podcasts: [], podcastDiscovery: '' };
|
||||
},
|
||||
|
||||
startFederationSearch(query) {
|
||||
this.stopFederationSearch();
|
||||
this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [] };
|
||||
this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [], podcasts: [], podcastDiscovery: '' };
|
||||
const source = new EventSource(`/api/player/federation/search/events?q=${encodeURIComponent(query)}`);
|
||||
this._federationSearchSource = source;
|
||||
const updateResults = (kind, keyFor, labelFor, entity) => {
|
||||
@@ -5199,10 +5531,31 @@ document.addEventListener('alpine:init', () => {
|
||||
if (!release?.key) return;
|
||||
updateResults('releases', item => JSON.stringify(item.key || {}), item => item.title, release);
|
||||
});
|
||||
source.addEventListener('search.completed', () => {
|
||||
source.addEventListener('federation.podcast', event => {
|
||||
if (this._federationSearchSource !== source) return;
|
||||
let payload = null;
|
||||
try { payload = JSON.parse(event.data); } catch { return; }
|
||||
// Public podcast cards: typed, sanitized, ordered by their
|
||||
// place among the music results.
|
||||
const hit = Alpine.store('podcasts')?.foundHit?.(payload?.entity);
|
||||
if (!hit) return;
|
||||
const podcasts = (this.federationSearch.podcasts || [])
|
||||
.filter(item => item.podcast_id !== hit.podcast_id);
|
||||
podcasts.push(hit);
|
||||
podcasts.sort((left, right) => left.rank - right.rank);
|
||||
this.federationSearch = { ...this.federationSearch, podcasts };
|
||||
});
|
||||
source.addEventListener('search.completed', event => {
|
||||
window.clearTimeout(watchdog);
|
||||
// `running`, `off` or `failed`: a failed podcast search must
|
||||
// not look like "no podcasts found".
|
||||
let podcastDiscovery = '';
|
||||
try {
|
||||
const state = JSON.parse(event?.data)?.entity?.podcast_discovery;
|
||||
if (['running', 'off', 'failed'].includes(state)) podcastDiscovery = state;
|
||||
} catch {}
|
||||
if (this._federationSearchSource === source) {
|
||||
this.federationSearch = { ...this.federationSearch, loading: false };
|
||||
this.federationSearch = { ...this.federationSearch, loading: false, podcastDiscovery };
|
||||
}
|
||||
source.close();
|
||||
if (this._federationSearchSource === source) this._federationSearchSource = null;
|
||||
@@ -5232,6 +5585,15 @@ document.addEventListener('alpine:init', () => {
|
||||
};
|
||||
},
|
||||
|
||||
markFoundPodcastSubscribed(podcastId, localPodcastId) {
|
||||
const podcasts = (this.federationSearch?.podcasts || []).map(item => (
|
||||
item.podcast_id === podcastId
|
||||
? { ...item, subscribed: true, local_podcast_id: localPodcastId || item.local_podcast_id }
|
||||
: item
|
||||
));
|
||||
this.federationSearch = { ...this.federationSearch, podcasts };
|
||||
},
|
||||
|
||||
normalizeFederationSearchText(value) {
|
||||
return String(value || '')
|
||||
.normalize('NFKD')
|
||||
|
||||
@@ -625,6 +625,41 @@
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
<div class="search-podcasts-section"
|
||||
x-show="!$store.library.similaritySearchLabel && ($store.library.federationSearch.podcasts || []).length > 0"
|
||||
x-cloak>
|
||||
<h3 class="search-subsection-title">{{ t.player_podcast_found_title }}</h3>
|
||||
<div class="search-releases-row">
|
||||
<template x-for="hit in ($store.library.federationSearch.podcasts || [])"
|
||||
:key="hit.podcast_id">
|
||||
<div class="search-release-card federation-entity-card podcast-found-card"
|
||||
role="button" tabindex="0"
|
||||
@click="$store.podcasts.openFound(hit)"
|
||||
@keydown.enter="$store.podcasts.openFound(hit)">
|
||||
<div class="search-release-cover">
|
||||
<template x-if="hit.image_url">
|
||||
<img class="artwork-image" :src="hit.image_url" alt="" aria-hidden="true"
|
||||
loading="lazy" referrerpolicy="no-referrer"
|
||||
@load="$event.currentTarget.classList.add('artwork-loaded')"
|
||||
@error="$event.currentTarget.classList.remove('artwork-loaded')">
|
||||
</template>
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
|
||||
<rect x="9" y="2" width="6" height="12" rx="3"/><path d="M5 10a7 7 0 0014 0M12 17v5M8 22h8"/>
|
||||
</svg>
|
||||
</div>
|
||||
<div class="podcast-found-badges">
|
||||
<span class="podcast-type-badge">{{ t.player_podcast_type }}</span>
|
||||
<span class="podcast-subscribed-badge" x-show="hit.subscribed">{{ t.player_podcast_found_subscribed }}</span>
|
||||
</div>
|
||||
<div class="card-title" x-text="hit.title"></div>
|
||||
<div class="card-subtitle" x-text="$store.podcasts.foundSubtitle(hit)"></div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
<div class="federation-search-status podcast-search-failed"
|
||||
x-show="!$store.library.similaritySearchLabel && $store.library.federationSearch.podcastDiscovery === 'failed'"
|
||||
x-cloak>{{ t.player_podcast_found_failed }}</div>
|
||||
<template x-for="(track, idx) in $store.library.federationSearch.tracks"
|
||||
:key="track.key.content_id">
|
||||
<div class="track-row federation-track-row"
|
||||
@@ -762,6 +797,7 @@
|
||||
<div class="card-subtitle" x-text="podcast.author || ''"></div>
|
||||
<div class="card-subtitle" x-text="$store.podcasts.podcastCounts(podcast)"></div>
|
||||
<div class="card-subtitle podcast-unsubscribed-label" x-show="!podcast.subscribed" x-cloak>{{ t.player_podcast_unsubscribed }}</div>
|
||||
<div class="card-subtitle podcast-shared-label" x-show="podcast.subscribed && podcast.shared" x-cloak>{{ t.player_podcast_shared_badge }}</div>
|
||||
<div class="card-subtitle podcast-fetch-error"
|
||||
x-show="podcast.fetch && podcast.fetch.last_error"
|
||||
x-cloak
|
||||
@@ -824,6 +860,17 @@
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 12a9 9 0 11-3-6.7L21 8"/><path d="M21 3v5h-5"/></svg>
|
||||
<span x-text="$store.podcasts.refreshing ? '{{ t.player_podcast_refreshing }}' : '{{ t.player_podcast_refresh }}'"></span>
|
||||
</button>
|
||||
<button class="release-action-btn secondary podcast-share-toggle"
|
||||
x-show="$store.podcasts.current.subscribed && $store.podcasts.share"
|
||||
x-cloak
|
||||
:class="{ active: $store.podcasts.share && $store.podcasts.share.shared }"
|
||||
:aria-pressed="$store.podcasts.share && $store.podcasts.share.shared ? 'true' : 'false'"
|
||||
:disabled="$store.podcasts.sharing || !$store.podcasts.share || (!$store.podcasts.share.shared && !$store.podcasts.share.can_share)"
|
||||
:title="$store.podcasts.share && $store.podcasts.share.card_error ? '{{ t.player_podcast_share_unavailable }}' : ''"
|
||||
@click="$store.podcasts.toggleShare()">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.6" y1="13.5" x2="15.4" y2="17.5"/><line x1="15.4" y1="6.5" x2="8.6" y2="10.5"/></svg>
|
||||
<span x-text="$store.podcasts.sharing ? '{{ t.player_podcast_sharing }}' : ($store.podcasts.share && $store.podcasts.share.shared ? '{{ t.player_podcast_unshare }}' : '{{ t.player_podcast_share }}')"></span>
|
||||
</button>
|
||||
<button class="release-action-btn secondary podcast-unsubscribe"
|
||||
x-show="$store.podcasts.current.subscribed"
|
||||
@click="$store.podcasts.unsubscribe()">
|
||||
@@ -836,6 +883,18 @@
|
||||
<span>{{ t.player_podcast_subscribe }}</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="podcast-share-status"
|
||||
x-show="$store.podcasts.current.subscribed && $store.podcasts.share"
|
||||
x-cloak
|
||||
:class="{ pending: $store.podcasts.share && $store.podcasts.share.pending_confirmation }">
|
||||
<span x-text="$store.podcasts.shareStatus()"></span>
|
||||
<button class="podcast-share-review"
|
||||
type="button"
|
||||
x-show="$store.podcasts.share && $store.podcasts.share.pending_confirmation"
|
||||
:disabled="$store.podcasts.sharing"
|
||||
@click="$store.podcasts.reviewShare()">{{ t.player_podcast_share_review }}</button>
|
||||
</div>
|
||||
<div class="podcast-message error" x-show="$store.podcasts.shareError" x-cloak x-text="$store.podcasts.shareError"></div>
|
||||
<div class="podcast-message" x-show="$store.podcasts.refreshMessage" x-cloak x-text="$store.podcasts.refreshMessage"></div>
|
||||
<div class="podcast-message error" x-show="$store.podcasts.actionError" x-cloak x-text="$store.podcasts.actionError"></div>
|
||||
</div>
|
||||
|
||||
@@ -7097,6 +7097,173 @@ button.user-stat:hover {
|
||||
text-align: center;
|
||||
}
|
||||
.podcast-unsubscribed-label { color: var(--text-subdued); }
|
||||
.podcast-shared-label { color: var(--accent); }
|
||||
.podcast-share-toggle.active {
|
||||
border-color: var(--accent);
|
||||
color: var(--accent);
|
||||
}
|
||||
.podcast-share-status {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
margin: 6px 0 4px;
|
||||
max-width: 720px;
|
||||
line-height: 1.45;
|
||||
}
|
||||
.podcast-share-status.pending { color: #f0b84d; }
|
||||
.podcast-share-review {
|
||||
margin-left: 8px;
|
||||
background: transparent;
|
||||
border: 1px solid currentColor;
|
||||
border-radius: 12px;
|
||||
color: inherit;
|
||||
cursor: pointer;
|
||||
font: inherit;
|
||||
padding: 1px 8px;
|
||||
}
|
||||
.podcast-share-dialog { max-width: 520px; }
|
||||
.search-podcasts-section { margin: 4px 0 18px; }
|
||||
.search-subsection-title {
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.02em;
|
||||
margin: 0 0 8px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
.podcast-found-badges {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 4px;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
.podcast-type-badge,
|
||||
.podcast-subscribed-badge {
|
||||
border-radius: 8px;
|
||||
font-size: 10px;
|
||||
font-weight: 600;
|
||||
line-height: 1.6;
|
||||
padding: 0 6px;
|
||||
}
|
||||
.podcast-type-badge { background: var(--bg-active); color: var(--text-secondary); }
|
||||
.podcast-subscribed-badge { background: var(--accent); color: #000; }
|
||||
.podcast-found-dialog {
|
||||
max-width: 600px;
|
||||
max-height: calc(100vh - 48px);
|
||||
overflow-y: auto;
|
||||
}
|
||||
.podcast-found-head {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: flex-start;
|
||||
}
|
||||
.podcast-found-cover {
|
||||
position: relative;
|
||||
flex: 0 0 96px;
|
||||
width: 96px;
|
||||
height: 96px;
|
||||
border-radius: 8px;
|
||||
background: var(--bg-elevated);
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
.podcast-found-cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
|
||||
.podcast-found-cover svg { width: 36px; height: 36px; color: var(--text-subdued); }
|
||||
.podcast-found-heading { min-width: 0; }
|
||||
.podcast-found-heading h3 { margin: 2px 0 4px; overflow-wrap: anywhere; }
|
||||
.podcast-found-author,
|
||||
.podcast-found-meta {
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.podcast-found-link {
|
||||
color: var(--accent);
|
||||
display: block;
|
||||
font-size: 12px;
|
||||
margin-top: 4px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.podcast-found-description {
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
margin: 12px 0;
|
||||
white-space: pre-line;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.podcast-found-label {
|
||||
color: var(--text-subdued);
|
||||
font-size: 12px;
|
||||
margin-top: 8px;
|
||||
}
|
||||
.podcast-found-dialog .podcast-share-hint {
|
||||
color: var(--text-subdued);
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
.podcast-found-preview { margin: 8px 0; }
|
||||
.podcast-found-seasons { margin: 8px 0; }
|
||||
.podcast-found-status {
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
margin: 6px 0;
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
align-items: center;
|
||||
}
|
||||
.podcast-found-status.error { color: #ff6b6b; }
|
||||
.podcast-found-episodes {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
max-height: 240px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.podcast-found-episodes li {
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
padding: 6px 2px;
|
||||
}
|
||||
.podcast-found-episode-title {
|
||||
font-size: 13px;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.podcast-found-episode-meta {
|
||||
color: var(--text-subdued);
|
||||
font-size: 12px;
|
||||
}
|
||||
@media (max-width: 600px) {
|
||||
.podcast-found-head { flex-direction: column; }
|
||||
}
|
||||
.podcast-share-dialog p {
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
margin: 8px 0;
|
||||
}
|
||||
.podcast-share-dialog .podcast-share-dialog-title {
|
||||
color: var(--text-primary);
|
||||
font-weight: 600;
|
||||
}
|
||||
.podcast-share-warnings {
|
||||
color: #f0b84d;
|
||||
font-size: 13px;
|
||||
margin: 4px 0 8px 18px;
|
||||
}
|
||||
.podcast-share-url {
|
||||
background: var(--bg-elevated);
|
||||
border-radius: 6px;
|
||||
color: var(--text-primary);
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
||||
font-size: 12px;
|
||||
margin: 4px 0;
|
||||
padding: 6px 8px;
|
||||
word-break: break-all;
|
||||
}
|
||||
.podcast-share-dialog .podcast-share-hint { color: var(--text-subdued); }
|
||||
.podcast-header-info { min-width: 0; }
|
||||
.podcast-description {
|
||||
color: var(--text-secondary);
|
||||
|
||||
@@ -786,7 +786,7 @@ test('a mirrored Jam state never plays or saves an episode', async () => {
|
||||
// Podcasts store
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function podcastsStore(stores = {}) {
|
||||
function podcastsStore(stores = {}, overrides = {}) {
|
||||
const start = template.indexOf(" Alpine.store('podcasts', {");
|
||||
assert.ok(start >= 0);
|
||||
const open = template.indexOf('{', start);
|
||||
@@ -799,10 +799,15 @@ function podcastsStore(stores = {}) {
|
||||
podcastFetchError: 'Last check failed', podcastLastChecked: 'Checked', podcastNeverChecked: 'Not checked yet',
|
||||
podcastJamBlocked: 'jam blocked', podcastRequestFailed: 'request failed',
|
||||
},
|
||||
URLSearchParams, GUEST: false,
|
||||
fetch: async (url, init = {}) => { calls.push({url, init}); return {ok: true, json: async () => ({})}; },
|
||||
URLSearchParams, URL, AbortController, GUEST: false,
|
||||
fetch: async (url, init = {}) => {
|
||||
calls.push({url, init});
|
||||
if (overrides.respond) return overrides.respond(url, init);
|
||||
return {ok: true, json: async () => ({})};
|
||||
},
|
||||
Alpine: {store: name => stores[name]},
|
||||
});
|
||||
Object.assign(context.T, overrides.T || {});
|
||||
vm.runInContext('this.store = ' + template.slice(open, end + 6) + ';', context);
|
||||
return {store: context.store, calls};
|
||||
}
|
||||
@@ -839,6 +844,97 @@ test('the podcasts store formats episodes and builds list queries', () => {
|
||||
assert.equal(store._episodeQuery(0), 'limit=50&offset=0');
|
||||
});
|
||||
|
||||
test('sharing a feed URL that looks private asks first and sends back exactly the shown URLs', async () => {
|
||||
const secret = 'https://members.example/feed.xml?token=8f3a9c2e7d1b4a6f';
|
||||
const warnings = [{reason: 'secret_query_parameter', name: 'token'}];
|
||||
const privateShare = {
|
||||
shared: false, pending_confirmation: false, can_share: true, card_error: null, warnings,
|
||||
feed_url: secret, id_feed_url: null, discovery: 'running', published: false,
|
||||
};
|
||||
const answers = [];
|
||||
const {store, calls} = podcastsStore({}, {
|
||||
T: {
|
||||
podcastSharePrivate: 'private', podcastSharePublished: 'published', podcastSharePublishing: 'publishing',
|
||||
podcastShareDiscoveryOff: 'discovery off', podcastSharePending: 'pending', podcastShareUnavailable: 'unavailable',
|
||||
podcastShareDiscoveryUnavailable: 'discovery unavailable', podcastShareOverLimit: 'over limit',
|
||||
podcastShareFailed: 'publication failed',
|
||||
podcastWarningSecretQuery: 'parameter «{name}»', podcastWarningPrivateHost: 'host {host}',
|
||||
podcastWarningEntropyQuery: 'value «{name}»', podcastWarningEntropyPath: 'path', podcastWarningOther: 'other',
|
||||
},
|
||||
respond: async () => answers.shift(),
|
||||
});
|
||||
store.current = {podcast_id: 'pod1:x', title: 'Members Only', subscribed: true};
|
||||
store.share = privateShare;
|
||||
store.loadList = () => {};
|
||||
assert.equal(store.shareStatus(), 'private');
|
||||
assert.equal(store.warningText(warnings[0]), 'parameter «token»');
|
||||
assert.equal(store.warningText({reason: 'private_feed_host', host: 'patreon.com'}), 'host patreon.com');
|
||||
assert.equal(store.warningText({reason: 'something_new'}), 'other');
|
||||
|
||||
// The toggle opens the dialog instead of sharing.
|
||||
store.toggleShare();
|
||||
assert.equal(calls.length, 0);
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(store.shareDialog)), {
|
||||
podcastId: 'pod1:x', title: 'Members Only', warnings, feed_url: secret, id_feed_url: null,
|
||||
});
|
||||
store.cancelShare();
|
||||
assert.equal(store.shareDialog, null);
|
||||
assert.equal(calls.length, 0);
|
||||
|
||||
// The feed moved meanwhile: the server asks about the new URL.
|
||||
const moved = Object.assign({}, privateShare, {feed_url: secret + '&v=2', id_feed_url: secret});
|
||||
answers.push({ok: false, status: 409, json: async () => ({error: 'confirm', needs_confirmation: true, share: moved})});
|
||||
store.toggleShare();
|
||||
await store.confirmShare();
|
||||
assert.equal(calls.length, 1);
|
||||
assert.equal(calls[0].url, '/api/podcasts/feeds/pod1:x/share');
|
||||
assert.equal(calls[0].init.headers['Content-Type'], 'application/json');
|
||||
assert.deepEqual(JSON.parse(calls[0].init.body), {shared: true, confirm: {feed_url: secret, id_feed_url: null}});
|
||||
assert.equal(store.shareDialog.feed_url, secret + '&v=2');
|
||||
assert.equal(store.shareDialog.id_feed_url, secret);
|
||||
assert.equal(store.shareError, '');
|
||||
|
||||
const published = Object.assign({}, moved, {shared: true, published: true});
|
||||
answers.push({ok: true, status: 200, json: async () => ({podcast: {podcast_id: 'pod1:x', title: 'Members Only', subscribed: true, shared: true}, share: published})});
|
||||
await store.confirmShare();
|
||||
assert.deepEqual(JSON.parse(calls[1].init.body), {
|
||||
shared: true, confirm: {feed_url: secret + '&v=2', id_feed_url: secret},
|
||||
});
|
||||
assert.equal(store.shareDialog, null);
|
||||
assert.equal(store.shareStatus(), 'published');
|
||||
assert.equal(store.sharing, false);
|
||||
|
||||
// Turning it off needs no confirmation.
|
||||
answers.push({ok: true, status: 200, json: async () => ({podcast: store.current, share: privateShare})});
|
||||
await store.toggleShare();
|
||||
assert.deepEqual(JSON.parse(calls[2].init.body), {shared: false});
|
||||
assert.equal(store.shareStatus(), 'private');
|
||||
|
||||
// States of the status line.
|
||||
store.share = Object.assign({}, privateShare, {shared: true, pending_confirmation: true});
|
||||
assert.equal(store.shareStatus(), 'pending');
|
||||
store.share = Object.assign({}, privateShare, {shared: true, discovery: 'off'});
|
||||
assert.equal(store.shareStatus(), 'discovery off');
|
||||
store.share = Object.assign({}, privateShare, {shared: true, discovery: 'unavailable'});
|
||||
assert.equal(store.shareStatus(), 'discovery unavailable');
|
||||
store.share = Object.assign({}, privateShare, {shared: true});
|
||||
assert.equal(store.shareStatus(), 'publishing');
|
||||
store.share = Object.assign({}, privateShare, {shared: true, held: 'over_limit'});
|
||||
assert.equal(store.shareStatus(), 'over limit');
|
||||
store.share = Object.assign({}, privateShare, {shared: true, held: 'failed'});
|
||||
assert.equal(store.shareStatus(), 'publication failed');
|
||||
store.share = Object.assign({}, privateShare, {shared: true, published: true, held: null});
|
||||
assert.equal(store.shareStatus(), 'published');
|
||||
|
||||
// A public feed is shared directly; a failure is shown.
|
||||
store.share = Object.assign({}, privateShare, {warnings: []});
|
||||
answers.push({ok: false, status: 409, json: async () => ({error: 'not subscribed'})});
|
||||
await store.toggleShare();
|
||||
assert.deepEqual(JSON.parse(calls[3].init.body), {shared: true});
|
||||
assert.equal(store.shareError, 'not subscribed');
|
||||
assert.equal(store.shareDialog, null);
|
||||
});
|
||||
|
||||
test('playing an episode from the list puts it next in the queue', () => {
|
||||
const played = [];
|
||||
const queue = {
|
||||
@@ -1014,3 +1110,270 @@ test('Play after a failed source waits for the final report and resumes where th
|
||||
await flush();
|
||||
assert.deepEqual(other.calls.map(call => call.url.split('/').pop()), ['playback', 'progress']);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public podcasts in the federated search
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const POD_A = 'pod1:' + 'a'.repeat(52);
|
||||
const POD_B = 'pod1:' + 'b'.repeat(52);
|
||||
const POD_LOCAL = 'pod1:' + 'c'.repeat(52);
|
||||
|
||||
function foundEntity(extra = {}) {
|
||||
return Object.assign({
|
||||
type: 'podcast', kind: 'podcast', podcast_id: POD_A,
|
||||
feed_url: 'https://feeds.example/show.xml?utm=x', title: 'Night Radio', author: 'Host',
|
||||
description: '<img src=x onerror=alert(1)>', image_url: 'https://cdn.example/art.jpg',
|
||||
link: 'https://show.example/', language: 'en', categories: ['Technology', 7],
|
||||
episode_count: 12, latest_episode_at_ms: 1700000000000, publisher_count: 2,
|
||||
rank: 1, exact_match: true, subscribed: false, local_podcast_id: null,
|
||||
}, extra);
|
||||
}
|
||||
|
||||
const FOUND_T = {
|
||||
podcastFoundLatest: 'Latest', podcastFoundMore: 'and {count} more', podcastFoundDone: 'subscribed, private',
|
||||
podcastAlready: 'already', podcastFoundSubscribing: 'subscribing', podcastSubscribe: 'Subscribe',
|
||||
};
|
||||
|
||||
test('podcast cards from the search keep only typed fields and safe URLs', () => {
|
||||
const {store} = podcastsStore({}, {T: FOUND_T});
|
||||
const hit = store.foundHit(foundEntity());
|
||||
// The feed URL is passed on exactly as found (the server hashes it).
|
||||
assert.equal(hit.feed_url, 'https://feeds.example/show.xml?utm=x');
|
||||
assert.equal(hit.description, '<img src=x onerror=alert(1)>', 'text stays text (x-text)');
|
||||
assert.deepEqual(Array.from(hit.categories), ['Technology']);
|
||||
assert.equal(hit.image_url, 'https://cdn.example/art.jpg');
|
||||
assert.equal(store.foundMeta(hit).startsWith('12 episodes · Latest: '), true);
|
||||
assert.equal(store.foundSubtitle(hit), 'Host · 12 episodes');
|
||||
|
||||
const unsafe = store.foundHit(foundEntity({
|
||||
link: 'javascript:alert(1)', image_url: 'http://cdn.example/art.jpg',
|
||||
local_podcast_id: 'javascript:x', rank: 'first', subscribed: 'yes',
|
||||
}));
|
||||
assert.equal(unsafe.link, '');
|
||||
assert.equal(unsafe.image_url, '', 'artwork only over https');
|
||||
assert.equal(unsafe.local_podcast_id, null);
|
||||
assert.equal(unsafe.rank, Number.MAX_SAFE_INTEGER);
|
||||
assert.equal(unsafe.subscribed, false);
|
||||
assert.equal(store.foundHit(foundEntity({link: 'https://user:pw@show.example/'})).link, '');
|
||||
|
||||
for (const bad of [
|
||||
foundEntity({type: 'music'}), foundEntity({podcast_id: 'pod1:x'}), foundEntity({feed_url: 'javascript:alert(1)'}),
|
||||
foundEntity({feed_url: 'ftp://feeds.example/show.xml'}), foundEntity({title: ' '}), null, 'podcast',
|
||||
]) {
|
||||
assert.equal(store.foundHit(bad), null, JSON.stringify(bad));
|
||||
}
|
||||
});
|
||||
|
||||
test('the federated search shows podcast cards next to music, ordered by rank', () => {
|
||||
const podcasts = podcastsStore({}, {T: FOUND_T}).store;
|
||||
const sources = [];
|
||||
class FakeEventSource {
|
||||
constructor(url) { this.url = url; this.listeners = {}; sources.push(this); }
|
||||
addEventListener(name, listener) { this.listeners[name] = listener; }
|
||||
emit(name, data) { this.listeners[name]({data: JSON.stringify(data)}); }
|
||||
close() { this.closed = true; }
|
||||
}
|
||||
const context = vm.createContext({
|
||||
EventSource: FakeEventSource, encodeURIComponent,
|
||||
window: {setTimeout: () => 1, clearTimeout: () => {}},
|
||||
Alpine: {store: name => (name === 'podcasts' ? podcasts : null)},
|
||||
});
|
||||
vm.runInContext(`this.library = {
|
||||
federationSearch: null, _federationSearchSource: null,
|
||||
federationSearchScore() { return 0; },
|
||||
withTrackAvailability(track) { return track; },
|
||||
withFederationArtistFallback(artist) { return artist; },
|
||||
hydrateFederationSearchRelease(release) { return release; },
|
||||
${method('stopFederationSearch')}
|
||||
${method('startFederationSearch')}
|
||||
${method('markFoundPodcastSubscribed')}
|
||||
};`, context);
|
||||
const library = context.library;
|
||||
library.startFederationSearch('night radio');
|
||||
const source = sources[0];
|
||||
assert.equal(source.url, '/api/player/federation/search/events?q=night%20radio');
|
||||
assert.deepEqual(Array.from(library.federationSearch.podcasts), []);
|
||||
|
||||
// Music events are handled as before.
|
||||
source.emit('federation.track', {type: 'music', kind: 'federation.track', entity: {key: {content_id: 'b3:1'}, metadata: {title: 'Theme'}}});
|
||||
source.emit('federation.podcast', {type: 'podcast', kind: 'federation.podcast', entity: foundEntity({podcast_id: POD_B, title: 'Night Radio Talks', rank: 3})});
|
||||
source.emit('federation.podcast', {type: 'podcast', kind: 'federation.podcast', entity: foundEntity({rank: 1, subscribed: true, local_podcast_id: POD_LOCAL})});
|
||||
// Not a podcast card: ignored.
|
||||
source.emit('federation.podcast', {entity: {type: 'music', podcast_id: POD_A}});
|
||||
// The same podcast again replaces its card.
|
||||
source.emit('federation.podcast', {entity: foundEntity({podcast_id: POD_B, title: 'Night Radio Talks', rank: 3, author: 'New host'})});
|
||||
|
||||
assert.equal(library.federationSearch.tracks.length, 1);
|
||||
const ids = Array.from(library.federationSearch.podcasts, hit => hit.podcast_id);
|
||||
assert.deepEqual(ids, [POD_A, POD_B]);
|
||||
assert.equal(library.federationSearch.podcasts[0].subscribed, true);
|
||||
assert.equal(library.federationSearch.podcasts[1].author, 'New host');
|
||||
|
||||
library.markFoundPodcastSubscribed(POD_B, POD_B);
|
||||
assert.equal(library.federationSearch.podcasts[1].subscribed, true);
|
||||
assert.equal(library.federationSearch.podcasts[1].local_podcast_id, POD_B);
|
||||
|
||||
source.emit('search.completed', {entity: {partial: false, podcast_discovery: 'running'}});
|
||||
assert.equal(library.federationSearch.loading, false);
|
||||
assert.equal(library.federationSearch.podcastDiscovery, 'running');
|
||||
assert.equal(source.closed, true);
|
||||
// Events of a closed search change nothing.
|
||||
library.startFederationSearch('other');
|
||||
source.emit('federation.podcast', {entity: foundEntity()});
|
||||
assert.deepEqual(Array.from(library.federationSearch.podcasts), []);
|
||||
assert.equal(library.federationSearch.podcastDiscovery, '');
|
||||
|
||||
// A failed podcast search is reported, not shown as "nothing found";
|
||||
// the music results stay.
|
||||
const failing = sources[1];
|
||||
failing.emit('federation.track', {type: 'music', kind: 'federation.track', entity: {key: {content_id: 'b3:2'}, metadata: {title: 'Other'}}});
|
||||
failing.emit('search.completed', {entity: {partial: false, podcast_discovery: 'failed'}});
|
||||
assert.equal(library.federationSearch.podcastDiscovery, 'failed');
|
||||
assert.equal(library.federationSearch.tracks.length, 1);
|
||||
assert.equal(library.federationSearch.error, '');
|
||||
// Unknown states and malformed data are ignored.
|
||||
library.startFederationSearch('third');
|
||||
sources[2].emit('search.completed', {entity: {podcast_discovery: '<b>x</b>'}});
|
||||
assert.equal(library.federationSearch.podcastDiscovery, '');
|
||||
library.startFederationSearch('fourth');
|
||||
sources[3].listeners['search.completed']({data: 'not json'});
|
||||
assert.equal(library.federationSearch.loading, false);
|
||||
assert.equal(library.federationSearch.podcastDiscovery, '');
|
||||
});
|
||||
|
||||
test('a found podcast is previewed, then subscribed with the card id and origin URL', async () => {
|
||||
const answers = [];
|
||||
const marked = [];
|
||||
const library = {markFoundPodcastSubscribed: (id, local) => marked.push([id, local])};
|
||||
const {store, calls} = podcastsStore({library}, {T: FOUND_T, respond: async () => answers.shift()});
|
||||
let listLoads = 0;
|
||||
store.loadList = () => { listLoads += 1; };
|
||||
const opened = [];
|
||||
store.open = id => opened.push(id);
|
||||
const hit = store.foundHit(foundEntity());
|
||||
|
||||
answers.push({ok: true, status: 200, json: async () => ({
|
||||
podcast_id: POD_A, feed_url: hit.feed_url, moved: false, title: 'Night Radio',
|
||||
link: 'javascript:alert(1)', image_url: 'https://cdn.example/feed.jpg', episode_count: 60,
|
||||
seasons: [{season: 1, episode_count: 59}, {season: null, episode_count: 1}],
|
||||
episodes: [{title: 'Newest', season: 1, episode_number: 59, duration_ms: 60000}],
|
||||
subscribed: false, local_podcast_id: null,
|
||||
})});
|
||||
store.openFound(hit);
|
||||
assert.equal(store.found.previewLoading, true);
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
assert.equal(calls[0].url, '/api/podcasts/discovery/preview');
|
||||
assert.equal(calls[0].init.method, 'POST');
|
||||
assert.equal(calls[0].init.headers['Content-Type'], 'application/json');
|
||||
assert.deepEqual(JSON.parse(calls[0].init.body), {podcast_id: POD_A, feed_url: 'https://feeds.example/show.xml?utm=x'});
|
||||
assert.equal(store.found.previewLoading, false);
|
||||
assert.equal(store.found.preview.link, '', 'feed links are checked too');
|
||||
assert.equal(store.found.preview.episodes[0].title, 'Newest');
|
||||
assert.equal(store.foundMore(store.found.preview), 'and 59 more');
|
||||
assert.equal(store.foundButtonLabel(), 'Subscribe');
|
||||
|
||||
// Subscribing: the account already knew the feed under another id.
|
||||
answers.push({ok: true, status: 200, json: async () => ({podcast: {podcast_id: POD_LOCAL}, already_subscribed: false, new_episodes: 60})});
|
||||
const pending = store.subscribeFound();
|
||||
assert.equal(store.foundButtonLabel(), 'subscribing');
|
||||
await pending;
|
||||
assert.equal(calls[1].url, '/api/podcasts/discovery/subscribe');
|
||||
assert.deepEqual(JSON.parse(calls[1].init.body), {podcast_id: POD_A, feed_url: 'https://feeds.example/show.xml?utm=x'});
|
||||
assert.deepEqual(marked, [[POD_A, POD_LOCAL]]);
|
||||
assert.equal(listLoads, 1);
|
||||
assert.equal(store.found.subscribed, true);
|
||||
assert.equal(store.found.message, 'subscribed, private');
|
||||
// Subscribed: the button opens the account's podcast; no second request.
|
||||
await store.subscribeFound();
|
||||
assert.equal(calls.length, 2);
|
||||
store.openFoundPodcast();
|
||||
assert.equal(store.found, null);
|
||||
assert.deepEqual(opened, [POD_LOCAL]);
|
||||
|
||||
// An already subscribed card opens directly; a failure is shown.
|
||||
answers.push({ok: true, status: 200, json: async () => ({podcast_id: POD_A, seasons: [], episodes: [], episode_count: 0})});
|
||||
store.openFound(store.foundHit(foundEntity({subscribed: true, local_podcast_id: POD_LOCAL})));
|
||||
assert.equal(store.found.subscribed, true);
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
store.found.subscribed = false;
|
||||
answers.push({ok: false, status: 502, json: async () => ({error: 'the feed could not be loaded: timeout'})});
|
||||
await store.subscribeFound();
|
||||
assert.equal(store.found.error, 'the feed could not be loaded: timeout');
|
||||
assert.equal(store.found.subscribing, false);
|
||||
|
||||
// Closing during the preview drops its late answer.
|
||||
let release;
|
||||
answers.push(new Promise(resolve => { release = resolve; }));
|
||||
store.openFound(hit);
|
||||
store.closeFound();
|
||||
release({ok: true, status: 200, json: async () => ({title: 'late'})});
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
assert.equal(store.found, null);
|
||||
});
|
||||
|
||||
test('the seasons of a found podcast can be browsed, and an abandoned preview is cancelled', async () => {
|
||||
const pending = [];
|
||||
const {store, calls} = podcastsStore({}, {
|
||||
T: Object.assign({}, FOUND_T, {podcastAllSeasons: 'All seasons'}),
|
||||
respond: (url, init) => new Promise((resolve, reject) => {
|
||||
pending.push(resolve);
|
||||
init.signal?.addEventListener('abort', () => reject(Object.assign(new Error('aborted'), {name: 'AbortError'})));
|
||||
}),
|
||||
});
|
||||
const tick = () => new Promise(resolve => setImmediate(resolve));
|
||||
const answer = (index, preview) => pending[index]({ok: true, status: 200, json: async () => preview});
|
||||
const body = index => JSON.parse(calls[index].init.body);
|
||||
const hit = store.foundHit(foundEntity());
|
||||
const preview = (extra = {}) => Object.assign({
|
||||
podcast_id: POD_A, feed_url: hit.feed_url, moved: false, title: 'Night Radio', episode_count: 4,
|
||||
seasons: [{season: 1, episode_count: 2}, {season: 2, episode_count: 1}, {season: null, episode_count: 1}],
|
||||
listed_count: 4, episodes: [{title: 'Newest', season: 2}], subscribed: false, local_podcast_id: null,
|
||||
}, extra);
|
||||
|
||||
store.openFound(hit);
|
||||
assert.equal(store.found.season, 'all');
|
||||
assert.equal(calls[0].init.signal.aborted, false);
|
||||
answer(0, preview());
|
||||
await tick();
|
||||
assert.equal(store.foundMore(store.found.preview), 'and 3 more');
|
||||
|
||||
// A season chip lists that season; choosing another one cancels the
|
||||
// request still running.
|
||||
store.selectFoundSeason('1');
|
||||
assert.equal(store.found.season, '1');
|
||||
assert.deepEqual(body(1), {podcast_id: POD_A, feed_url: hit.feed_url, season: '1'});
|
||||
store.selectFoundSeason('none');
|
||||
assert.equal(calls[1].init.signal.aborted, true);
|
||||
assert.deepEqual(body(2), {podcast_id: POD_A, feed_url: hit.feed_url, season: 'none'});
|
||||
await tick();
|
||||
// The cancelled request shows no error.
|
||||
assert.equal(store.found.previewError, '');
|
||||
answer(2, preview({listed_count: 1, episodes: [{title: 'Bonus', season: null}]}));
|
||||
await tick();
|
||||
assert.equal(store.found.previewLoading, false);
|
||||
assert.equal(store.found.preview.episodes[0].title, 'Bonus');
|
||||
assert.equal(store.foundMore(store.found.preview), '');
|
||||
// The same chip again shows every season.
|
||||
store.selectFoundSeason('none');
|
||||
assert.equal(store.found.season, 'all');
|
||||
assert.deepEqual(body(3), {podcast_id: POD_A, feed_url: hit.feed_url});
|
||||
store.selectFoundSeason({season: 1});
|
||||
assert.equal(calls.length, 4, 'only season values from the chips');
|
||||
|
||||
// Closing the card cancels its preview.
|
||||
store.closeFound();
|
||||
assert.equal(calls[3].init.signal.aborted, true);
|
||||
assert.equal(store.found, null);
|
||||
// So does opening another card.
|
||||
store.openFound(hit);
|
||||
store.openFound(store.foundHit(foundEntity({podcast_id: POD_B})));
|
||||
assert.equal(calls[4].init.signal.aborted, true);
|
||||
assert.equal(calls[5].init.signal.aborted, false);
|
||||
assert.equal(body(5).podcast_id, POD_B);
|
||||
answer(5, preview({podcast_id: POD_B}));
|
||||
await tick();
|
||||
assert.equal(store.found.hit.podcast_id, POD_B);
|
||||
assert.equal(store.found.preview.podcast_id, POD_B);
|
||||
assert.equal(store.found.previewError, '');
|
||||
});
|
||||
Reference in new issue
Block a user