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
@@ -0,0 +1,155 @@
|
||||
//! Changing the library (likes, dislikes, playlists), lyrics, and the
|
||||
//! server-side play queue that other devices can continue from.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::library::PlaylistId;
|
||||
|
||||
/// What the user has liked or disliked, by id, for drawing hearts and bans.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct LikedIds {
|
||||
pub albums: HashSet<i64>,
|
||||
pub artists: HashSet<i64>,
|
||||
pub playlists: HashSet<PlaylistId>,
|
||||
pub disliked_tracks: HashSet<String>,
|
||||
}
|
||||
|
||||
/// One line of lyrics; synced lyrics carry the moment it's sung.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LyricLine {
|
||||
pub at: Option<Duration>,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Lyrics {
|
||||
pub lines: Vec<LyricLine>,
|
||||
pub synced: bool,
|
||||
pub writers: Vec<String>,
|
||||
}
|
||||
|
||||
impl Lyrics {
|
||||
/// The line being sung at `position`: the last line that has started.
|
||||
pub fn current(&self, position: Duration) -> Option<usize> {
|
||||
if !self.synced {
|
||||
return None;
|
||||
}
|
||||
self.lines.iter().rposition(|l| l.at.is_some_and(|at| at <= position))
|
||||
}
|
||||
|
||||
/// Plain text: one line per line, blank lines kept as paragraph breaks.
|
||||
pub(crate) fn from_text(text: &str, writers: Vec<String>) -> Self {
|
||||
Self {
|
||||
lines: text.lines().map(|l| LyricLine { at: None, text: l.trim_end().to_owned() }).collect(),
|
||||
synced: false,
|
||||
writers,
|
||||
}
|
||||
}
|
||||
|
||||
/// LRC: `[mm:ss.xx] words`. Lines with several stamps repeat; metadata tags are dropped.
|
||||
pub(crate) fn from_lrc(lrc: &str, writers: Vec<String>) -> Self {
|
||||
let mut lines = Vec::new();
|
||||
for raw in lrc.lines() {
|
||||
let mut rest = raw.trim();
|
||||
let mut stamps = Vec::new();
|
||||
while let Some(stripped) = rest.strip_prefix('[') {
|
||||
let Some(end) = stripped.find(']') else { break };
|
||||
match parse_stamp(&stripped[..end]) {
|
||||
Some(at) => stamps.push(at),
|
||||
// `[ar:Artist]` and friends: not a lyric line.
|
||||
None => {
|
||||
stamps.clear();
|
||||
rest = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
rest = stripped[end + 1..].trim_start();
|
||||
}
|
||||
for at in stamps {
|
||||
lines.push(LyricLine { at: Some(at), text: rest.to_owned() });
|
||||
}
|
||||
}
|
||||
lines.sort_by_key(|l| l.at);
|
||||
Self { lines, synced: true, writers }
|
||||
}
|
||||
}
|
||||
|
||||
/// `mm:ss`, `mm:ss.x`, `mm:ss.xx` or `mm:ss.xxx`.
|
||||
fn parse_stamp(s: &str) -> Option<Duration> {
|
||||
let (min, sec) = s.split_once(':')?;
|
||||
let min: u64 = min.trim().parse().ok()?;
|
||||
let (whole, frac) = sec.split_once('.').unwrap_or((sec, ""));
|
||||
let whole: u64 = whole.parse().ok()?;
|
||||
if whole >= 60 || frac.len() > 3 || !frac.chars().all(|c| c.is_ascii_digit()) {
|
||||
return None;
|
||||
}
|
||||
let millis = if frac.is_empty() { 0 } else { frac.parse::<u64>().ok()? * 10u64.pow(3 - frac.len() as u32) };
|
||||
Some(Duration::from_millis((min * 60 + whole) * 1000 + millis))
|
||||
}
|
||||
|
||||
/// Where a server queue's tracks come from; other devices show it as "Playing from…".
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum QueueContext {
|
||||
Album(i64),
|
||||
Playlist(PlaylistId),
|
||||
Artist(i64),
|
||||
/// Liked tracks.
|
||||
MyMusic,
|
||||
/// Search results, the chart, anything else.
|
||||
Various,
|
||||
}
|
||||
|
||||
impl QueueContext {
|
||||
pub(crate) fn to_json(&self) -> serde_json::Value {
|
||||
let (kind, id) = match self {
|
||||
QueueContext::Album(id) => ("album", id.to_string()),
|
||||
QueueContext::Playlist(p) => ("playlist", format!("{}:{}", p.owner, p.kind)),
|
||||
QueueContext::Artist(id) => ("artist", id.to_string()),
|
||||
QueueContext::MyMusic => ("my_music", String::new()),
|
||||
QueueContext::Various => ("various", String::new()),
|
||||
};
|
||||
serde_json::json!({ "type": kind, "id": id, "description": "" })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn lrc_parses_stamps_and_drops_tags() {
|
||||
let l = Lyrics::from_lrc(
|
||||
"[ar:Кино]\n[00:12.50] Первая\n[00:20.1]Вторая\n[01:02.345][00:05.00] Повтор\n\nбез метки",
|
||||
vec![],
|
||||
);
|
||||
let got: Vec<(u64, &str)> = l.lines.iter().map(|x| (x.at.unwrap().as_millis() as u64, x.text.as_str())).collect();
|
||||
assert_eq!(got, [(5_000, "Повтор"), (12_500, "Первая"), (20_100, "Вторая"), (62_345, "Повтор")]);
|
||||
assert!(l.synced);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_line_follows_position() {
|
||||
let l = Lyrics::from_lrc("[00:10.00]a\n[00:20.00]b", vec![]);
|
||||
assert_eq!(l.current(Duration::from_secs(5)), None);
|
||||
assert_eq!(l.current(Duration::from_secs(10)), Some(0));
|
||||
assert_eq!(l.current(Duration::from_secs(25)), Some(1));
|
||||
let plain = Lyrics::from_text("a\nb", vec![]);
|
||||
assert_eq!(plain.current(Duration::from_secs(25)), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_stamps_are_not_lyrics() {
|
||||
assert_eq!(parse_stamp("01:02"), Some(Duration::from_secs(62)));
|
||||
assert_eq!(parse_stamp("1:75"), None);
|
||||
assert_eq!(parse_stamp("ti"), None);
|
||||
assert_eq!(parse_stamp("00:01.1234"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_contexts_match_the_api_shape() {
|
||||
let p = QueueContext::Playlist(PlaylistId { owner: 9, kind: 3 }).to_json();
|
||||
assert_eq!(p, serde_json::json!({"type": "playlist", "id": "9:3", "description": ""}));
|
||||
assert_eq!(QueueContext::MyMusic.to_json()["type"], "my_music");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user