Show public podcasts in the federated search and subscribe from a card

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-20 15:12:51 +02:00
1 parent 20bf30eae8
commit 59d94203db
6 files changed
+625 -5

No files matched your search

+183 -1
View File
@@ -799,7 +799,7 @@ function podcastsStore(stores = {}, overrides = {}) {
podcastFetchError: 'Last check failed', podcastLastChecked: 'Checked', podcastNeverChecked: 'Not checked yet',
podcastJamBlocked: 'jam blocked', podcastRequestFailed: 'request failed',
},
URLSearchParams, GUEST: false,
URLSearchParams, URL, GUEST: false,
fetch: async (url, init = {}) => {
calls.push({url, init});
if (overrides.respond) return overrides.respond(url, init);
@@ -1100,3 +1100,185 @@ 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(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), []);
});
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);
});