sonda/internal/smart/suggested.go
tgiachi 82b92eaf08 feat(smart): covers on the Smart playlists page
- GET /smart-playlists/{id}/covers and GET /smart-playlists/suggested/{key}/covers say which albums stand for a smart playlist: up to 4 albums that have a cover, in the order of the songs the rules give now (for the variant asked), each once. They are worked out apart from the songs, and the answer may be kept for a few minutes.
- The cards of the Smart playlists page are a grid with the collage. The list shows at once and each card asks for its own covers after that, showing the rings until they arrive or when they cannot be read.
- The pages of a smart playlist and of a suggested one use the same covers for the variant on screen, so only albums that have a cover are shown.
2026-11-13 11:41:23 +01:00

74 lines
3.2 KiB
Go

package smart
import (
"context"
"slices"
"codeberg.org/squidd/sonda/internal/catalog"
)
// SuggestedPlaylist is a smart playlist that Sonda makes for everybody, with no need to ask: the rules are the
// same for every user and are worked out on each one's own library and history, when it is opened. Nothing is
// stored for it, so it follows the library, and a client says its name in its own language from the key.
type SuggestedPlaylist struct {
Key string // "recently-added", "never-played"...
Rules Rules
Sort Sort
Limit int
}
// suggested is the set. Its rules are valid ones (a test checks it), and a copy of one is a smart playlist
// the user can make their own with Create.
var suggested = []SuggestedPlaylist{
{Key: "recently-added", Rules: Rules{AddedWithinDays: 30}, Sort: SortRecentlyAdded, Limit: 100},
{Key: "on-rotation", Rules: Rules{PlaysWithinDays: 30, MinPlays: 2}, Sort: SortMostPlayed, Limit: DefaultLimit},
{Key: "top-year", Rules: Rules{PlaysWithinDays: 365, MinPlays: 2}, Sort: SortMostPlayed, Limit: DefaultLimit},
{Key: "most-played", Rules: Rules{MinPlays: 1}, Sort: SortMostPlayed, Limit: DefaultLimit},
{Key: "never-played", Rules: Rules{NeverPlayed: true}, Sort: SortRandom, Limit: DefaultLimit},
{Key: "forgotten", Rules: Rules{MinPlays: 1, NotPlayedWithinDays: 90}, Sort: SortRandom, Limit: DefaultLimit},
{Key: "high-quality", Rules: Rules{MinBitrateKbps: 900}, Sort: SortRandom, Limit: DefaultLimit},
{Key: "long-tracks", Rules: Rules{MinDurationSec: 360}, Sort: SortRandom, Limit: DefaultLimit},
}
// Suggested returns the suggested smart playlists. The list is a copy: changing it changes nothing.
func Suggested() []SuggestedPlaylist {
out := make([]SuggestedPlaylist, len(suggested))
for i, s := range suggested {
s.Rules.Genres = slices.Clone(s.Rules.Genres)
out[i] = s
}
return out
}
// SuggestedCovers says which albums stand for a suggested smart playlist, as Covers does for one of the user's own.
func (s *Service) SuggestedCovers(ctx context.Context, userID int64, key string, variant int) ([]int64, error) {
if err := checkVariant(variant); err != nil {
return nil, err
}
i := slices.IndexFunc(suggested, func(x SuggestedPlaylist) bool { return x.Key == key })
if i < 0 {
return nil, ErrNotFound
}
sg := suggested[i]
picked, err := s.pick(ctx, userID, -int64(i+1), sg.Rules, sg.Sort, sg.Limit, variant)
if err != nil {
return nil, err
}
return s.albumCovers(ctx, picked)
}
// SuggestedTracks works the songs of a suggested smart playlist out for the user. variant 0 is the songs of
// the day; with a random order another number gives other ones. An unknown key is ErrNotFound.
func (s *Service) SuggestedTracks(ctx context.Context, userID int64, key string, variant int) (SuggestedPlaylist, []catalog.Track, error) {
if err := checkVariant(variant); err != nil {
return SuggestedPlaylist{}, nil, err
}
i := slices.IndexFunc(suggested, func(x SuggestedPlaylist) bool { return x.Key == key })
if i < 0 {
return SuggestedPlaylist{}, nil, ErrNotFound
}
sg := suggested[i]
// The seed has an id of its own, below 0 so that it is never one of a kept playlist.
tracks, err := s.run(ctx, userID, -int64(i+1), sg.Rules, sg.Sort, sg.Limit, variant)
return Suggested()[i], tracks, err
}