From d3695d00d6a5d130e3146686a836ed001cc29af7 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Sat, 14 Nov 2026 10:19:39 +0100 Subject: [PATCH] feat(smart): smart playlists by the year a song came out I can now ask for songs from a year to a year, for example the eighties. - The year is read from the tags of each file (the original date first, then the date) and kept on the track: migration 0028 adds tracks.year (SQLite and PostgreSQL). - Two new rules, min_year and max_year, both included and checked (1000 to 2999, the first not after the second). The index answers them. A song with no year never matches. - Files indexed before have no year until a full rescan reads their tags again; the editor and the API description say so. - The smart playlist editor gets From year and Up to year, and the rules are described in words. - English and Italian. --- README.md | 2 +- internal/api/http/smart_test.go | 29 +++++++++++++---- internal/api/openapi/openapi.yaml | 2 ++ internal/library/taglib.go | 17 ++++++++++ internal/library/taglib_test.go | 26 ++++++++++++++++ internal/library/types.go | 7 +++++ internal/smart/smart.go | 22 ++++++++++++- internal/smart/smart_test.go | 33 ++++++++++++++++++++ internal/storage/library.go | 5 +-- internal/storage/library_test.go | 41 +++++++++++++++++++++++++ internal/storage/smart.go | 6 ++++ internal/storage/smart_test.go | 33 ++++++++++++++++++++ internal/storage/storage_test.go | 4 +-- migrations/postgres/0028_track_year.sql | 3 ++ migrations/sqlite/0028_track_year.sql | 4 +++ web/src/api/schema.d.ts | 4 +++ web/src/i18n/locales/en.json | 8 ++++- web/src/i18n/locales/it.json | 8 ++++- web/src/pages/SmartEditorPage.tsx | 9 ++++++ web/src/pages/smart.test.tsx | 17 +++++++++- web/src/pages/smartRules.ts | 9 +++++- 21 files changed, 273 insertions(+), 16 deletions(-) create mode 100644 migrations/postgres/0028_track_year.sql create mode 100644 migrations/sqlite/0028_track_year.sql diff --git a/README.md b/README.md index d124040..c7d3472 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr - **Artist photos.** `GET /artists/{id}/image` serves a free picture of an artist from Wikimedia Commons, and `GET /artists/{id}/image/credit` says whose it is and under which licence, which you must show next to it. Only free licences are kept (public domain, CC0, CC BY, CC BY-SA): non-commercial, no-derivatives, fair-use and unknown ones are refused, because Sonda stores a copy (`?size=small` is 300 pixels, `large` 1000) in the data folder. With the `commons` provider and the `musicbrainz` one switched on, Sonda follows ids and never a name: the artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the picture Wikidata names for it, and Commons for its licence. Many artists will have none. Kept for 30 days (a week when there is none) and removed, files included, with the provider's data. To look for the pictures of the whole library in one go, an admin starts a background job like a scan: `POST /library/artist-images` (follow it with `GET /library/artist-images`, stop it with `DELETE /library/artist-images`; `?refresh=true` searches every artist again). It only goes through artists that have a MusicBrainz id, never replaces a picture by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). - **Album covers.** `GET /albums/{id}/cover` serves the cover of an album from your own files: the picture embedded in them, or else a `cover`, `folder`, `front`, `album` or `albumart` image in the album's folder. It is built the first time it is asked for (a few at a time), scaled (`?size=small` is 300 pixels, `large` 1000) and kept in the data folder (`storage.data_dir`, you can delete it: covers are built again). A picture you replace in a folder is noticed within a week, or at once after a full rescan. When your files have no picture at all and an admin switched on the `coverartarchive` provider, the cover is asked from the Cover Art Archive by the MusicBrainz release group id that the tags of the album's files carry (`MUSICBRAINZ_RELEASEGROUPID`; run a full rescan to read it for music you already indexed); your own files always come first, and only that id leaves the server. An album whose files have no id is looked up in MusicBrainz by its artist and title when the `musicbrainz` provider is on too (see the list of what leaves your server). For an album split in `CD1`/`Disc 2` folders, the picture in the folder above is used. Nothing is sent anywhere unless you switched on the `coverartarchive` provider (see [External providers](#external-providers)). It also takes `?access_token=`, so an `` element can use it. To build the covers of the whole library in one go instead of one album at a time, an admin starts a background job like a scan: `POST /library/covers` (follow it with `GET /library/covers`, stop it with `DELETE /library/covers`; `?refresh=true` searches every album again; it never replaces a cover by "no cover" because a disk was not mounted, and keeps the covers that came from the archive if you switched it off). - **Mixes.** `GET /mixes` lists playlists that Sonda makes for you, like the mixes of a streaming service, from what you listen to and from the genres of your music: one mix for each genre of your library that has at least 30 songs by 4 artists (the genres you play most first), and, once you have some listening history, *Your favourites* and *Rediscover* (artists you like and have not played for a month); *New to you* is the songs you never played. `GET /mixes/{id}` gives a mix of up to 50 songs, at most 3 by one artist and never two of the same artist in a row. They are picked by weighted chance: the artists you play weigh more and songs you skipped lately or artists you avoid are left out. A mix stays the same for a day, and `?variant=2` gives another; `POST /mixes/{id}/playlist` keeps the one you are looking at as a playlist of your own. A mix is worked out when you ask for it, from your own history and the genre tags of your files: nothing is stored and nothing is sent to a provider. The id of a genre mix has the genre in it (`genre:hard rock`), so it is percent-encoded in the path. Each mix says which albums stand for it (`album_ids`: up to 4 albums that have a cover, by different artists, with most of the songs the mix draws from), for a picture made of their covers; the covers are the ones the library already has, so a mix has a picture once the covers of its albums were looked up (browsing the albums, or the covers job), and none is fetched for it. -- **Smart playlists.** A smart playlist is a set of rules instead of a list of songs, worked out again every time you open it, so new songs and your listening change it by themselves. `POST /smart-playlists` keeps a name, the rules, an order (`random`, `most_played`, `least_played`, `recently_added` or `title`) and a limit (50 by default, 500 at most); `GET /smart-playlists/{id}/tracks` gives the songs now. Every rule that is set must hold: genres, artists and albums (is, or is not), file formats and a minimum bitrate (about the same file), added in the last N days (by the date of the file), never played, played or not played in the last N days, played at least N times (optionally counting only the plays of the last N days, which is what makes the songs in rotation lately and the top of the year), skipped or not skipped lately, only artists you like or without the ones you avoid, and the length of the song. `POST /smart-playlists/preview` tries rules without keeping them, and `POST /smart-playlists/{id}/playlist` keeps what you see as a normal playlist (a copy). Only the rules are stored; the songs come from the files and from your own history, nothing is sent to a provider, and a smart playlist is yours alone, and `GET /genres` lists the genres to choose from. A random order stays the same for a day, and `?variant=2` gives another. `GET /smart-playlists/{id}/covers` and `GET /smart-playlists/suggested/{key}/covers` say which albums stand for one (up to 4 that have a cover, in the order of the songs it gives now), for its picture; they are apart from the songs so that the list of smart playlists shows at once and each card fills in its cover when it arrives. Some are there without anybody making them: `GET /smart-playlists/suggested` lists a fixed set (`recently-added`, `on-rotation`, `top-year`, `most-played`, `never-played`, `forgotten`, `high-quality`, `long-tracks`), the same rules for everyone and worked out on your own library and history, nothing stored; `GET /smart-playlists/suggested/{key}/tracks` gives their songs and `POST /smart-playlists/suggested/{key}/playlist` keeps them as a playlist, and `on-rotation` is the songs you played most in the last 30 days and `top-year` the same over a year, so they change as you listen; a copy of their rules becomes a smart playlist of your own with `POST /smart-playlists`. The year of a song is not in the index yet, so there is no rule about it. A user can have 200. +- **Smart playlists.** A smart playlist is a set of rules instead of a list of songs, worked out again every time you open it, so new songs and your listening change it by themselves. `POST /smart-playlists` keeps a name, the rules, an order (`random`, `most_played`, `least_played`, `recently_added` or `title`) and a limit (50 by default, 500 at most); `GET /smart-playlists/{id}/tracks` gives the songs now. Every rule that is set must hold: genres, artists and albums (is, or is not), file formats and a minimum bitrate (about the same file), added in the last N days (by the date of the file), never played, played or not played in the last N days, played at least N times (optionally counting only the plays of the last N days, which is what makes the songs in rotation lately and the top of the year), skipped or not skipped lately, only artists you like or without the ones you avoid, the length of the song, and the year it came out (`min_year` and `max_year`, both included: 1980 to 1989 is the eighties). The year comes from the tags of the file (the original date first, then the date); a song with no year never matches a year rule, and the files indexed before this existed have none until a full rescan (`POST /library/scan?full=true`) reads their tags again. `POST /smart-playlists/preview` tries rules without keeping them, and `POST /smart-playlists/{id}/playlist` keeps what you see as a normal playlist (a copy). Only the rules are stored; the songs come from the files and from your own history, nothing is sent to a provider, and a smart playlist is yours alone, and `GET /genres` lists the genres to choose from. A random order stays the same for a day, and `?variant=2` gives another. `GET /smart-playlists/{id}/covers` and `GET /smart-playlists/suggested/{key}/covers` say which albums stand for one (up to 4 that have a cover, in the order of the songs it gives now), for its picture; they are apart from the songs so that the list of smart playlists shows at once and each card fills in its cover when it arrives. Some are there without anybody making them: `GET /smart-playlists/suggested` lists a fixed set (`recently-added`, `on-rotation`, `top-year`, `most-played`, `never-played`, `forgotten`, `high-quality`, `long-tracks`), the same rules for everyone and worked out on your own library and history, nothing stored; `GET /smart-playlists/suggested/{key}/tracks` gives their songs and `POST /smart-playlists/suggested/{key}/playlist` keeps them as a playlist, and `on-rotation` is the songs you played most in the last 30 days and `top-year` the same over a year, so they change as you listen; a copy of their rules becomes a smart playlist of your own with `POST /smart-playlists`. The year of a song is not in the index yet, so there is no rule about it. A user can have 200. - **Playlists.** `POST /playlists` makes a playlist (a name and an optional description) and `GET /playlists` lists yours, the one changed last first. `POST /playlists/{id}/tracks` adds 1 to 500 tracks at the end or at a `position`, `GET /playlists/{id}/tracks` lists them in order, `PATCH /playlists/{id}/tracks/{item}` moves one and `DELETE` takes one out; `PATCH /playlists/{id}` renames and `DELETE /playlists/{id}` deletes (the tracks stay in the library). A track can be in a playlist more than once, which is why each place has its own item id. A playlist is yours alone: someone else's answers `playlist_not_found`, as one that does not exist. A track whose files were deleted or moved away stays in the playlist and shows `available: false`; it plays again when the files come back and a scan finds them. Each playlist says which albums stand for it (`album_ids`: up to 4 albums that have a cover, in the order they first appear in it, each once), for a picture made of their covers; nothing is fetched for it. A user can have 1000 playlists of up to 10000 tracks. Nothing is shared and nothing leaves your server. - **Wishlist and "not interested".** `PUT /wishlist/{artist}` puts an artist on your wishlist (`GET /wishlist` lists it, `DELETE` takes it off), and `POST /discover/{artist}/dismiss` hides an artist from your Discover for good (`DELETE` brings it back, `GET /discover/dismissed` lists them). An artist is wished or dismissed, never both, and both lists are yours alone. Discover marks the wished artists it shows (`wishlisted`) and never suggests a dismissed one. The wishlist is a list for you and sends nothing anywhere; Sonda downloads nothing, but an admin can send an artist that Discover showed to their own Lidarr (see [External providers](#external-providers)), which works for an artist that has a MusicBrainz id and that Sonda knows of from a list or from your files. An artist that only Last.fm named is kept while you have it marked (your lists still show it, but other users cannot see that it exists), and goes with the rest of Last.fm's data if an admin removes Last.fm's data. Removing MusicBrainz's data does not touch it. - **Your own settings.** `GET` and `PATCH /me/settings` hold choices that belong to you, not to the server. Everyone starts private: `discover_from_listening` is off, so this setting never sends your listening to a provider. When you turn it on, Discover may start from the artists you listen to most, and their names are sent to Last.fm (only if an admin switched it on). What Last.fm answers is kept in the shared database like any other lookup, so another local user can see, from `retrieved_at` of `GET /artists/{id}/similar`, that someone looked up an artist, but not who. diff --git a/internal/api/http/smart_test.go b/internal/api/http/smart_test.go index 77abd7a..07dfb42 100644 --- a/internal/api/http/smart_test.go +++ b/internal/api/http/smart_test.go @@ -91,12 +91,14 @@ func TestASmartPlaylistIsMadeReadChangedAndDeleted(t *testing.T) { func TestWhatIsNotAcceptedIsSaidSo(t *testing.T) { m := newMixAPI(t) for name, body := range map[string]map[string]any{ - "no name": {"rules": map[string]any{}}, - "an unknown order": {"name": "a", "sort": "loudest"}, - "a limit too big": {"name": "a", "limit": 501}, - "days below zero": {"name": "a", "rules": map[string]any{"played_within_days": -1}}, - "a genre that isn't": {"name": "a", "rules": map[string]any{"genres": []string{"unknown"}}}, - "a bad format": {"name": "a", "rules": map[string]any{"formats": []string{"fl/ac"}}}, + "no name": {"rules": map[string]any{}}, + "an unknown order": {"name": "a", "sort": "loudest"}, + "a limit too big": {"name": "a", "limit": 501}, + "days below zero": {"name": "a", "rules": map[string]any{"played_within_days": -1}}, + "a genre that isn't": {"name": "a", "rules": map[string]any{"genres": []string{"unknown"}}}, + "a bad format": {"name": "a", "rules": map[string]any{"formats": []string{"fl/ac"}}}, + "a year too early": {"name": "a", "rules": map[string]any{"min_year": 99}}, + "years the wrong way": {"name": "a", "rules": map[string]any{"min_year": 1990, "max_year": 1980}}, } { if r := do(t, m.h, "POST", sp, m.alice, body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Errorf("%s: %d %v", name, r.Code, r.Body) @@ -112,6 +114,21 @@ func TestWhatIsNotAcceptedIsSaidSo(t *testing.T) { } } +func TestTheYearRulesAreKeptAndShown(t *testing.T) { + m := newMixAPI(t) + id := smartMade(t, m, map[string]any{"name": "Eighties", "rules": map[string]any{"min_year": 1980, "max_year": 1989}}) + r := do(t, m.h, "GET", fmt.Sprintf("%s/%d", sp, id), m.alice, nil) + rules, _ := r.Body["rules"].(map[string]any) + if r.Code != http.StatusOK || rules["min_year"] != float64(1980) || rules["max_year"] != float64(1989) { + t.Errorf("get: %d %v", r.Code, r.Body) + } + // None of the songs of this library has a year, so the rule leaves none. + tr := do(t, m.h, "GET", fmt.Sprintf("%s/%d/tracks", sp, id), m.alice, nil) + if tr.Code != http.StatusOK || len(trackIDs(t, tr)) != 0 { + t.Errorf("tracks: %d %v", tr.Code, tr.Body) + } +} + func TestTheSongsAreWorkedOutFromTheRules(t *testing.T) { m := newMixAPI(t) id := smartMade(t, m, map[string]any{"name": "Hard", "sort": "title", "limit": 5, "rules": map[string]any{"genres": []string{"hard rock"}}}) diff --git a/internal/api/openapi/openapi.yaml b/internal/api/openapi/openapi.yaml index d138bdf..981b6e7 100644 --- a/internal/api/openapi/openapi.yaml +++ b/internal/api/openapi/openapi.yaml @@ -4939,6 +4939,8 @@ components: without_avoided_artists: {type: boolean, description: Leaves out songs by artists you avoid.} min_duration_sec: {type: integer, minimum: 0, maximum: 86400} max_duration_sec: {type: integer, minimum: 0, maximum: 86400} + min_year: {type: integer, minimum: 0, maximum: 2999, description: "The song came out in this year or later (0 or a year from 1000). The year comes from the tags of the file, the original date first. A song whose file has no year never matches a year rule, and the files indexed before this rule existed have none until a full rescan (`POST /library/scan?full=true`) reads their tags again."} + max_year: {type: integer, minimum: 0, maximum: 2999, description: "The song came out in this year or earlier. With `min_year` set it cannot be lower than it. See `min_year`."} SuggestedSmartPlaylist: type: object diff --git a/internal/library/taglib.go b/internal/library/taglib.go index 2c41324..d936423 100644 --- a/internal/library/taglib.go +++ b/internal/library/taglib.go @@ -58,6 +58,7 @@ func (TaglibReader) Read(path string) (Metadata, error) { AlbumMBID: albumID, TrackNo: leadingInt(first(tags, "TRACKNUMBER")), DiscNo: leadingInt(first(tags, "DISCNUMBER")), + Year: yearOf(tags), }, Properties: Properties{ Format: strings.ToLower(props.Format), @@ -81,6 +82,22 @@ func (TaglibReader) Read(path string) (Metadata, error) { return m, nil } +// yearOf reads the year a song came out: the original date when the file has one (a reissue says when it was +// reissued in DATE), then the date. Both are written as 1997, 1997-05-21 or 1997-05-21T10:00:00; anything that does +// not start with four digits that make a plausible year is no year. +func yearOf(tags map[string][]string) int { + for _, key := range []string{"ORIGINALDATE", "ORIGINALYEAR", "DATE", "YEAR"} { + v := strings.TrimSpace(first(tags, key)) + if len(v) < 4 { + continue + } + if y, err := strconv.Atoi(v[:4]); err == nil && y >= MinYear && y <= MaxYear && (len(v) == 4 || v[4] < '0' || v[4] > '9') { + return y + } + } + return 0 +} + // maxGenreLen bounds one genre; a longer one is a sentence, not a genre. const maxGenreLen = 100 diff --git a/internal/library/taglib_test.go b/internal/library/taglib_test.go index 8c50897..41c3f0d 100644 --- a/internal/library/taglib_test.go +++ b/internal/library/taglib_test.go @@ -302,3 +302,29 @@ func TestCleanGenres(t *testing.T) { t.Errorf("a 101-character genre is noise, kept as %v", got) } } + +func TestTaglibReaderYear(t *testing.T) { + dir := t.TempDir() + for name, tc := range map[string]struct { + meta map[string]string + want int + }{ + "a full date": {map[string]string{"title": "a", "date": "1997-05-21"}, 1997}, + "only a year": {map[string]string{"title": "b", "date": "1973"}, 1973}, + "the original year wins": {map[string]string{"title": "c", "date": "2011", "originaldate": "1979-11-30"}, 1979}, + "no date": {map[string]string{"title": "d"}, 0}, + "something that is no year": {map[string]string{"title": "e", "date": "unknown"}, 0}, + "a year out of range": {map[string]string{"title": "f", "date": "0000"}, 0}, + } { + t.Run(name, func(t *testing.T) { + p := makeAudio(t, dir, strings.ReplaceAll(name, " ", "_")+".flac", tc.meta) + m, err := TaglibReader{}.Read(p) + if err != nil { + t.Fatal(err) + } + if m.Year != tc.want { + t.Errorf("year = %d, want %d", m.Year, tc.want) + } + }) + } +} diff --git a/internal/library/types.go b/internal/library/types.go index 417fa91..500ccad 100644 --- a/internal/library/types.go +++ b/internal/library/types.go @@ -31,8 +31,15 @@ type Tags struct { AlbumMBID string // the MusicBrainz release group id of the album, when the file has exactly one TrackNo int DiscNo int + Year int // the year the song came out (the original date first), 0 when the file has none or it is not a year } +// MinYear and MaxYear bound what is taken for the year of a song. +const ( + MinYear = 1000 + MaxYear = 2999 +) + // MaxGenresPerTrack bounds the genres kept for one file. const MaxGenresPerTrack = 5 diff --git a/internal/smart/smart.go b/internal/smart/smart.go index d23245c..edf30dd 100644 --- a/internal/smart/smart.go +++ b/internal/smart/smart.go @@ -37,6 +37,8 @@ const ( MaxPlays = 1_000_000 MaxBitrateKbps = 100_000 MaxDurationSec = 86_400 + MinYear = 1000 // the first and the last year a rule can name + MaxYear = 2999 DefaultLimit = 50 MaxLimit = 500 MaxPlaylists = 200 // smart playlists of one user @@ -108,6 +110,10 @@ type Rules struct { WithoutAvoidedArtists bool `json:"without_avoided_artists,omitempty"` MinDurationSec int `json:"min_duration_sec,omitempty"` MaxDurationSec int `json:"max_duration_sec,omitempty"` + // MinYear and MaxYear are about the year the song came out, from the tags of its file: both limits count + // (1980 to 1989 is the eighties). A song whose file has no year never matches a year rule. + MinYear int `json:"min_year,omitempty"` + MaxYear int `json:"max_year,omitempty"` } // SmartPlaylist is a smart playlist. @@ -161,6 +167,7 @@ type Filter struct { AlbumIDs, NotAlbumIDs []int64 Formats []string MinBitrateKbps int + MinYear, MaxYear int // 0 is no limit } // Store keeps the smart playlists and reads what they are made of. Every call that is about one playlist is @@ -329,6 +336,17 @@ func cleanRules(r Rules) (Rules, error) { if r.MaxDurationSec > 0 && r.MinDurationSec > r.MaxDurationSec { return Rules{}, invalid("min_duration_sec is more than max_duration_sec") } + for _, y := range []struct { + name string + got int + }{{"min_year", r.MinYear}, {"max_year", r.MaxYear}} { + if y.got != 0 && (y.got < MinYear || y.got > MaxYear) { + return Rules{}, invalid("%s must be 0 or a year from %d to %d", y.name, MinYear, MaxYear) + } + } + if r.MinYear > 0 && r.MaxYear > 0 && r.MinYear > r.MaxYear { + return Rules{}, invalid("min_year is more than max_year") + } return r, nil } @@ -538,7 +556,7 @@ func (s *Service) run(ctx context.Context, userID, id int64, r Rules, order Sort func (s *Service) pick(ctx context.Context, userID, id int64, r Rules, order Sort, limit, variant int) ([]int64, error) { f := Filter{ ArtistIDs: r.ArtistIDs, NotArtistIDs: r.NotArtistIDs, AlbumIDs: r.AlbumIDs, NotAlbumIDs: r.NotAlbumIDs, - Formats: r.Formats, MinBitrateKbps: r.MinBitrateKbps, + Formats: r.Formats, MinBitrateKbps: r.MinBitrateKbps, MinYear: r.MinYear, MaxYear: r.MaxYear, } if len(r.Genres) > 0 || len(r.NotGenres) > 0 { all, err := s.store.Genres(ctx) @@ -734,5 +752,7 @@ func (r Rules) Summary() string { add(r.WithoutAvoidedArtists, "without avoided artists") add(r.MinDurationSec > 0, "at least %d minutes long", r.MinDurationSec/60) add(r.MaxDurationSec > 0, "at most %d minutes long", r.MaxDurationSec/60) + add(r.MinYear > 0, "from %d", r.MinYear) + add(r.MaxYear > 0, "up to %d", r.MaxYear) return strings.Join(parts, "; ") } diff --git a/internal/smart/smart_test.go b/internal/smart/smart_test.go index 4806475..4b1f711 100644 --- a/internal/smart/smart_test.go +++ b/internal/smart/smart_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "slices" + "strings" "testing" "time" @@ -794,3 +795,35 @@ func TestTheCoversOfASuggestedOneAreThoseOfItsSongs(t *testing.T) { t.Errorf("unknown key: %v", err) } } + +func TestTheYearRulesGoToTheIndexAndAreChecked(t *testing.T) { + st := newStore() + st.cands = songs(3) + svc := newService(st, taste.Learned{}) + preview(t, svc, Rules{MinYear: 1980, MaxYear: 1989}, SortTitle) + if len(st.asked) != 1 || st.asked[0].MinYear != 1980 || st.asked[0].MaxYear != 1989 { + t.Errorf("the index was asked %+v", st.asked) + } + for name, r := range map[string]Rules{ + "a year before 1000": {MinYear: 999}, + "a year after 2999": {MaxYear: 3000}, + "a negative year": {MinYear: -1}, + "from after up to": {MinYear: 1990, MaxYear: 1980}, + } { + if _, err := svc.Preview(context.Background(), 1, Input{Rules: r}, 0); !errors.Is(err, ErrInvalidInput) { + t.Errorf("%s: err = %v, want ErrInvalidInput", name, err) + } + } + for name, r := range map[string]Rules{ + "one year": {MinYear: 1985, MaxYear: 1985}, + "only a start": {MinYear: 1950}, + "only an end": {MaxYear: 2024}, + } { + if _, err := svc.Preview(context.Background(), 1, Input{Rules: r}, 0); err != nil { + t.Errorf("%s: %v", name, err) + } + } + if got := (Rules{MinYear: 1980, MaxYear: 1989}).Summary(); !strings.Contains(got, "from 1980") || !strings.Contains(got, "up to 1989") { + t.Errorf("summary = %q", got) + } +} diff --git a/internal/storage/library.go b/internal/storage/library.go index 070fcde..c424bd0 100644 --- a/internal/storage/library.go +++ b/internal/storage/library.go @@ -47,6 +47,7 @@ type trackModel struct { TrackNo int `bun:"track_no,nullzero"` DiscNo int `bun:"disc_no,nullzero"` AlbumID int64 `bun:"album_id,nullzero"` + Year int `bun:"year,nullzero"` } type trackArtistModel struct { @@ -152,7 +153,7 @@ func (r *LibraryRepo) SaveFile(ctx context.Context, libraryID int64, f library.P } track := &trackModel{ - Title: f.Title, TitleKey: matchKey(f.Title), SearchKey: searchKey(f.Title), DurationMs: f.DurationMs, TrackNo: f.TrackNo, DiscNo: f.DiscNo, AlbumID: albumID, + Title: f.Title, TitleKey: matchKey(f.Title), SearchKey: searchKey(f.Title), DurationMs: f.DurationMs, TrackNo: f.TrackNo, DiscNo: f.DiscNo, AlbumID: albumID, Year: f.Year, } if existingID == 0 { @@ -187,7 +188,7 @@ func (r *LibraryRepo) SaveFile(ctx context.Context, libraryID int64, f library.P return err } if _, err := tx.NewUpdate().Model(track). - Column("title", "title_key", "search_key", "duration_ms", "track_no", "disc_no", "album_id"). + Column("title", "title_key", "search_key", "duration_ms", "track_no", "disc_no", "album_id", "year"). WherePK().Exec(ctx); err != nil { return fmt.Errorf("update track: %w", err) } diff --git a/internal/storage/library_test.go b/internal/storage/library_test.go index 9307482..c4fd9d3 100644 --- a/internal/storage/library_test.go +++ b/internal/storage/library_test.go @@ -621,3 +621,44 @@ func TestTheIDOfAnAlbumWithNoTracksLeftPassesToTheRetaggedOne(t *testing.T) { } }) } + +func TestLibraryRepoKeepsTheYearOfATrackAndUpdatesItOnARescan(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + repo := NewLibraryRepo(db) + lib := mkLib(t, repo, "music") + f := parsed("a/1.flac", "Song", "Album", "Radiohead") + f.Year = 1997 + if _, err := repo.SaveFile(ctx, lib, f, 0); err != nil { + t.Fatal(err) + } + year := func() sql.NullInt64 { + var y sql.NullInt64 + if err := db.QueryRow("SELECT year FROM tracks WHERE title = 'Song'").Scan(&y); err != nil { + t.Fatal(err) + } + return y + } + if y := year(); !y.Valid || y.Int64 != 1997 { + t.Fatalf("year = %v, want 1997", y) + } + var fileID int64 + if err := db.QueryRow("SELECT id FROM audio_files").Scan(&fileID); err != nil { + t.Fatal(err) + } + f.Year = 1998 + if _, err := repo.SaveFile(ctx, lib, f, fileID); err != nil { + t.Fatal(err) + } + if y := year(); y.Int64 != 1998 { + t.Errorf("after a rescan year = %v, want 1998", y) + } + f.Year = 0 + if _, err := repo.SaveFile(ctx, lib, f, fileID); err != nil { + t.Fatal(err) + } + if y := year(); y.Valid { + t.Errorf("a file with no year leaves the track with none, got %v", y) + } + }) +} diff --git a/internal/storage/smart.go b/internal/storage/smart.go index 5ca224c..a54fa29 100644 --- a/internal/storage/smart.go +++ b/internal/storage/smart.go @@ -261,6 +261,12 @@ func (r *SmartRepo) Candidates(ctx context.Context, f smart.Filter) ([]smart.Can if len(f.NotAlbumIDs) > 0 { add("(t.album_id IS NULL OR t.album_id NOT IN (?))", bun.In(f.NotAlbumIDs)) } + if f.MinYear > 0 { + add("t.year >= ?", f.MinYear) + } + if f.MaxYear > 0 { + add("t.year <= ?", f.MaxYear) + } if len(f.Formats) > 0 || f.MinBitrateKbps > 0 { file := "EXISTS (SELECT 1 FROM audio_files f WHERE f.track_id = t.id AND f.missing_at IS NULL" var fargs []any diff --git a/internal/storage/smart_test.go b/internal/storage/smart_test.go index bdb4458..5000199 100644 --- a/internal/storage/smart_test.go +++ b/internal/storage/smart_test.go @@ -412,3 +412,36 @@ func TestSmartRepoAlbumCoversFollowTheOrderOfTheSongsAndSkipWhatHasNoCover(t *te } }) } + +func TestSmartRepoCandidatesNarrowByYear(t *testing.T) { + smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { + ctx := context.Background() + // a 1975, b 1985, c 1985, d 1995, e has no year. + for title, year := range map[string]int{"a": 1975, "b": 1985, "c": 1985, "d": 1995} { + if _, err := e.db.Exec("UPDATE tracks SET year = ? WHERE id = ?", year, e.tracks[title]); err != nil { + t.Fatal(err) + } + } + for name, tc := range map[string]struct { + f smart.Filter + want []string + }{ + "from a year": {smart.Filter{MinYear: 1985}, []string{"b", "c", "d"}}, + "up to a year": {smart.Filter{MaxYear: 1985}, []string{"a", "b", "c"}}, + "between two years": {smart.Filter{MinYear: 1980, MaxYear: 1989}, []string{"b", "c"}}, + "one year": {smart.Filter{MinYear: 1995, MaxYear: 1995}, []string{"d"}}, + "a year and a genre": {smart.Filter{MinYear: 1980, Genres: []string{"rock"}}, []string{"b", "c"}}, + "no year rule: a song with no year is still there": {smart.Filter{}, []string{"a", "b", "c", "d", "e"}}, + } { + t.Run(name, func(t *testing.T) { + got, err := e.repo.Candidates(ctx, tc.f) + if err != nil { + t.Fatal(err) + } + if g := names(e, got); !slices.Equal(g, tc.want) { + t.Errorf("got %v, want %v", g, tc.want) + } + }) + } + }) +} diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go index f93c65a..5dd8c7c 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/storage_test.go @@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) { if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil { t.Fatal(err) } - if applied != 27 { - t.Errorf("applied migrations = %d, want 27", applied) + if applied != 28 { + t.Errorf("applied migrations = %d, want 28", applied) } }) } diff --git a/migrations/postgres/0028_track_year.sql b/migrations/postgres/0028_track_year.sql new file mode 100644 index 0000000..5676d17 --- /dev/null +++ b/migrations/postgres/0028_track_year.sql @@ -0,0 +1,3 @@ +-- PostgreSQL flavour of sqlite/0028_track_year.sql. + +ALTER TABLE tracks ADD COLUMN year INTEGER; diff --git a/migrations/sqlite/0028_track_year.sql b/migrations/sqlite/0028_track_year.sql new file mode 100644 index 0000000..b6c400f --- /dev/null +++ b/migrations/sqlite/0028_track_year.sql @@ -0,0 +1,4 @@ +-- The year a song came out, from the tags of its file (the original date first). NULL when the file has none, and for the +-- files already indexed until a full rescan reads their tags again. The smart playlists use it. + +ALTER TABLE tracks ADD COLUMN year INTEGER; diff --git a/web/src/api/schema.d.ts b/web/src/api/schema.d.ts index 9c0f384..38a4faf 100644 --- a/web/src/api/schema.d.ts +++ b/web/src/api/schema.d.ts @@ -3367,6 +3367,10 @@ export interface components { without_avoided_artists?: boolean; min_duration_sec?: number; max_duration_sec?: number; + /** @description The song came out in this year or later (0 or a year from 1000). The year comes from the tags of the file, the original date first. A song whose file has no year never matches a year rule, and the files indexed before this rule existed have none until a full rescan (`POST /library/scan?full=true`) reads their tags again. */ + min_year?: number; + /** @description The song came out in this year or earlier. With `min_year` set it cannot be lower than it. See `min_year`. */ + max_year?: number; }; SuggestedSmartPlaylist: { /** @enum {string} */ diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 146b332..69c01ba 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -918,7 +918,9 @@ "liked": "Only artists I like", "noAvoided": "Without artists I avoid", "minLength": "At least {{minutes}} min long", - "maxLength": "At most {{minutes}} min long" + "maxLength": "At most {{minutes}} min long", + "minYear": "From the year {{year}}", + "maxYear": "Up to the year {{year}}" }, "editor": { "titleNew": "New smart playlist", @@ -951,6 +953,10 @@ "length": "Length of the song", "minMinutes": "At least (minutes)", "maxMinutes": "At most (minutes)", + "year": "Year", + "minYear": "From year", + "maxYear": "Up to year", + "yearHelp": "The year comes from the tags of the files. A song with no year in its tags never matches. Files added before this rule existed have none until a full rescan reads their tags again.", "order": "Order", "limit": "Most songs", "preview": "Preview", diff --git a/web/src/i18n/locales/it.json b/web/src/i18n/locales/it.json index 2f41325..2037d34 100644 --- a/web/src/i18n/locales/it.json +++ b/web/src/i18n/locales/it.json @@ -918,7 +918,9 @@ "liked": "Solo artisti che amo", "noAvoided": "Senza artisti che evito", "minLength": "Lunghi almeno {{minutes}} min", - "maxLength": "Lunghi al massimo {{minutes}} min" + "maxLength": "Lunghi al massimo {{minutes}} min", + "minYear": "Dall'anno {{year}}", + "maxYear": "Fino all'anno {{year}}" }, "editor": { "titleNew": "Nuova playlist smart", @@ -951,6 +953,10 @@ "length": "Durata del brano", "minMinutes": "Almeno (minuti)", "maxMinutes": "Al massimo (minuti)", + "year": "Anno", + "minYear": "Dall'anno", + "maxYear": "Fino all'anno", + "yearHelp": "L'anno viene dai tag dei file. Una canzone senza anno nei tag non corrisponde mai. I file aggiunti prima di questa regola non hanno anno finché una riscansione completa non rilegge i loro tag.", "order": "Ordine", "limit": "Massimo di brani", "preview": "Anteprima", diff --git a/web/src/pages/SmartEditorPage.tsx b/web/src/pages/SmartEditorPage.tsx index df0cfe3..e41b673 100644 --- a/web/src/pages/SmartEditorPage.tsx +++ b/web/src/pages/SmartEditorPage.tsx @@ -164,6 +164,15 @@ function SmartEditor({ existing }: { existing: SmartPlaylist | null }) { set('maxMinutes', v)} /> + +
+ {t('smart.editor.year')} +
+ set('minYear', v)} /> + set('maxYear', v)} /> +
+

