diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs index 7315cbe01..c11ba1d00 100644 --- a/src-tauri/src/commands_v2.rs +++ b/src-tauri/src/commands_v2.rs @@ -363,12 +363,21 @@ pub async fn runtime_bootstrap( return Ok(status); } - // Step 3: Check for saved credentials and attempt auto-login + // Step 3: Check for saved credentials and attempt auto-login. + // NOTE: user_id hint is optional; credentials are the source of truth. + // This keeps bootstrap robust even if last_user_id is missing/corrupt. let creds = crate::credentials::load_qobuz_credentials(); - let last_user_id = crate::user_data::UserDataPaths::load_last_user_id(); + let last_user_id_hint = crate::user_data::UserDataPaths::load_last_user_id(); - if let (Ok(Some(creds)), Some(user_id)) = (creds, last_user_id) { - log::info!("[Runtime] Found saved credentials, attempting auto-login for user {}", user_id); + if let Ok(Some(creds)) = creds { + if let Some(uid) = last_user_id_hint { + log::info!( + "[Runtime] Found saved credentials, attempting auto-login (user hint: {})", + uid + ); + } else { + log::info!("[Runtime] Found saved credentials, attempting auto-login (no user hint)"); + } // Login to legacy client let client = app_state.client.read().await; @@ -424,7 +433,7 @@ pub async fn runtime_bootstrap( } } } else { - log::info!("[Runtime] No saved credentials or last user ID, staying in InitializedNoAuth"); + log::info!("[Runtime] No saved credentials, staying in InitializedNoAuth"); } manager.set_bootstrap_in_progress(false).await; @@ -486,6 +495,25 @@ pub struct V2LoginResponse { pub error_code: Option, } +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct V2UserInfo { + pub user_name: String, + pub subscription: String, + pub subscription_valid_until: Option, +} + +#[tauri::command] +pub async fn v2_get_user_info( + app_state: State<'_, AppState>, +) -> Result, RuntimeError> { + let client = app_state.client.read().await; + Ok(client.get_user_info().await.map(|(name, subscription, valid_until)| V2UserInfo { + user_name: name, + subscription, + subscription_valid_until: valid_until, + })) +} + /// Auto-login using saved credentials (Phase 2 of runtime initialization) /// /// This command: @@ -1197,6 +1225,34 @@ pub async fn v2_plex_ping( crate::plex::plex_ping(baseUrl, token).await } +#[tauri::command] +#[allow(non_snake_case)] +pub async fn v2_plex_get_track_metadata( + baseUrl: String, + token: String, + ratingKey: String, +) -> Result { + crate::plex::plex_get_track_metadata(baseUrl, token, ratingKey).await +} + +#[tauri::command] +#[allow(non_snake_case)] +pub async fn v2_plex_auth_pin_start( + clientIdentifier: String, +) -> Result { + crate::plex::plex_auth_pin_start(clientIdentifier).await +} + +#[tauri::command] +#[allow(non_snake_case)] +pub async fn v2_plex_auth_pin_check( + clientIdentifier: String, + pinId: u64, + code: Option, +) -> Result { + crate::plex::plex_auth_pin_check(clientIdentifier, pinId, code).await +} + #[tauri::command] pub fn v2_set_visualizer_enabled( enabled: bool, @@ -1388,6 +1444,11 @@ pub fn v2_plex_cache_save_sections( crate::plex::plex_cache_save_sections(server_id, sections) } +#[tauri::command] +pub fn v2_plex_cache_get_sections() -> Result, String> { + crate::plex::plex_cache_get_sections() +} + #[tauri::command] pub fn v2_plex_cache_save_tracks( server_id: Option, @@ -1402,6 +1463,48 @@ pub fn v2_plex_cache_clear() -> Result<(), String> { crate::plex::plex_cache_clear() } +#[tauri::command] +pub fn v2_plex_cache_get_tracks( + sectionKey: Option, + limit: Option, +) -> Result, String> { + crate::plex::plex_cache_get_tracks(sectionKey, limit) +} + +#[tauri::command] +pub fn v2_plex_cache_get_albums() -> Result, String> { + crate::plex::plex_cache_get_albums() +} + +#[tauri::command] +pub fn v2_plex_cache_search_tracks( + query: String, + limit: Option, +) -> Result, String> { + crate::plex::plex_cache_search_tracks(query, limit) +} + +#[tauri::command] +pub fn v2_plex_cache_get_album_tracks( + albumKey: String, +) -> Result, String> { + crate::plex::plex_cache_get_album_tracks(albumKey) +} + +#[tauri::command] +pub fn v2_plex_cache_update_track_quality( + updates: Vec, +) -> Result { + crate::plex::plex_cache_update_track_quality(updates) +} + +#[tauri::command] +pub fn v2_plex_cache_get_tracks_needing_hydration( + limit: Option, +) -> Result, String> { + crate::plex::plex_cache_get_tracks_needing_hydration(limit) +} + // ==================== Casting / Local Library Commands (V2 Native) ==================== #[tauri::command] @@ -1787,6 +1890,42 @@ pub async fn v2_library_remove_folder( Ok(()) } +#[tauri::command] +pub async fn v2_library_check_folder_accessible(path: String) -> Result { + log::info!("[V2] library_check_folder_accessible {}", path); + + let path_ref = std::path::Path::new(&path); + if !path_ref.exists() { + return Ok(false); + } + + // Avoid UI stalls on slow/unresponsive mounts. + let path_clone = path.clone(); + let check_result = tokio::time::timeout( + std::time::Duration::from_secs(6), + tokio::task::spawn_blocking(move || std::fs::read_dir(std::path::Path::new(&path_clone)).is_ok()), + ) + .await; + + match check_result { + Ok(Ok(accessible)) => Ok(accessible), + Ok(Err(_)) => { + log::warn!("[V2] Failed to spawn blocking task for folder check: {}", path); + Ok(false) + } + Err(_) => { + // Mounted-but-slow network shares can timeout but still be usable. + let exists = std::path::Path::new(&path).exists(); + log::warn!( + "[V2] Timeout checking folder accessibility: {} (exists={})", + path, + exists + ); + Ok(exists) + } + } +} + #[tauri::command] pub async fn v2_library_clear_artwork_cache() -> Result { let artwork_dir = get_artwork_cache_dir(); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8d4a730a1..056d4a8ea 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1160,6 +1160,7 @@ pub fn run() { commands_v2::v2_init_client, commands_v2::v2_auto_login, commands_v2::v2_manual_login, + commands_v2::v2_get_user_info, commands_v2::v2_save_credentials, commands_v2::v2_clear_saved_credentials, commands_v2::v2_set_api_locale, @@ -1180,6 +1181,9 @@ pub fn run() { commands_v2::v2_plex_ping, commands_v2::v2_plex_get_music_sections, commands_v2::v2_plex_get_section_tracks, + commands_v2::v2_plex_get_track_metadata, + commands_v2::v2_plex_auth_pin_start, + commands_v2::v2_plex_auth_pin_check, commands_v2::v2_plex_play_track, commands_v2::v2_set_visualizer_enabled, commands_v2::v2_get_developer_settings, @@ -1198,8 +1202,15 @@ pub fn run() { commands_v2::v2_set_blacklist_enabled, commands_v2::v2_clear_artist_blacklist, commands_v2::v2_plex_open_auth_url, + commands_v2::v2_plex_cache_get_sections, commands_v2::v2_plex_cache_save_sections, commands_v2::v2_plex_cache_save_tracks, + commands_v2::v2_plex_cache_get_tracks, + commands_v2::v2_plex_cache_get_albums, + commands_v2::v2_plex_cache_get_album_tracks, + commands_v2::v2_plex_cache_search_tracks, + commands_v2::v2_plex_cache_update_track_quality, + commands_v2::v2_plex_cache_get_tracks_needing_hydration, commands_v2::v2_plex_cache_clear, commands_v2::v2_cast_start_discovery, commands_v2::v2_cast_stop_discovery, @@ -1232,6 +1243,7 @@ pub fn run() { commands_v2::v2_airplay_set_volume, commands_v2::v2_clear_offline_cache, commands_v2::v2_library_remove_folder, + commands_v2::v2_library_check_folder_accessible, commands_v2::v2_library_clear_artwork_cache, commands_v2::v2_library_clear_thumbnails_cache, commands_v2::v2_library_play_track,