fix(playlists): restore suggestions and duplicate-check commands in v2

cx
This commit is contained in:
vicrodh committed 2026-02-17 11:22:27 -06:00
1 parent 76cf401a3a
commit 7e0c54c9f8
4 files changed
+182 -3

No files matched your search

+178 -1
View File
@@ -18,7 +18,7 @@ use qbz_models::{
use crate::artist_blacklist::BlacklistState; use crate::artist_blacklist::BlacklistState;
use crate::artist_vectors::ArtistVectorStoreState; use crate::artist_vectors::ArtistVectorStoreState;
use crate::cache::{AudioCache, CacheStats}; use crate::cache::{AudioCache, CacheStats};
use crate::api::models::PlaylistWithTrackIds; use crate::api::models::{PlaylistDuplicateResult, PlaylistWithTrackIds};
use crate::api_cache::ApiCacheState; use crate::api_cache::ApiCacheState;
use crate::audio::{AlsaPlugin, AudioBackendType, AudioDevice, BackendManager}; use crate::audio::{AlsaPlugin, AudioBackendType, AudioDevice, BackendManager};
use crate::cast::{AirPlayMetadata, AirPlayState, CastState, DlnaMetadata, DlnaState, MediaMetadata}; use crate::cast::{AirPlayMetadata, AirPlayState, CastState, DlnaMetadata, DlnaState, MediaMetadata};
@@ -50,6 +50,21 @@ use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Mutex; use std::sync::Mutex;
#[derive(Debug, Clone, serde::Deserialize)]
pub struct V2SuggestionArtistInput {
pub name: String,
pub qobuz_id: Option<u64>,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct V2PlaylistSuggestionsInput {
pub artists: Vec<V2SuggestionArtistInput>,
pub exclude_track_ids: Vec<u64>,
#[serde(default)]
pub include_reasons: bool,
pub config: Option<crate::artist_vectors::SuggestionConfig>,
}
// ==================== Helper Functions ==================== // ==================== Helper Functions ====================
/// Backend information for UI display /// Backend information for UI display
@@ -1476,6 +1491,134 @@ pub async fn v2_clear_vector_store(
store.clear_all() store.clear_all()
} }
#[tauri::command]
pub async fn v2_get_playlist_suggestions(
input: V2PlaylistSuggestionsInput,
store_state: State<'_, ArtistVectorStoreState>,
musicbrainz: State<'_, crate::musicbrainz::MusicBrainzSharedState>,
app_state: State<'_, AppState>,
runtime: State<'_, RuntimeManagerState>,
) -> Result<crate::artist_vectors::SuggestionResult, RuntimeError> {
runtime
.manager()
.check_requirements(CommandRequirement::RequiresCoreBridgeAuth)
.await?;
if input.artists.is_empty() {
return Ok(crate::artist_vectors::SuggestionResult {
tracks: Vec::new(),
source_artists: Vec::new(),
playlist_artists_count: 0,
similar_artists_count: 0,
});
}
let mut resolved_artists: Vec<(String, String)> = Vec::new();
let mut seen_mbids = std::collections::HashSet::new();
for artist in &input.artists {
let mbid_from_qobuz = if let Some(qobuz_id) = artist.qobuz_id {
let qobuz_artist_name = {
let client = app_state.client.read().await;
match client.get_artist(qobuz_id, false).await {
Ok(qobuz_artist) => Some(qobuz_artist.name),
Err(err) => {
log::warn!(
"[V2/Suggestions] Failed to fetch Qobuz artist {} for MBID resolution: {}",
qobuz_id,
err
);
None
}
}
};
if let Some(artist_name) = qobuz_artist_name {
match musicbrainz.client.search_artist(&artist_name).await {
Ok(search) => search
.artists
.into_iter()
.find(|candidate| candidate.score.unwrap_or(0) >= 80)
.map(|candidate| candidate.id),
Err(err) => {
log::warn!(
"[V2/Suggestions] Failed Qobuz->MBID search for {} ({}): {}",
artist.name,
qobuz_id,
err
);
None
}
}
} else {
None
}
} else {
None
};
let resolved_mbid = if mbid_from_qobuz.is_some() {
mbid_from_qobuz
} else {
match musicbrainz.client.search_artist(&artist.name).await {
Ok(search) => search
.artists
.into_iter()
.find(|candidate| candidate.score.unwrap_or(0) >= 80)
.map(|candidate| candidate.id),
Err(err) => {
log::warn!(
"[V2/Suggestions] Failed name->MBID resolution for {}: {}",
artist.name,
err
);
None
}
}
};
if let Some(mbid) = resolved_mbid {
if seen_mbids.insert(mbid.clone()) {
resolved_artists.push((mbid, artist.name.clone()));
}
}
}
if resolved_artists.is_empty() {
log::warn!("[V2/Suggestions] No artists could be resolved to MusicBrainz IDs");
return Ok(crate::artist_vectors::SuggestionResult {
tracks: Vec::new(),
source_artists: Vec::new(),
playlist_artists_count: input.artists.len(),
similar_artists_count: 0,
});
}
let config = input.config.unwrap_or_default();
let builder = std::sync::Arc::new(crate::artist_vectors::ArtistVectorBuilder::new(
store_state.store.clone(),
musicbrainz.client.clone(),
musicbrainz.cache.clone(),
app_state.client.clone(),
crate::artist_vectors::RelationshipWeights::default(),
));
let engine = crate::artist_vectors::SuggestionsEngine::new(
store_state.store.clone(),
builder,
app_state.client.clone(),
config,
);
let exclude_track_ids: std::collections::HashSet<u64> =
input.exclude_track_ids.into_iter().collect();
engine
.generate_suggestions(&resolved_artists, &exclude_track_ids, input.include_reasons)
.await
.map_err(RuntimeError::Internal)
}
#[tauri::command] #[tauri::command]
pub fn v2_add_to_artist_blacklist( pub fn v2_add_to_artist_blacklist(
artist_id: u64, artist_id: u64,
@@ -4696,6 +4839,40 @@ pub async fn v2_get_playlist_track_ids(
.map_err(|e| RuntimeError::Internal(e.to_string())) .map_err(|e| RuntimeError::Internal(e.to_string()))
} }
/// Check duplicates before adding tracks to a playlist (V2).
#[tauri::command]
#[allow(non_snake_case)]
pub async fn v2_check_playlist_duplicates(
playlistId: u64,
trackIds: Vec<u64>,
app_state: State<'_, AppState>,
runtime: State<'_, RuntimeManagerState>,
) -> Result<PlaylistDuplicateResult, RuntimeError> {
runtime
.manager()
.check_requirements(CommandRequirement::RequiresUserSession)
.await?;
let client = app_state.client.read().await;
let playlist = client
.get_playlist_track_ids(playlistId)
.await
.map_err(|e| RuntimeError::Internal(e.to_string()))?;
let existing_ids: std::collections::HashSet<u64> = playlist.track_ids.into_iter().collect();
let duplicate_track_ids: std::collections::HashSet<u64> = trackIds
.iter()
.copied()
.filter(|track_id| existing_ids.contains(track_id))
.collect();
Ok(PlaylistDuplicateResult {
total_tracks: trackIds.len(),
duplicate_count: duplicate_track_ids.len(),
duplicate_track_ids,
})
}
/// Add tracks to playlist (V2 - uses QbzCore) /// Add tracks to playlist (V2 - uses QbzCore)
#[tauri::command] #[tauri::command]
#[allow(non_snake_case)] #[allow(non_snake_case)]
+2
View File
@@ -695,6 +695,7 @@ pub fn run() {
commands_v2::v2_clear_cache, commands_v2::v2_clear_cache,
commands_v2::v2_clear_artist_cache, commands_v2::v2_clear_artist_cache,
commands_v2::v2_get_vector_store_stats, commands_v2::v2_get_vector_store_stats,
commands_v2::v2_get_playlist_suggestions,
commands_v2::v2_clear_vector_store, commands_v2::v2_clear_vector_store,
commands_v2::v2_add_to_artist_blacklist, commands_v2::v2_add_to_artist_blacklist,
commands_v2::v2_remove_from_artist_blacklist, commands_v2::v2_remove_from_artist_blacklist,
@@ -904,6 +905,7 @@ pub fn run() {
commands_v2::v2_get_user_playlists, commands_v2::v2_get_user_playlists,
commands_v2::v2_get_playlist, commands_v2::v2_get_playlist,
commands_v2::v2_get_playlist_track_ids, commands_v2::v2_get_playlist_track_ids,
commands_v2::v2_check_playlist_duplicates,
commands_v2::v2_add_tracks_to_playlist, commands_v2::v2_add_tracks_to_playlist,
commands_v2::v2_remove_tracks_from_playlist, commands_v2::v2_remove_tracks_from_playlist,
commands_v2::v2_create_playlist, commands_v2::v2_create_playlist,
+1 -1
View File
@@ -357,7 +357,7 @@
try { try {
// Check for duplicates in regular playlists with Qobuz tracks // Check for duplicates in regular playlists with Qobuz tracks
const result = await invoke<PlaylistDuplicateResult>('check_playlist_duplicates', { const result = await invoke<PlaylistDuplicateResult>('v2_check_playlist_duplicates', {
playlistId: selectedPlaylistId, playlistId: selectedPlaylistId,
trackIds trackIds
}); });
@@ -127,7 +127,7 @@ export async function getPlaylistSuggestionsV2(
} }
// Call backend - MBID resolution happens server-side with caching // Call backend - MBID resolution happens server-side with caching
const result = await invoke<SuggestionResult>('get_playlist_suggestions_v2', { const result = await invoke<SuggestionResult>('v2_get_playlist_suggestions', {
input: { input: {
artists: dedupedArtists.map(a => ({ artists: dedupedArtists.map(a => ({
name: a.name, name: a.name,