sonda/internal/storage/taste.go
tgiachi ca5cbbf088 feat: favourites teach Sonda my taste
A heart adds to what the listening says of an artist: a favourite artist counts
as much as three complete listens, a favourite album 1.5 for its album artist and
a favourite song 1 for each of its main artists, at most 6 for one artist, without
fading with time. An artist hearted but never played is liked, with no listens.
That feeds Discover, the mixes and the smart playlists that use the taste, and
GET /me/taste.

In the mixes of what the listener likes (genre mixes, Your favourites,
Rediscover) a hearted song is wanted 1.5 times as much and a song on a hearted
album 1.25 times. New to you is not changed.
2026-11-16 11:08:20 +01:00

236 lines
6.7 KiB
Go

package storage
import (
"context"
"fmt"
"time"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/internal/listening"
"git.kmalloc.org/squid/sonda/internal/providers"
"git.kmalloc.org/squid/sonda/internal/taste"
)
// TasteRepo reads what the taste model needs. It implements taste.Store and
// never writes.
type TasteRepo struct{ db *bun.DB }
// NewTasteRepo builds a TasteRepo.
func NewTasteRepo(db *bun.DB) *TasteRepo { return &TasteRepo{db: db} }
// ArtistEvents returns the user's events since the given time, one per credited
// artist of the track. Tracks whose files are gone still count: the history is
// the listener's own.
func (r *TasteRepo) ArtistEvents(ctx context.Context, userID int64, since time.Time) ([]taste.Event, error) {
rows, err := r.db.QueryContext(ctx,
`SELECT ta.artist_id, e.event_type, e.duration_played_ms, e.occurred_at
FROM listening_events e JOIN track_artists ta ON ta.track_id = e.track_id
WHERE e.user_id = ? AND e.occurred_at >= ? AND ta.role = 'main'
ORDER BY e.occurred_at, e.id, ta.artist_id`,
userID, since.Unix())
if err != nil {
return nil, fmt.Errorf("read listening events: %w", err)
}
defer rows.Close()
var out []taste.Event
for rows.Next() {
var (
e taste.Event
typ string
at int64
)
if err := rows.Scan(&e.ArtistID, &typ, &e.PlayedMs, &at); err != nil {
return nil, err
}
e.Type, e.At = listening.EventType(typ), time.Unix(at, 0)
out = append(out, e)
}
return out, rows.Err()
}
// ArtistNames returns the names of the artists that exist.
func (r *TasteRepo) ArtistNames(ctx context.Context, ids []int64) (map[int64]string, error) {
out := map[int64]string{}
if len(ids) == 0 {
return out, nil
}
rows, err := r.db.QueryContext(ctx, "SELECT id, name FROM artists WHERE id IN (?)", bun.In(ids))
if err != nil {
return nil, fmt.Errorf("read artist names: %w", err)
}
defer rows.Close()
for rows.Next() {
var (
id int64
name string
)
if err := rows.Scan(&id, &name); err != nil {
return nil, err
}
out[id] = name
}
return out, rows.Err()
}
// ArtistTags returns the stored tags of the artists with weights from 0 to 1,
// from the genres of the user's files and from what the providers once said. It
// asks nobody. Each source is scaled to its own best tag (Last.fm's weights are
// already a percentage, MusicBrainz's are votes, the files' are track counts) and
// a tag several sources know keeps the largest weight.
func (r *TasteRepo) ArtistTags(ctx context.Context, ids []int64) (map[int64]map[string]float64, error) {
out := map[int64]map[string]float64{}
if len(ids) == 0 {
return out, nil
}
put := func(artist int64, tag string, w float64) {
if w > 1 {
w = 1
}
if out[artist] == nil {
out[artist] = map[string]float64{}
}
if w > out[artist][tag] {
out[artist][tag] = w
}
}
// Files: track counts, scaled to the artist's most common genre. Only tracks
// that still have a file: the genre of a file that is gone says nothing about
// the music that is there (the same rule as the genres of /artists/{id}/tags).
vis, visArgs := visible("t", 0)
files, err := r.db.QueryContext(ctx,
`SELECT ta.artist_id, g.genre, COUNT(DISTINCT ta.track_id)
FROM track_artists ta JOIN track_genres g ON g.track_id = ta.track_id JOIN tracks t ON t.id = ta.track_id
WHERE ta.artist_id IN (?) AND ta.role = 'main' AND `+vis+` GROUP BY ta.artist_id, g.genre`,
append([]any{bun.In(ids)}, visArgs...)...)
if err != nil {
return nil, fmt.Errorf("read genres: %w", err)
}
if err := scaleToBest(files, put); err != nil {
return nil, err
}
// Providers.
stored, err := r.db.QueryContext(ctx,
"SELECT artist_id, provider, tag, weight FROM artist_tags WHERE artist_id IN (?)", bun.In(ids))
if err != nil {
return nil, fmt.Errorf("read tags: %w", err)
}
defer stored.Close()
type key struct {
artist int64
provider string
}
type row struct {
artist int64
tag string
weight float64
}
byProvider := map[key][]row{}
best := map[key]float64{}
for stored.Next() {
var (
rw row
provider string
)
if err := stored.Scan(&rw.artist, &provider, &rw.tag, &rw.weight); err != nil {
return nil, err
}
k := key{rw.artist, provider}
byProvider[k] = append(byProvider[k], rw)
if rw.weight > best[k] {
best[k] = rw.weight
}
}
if err := stored.Err(); err != nil {
return nil, err
}
for k, rows := range byProvider {
for _, rw := range rows {
switch {
case k.provider == providers.LastFM:
put(rw.artist, rw.tag, rw.weight/100)
case best[k] > 0:
put(rw.artist, rw.tag, rw.weight/best[k])
}
}
}
return out, nil
}
// scaleToBest reads (artist, tag, count) rows and hands each one over scaled to
// the largest count of its artist.
func scaleToBest(rows interface {
Next() bool
Scan(...any) error
Err() error
Close() error
}, put func(int64, string, float64)) error {
defer rows.Close()
type row struct {
artist int64
tag string
n float64
}
var all []row
best := map[int64]float64{}
for rows.Next() {
var rw row
if err := rows.Scan(&rw.artist, &rw.tag, &rw.n); err != nil {
return err
}
all = append(all, rw)
if rw.n > best[rw.artist] {
best[rw.artist] = rw.n
}
}
if err := rows.Err(); err != nil {
return err
}
for _, rw := range all {
put(rw.artist, rw.tag, rw.n/best[rw.artist])
}
return nil
}
// ArtistFavorites returns, per artist, the hearts of the user that point at it: the artist itself, the albums it is the
// album artist of, and the songs it is a main artist of. A heart counts whether or not the music can be played now.
func (r *TasteRepo) ArtistFavorites(ctx context.Context, userID int64) (map[int64]taste.Favorites, error) {
rows, err := r.db.QueryContext(ctx,
`SELECT artist_id, kind, COUNT(*) FROM (
SELECT a.id AS artist_id, 'artist' AS kind FROM favorites f JOIN artists a ON a.id = f.item_id
WHERE f.user_id = ? AND f.kind = 'artist'
UNION ALL
SELECT al.artist_id, 'album' FROM favorites f JOIN albums al ON al.id = f.item_id
WHERE f.user_id = ? AND f.kind = 'album' AND al.artist_id IS NOT NULL
UNION ALL
SELECT ta.artist_id, 'track' FROM favorites f JOIN track_artists ta ON ta.track_id = f.item_id AND ta.role = 'main'
WHERE f.user_id = ? AND f.kind = 'track'
) x GROUP BY artist_id, kind`, userID, userID, userID)
if err != nil {
return nil, fmt.Errorf("read favourites: %w", err)
}
defer rows.Close()
out := map[int64]taste.Favorites{}
for rows.Next() {
var (
id, n int64
kind string
)
if err := rows.Scan(&id, &kind, &n); err != nil {
return nil, err
}
f := out[id]
switch kind {
case "artist":
f.Artist = true
case "album":
f.Albums = int(n)
default:
f.Tracks = int(n)
}
out[id] = f
}
return out, rows.Err()
}