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

+200 -4
View File
@@ -94,6 +94,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 }}",
@@ -3692,6 +3698,8 @@ document.addEventListener('alpine:init', () => {
sharing: false,
shareError: '',
shareDialog: null,
found: null,
_foundToken: 0,
busy: {},
expanded: {},
_searchTimer: null,
@@ -3979,6 +3987,171 @@ document.addEventListener('alpine:init', () => {
}
},
// 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 more = Number(preview?.episode_count || 0) - (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._foundToken += 1;
this.found = {
hit,
preview: null,
previewLoading: false,
previewError: '',
subscribing: false,
error: '',
message: '',
subscribed: hit.subscribed === true,
localPodcastId: hit.local_podcast_id || null,
};
this.loadFoundPreview();
},
closeFound() {
this._foundToken += 1;
this.found = null;
},
_foundBody(hit) {
// 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 }),
};
},
async loadFoundPreview() {
const found = this.found;
if (!found || found.previewLoading) return;
const token = this._foundToken;
this.found = { ...found, previewLoading: true, previewError: '' };
try {
const preview = await this._json('/api/podcasts/discovery/preview', this._foundBody(found.hit));
if (token !== this._foundToken) return;
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 (token !== this._foundToken) return;
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?.();
@@ -4172,7 +4345,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: [] },
artistFederation: { loading: false, error: '', releases: [], tracks: [] },
federationPreparing: {},
federationFailures: {},
@@ -5112,7 +5285,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: [] };
this.similaritySearchStats = { loading: true, tracks: 0, artists: 0, peers: 0, elapsed_ms: 0 };
Alpine.store('info').close();
try {
@@ -5254,12 +5427,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: [] };
},
startFederationSearch(query) {
this.stopFederationSearch();
this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [] };
this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [], podcasts: [] };
const source = new EventSource(`/api/player/federation/search/events?q=${encodeURIComponent(query)}`);
this._federationSearchSource = source;
const updateResults = (kind, keyFor, labelFor, entity) => {
@@ -5312,6 +5485,20 @@ document.addEventListener('alpine:init', () => {
if (!release?.key) return;
updateResults('releases', item => JSON.stringify(item.key || {}), item => item.title, release);
});
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', () => {
window.clearTimeout(watchdog);
if (this._federationSearchSource === source) {
@@ -5345,6 +5532,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')