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.
This commit is contained in:
tgiachi 2026-11-14 10:19:39 +01:00
commit d3695d00d6
6 changed files with 273 additions and 16 deletions

View file

@ -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} */

View file

@ -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",

View file

@ -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",

View file

@ -164,6 +164,15 @@ function SmartEditor({ existing }: { existing: SmartPlaylist | null }) {
<NumberField label={t('smart.editor.maxMinutes')} value={form.maxMinutes} step="any" onChange={(v) => set('maxMinutes', v)} />
</div>
</fieldset>
<fieldset className="flex flex-col gap-4">
<legend className="mb-2 text-sm font-semibold text-muted-foreground">{t('smart.editor.year')}</legend>
<div className="grid gap-4 sm:grid-cols-2">
<NumberField label={t('smart.editor.minYear')} value={form.minYear} onChange={(v) => set('minYear', v)} />
<NumberField label={t('smart.editor.maxYear')} value={form.maxYear} onChange={(v) => set('maxYear', v)} />
</div>
<p className="text-sm text-muted-foreground">{t('smart.editor.yearHelp')}</p>
</fieldset>
<p className="text-sm text-muted-foreground">{t('smart.editor.blank')}</p>
<ChoiceGroup<SmartSort>

View file

@ -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(

View file

@ -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