Files
playlistd/src/web.rs
T
Claude a594c56abc Set up logging properly
Rocket's logger was the only one, so messages from the daemons depended on
it having launched, and most messages bypassed logging with println!.

- Install env_logger at startup: level from log.level (LOG_LEVEL), with
  RUST_LOG overriding it. Rocket's per-request logs and sqlx's Postgres
  notices are kept to warnings.
- Under systemd, prefix lines with their syslog priority so journald
  records levels (journalctl -p warning works).
- Replace every println! with a log macro at a fitting level.
- Recurring failures are a warning when they start or change, debug while
  they repeat, and an info message when they recover.
- Listenbrainz errors are readable: an unknown user is "Listenbrainz user
  "x" doesn't exist" rather than a JSON decode error, and a period without
  stats yet (204) leaves the playlist alone instead of failing to decode.
  The UI's Refresh shows the same messages.
- Warnings name the playlist or rule they're about.
- Songs missing from the library are debug output, not errors every tick.
- Daemons no longer run twice at startup (interval's first tick is
  immediate), and a failing playlist rule or daemon is logged instead of
  panicking the task.
- Subsonic errors display as messages rather than Debug output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018opPJMJjLWr4RqdG6vuK6p
2026-11-13 21:48:54 +00:00

392 lines
11 KiB
Rust

