From b9364909691160647729d674e7f83816e0f484d1 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Thu, 5 Feb 2026 23:19:23 -0600 Subject: [PATCH] add user_id to LoginResponse and activate/deactivate session commands LoginResponse now includes user_id from the Qobuz API session. activate_user_session: called after login with user_id, runs one-time flat-to-user migration, then initializes all 22+ per-user stores at ~/.local/share/qbz/users/{uid}/ and ~/.cache/qbz/users/{uid}/. Also runs deferred subscription purge check. deactivate_user_session: called before logout, tears down all per-user stores to close database connections and clears the active user. --- src-tauri/src/commands/auth.rs | 9 + src-tauri/src/commands/mod.rs | 2 + src-tauri/src/commands/user_session.rs | 237 +++++++++++++++++++++++++ src-tauri/src/lib.rs | 3 + 4 files changed, 251 insertions(+) create mode 100644 src-tauri/src/commands/user_session.rs diff --git a/src-tauri/src/commands/auth.rs b/src-tauri/src/commands/auth.rs index 2f3a54726..4da636bcc 100644 --- a/src-tauri/src/commands/auth.rs +++ b/src-tauri/src/commands/auth.rs @@ -13,6 +13,7 @@ use crate::offline_cache::OfflineCacheState; pub struct LoginResponse { pub success: bool, pub user_name: Option, + pub user_id: Option, pub subscription: Option, pub subscription_valid_until: Option, pub error: Option, @@ -90,6 +91,7 @@ pub async fn login( Ok(LoginResponse { success: true, user_name: Some(session.display_name), + user_id: Some(session.user_id), subscription: Some(session.subscription_label), subscription_valid_until: session.subscription_valid_until, error: None, @@ -112,6 +114,7 @@ pub async fn login( Ok(LoginResponse { success: false, user_name: None, + user_id: None, subscription: None, subscription_valid_until: None, error: Some("No active subscription".to_string()), @@ -121,6 +124,7 @@ pub async fn login( Err(e) => Ok(LoginResponse { success: false, user_name: None, + user_id: None, subscription: None, subscription_valid_until: None, error: Some(e.to_string()), @@ -204,6 +208,7 @@ pub async fn auto_login( return Ok(LoginResponse { success: false, user_name: None, + user_id: None, subscription: None, subscription_valid_until: None, error: Some("No saved credentials".to_string()), @@ -214,6 +219,7 @@ pub async fn auto_login( return Ok(LoginResponse { success: false, user_name: None, + user_id: None, subscription: None, subscription_valid_until: None, error: Some(e), @@ -235,6 +241,7 @@ pub async fn auto_login( Ok(LoginResponse { success: true, user_name: Some(session.display_name), + user_id: Some(session.user_id), subscription: Some(session.subscription_label), subscription_valid_until: session.subscription_valid_until, error: None, @@ -257,6 +264,7 @@ pub async fn auto_login( Ok(LoginResponse { success: false, user_name: None, + user_id: None, subscription: None, subscription_valid_until: None, error: Some("No active subscription".to_string()), @@ -270,6 +278,7 @@ pub async fn auto_login( Ok(LoginResponse { success: false, user_name: None, + user_id: None, subscription: None, subscription_valid_until: None, error: Some(e.to_string()), diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs index 884306a70..e471da844 100644 --- a/src-tauri/src/commands/mod.rs +++ b/src-tauri/src/commands/mod.rs @@ -25,6 +25,7 @@ pub mod search; pub mod share; pub mod smart_playlists; pub mod remote_metadata; +pub mod user_session; pub mod visualizer; pub use artist_blacklist::*; @@ -50,4 +51,5 @@ pub use search::*; pub use share::*; pub use smart_playlists::*; pub use remote_metadata::*; +pub use user_session::*; pub use visualizer::*; diff --git a/src-tauri/src/commands/user_session.rs b/src-tauri/src/commands/user_session.rs new file mode 100644 index 000000000..2fc9e9765 --- /dev/null +++ b/src-tauri/src/commands/user_session.rs @@ -0,0 +1,237 @@ +//! Per-user session activation and teardown +//! +//! After login, the frontend calls `activate_user_session` with the Qobuz user_id. +//! This initializes all per-user database stores at the user-scoped directory. +//! On logout, `deactivate_user_session` tears everything down. + +use std::path::Path; +use std::sync::{Arc, Mutex}; +use tauri::State; + +use crate::api_cache::ApiCacheState; +use crate::artist_blacklist::BlacklistState; +use crate::artist_vectors::ArtistVectorStoreState; +use crate::config::{ + audio_settings::AudioSettingsState, + download_settings::{DownloadSettingsState, DownloadSettingsStore}, + favorites_cache::FavoritesCacheState, + favorites_preferences::FavoritesPreferencesState, + legal_settings::{LegalSettingsState, LegalSettingsStore}, + playback_preferences::PlaybackPreferencesState, + remote_control_settings::{AllowedOriginsState, RemoteControlSettingsState}, + subscription_state::{SubscriptionStateState, SubscriptionStateStore}, + tray_settings::TraySettingsState, +}; +use crate::library::commands::LibraryState; +use crate::listenbrainz::ListenBrainzSharedState; +use crate::lyrics::LyricsState; +use crate::musicbrainz::MusicBrainzSharedState; +use crate::offline::OfflineState; +use crate::offline_cache::OfflineCacheState; +use crate::reco_store::RecoState; +use crate::session_store::SessionStoreState; +use crate::updates::UpdatesState; +use crate::user_data::UserDataPaths; + +/// Helper to init a type-alias state (Arc>>) at a path +fn init_type_alias_state( + state: &Arc>>, + base_dir: &Path, + constructor: F, +) -> Result<(), String> +where + F: FnOnce(&Path) -> Result, +{ + let store = constructor(base_dir)?; + let mut guard = state.lock().map_err(|e| format!("Lock error: {}", e))?; + *guard = Some(store); + Ok(()) +} + +/// Helper to teardown a type-alias state +fn teardown_type_alias_state(state: &Arc>>) { + if let Ok(mut guard) = state.lock() { + *guard = None; + } +} + +/// Activate the per-user session after login. +/// +/// This runs the one-time migration (if needed) and initializes all +/// per-user database stores at `~/.local/share/qbz/users/{user_id}/` +/// and cache stores at `~/.cache/qbz/users/{user_id}/`. +#[tauri::command] +pub async fn activate_user_session( + user_id: u64, + user_paths: State<'_, UserDataPaths>, + session_store: State<'_, SessionStoreState>, + favorites_cache: State<'_, FavoritesCacheState>, + subscription_state: State<'_, SubscriptionStateState>, + playback_prefs: State<'_, PlaybackPreferencesState>, + favorites_prefs: State<'_, FavoritesPreferencesState>, + download_settings: State<'_, DownloadSettingsState>, + audio_settings: State<'_, AudioSettingsState>, + tray_settings: State<'_, TraySettingsState>, + remote_control_settings: State<'_, RemoteControlSettingsState>, + allowed_origins: State<'_, AllowedOriginsState>, + legal_settings: State<'_, LegalSettingsState>, + updates: State<'_, UpdatesState>, + library: State<'_, LibraryState>, + reco: State<'_, RecoState>, + api_cache: State<'_, ApiCacheState>, + artist_vectors: State<'_, ArtistVectorStoreState>, + blacklist: State<'_, BlacklistState>, + offline: State<'_, OfflineState>, + offline_cache: State<'_, OfflineCacheState>, + lyrics: State<'_, LyricsState>, + musicbrainz: State<'_, MusicBrainzSharedState>, + listenbrainz: State<'_, ListenBrainzSharedState>, +) -> Result<(), String> { + log::info!("Activating user session for user_id={}", user_id); + + // Set the active user for path resolution + user_paths.set_user(user_id); + + // Run one-time flat-to-user migration if needed + if let Err(e) = crate::migration::migrate_flat_to_user(user_id) { + log::error!("Migration failed: {}", e); + // Non-fatal: user gets a fresh slate if migration fails + } + + // Resolve user-scoped directories + let data_dir = user_paths.user_data_dir()?; + let cache_dir = user_paths.user_cache_dir()?; + + // Ensure directories exist + std::fs::create_dir_all(&data_dir) + .map_err(|e| format!("Failed to create user data dir: {}", e))?; + std::fs::create_dir_all(&cache_dir) + .map_err(|e| format!("Failed to create user cache dir: {}", e))?; + + log::info!("User data dir: {}", data_dir.display()); + log::info!("User cache dir: {}", cache_dir.display()); + + // Initialize all per-user states at the user directory. + // Data-dir stores: + session_store.init_at(&data_dir)?; + favorites_cache.init_at(&data_dir)?; + playback_prefs.init_at(&data_dir)?; + favorites_prefs.init_at(&data_dir)?; + audio_settings.init_at(&data_dir)?; + tray_settings.init_at(&data_dir)?; + remote_control_settings.init_at(&data_dir)?; + allowed_origins.init_at(&data_dir)?; + updates.init_at(&data_dir)?; + library.init_at(&data_dir)?; + reco.init_at(&data_dir)?; + api_cache.init_at(&data_dir)?; + artist_vectors.init_at(&data_dir)?; + blacklist.init_at(&data_dir)?; + offline.init_at(&data_dir)?; + musicbrainz.init_at(&data_dir)?; + listenbrainz.init_at(&data_dir)?; + + // Type-alias states (no init_at method — init inline) + init_type_alias_state(&*subscription_state, &data_dir, SubscriptionStateStore::new_at)?; + init_type_alias_state(&*download_settings, &data_dir, DownloadSettingsStore::new_at)?; + init_type_alias_state(&*legal_settings, &data_dir, LegalSettingsStore::new_at)?; + + // Cache-dir stores: + offline_cache.init_at(&cache_dir)?; + lyrics.init_at(&cache_dir)?; + + // Run deferred subscription purge check (was removed from startup) + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default() + .as_secs() as i64; + + let should_purge = { + let guard = subscription_state.lock().map_err(|e| format!("Lock error: {}", e))?; + guard.as_ref() + .and_then(|s| s.should_purge_offline_cache(now).ok()) + .unwrap_or(false) + }; + + if should_purge { + log::warn!("Subscription invalid for >3 days. Purging offline cache."); + if let Err(e) = crate::offline_cache::commands::purge_all_cached_files( + offline_cache.inner(), + library.inner(), + ).await { + log::error!("Failed to purge offline cache: {}", e); + } else { + let guard = subscription_state.lock().map_err(|e| format!("Lock error: {}", e))?; + if let Some(store) = guard.as_ref() { + let _ = store.mark_offline_cache_purged(now); + } + } + } + + log::info!("User session activated for user_id={}", user_id); + Ok(()) +} + +/// Deactivate the per-user session on logout. +/// +/// Tears down all per-user stores, closing database connections. +#[tauri::command] +pub async fn deactivate_user_session( + user_paths: State<'_, UserDataPaths>, + session_store: State<'_, SessionStoreState>, + favorites_cache: State<'_, FavoritesCacheState>, + subscription_state: State<'_, SubscriptionStateState>, + playback_prefs: State<'_, PlaybackPreferencesState>, + favorites_prefs: State<'_, FavoritesPreferencesState>, + download_settings: State<'_, DownloadSettingsState>, + audio_settings: State<'_, AudioSettingsState>, + tray_settings: State<'_, TraySettingsState>, + remote_control_settings: State<'_, RemoteControlSettingsState>, + allowed_origins: State<'_, AllowedOriginsState>, + legal_settings: State<'_, LegalSettingsState>, + updates: State<'_, UpdatesState>, + library: State<'_, LibraryState>, + reco: State<'_, RecoState>, + api_cache: State<'_, ApiCacheState>, + artist_vectors: State<'_, ArtistVectorStoreState>, + blacklist: State<'_, BlacklistState>, + offline: State<'_, OfflineState>, + offline_cache: State<'_, OfflineCacheState>, + lyrics: State<'_, LyricsState>, + musicbrainz: State<'_, MusicBrainzSharedState>, + listenbrainz: State<'_, ListenBrainzSharedState>, +) -> Result<(), String> { + log::info!("Deactivating user session"); + + // Teardown all per-user stores (closes DB connections) + session_store.teardown(); + favorites_cache.teardown()?; + playback_prefs.teardown()?; + favorites_prefs.teardown()?; + audio_settings.teardown()?; + tray_settings.teardown()?; + remote_control_settings.teardown()?; + allowed_origins.teardown()?; + updates.teardown(); + library.teardown(); + reco.teardown(); + api_cache.teardown(); + artist_vectors.teardown(); + blacklist.teardown(); + offline.teardown(); + offline_cache.teardown(); + lyrics.teardown(); + musicbrainz.teardown(); + listenbrainz.teardown(); + + // Type-alias states + teardown_type_alias_state(&*subscription_state); + teardown_type_alias_state(&*download_settings); + teardown_type_alias_state(&*legal_settings); + + // Clear the active user + user_paths.clear_user(); + + log::info!("User session deactivated"); + Ok(()) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 1b62fb6db..3fc6b2223 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -428,6 +428,9 @@ pub fn run() { commands::save_credentials, commands::clear_saved_credentials, commands::auto_login, + // User session lifecycle + commands::activate_user_session, + commands::deactivate_user_session, // Search commands commands::search_albums, commands::search_tracks,