Compare commits
2 Commits
2444c93d48
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f5d3f597a | |||
| 05756d95cc |
+42
-21
@@ -1,4 +1,4 @@
|
||||
use sea_orm::{ActiveValue::NotSet, ActiveValue::Set, DatabaseConnection};
|
||||
use sea_orm::{ActiveModelTrait, ActiveValue::NotSet, ActiveValue::Set, DatabaseConnection};
|
||||
use shanty_data::{MetadataFetcher as MetadataProvider, RecordingDetails, ReleaseRef};
|
||||
use shanty_db::entities::track;
|
||||
use shanty_db::queries;
|
||||
@@ -78,23 +78,36 @@ pub async fn tag_track(
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// Use existing artist_id if already set (e.g., from download pipeline).
|
||||
// Only upsert from MB when the track has no artist association yet.
|
||||
let artist_id = if track.artist_id.is_some() {
|
||||
track.artist_id
|
||||
} else {
|
||||
match &details.artist_mbid {
|
||||
Some(mbid) => Some(
|
||||
queries::artists::upsert(conn, &details.artist, Some(mbid))
|
||||
.await?
|
||||
.id,
|
||||
),
|
||||
None => Some(
|
||||
queries::artists::upsert(conn, &details.artist, None)
|
||||
.await?
|
||||
.id,
|
||||
),
|
||||
}
|
||||
// If the resolved MBID differs from the track's original MBID, update the wanted_item
|
||||
// so the cleanup step doesn't delete files whose MBID changed during tagging
|
||||
if let Some(ref old_mbid) = track.musicbrainz_id
|
||||
&& old_mbid != &details.mbid
|
||||
&& let Ok(Some(wanted)) = queries::wanted::find_by_mbid(conn, old_mbid).await
|
||||
{
|
||||
let mut active: shanty_db::entities::wanted_item::ActiveModel = wanted.into();
|
||||
active.musicbrainz_id = Set(Some(details.mbid.clone()));
|
||||
let _ = active.update(conn).await;
|
||||
tracing::info!(
|
||||
old_mbid = %old_mbid,
|
||||
new_mbid = %details.mbid,
|
||||
"updated wanted_item MBID after tagger fallback"
|
||||
);
|
||||
}
|
||||
|
||||
// Always resolve artist_id from MB data — this is the authoritative source for the
|
||||
// primary artist. The indexer or download worker may have set artist_id to a collaborator
|
||||
// artist (e.g., "Bass Drum of Death, Not Documented"), so we always correct it here.
|
||||
let artist_id = match &details.artist_mbid {
|
||||
Some(mbid) => Some(
|
||||
queries::artists::upsert(conn, &details.artist, Some(mbid))
|
||||
.await?
|
||||
.id,
|
||||
),
|
||||
None => Some(
|
||||
queries::artists::upsert(conn, &details.artist, None)
|
||||
.await?
|
||||
.id,
|
||||
),
|
||||
};
|
||||
|
||||
// Upsert album from best release
|
||||
@@ -121,14 +134,22 @@ pub async fn tag_track(
|
||||
|
||||
let genre = details.genres.first().cloned();
|
||||
|
||||
// Update track metadata
|
||||
// Update track metadata — preserve artist name when artist_id was already set
|
||||
let artist_name_for_track = if track.artist_id.is_some() {
|
||||
track
|
||||
.artist
|
||||
.clone()
|
||||
.or_else(|| Some(details.artist.clone()))
|
||||
} else {
|
||||
Some(details.artist.clone())
|
||||
};
|
||||
let active = track::ActiveModel {
|
||||
id: Set(track.id),
|
||||
file_path: Set(track.file_path.clone()),
|
||||
title: Set(Some(details.title.clone())),
|
||||
artist: Set(Some(details.artist.clone())),
|
||||
artist: Set(artist_name_for_track.clone()),
|
||||
album: Set(album_name),
|
||||
album_artist: Set(Some(details.artist.clone())),
|
||||
album_artist: Set(artist_name_for_track),
|
||||
musicbrainz_id: Set(Some(details.mbid.clone())),
|
||||
artist_id: Set(artist_id),
|
||||
album_id: Set(album_id),
|
||||
|
||||
Reference in New Issue
Block a user