//! API for the discover UI: mixes, suggested songs, and audio/cover streaming
//! from Subsonic or YouTube.
use crate::models::{ClusterMixRule, MixSuggestion};
use crate::subsonic::Subsonic;
use crate::track::Track;
use crate::youtube::YouTube;
use futures_util::TryStreamExt;
use rocket::http::Status;
use rocket::request::{FromRequest, Outcome};
use rocket::response::{self, Responder};
use rocket::serde::json::Json;
use rocket::{get, post, put, routes, Request, Response, Route, State};
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPool;
use std::convert::Infallible;
use std::sync::Arc;
use tokio_util::io::StreamReader;
use uuid::Uuid;
pub fn routes() -> Vec<Route> {
routes![rules, mixes, mix, liked, set_status, rebuild, stream, cover, youtube]
}
type ApiResult<T> = Result<Json<T>, Status>;
fn db_error(e: sqlx::Error) -> Status {
match e {
sqlx::Error::RowNotFound => Status::NotFound,
e => {
log::error!("Database error: {}", e);
Status::InternalServerError
}
}
}
fn parse_id(id: &str) -> Result<Uuid, Status> {
Uuid::parse_str(id).map_err(|_| Status::NotFound)
}
#[get("/rules")]
async fn rules(pool: &State<PgPool>) -> ApiResult<Vec<ClusterMixRule>> {
ClusterMixRule::all(pool.inner())
.await
.map(Json)
.map_err(db_error)
}
#[derive(Serialize)]
struct MixSummary {
id: Uuid,
playlist_id: String,
name: Option<String>,
tags: Vec<String>,
position: i32,
new_count: i64,
}
#[get("/rules/<id>/mixes")]
async fn mixes(id: &str, pool: &State<PgPool>) -> ApiResult<Vec<MixSummary>> {
sqlx::query_as!(
MixSummary,
r#"
SELECT p.id, p.playlist_id, p.name, p.tags, p.position,
COUNT(s.id) FILTER (WHERE s.status = 'new') AS "new_count!"
FROM "cluster_mix_playlist" p
LEFT JOIN "mix_suggestion" s ON s.mix_id = p.id
WHERE p.rule_id = $1
GROUP BY p.id
ORDER BY p.position
"#,
parse_id(id)?
)
.fetch_all(pool.inner())
.await
.map(Json)
.map_err(db_error)
}
#[derive(Serialize)]
struct MixDetail {
#[serde(flatten)]
summary: MixSummary,
songs: Vec<Track>,
/// Songs in `songs` the user played recently
known_song_ids: Vec<String>,
suggestions: Vec<MixSuggestion>,
}
#[get("/mixes/<id>")]
async fn mix(
id: &str,
pool: &State<PgPool>,
media_client: &State<Arc<Subsonic>>,
) -> ApiResult<MixDetail> {
let id = parse_id(id)?;
let summary = sqlx::query_as!(
MixSummary,
r#"
SELECT p.id, p.playlist_id, p.name, p.tags, p.position,
COUNT(s.id) FILTER (WHERE s.status = 'new') AS "new_count!"
FROM "cluster_mix_playlist" p
LEFT JOIN "mix_suggestion" s ON s.mix_id = p.id
WHERE p.id = $1
GROUP BY p.id
"#,
id
)
.fetch_one(pool.inner())
.await
.map_err(db_error)?;
let songs = media_client
.get_playlist(&summary.playlist_id)
.await
.map_err(|e| {
log::warn!("Could not load playlist {}: {}", summary.playlist_id, e);
Status::BadGateway
})?
.entry;
let known_song_ids = sqlx::query_scalar!(
r#"SELECT known_song_ids FROM "cluster_mix_playlist" WHERE id = $1"#,
id
)
.fetch_one(pool.inner())
.await
.map_err(db_error)?;
let suggestions = sqlx::query_as!(
MixSuggestion,
r#"
SELECT * FROM "mix_suggestion"
WHERE mix_id = $1 AND status <> 'dismissed'
ORDER BY score DESC
"#,
id
)
.fetch_all(pool.inner())
.await
.map_err(db_error)?;
Ok(Json(MixDetail {
summary,
songs,
known_song_ids,
suggestions,
}))
}
#[get("/rules/<id>/liked")]
async fn liked(id: &str, pool: &State<PgPool>) -> ApiResult<Vec<MixSuggestion>> {
sqlx::query_as!(
MixSuggestion,
r#"
SELECT * FROM "mix_suggestion"
WHERE rule_id = $1 AND status = 'liked'
ORDER BY created_at DESC
"#,
parse_id(id)?
)
.fetch_all(pool.inner())
.await
.map(Json)
.map_err(db_error)
}
#[derive(Deserialize)]
struct StatusUpdate {
status: String,
}
/// Like, dismiss, or reset a suggestion
#[put("/suggestions/<id>", data = "<update>")]
async fn set_status(
id: &str,
update: Json<StatusUpdate>,
pool: &State<PgPool>,
) -> ApiResult<MixSuggestion> {
if !["new", "liked", "dismissed"].contains(&update.status.as_str()) {
return Err(Status::UnprocessableEntity);
}
sqlx::query_as!(
MixSuggestion,
r#"UPDATE "mix_suggestion" SET status = $1 WHERE id = $2 RETURNING *"#,
update.status,
parse_id(id)?
)
.fetch_one(pool.inner())
.await
.map(Json)
.map_err(db_error)
}
/// Rebuild a rule's mixes on the next daemon tick
#[post("/rules/<id>/rebuild")]
async fn rebuild(id: &str, pool: &State<PgPool>) -> Status {
let Ok(id) = parse_id(id) else {
return Status::NotFound;
};
match sqlx::query!(
r#"UPDATE "cluster_mix_rule" SET last_run = NULL WHERE id = $1"#,
id
)
.execute(pool.inner())
.await
{
Ok(r) if r.rows_affected() > 0 => Status::Accepted,
Ok(_) => Status::NotFound,
Err(e) => db_error(e),
}
}
/// The request's Range header, so audio can be seeked
pub struct Range(Option<String>);
#[rocket::async_trait]
impl<'r> FromRequest<'r> for Range {
type Error = Infallible;
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
Outcome::Success(Range(
request.headers().get_one("Range").map(str::to_string),
))
}
}
/// Stream an upstream response through, keeping the headers a media element needs
pub struct Proxied(reqwest::Response);
impl<'r> Responder<'r, 'static> for Proxied {
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
let upstream = self.0;
let mut response = Response::build();
response.status(Status::new(upstream.status().as_u16()));
for name in [
"content-type",
"content-range",
"accept-ranges",
"cache-control",
"last-modified",
"etag",
] {
if let Some(value) = upstream.headers().get(name).and_then(|v| v.to_str().ok()) {
response.raw_header(name, value.to_string());
}
}
let size = upstream.content_length();
let body = StreamReader::new(upstream.bytes_stream().map_err(std::io::Error::other));
match size {
// a known length lets browsers show durations and seek
Some(size) => response.sized_body(size as usize, LengthReader(body)),
None => response.streamed_body(body),
};
response.ok()
}
}
/// Rocket only accepts a known body length for seekable bodies, but never seeks one whose size
/// is given up front. This satisfies the trait for a plain stream.
struct LengthReader<R>(R);
impl<R: tokio::io::AsyncRead + Unpin> tokio::io::AsyncRead for LengthReader<R> {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<std::io::Result<()>> {
std::pin::Pin::new(&mut self.0).poll_read(cx, buf)
}
}
impl<R: Unpin> tokio::io::AsyncSeek for LengthReader<R> {
fn start_seek(self: std::pin::Pin<&mut Self>, _: std::io::SeekFrom) -> std::io::Result<()> {
Err(std::io::Error::other("streamed body can't be seeked"))
}
fn poll_complete(
self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
) -> std::task::Poll<std::io::Result<u64>> {
std::task::Poll::Ready(Err(std::io::Error::other("streamed body can't be seeked")))
}
}
/// Stream a song from Subsonic
#[get("/stream/<id>")]
async fn stream(
id: &str,
range: Range,
media_client: &State<Arc<Subsonic>>,
) -> Result<Proxied, Status> {
let format = crate::CONFIG
.get()
.and_then(|c| c.get::<String>("discover.format").ok())
.unwrap_or_else(|| "raw".to_string());
media_client
.raw(
"stream",
&[("id", id.to_string()), ("format", format)],
range.0.as_deref(),
)
.await
.map(Proxied)
.map_err(|_| Status::BadGateway)
}
/// Cover art from Subsonic
#[get("/cover/<id>?<size>")]
async fn cover(
id: &str,
size: Option<u32>,
media_client: &State<Arc<Subsonic>>,
) -> Result<Proxied, Status> {
media_client
.raw(
"getCoverArt",
&[
("id", id.to_string()),
("size", size.unwrap_or(300).to_string()),
],
None,
)
.await
.map(Proxied)
.map_err(|_| Status::BadGateway)
}
/// Find a suggested song on YouTube and stream its audio
#[get("/youtube/<id>")]
async fn youtube(
id: &str,
range: Range,
pool: &State<PgPool>,
youtube: &State<YouTube>,
) -> Result<Proxied, Status> {
let suggestion = sqlx::query_as!(
MixSuggestion,
r#"SELECT * FROM "mix_suggestion" WHERE id = $1"#,
parse_id(id)?
)
.fetch_one(pool.inner())
.await
.map_err(db_error)?;
let video_id = match suggestion.youtube_id {
Some(video_id) => video_id,
None => {
let video_id = youtube
.search(&suggestion.artist, &suggestion.title)
.await
.map_err(|e| {
log::warn!(
"YouTube search failed for {} - {}: {}",
suggestion.artist,
suggestion.title,
e
);
Status::BadGateway
})?
.ok_or(Status::NotFound)?;
sqlx::query!(
r#"UPDATE "mix_suggestion" SET youtube_id = $1 WHERE id = $2"#,
video_id,
suggestion.id
)
.execute(pool.inner())
.await
.map_err(db_error)?;
video_id
}
};
// a cached URL may have expired; fetch a fresh one once
for refresh in [false, true] {
let url = youtube.audio_url(&video_id, refresh).await.map_err(|e| {
log::warn!("Could not get audio for YouTube video {}: {}", video_id, e);
Status::BadGateway
})?;
let mut request = youtube.client.get(url);
if let Some(range) = &range.0 {
request = request.header(reqwest::header::RANGE, range);
}
let response = request.send().await.map_err(|_| Status::BadGateway)?;
if !refresh && matches!(response.status().as_u16(), 403 | 404 | 410) {
continue;
}
return Ok(Proxied(response));
}
Err(Status::BadGateway)
}