feat: likes, track actions, playlist editing, lyrics and queue sync
- ym-api: like/unlike tracks, albums, artists and playlists; dislike tracks; liked ids for drawing state; create, rename, publish/hide and delete playlists; add, remove and move tracks (fresh revision per edit); lyrics (LRC parsed, plain text fallback); server queues - Hearts on every track row, the player bar and album, artist and playlist pages, applied at once and re-read if Yandex refuses - Track menu: Play next (moves a queued track up rather than duplicating it; the playing track's menu offers Next track instead), Add to queue, like, Add to playlist or a new one, Go to album/artist, Don't recommend, and Move up/down/Remove on the user's own playlists - New playlist from the sidebar; Rename, Make public/private and Delete (confirmed) on own playlist pages; short status messages for every edit - Lyrics panel highlights and follows the sung line and seeks on click - Lists played are published as Yandex queues and their position kept - lyricsInfo decides lyrics availability (the old flag over-reports) - MUSYX_TRACE=1 logs user actions with the resulting queue - Headless UI tests (iced_test) click through menus over the full window
This commit is contained in:
1 parent
99f795b500
commit
e2312f2d40
22 files changed
+1733
-34
No files matched your search
@@ -5,12 +5,20 @@ use crate::library::{
|
||||
AlbumPage, AlbumSummary, ArtistPage, ArtistSummary, LibraryPlaylists, PlaylistId, PlaylistPage,
|
||||
PlaylistSummary,
|
||||
};
|
||||
use crate::edit::{LikedIds, Lyrics, QueueContext};
|
||||
use crate::discover::{ChartEntry, Genre, Home, SearchKind, SearchResults};
|
||||
use crate::models::{Stream, Track, best_variant};
|
||||
use crate::wave::{PlayReport, Station, WaveBatch, WaveEvent, WaveSettings, iso_millis, numeric_id};
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
/// Playlist diffs need numeric track and album ids.
|
||||
fn diff_ids(track: &Track) -> Result<(i64, i64)> {
|
||||
let id = crate::wave::numeric_id(&track.id).ok_or_else(|| Error::Api("this track can't go in a playlist".into()))?;
|
||||
let album = track.album.as_ref().map(|a| a.id).ok_or_else(|| Error::Api("track has no album".into()))?;
|
||||
Ok((id, album))
|
||||
}
|
||||
|
||||
/// How many featured podcasts to resolve.
|
||||
pub const FEATURED_PODCASTS: usize = 48;
|
||||
|
||||
@@ -410,6 +418,175 @@ impl YmClient {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---- Likes ----
|
||||
|
||||
/// Track ids are `full_id`s or plain ids; Yandex accepts both.
|
||||
pub async fn set_track_liked(&self, track_id: &str, liked: bool) -> Result<()> {
|
||||
self.inner.set_liked(yamuse::Likeable::Track, [track_id], liked, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// "Don't recommend": the track stops appearing in waves and mixes.
|
||||
pub async fn set_track_disliked(&self, track_id: &str, disliked: bool) -> Result<()> {
|
||||
self.inner.set_disliked(yamuse::Dislikeable::Track, [track_id], disliked, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_album_liked(&self, id: i64, liked: bool) -> Result<()> {
|
||||
self.inner.set_liked(yamuse::Likeable::Album, [id], liked, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_artist_liked(&self, id: i64, liked: bool) -> Result<()> {
|
||||
self.inner.set_liked(yamuse::Likeable::Artist, [id], liked, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_playlist_liked(&self, id: PlaylistId, liked: bool) -> Result<()> {
|
||||
let key = format!("{}:{}", id.owner, id.kind);
|
||||
self.inner.set_liked(yamuse::Likeable::Playlist, [key], liked, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Ids only (no details), for drawing like state on every page.
|
||||
pub async fn liked_ids(&self) -> Result<LikedIds> {
|
||||
let albums = self.inner.liked_albums(None).await?;
|
||||
let artists = self.inner.liked_artists(None).await?;
|
||||
let playlists = self.inner.liked_playlists(None).await?;
|
||||
let disliked = self.inner.disliked_tracks(None).await?;
|
||||
Ok(LikedIds {
|
||||
albums: albums
|
||||
.into_iter()
|
||||
.filter_map(|l| l.album.and_then(|a| a.id).or_else(|| l.id.and_then(|i| i.as_number())))
|
||||
.collect(),
|
||||
artists: artists.into_iter().filter_map(|a| a.id.and_then(|i| i.as_number())).collect(),
|
||||
playlists: playlists
|
||||
.into_iter()
|
||||
.filter_map(|l| {
|
||||
let p = l.playlist?;
|
||||
Some(PlaylistId { owner: p.uid.or_else(|| p.owner.and_then(|o| o.uid))?, kind: p.kind? })
|
||||
})
|
||||
.collect(),
|
||||
disliked_tracks: disliked
|
||||
.tracks
|
||||
.into_iter()
|
||||
.filter_map(|t| match t.id? {
|
||||
yamuse::models::Id::Number(n) => Some(n.to_string()),
|
||||
yamuse::models::Id::Text(s) => s.split(':').next().map(str::to_owned),
|
||||
})
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
|
||||
// ---- Playlists ----
|
||||
|
||||
pub async fn create_playlist(&self, title: &str, public: bool) -> Result<PlaylistSummary> {
|
||||
let visibility = if public { yamuse::Visibility::Public } else { yamuse::Visibility::Private };
|
||||
let p = self.inner.create_playlist(title, visibility).await?;
|
||||
PlaylistSummary::from_yamuse(&p).ok_or_else(|| Error::Api("created playlist has no id".into()))
|
||||
}
|
||||
|
||||
pub async fn rename_playlist(&self, id: PlaylistId, title: &str) -> Result<()> {
|
||||
self.inner.rename_playlist(id.kind, title).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_playlist_public(&self, id: PlaylistId, public: bool) -> Result<()> {
|
||||
let visibility = if public { yamuse::Visibility::Public } else { yamuse::Visibility::Private };
|
||||
self.inner.set_playlist_visibility(id.kind, visibility).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_playlist(&self, id: PlaylistId) -> Result<()> {
|
||||
self.inner.delete_playlist(id.kind).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The playlist's revision and length, which every edit needs fresh.
|
||||
async fn revision(&self, id: PlaylistId) -> Result<(i64, usize)> {
|
||||
let p = self.inner.user_playlist(id.kind, Some(id.owner)).await?;
|
||||
let len = p.track_count.map_or(p.tracks.len(), |n| n.max(0) as usize);
|
||||
Ok((p.revision.unwrap_or(0), len))
|
||||
}
|
||||
|
||||
/// Appends a track to the end of one of the user's playlists.
|
||||
pub async fn add_to_playlist(&self, id: PlaylistId, track: &Track) -> Result<()> {
|
||||
let (track_id, album_id) = diff_ids(track)?;
|
||||
let (revision, len) = self.revision(id).await?;
|
||||
self.inner.add_track_to_playlist(id.kind, track_id, album_id, len, revision).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Removes the track at `index`.
|
||||
pub async fn remove_from_playlist(&self, id: PlaylistId, index: usize) -> Result<()> {
|
||||
let (revision, _) = self.revision(id).await?;
|
||||
self.inner.remove_tracks_from_playlist(id.kind, index, index + 1, revision).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Moves the track at `from` so it ends up at `to`.
|
||||
pub async fn move_in_playlist(&self, id: PlaylistId, track: &Track, from: usize, to: usize) -> Result<()> {
|
||||
let (track_id, album_id) = diff_ids(track)?;
|
||||
let (revision, _) = self.revision(id).await?;
|
||||
let diff = yamuse::Difference::new()
|
||||
.delete(from, from + 1)
|
||||
.insert(to, [yamuse::DiffTrack::new(track_id, album_id)]);
|
||||
self.inner.change_playlist(id.kind, &diff, revision).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---- Lyrics ----
|
||||
|
||||
/// Synced lyrics when Yandex has them, else plain text; `None` when there are none.
|
||||
pub async fn lyrics(&self, track_id: &str) -> Result<Option<Lyrics>> {
|
||||
for format in [yamuse::LyricsFormat::Lrc, yamuse::LyricsFormat::Text] {
|
||||
let meta = match self.inner.track_lyrics(track_id, format).await {
|
||||
Ok(meta) => meta,
|
||||
Err(yamuse::Error::NotFound(_)) => continue,
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
let text = meta.text(self.inner.transport()).await?;
|
||||
if text.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let writers = meta.writers.clone();
|
||||
return Ok(Some(match format {
|
||||
yamuse::LyricsFormat::Lrc => Lyrics::from_lrc(&text, writers),
|
||||
yamuse::LyricsFormat::Text => Lyrics::from_text(&text, writers),
|
||||
}));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
// ---- Server queue ----
|
||||
|
||||
/// Publishes what's playing so other devices can continue it. Returns the queue id.
|
||||
pub async fn create_queue(&self, context: &QueueContext, tracks: &[Track], current: usize) -> Result<Option<String>> {
|
||||
let tracks: Vec<serde_json::Value> = tracks
|
||||
.iter()
|
||||
.map(|t| {
|
||||
serde_json::json!({
|
||||
"trackId": t.id.split(':').next().unwrap_or(&t.id),
|
||||
"albumId": t.album.as_ref().map(|a| a.id.to_string()).unwrap_or_default(),
|
||||
"from": "musyx-desktop",
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let body = serde_json::json!({
|
||||
"context": context.to_json(),
|
||||
"tracks": tracks,
|
||||
"currentIndex": current,
|
||||
"from": "musyx-desktop",
|
||||
"isInteractive": true,
|
||||
});
|
||||
Ok(self.inner.create_queue(&body, None).await?)
|
||||
}
|
||||
|
||||
pub async fn update_queue_position(&self, queue_id: &str, index: usize) -> Result<()> {
|
||||
self.inner.update_queue_position(queue_id, index as i64, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Downloads an image (covers are a few kilobytes).
|
||||
pub async fn image(&self, url: &str) -> Result<Vec<u8>> {
|
||||
Ok(yamuse::download::Downloader::new(self.inner.transport(), [url.to_owned()])
|
||||
|
||||
Reference in new issue
Block a user