feat(playlist): add ability to copy Qobuz playlists to user library

When viewing a playlist not owned by the current user, a new "Copy to
My Library" button appears between the favorite and context menu buttons.
Clicking it creates a copy of the playlist with all its tracks in the
user's library.

- Add get_current_user_id command to check playlist ownership
- Add subscribe_playlist command to copy playlist with all tracks
- Add ListPlus button in PlaylistDetailView for non-owned playlists
- Button only shows for playlists owned by others (e.g., Qobuz curated)
This commit is contained in:
vicrodh committed 2026-01-25 01:34:43 -06:00
1 parent a276a30ff2
commit ee31c3e445
4 files changed
+105 -2

No files matched your search

+5
View File
@@ -177,6 +177,11 @@ impl QobuzClient {
}) })
} }
/// Get current user ID
pub async fn get_user_id(&self) -> Option<u64> {
self.session.read().await.as_ref().map(|s| s.user_id)
}
/// Get user auth token header value /// Get user auth token header value
async fn auth_token(&self) -> Result<String> { async fn auth_token(&self) -> Result<String> {
self.session self.session
+56
View File
@@ -155,3 +155,59 @@ pub async fn get_tracks_by_ids(
Ok(tracks) Ok(tracks)
} }
/// Get the current user's Qobuz ID
#[tauri::command]
pub async fn get_current_user_id(state: State<'_, AppState>) -> Result<Option<u64>, String> {
log::info!("Command: get_current_user_id");
let client = state.client.lock().await;
Ok(client.get_user_id().await)
}
/// Subscribe to (copy) a playlist to the user's library
/// This creates a new playlist with all the tracks from the source playlist
#[tauri::command]
pub async fn subscribe_playlist(
playlist_id: u64,
state: State<'_, AppState>,
) -> Result<Playlist, String> {
log::info!("Command: subscribe_playlist {}", playlist_id);
let client = state.client.lock().await;
// 1. Get the source playlist with all its tracks
let source = client
.get_playlist(playlist_id)
.await
.map_err(|e| format!("Failed to get source playlist: {}", e))?;
// 2. Extract track IDs from the source playlist
let track_ids: Vec<u64> = source
.tracks
.as_ref()
.map(|t| t.items.iter().map(|track| track.id).collect())
.unwrap_or_default();
if track_ids.is_empty() {
return Err("Source playlist has no tracks to copy".to_string());
}
// 3. Create a new playlist with the same name
let new_playlist = client
.create_playlist(&source.name, source.description.as_deref(), false)
.await
.map_err(|e| format!("Failed to create new playlist: {}", e))?;
// 4. Add all tracks to the new playlist
client
.add_tracks_to_playlist(new_playlist.id, &track_ids)
.await
.map_err(|e| format!("Failed to add tracks to new playlist: {}", e))?;
// 5. Return the new playlist with updated track count
Ok(Playlist {
tracks_count: track_ids.len() as u32,
..new_playlist
})
}
+2
View File
@@ -482,6 +482,8 @@ pub fn run() {
commands::remove_tracks_from_playlist, commands::remove_tracks_from_playlist,
commands::update_playlist, commands::update_playlist,
commands::get_tracks_by_ids, commands::get_tracks_by_ids,
commands::get_current_user_id,
commands::subscribe_playlist,
// Playlist import commands // Playlist import commands
commands::playlist_import_preview, commands::playlist_import_preview,
commands::playlist_import_execute, commands::playlist_import_execute,
@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { ArrowLeft, Play, Shuffle, ListMusic, Search, X, ChevronDown, ChevronRight, ImagePlus, Edit3, BarChart2, Heart, CloudDownload } from 'lucide-svelte'; import { ArrowLeft, Play, Shuffle, ListMusic, Search, X, ChevronDown, ChevronRight, ImagePlus, Edit3, BarChart2, Heart, CloudDownload, ListPlus } from 'lucide-svelte';
import AlbumMenu from '../AlbumMenu.svelte'; import AlbumMenu from '../AlbumMenu.svelte';
import PlaylistCollage from '../PlaylistCollage.svelte'; import PlaylistCollage from '../PlaylistCollage.svelte';
import PlaylistModal from '../PlaylistModal.svelte'; import PlaylistModal from '../PlaylistModal.svelte';
@@ -201,14 +201,26 @@
let editModalOpen = $state(false); let editModalOpen = $state(false);
let isFavorite = $state(false); let isFavorite = $state(false);
// User ownership state (to show "Copy to Library" button for non-owned playlists)
let currentUserId = $state<number | null>(null);
let isOwnPlaylist = $derived(playlist !== null && currentUserId !== null && playlist.owner.id === currentUserId);
let isCopying = $state(false);
async function scrollToTrack(trackId: number) { async function scrollToTrack(trackId: number) {
await tick(); await tick();
const target = scrollContainer?.querySelector<HTMLElement>(`[data-track-id="${trackId}"]`); const target = scrollContainer?.querySelector<HTMLElement>(`[data-track-id="${trackId}"]`);
target?.scrollIntoView({ block: 'center' }); target?.scrollIntoView({ block: 'center' });
} }
// Subscribe to offline status changes // Subscribe to offline status changes and fetch current user ID
onMount(() => { onMount(() => {
// Fetch current user ID for ownership check
invoke<number | null>('get_current_user_id').then(userId => {
currentUserId = userId;
}).catch(err => {
console.warn('Failed to get current user ID:', err);
});
const unsubscribe = subscribeOffline(() => { const unsubscribe = subscribeOffline(() => {
offlineStatus = getOfflineStatus(); offlineStatus = getOfflineStatus();
// Re-check local copies when offline status changes // Re-check local copies when offline status changes
@@ -475,6 +487,23 @@
} }
} }
async function copyPlaylistToLibrary() {
if (isCopying || !playlist) return;
isCopying = true;
try {
const newPlaylist = await invoke<Playlist>('subscribe_playlist', { playlistId: playlist.id });
// Notify parent to refresh sidebar
onPlaylistUpdated?.();
// Show success by navigating to the new playlist or show a toast
console.log('Playlist copied successfully:', newPlaylist);
} catch (err) {
console.error('Failed to copy playlist:', err);
} finally {
isCopying = false;
}
}
async function saveSort(field: SortField, order: SortOrder) { async function saveSort(field: SortField, order: SortOrder) {
sortBy = field; sortBy = field;
sortOrder = order; sortOrder = order;
@@ -1018,6 +1047,17 @@
fill={isFavorite ? 'var(--accent-primary)' : 'none'} fill={isFavorite ? 'var(--accent-primary)' : 'none'}
/> />
</button> </button>
{#if !isOwnPlaylist && playlist}
<button
class="action-btn-circle"
class:is-loading={isCopying}
onclick={copyPlaylistToLibrary}
disabled={isCopying}
title="Copy to My Library"
>
<ListPlus size={18} />
</button>
{/if}
<AlbumMenu <AlbumMenu
onPlayNext={handlePlayAllNext} onPlayNext={handlePlayAllNext}
onPlayLater={handlePlayAllLater} onPlayLater={handlePlayAllLater}