sonda/internal/smart/suggested.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

74 lines
3.2 KiB
Go

package smart
import (
"context"
"slices"
"git.kmalloc.org/squid/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
}