Formatting
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use actix_web::{web, HttpResponse};
|
||||
use actix_web::{HttpResponse, web};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use shanty_db::entities::wanted_item::WantedStatus;
|
||||
@@ -16,7 +16,9 @@ pub struct PaginationParams {
|
||||
#[serde(default)]
|
||||
offset: u64,
|
||||
}
|
||||
fn default_limit() -> u64 { 50 }
|
||||
fn default_limit() -> u64 {
|
||||
50
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct AddArtistRequest {
|
||||
@@ -67,10 +69,7 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
|
||||
.route(web::get().to(list_artists))
|
||||
.route(web::post().to(add_artist)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/artists/{id}/full")
|
||||
.route(web::get().to(get_artist_full)),
|
||||
)
|
||||
.service(web::resource("/artists/{id}/full").route(web::get().to(get_artist_full)))
|
||||
.service(
|
||||
web::resource("/artists/{id}")
|
||||
.route(web::get().to(get_artist))
|
||||
@@ -94,22 +93,25 @@ async fn list_artists(
|
||||
|
||||
// Check if we have cached artist-level totals from a prior detail page load
|
||||
let cache_key = format!("artist_totals:{}", a.id);
|
||||
let cached_totals: Option<(u32, u32, u32)> = if let Ok(Some(json)) =
|
||||
queries::cache::get(state.db.conn(), &cache_key).await
|
||||
{
|
||||
serde_json::from_str(&json).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let cached_totals: Option<(u32, u32, u32)> =
|
||||
if let Ok(Some(json)) = queries::cache::get(state.db.conn(), &cache_key).await {
|
||||
serde_json::from_str(&json).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let (total_watched, total_owned, total_items) = if let Some((avail, watched, owned)) = cached_totals {
|
||||
(watched as usize, owned as usize, avail as usize)
|
||||
} else {
|
||||
// Fall back to wanted item counts
|
||||
let total_items = artist_wanted.len();
|
||||
let total_owned = artist_wanted.iter().filter(|w| w.status == WantedStatus::Owned).count();
|
||||
(total_items, total_owned, total_items)
|
||||
};
|
||||
let (total_watched, total_owned, total_items) =
|
||||
if let Some((avail, watched, owned)) = cached_totals {
|
||||
(watched as usize, owned as usize, avail as usize)
|
||||
} else {
|
||||
// Fall back to wanted item counts
|
||||
let total_items = artist_wanted.len();
|
||||
let total_owned = artist_wanted
|
||||
.iter()
|
||||
.filter(|w| w.status == WantedStatus::Owned)
|
||||
.count();
|
||||
(total_items, total_owned, total_items)
|
||||
};
|
||||
|
||||
items.push(ArtistListItem {
|
||||
id: a.id,
|
||||
@@ -137,7 +139,9 @@ async fn get_artist(
|
||||
"albums": albums,
|
||||
})))
|
||||
} else {
|
||||
Err(ApiError::BadRequest("use /artists/{id}/full for MBID lookups".into()))
|
||||
Err(ApiError::BadRequest(
|
||||
"use /artists/{id}/full for MBID lookups".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,16 +157,23 @@ async fn get_cached_album_tracks(
|
||||
let cache_key = format!("artist_rg_tracks:{rg_id}");
|
||||
|
||||
// Check cache first
|
||||
if let Some(json) = queries::cache::get(state.db.conn(), &cache_key).await
|
||||
if let Some(json) = queries::cache::get(state.db.conn(), &cache_key)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(e.to_string()))?
|
||||
&& let Ok(cached) = serde_json::from_str::<CachedAlbumTracks>(&json)
|
||||
{
|
||||
if let Ok(cached) = serde_json::from_str::<CachedAlbumTracks>(&json) {
|
||||
// Extend TTL if artist is now watched (upgrades 7-day browse cache to permanent)
|
||||
if extend_ttl {
|
||||
let _ = queries::cache::set(state.db.conn(), &cache_key, "musicbrainz", &json, ttl_seconds).await;
|
||||
}
|
||||
return Ok(cached);
|
||||
// Extend TTL if artist is now watched (upgrades 7-day browse cache to permanent)
|
||||
if extend_ttl {
|
||||
let _ = queries::cache::set(
|
||||
state.db.conn(),
|
||||
&cache_key,
|
||||
"musicbrainz",
|
||||
&json,
|
||||
ttl_seconds,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
return Ok(cached);
|
||||
}
|
||||
|
||||
// Not cached — resolve release MBID and fetch tracks
|
||||
@@ -173,7 +184,8 @@ async fn get_cached_album_tracks(
|
||||
resolve_release_from_group(rg_id).await?
|
||||
};
|
||||
|
||||
let mb_tracks = state.mb_client
|
||||
let mb_tracks = state
|
||||
.mb_client
|
||||
.get_release_tracks(&release_mbid)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("MB error for release {release_mbid}: {e}")))?;
|
||||
@@ -190,9 +202,15 @@ async fn get_cached_album_tracks(
|
||||
};
|
||||
|
||||
// Cache with caller-specified TTL
|
||||
let json = serde_json::to_string(&cached)
|
||||
.map_err(|e| ApiError::Internal(e.to_string()))?;
|
||||
let _ = queries::cache::set(state.db.conn(), &cache_key, "musicbrainz", &json, ttl_seconds).await;
|
||||
let json = serde_json::to_string(&cached).map_err(|e| ApiError::Internal(e.to_string()))?;
|
||||
let _ = queries::cache::set(
|
||||
state.db.conn(),
|
||||
&cache_key,
|
||||
"musicbrainz",
|
||||
&json,
|
||||
ttl_seconds,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(cached)
|
||||
}
|
||||
@@ -258,10 +276,14 @@ pub async fn enrich_artist(
|
||||
let mbid = match &artist.musicbrainz_id {
|
||||
Some(m) => m.clone(),
|
||||
None => {
|
||||
let results = state.search.search_artist(&artist.name, 1).await
|
||||
let results = state
|
||||
.search
|
||||
.search_artist(&artist.name, 1)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(e.to_string()))?;
|
||||
results.into_iter().next().map(|a| a.id)
|
||||
.ok_or_else(|| ApiError::NotFound(format!("no MBID for artist '{}'", artist.name)))?
|
||||
results.into_iter().next().map(|a| a.id).ok_or_else(|| {
|
||||
ApiError::NotFound(format!("no MBID for artist '{}'", artist.name))
|
||||
})?
|
||||
}
|
||||
};
|
||||
(artist, Some(local_id), mbid)
|
||||
@@ -272,7 +294,8 @@ pub async fn enrich_artist(
|
||||
let local = {
|
||||
// Check if any local artist has this MBID
|
||||
let all = queries::artists::list(state.db.conn(), 1000, 0).await?;
|
||||
all.into_iter().find(|a| a.musicbrainz_id.as_deref() == Some(&mbid))
|
||||
all.into_iter()
|
||||
.find(|a| a.musicbrainz_id.as_deref() == Some(&mbid))
|
||||
};
|
||||
|
||||
if let Some(a) = local {
|
||||
@@ -280,7 +303,8 @@ pub async fn enrich_artist(
|
||||
(a, Some(local_id), mbid)
|
||||
} else {
|
||||
// Look up artist name from MusicBrainz by MBID — don't create a local record
|
||||
let (name, _disambiguation) = state.mb_client
|
||||
let (name, _disambiguation) = state
|
||||
.mb_client
|
||||
.get_artist_by_mbid(&mbid)
|
||||
.await
|
||||
.map_err(|e| ApiError::NotFound(format!("artist MBID {mbid} not found: {e}")))?;
|
||||
@@ -299,7 +323,10 @@ pub async fn enrich_artist(
|
||||
};
|
||||
|
||||
// Fetch release groups and filter by allowed secondary types
|
||||
let all_release_groups = state.search.get_release_groups(&mbid).await
|
||||
let all_release_groups = state
|
||||
.search
|
||||
.get_release_groups(&mbid)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(e.to_string()))?;
|
||||
let allowed = state.config.read().await.allowed_secondary_types.clone();
|
||||
let release_groups: Vec<_> = all_release_groups
|
||||
@@ -369,15 +396,21 @@ pub async fn enrich_artist(
|
||||
// If artist has any watched items, cache permanently (10 years);
|
||||
// otherwise cache for 7 days (just browsing)
|
||||
let is_watched = !artist_wanted.is_empty();
|
||||
let cache_ttl = if is_watched { 10 * 365 * 86400 } else { 7 * 86400 };
|
||||
let cache_ttl = if is_watched {
|
||||
10 * 365 * 86400
|
||||
} else {
|
||||
7 * 86400
|
||||
};
|
||||
|
||||
let cached = match get_cached_album_tracks(
|
||||
&state,
|
||||
state,
|
||||
&rg.id,
|
||||
rg.first_release_id.as_deref(),
|
||||
cache_ttl,
|
||||
is_watched,
|
||||
).await {
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
tracing::warn!(rg_id = %rg.id, title = %rg.title, error = %e, "failed to fetch tracks");
|
||||
@@ -435,7 +468,10 @@ pub async fn enrich_artist(
|
||||
.find(|a| a.name.to_lowercase() == rg.title.to_lowercase());
|
||||
let local_album_id = local.map(|a| a.id);
|
||||
let local_tracks = if let Some(aid) = local_album_id {
|
||||
queries::tracks::get_by_album(state.db.conn(), aid).await.unwrap_or_default().len() as u32
|
||||
queries::tracks::get_by_album(state.db.conn(), aid)
|
||||
.await
|
||||
.unwrap_or_default()
|
||||
.len() as u32
|
||||
} else {
|
||||
0
|
||||
};
|
||||
@@ -468,9 +504,14 @@ pub async fn enrich_artist(
|
||||
// Sort: owned first, then partial, then wanted, then unwatched; within each by date
|
||||
albums.sort_by(|a, b| {
|
||||
let order = |s: &str| match s {
|
||||
"owned" => 0, "partial" => 1, "wanted" => 2, _ => 3,
|
||||
"owned" => 0,
|
||||
"partial" => 1,
|
||||
"wanted" => 2,
|
||||
_ => 3,
|
||||
};
|
||||
order(&a.status).cmp(&order(&b.status)).then_with(|| a.date.cmp(&b.date))
|
||||
order(&a.status)
|
||||
.cmp(&order(&b.status))
|
||||
.then_with(|| a.date.cmp(&b.date))
|
||||
});
|
||||
|
||||
// Deduplicated artist-level totals
|
||||
@@ -478,7 +519,10 @@ pub async fn enrich_artist(
|
||||
let total_artist_watched = seen_watched.len() as u32;
|
||||
let total_artist_owned = seen_owned.len() as u32;
|
||||
|
||||
let artist_status = if total_artist_owned > 0 && total_artist_owned >= total_available_tracks && total_available_tracks > 0 {
|
||||
let artist_status = if total_artist_owned > 0
|
||||
&& total_artist_owned >= total_available_tracks
|
||||
&& total_available_tracks > 0
|
||||
{
|
||||
"owned"
|
||||
} else if total_artist_watched > 0 {
|
||||
"partial"
|
||||
@@ -487,16 +531,25 @@ pub async fn enrich_artist(
|
||||
};
|
||||
|
||||
// Cache artist-level totals for the library listing page
|
||||
if !skip_track_fetch {
|
||||
if let Some(local_id) = id {
|
||||
let cache_key = format!("artist_totals:{local_id}");
|
||||
let totals = serde_json::json!([total_available_tracks, total_artist_watched, total_artist_owned]);
|
||||
let _ = queries::cache::set(
|
||||
state.db.conn(), &cache_key, "computed",
|
||||
&totals.to_string(),
|
||||
if artist_wanted.is_empty() { 7 * 86400 } else { 10 * 365 * 86400 },
|
||||
).await;
|
||||
}
|
||||
if !skip_track_fetch && let Some(local_id) = id {
|
||||
let cache_key = format!("artist_totals:{local_id}");
|
||||
let totals = serde_json::json!([
|
||||
total_available_tracks,
|
||||
total_artist_watched,
|
||||
total_artist_owned
|
||||
]);
|
||||
let _ = queries::cache::set(
|
||||
state.db.conn(),
|
||||
&cache_key,
|
||||
"computed",
|
||||
&totals.to_string(),
|
||||
if artist_wanted.is_empty() {
|
||||
7 * 86400
|
||||
} else {
|
||||
10 * 365 * 86400
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
Ok(serde_json::json!({
|
||||
@@ -515,10 +568,7 @@ pub async fn enrich_all_watched_artists(state: &AppState) -> Result<u32, ApiErro
|
||||
let all_wanted = queries::wanted::list(state.db.conn(), None).await?;
|
||||
|
||||
// Collect unique artist IDs that have any wanted items
|
||||
let mut artist_ids: Vec<i32> = all_wanted
|
||||
.iter()
|
||||
.filter_map(|w| w.artist_id)
|
||||
.collect();
|
||||
let mut artist_ids: Vec<i32> = all_wanted.iter().filter_map(|w| w.artist_id).collect();
|
||||
artist_ids.sort();
|
||||
artist_ids.dedup();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user