forked from Furumi/furumi-web
Add the podcast share endpoint with privacy confirmation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c17cd790a7
commit
588ea4e99e
3 files changed
+298
-11
No files matched your search
+207
-2
@@ -24,6 +24,7 @@ use music_dht::podcast::{
|
||||
EpisodeId, EpisodeType, FeedUrl, ListenSession, PodcastEpisodeRef, PodcastId, PodcastMeta,
|
||||
ProgressRecord, ProgressStatus, reaches_completion,
|
||||
};
|
||||
use music_dht::podcast_dht::{FeedUrlPrivacyWarning, PodcastDht};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
@@ -162,7 +163,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 +378,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`: the server's overlay, when it runs).
|
||||
pub(crate) async fn podcast_detail(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
discovery: Option<&PodcastDht>,
|
||||
) -> 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 +416,7 @@ pub(crate) async fn podcast_detail(
|
||||
.collect();
|
||||
Ok(PodcastDetail {
|
||||
podcast,
|
||||
share,
|
||||
seasons,
|
||||
history,
|
||||
})
|
||||
@@ -476,6 +484,203 @@ 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, `off`
|
||||
/// otherwise (federation disabled or the overlay failed to start).
|
||||
pub(crate) discovery: &'static str,
|
||||
/// The server currently publishes a card of this podcast and this
|
||||
/// account shares it.
|
||||
pub(crate) published: bool,
|
||||
}
|
||||
|
||||
/// The account's [`ShareView`] of a podcast.
|
||||
pub(crate) async fn share_view(
|
||||
store: &PodcastStore,
|
||||
user_id: i64,
|
||||
podcast_id: &PodcastId,
|
||||
discovery: Option<&PodcastDht>,
|
||||
) -> 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: if discovery.is_some() {
|
||||
"running"
|
||||
} else {
|
||||
"off"
|
||||
},
|
||||
published: false,
|
||||
};
|
||||
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
|
||||
&& account.publishable()
|
||||
{
|
||||
view.published = dht
|
||||
.published()
|
||||
.await
|
||||
.map_err(|error| anyhow::anyhow!("podcast publications unavailable: {error}"))?
|
||||
.iter()
|
||||
.any(|publication| publication.active && publication.podcast_id == *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: Option<&PodcastDht>,
|
||||
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?,
|
||||
})))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Episodes
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user