{t('smart.editor.yearHelp')}

+

{t('smart.editor.blank')}

diff --git a/web/src/pages/smart.test.tsx b/web/src/pages/smart.test.tsx index f64f71d..ecc1c7e 100644 --- a/web/src/pages/smart.test.tsx +++ b/web/src/pages/smart.test.tsx @@ -80,7 +80,7 @@ test('rules go into the form and come back unchanged, keeping what the form has genres: ['rock', 'metal'], not_genres: ['pop'], artist_ids: [3, 4], not_album_ids: [7], formats: ['flac'], min_bitrate_kbps: 900, added_within_days: 30, never_played: true, played_within_days: 7, not_played_within_days: 60, min_plays: 2, plays_within_days: 30, skipped_within_days: 5, not_skipped_within_days: 14, liked_artists_only: true, without_avoided_artists: true, - min_duration_sec: 90, max_duration_sec: 600, + min_duration_sec: 90, max_duration_sec: 600, min_year: 1980, max_year: 1989, } expect(rulesFromForm(formFromRules(rules), rules)).toEqual(rules) }) @@ -312,6 +312,21 @@ test('the length is in minutes and goes to the server in seconds', async () => { await waitFor(() => expect(body(previewCalls(calls).at(-1)!).rules).toEqual({ min_duration_sec: 90, max_duration_sec: 600 }), { timeout: 3000 }) }) +test('the years go to the server as they are typed, and a year that is no year is no rule', async () => { + const { calls } = mockFetch(editorRoutes()) + renderApp('/smart-playlists/new') + await userEvent.type(await screen.findByRole('spinbutton', { name: 'From year' }), '1980') + await userEvent.type(screen.getByRole('spinbutton', { name: 'Up to year' }), '1989') + await waitFor(() => expect(body(previewCalls(calls).at(-1)!).rules).toEqual({ min_year: 1980, max_year: 1989 }), { timeout: 3000 }) + expect(screen.getByText(/full rescan/i)).toBeInTheDocument() +}) + +test('the years are described in words', async () => { + const { default: i18n } = await import('i18next') + const t = i18n.t.bind(i18n) + expect(describeRules(t, { min_year: 1980, max_year: 1989 })).toEqual(['From the year 1980', 'Up to the year 1989']) +}) + test('editing keeps what the editor cannot show, and changes only what was changed', async () => { const existing = smart(3, 'Fresh rock', { rules: { genres: ['rock'], artist_ids: [3, 4], formats: ['ogg'], min_plays: 5 }, sort: 'title', limit: 20 }) const { calls } = mockFetch( diff --git a/web/src/pages/smartRules.ts b/web/src/pages/smartRules.ts index 4d96c0d..16253de 100644 --- a/web/src/pages/smartRules.ts +++ b/web/src/pages/smartRules.ts @@ -18,11 +18,13 @@ export interface RuleForm { noAvoided: boolean minMinutes: string maxMinutes: string + minYear: string + maxYear: string } export const EMPTY_FORM: RuleForm = { genres: [], notGenres: [], format: 'any', minBitrate: '', addedDays: '', neverPlayed: false, playedDays: '', notPlayedDays: '', - minPlays: '', playsDays: '', notSkippedDays: '', likedOnly: false, noAvoided: false, minMinutes: '', maxMinutes: '', + minPlays: '', playsDays: '', notSkippedDays: '', likedOnly: false, noAvoided: false, minMinutes: '', maxMinutes: '', minYear: '', maxYear: '', } const text = (n: number | undefined) => (n ? String(n) : '') @@ -52,6 +54,8 @@ export function formFromRules(r: SmartRules): RuleForm { noAvoided: r.without_avoided_artists ?? false, minMinutes: minutes(r.min_duration_sec), maxMinutes: minutes(r.max_duration_sec), + minYear: text(r.min_year), + maxYear: text(r.max_year), } } @@ -77,6 +81,7 @@ export function rulesFromForm(f: RuleForm, base: SmartRules = {}): SmartRules { const numbers: [keyof SmartRules, string][] = [ ['min_bitrate_kbps', f.minBitrate], ['added_within_days', f.addedDays], ['played_within_days', f.playedDays], ['not_played_within_days', f.notPlayedDays], ['min_plays', f.minPlays], ['plays_within_days', f.playsDays], ['not_skipped_within_days', f.notSkippedDays], + ['min_year', f.minYear], ['max_year', f.maxYear], ] for (const [key, value] of numbers) { const n = whole(value) @@ -118,6 +123,8 @@ export function describeRules(t: TFunction, r: SmartRules): string[] { add(r.liked_artists_only, 'liked') add(r.without_avoided_artists, 'noAvoided') add(r.min_duration_sec, 'minLength', { minutes: Number(((r.min_duration_sec ?? 0) / 60).toFixed(2)) }) + add(r.min_year, 'minYear', { year: r.min_year }) + add(r.max_year, 'maxYear', { year: r.max_year }) add(r.max_duration_sec, 'maxLength', { minutes: Number(((r.max_duration_sec ?? 0) / 60).toFixed(2)) }) if (lines.length === 0) lines.push(t('smart.rule.none')) return lines