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:
1 parent
a276a30ff2
commit
ee31c3e445
4 files changed
+105
-2
No files matched your search
@@ -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
|
||||
async fn auth_token(&self) -> Result<String> {
|
||||
self.session
|
||||
|
||||
@@ -155,3 +155,59 @@ pub async fn get_tracks_by_ids(
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
@@ -482,6 +482,8 @@ pub fn run() {
|
||||
commands::remove_tracks_from_playlist,
|
||||
commands::update_playlist,
|
||||
commands::get_tracks_by_ids,
|
||||
commands::get_current_user_id,
|
||||
commands::subscribe_playlist,
|
||||
// Playlist import commands
|
||||
commands::playlist_import_preview,
|
||||
commands::playlist_import_execute,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<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 PlaylistCollage from '../PlaylistCollage.svelte';
|
||||
import PlaylistModal from '../PlaylistModal.svelte';
|
||||
@@ -201,14 +201,26 @@
|
||||
let editModalOpen = $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) {
|
||||
await tick();
|
||||
const target = scrollContainer?.querySelector<HTMLElement>(`[data-track-id="${trackId}"]`);
|
||||
target?.scrollIntoView({ block: 'center' });
|
||||
}
|
||||
|
||||
// Subscribe to offline status changes
|
||||
// Subscribe to offline status changes and fetch current user ID
|
||||
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(() => {
|
||||
offlineStatus = getOfflineStatus();
|
||||
// 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) {
|
||||
sortBy = field;
|
||||
sortOrder = order;
|
||||
@@ -1018,6 +1047,17 @@
|
||||
fill={isFavorite ? 'var(--accent-primary)' : 'none'}
|
||||
/>
|
||||
</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
|
||||
onPlayNext={handlePlayAllNext}
|
||||
onPlayLater={handlePlayAllLater}
|
||||
|
||||
Reference in new issue
Block a user