A person can mark songs, albums and artists as favourites. They are theirs
alone: never shown to a friend, never sent anywhere. Only what can be played is
listed.
- migration 0035 favorites, internal/favorites, storage.FavoritesRepo
- PUT/DELETE /me/favorites/{kind}/{id}, GET /me/favorites/{kind} and
GET /me/favorites/ids, in the OpenAPI spec
- catalog.Filter IDs now also reads artists by id
- OpenSubsonic: star, unstar, getStarred2, the starred date on songs, albums and
artists, and getAlbumList2 of type starred
106 lines
4.2 KiB
Go
106 lines
4.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/favorites"
|
|
)
|
|
|
|
// FavoritesRepo keeps the favourites of the people. It implements favorites.Store. What it lists is what can be
|
|
// played, like the catalogue: a favourite of music that is gone is kept but not listed.
|
|
type FavoritesRepo struct{ db *bun.DB }
|
|
|
|
// NewFavoritesRepo builds a FavoritesRepo.
|
|
func NewFavoritesRepo(db *bun.DB) *FavoritesRepo { return &FavoritesRepo{db: db} }
|
|
|
|
// Add marks an item; marking it again keeps the first date.
|
|
func (r *FavoritesRepo) Add(ctx context.Context, userID int64, kind favorites.Kind, id int64, at time.Time) error {
|
|
_, err := r.db.ExecContext(ctx,
|
|
`INSERT INTO favorites (user_id, kind, item_id, created_at) VALUES (?, ?, ?, ?) ON CONFLICT (user_id, kind, item_id) DO NOTHING`,
|
|
userID, string(kind), id, at.Unix())
|
|
if err != nil {
|
|
return fmt.Errorf("add favourite: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Remove unmarks an item.
|
|
func (r *FavoritesRepo) Remove(ctx context.Context, userID int64, kind favorites.Kind, id int64) error {
|
|
if _, err := r.db.ExecContext(ctx, `DELETE FROM favorites WHERE user_id = ? AND kind = ? AND item_id = ?`, userID, string(kind), id); err != nil {
|
|
return fmt.Errorf("remove favourite: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// playable is the join and the condition that keep only the items that can be played, for the favourites of a kind.
|
|
func playable(kind favorites.Kind) (join string, args []any) {
|
|
vis, visArgs := visible("t", 0)
|
|
switch kind {
|
|
case favorites.Track:
|
|
return "JOIN tracks t ON t.id = f.item_id AND " + vis, visArgs
|
|
case favorites.Album:
|
|
return "JOIN albums al ON al.id = f.item_id AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = al.id AND " + vis + ")", visArgs
|
|
default:
|
|
// The same as the artists of the catalogue: credited on a playable track, or the album artist of a playable album.
|
|
return `JOIN artists a ON a.id = f.item_id AND (
|
|
EXISTS (SELECT 1 FROM track_artists ta JOIN tracks t ON t.id = ta.track_id WHERE ta.artist_id = a.id AND ` + vis + `)
|
|
OR EXISTS (SELECT 1 FROM albums al JOIN tracks t ON t.album_id = al.id WHERE al.artist_id = a.id AND ` + vis + `))`,
|
|
append(append([]any{}, visArgs...), visArgs...)
|
|
}
|
|
}
|
|
|
|
// Page returns the ids of a page of the favourites of a kind, the newest first, and how many there are.
|
|
func (r *FavoritesRepo) Page(ctx context.Context, userID int64, kind favorites.Kind, limit, offset int) ([]int64, int, error) {
|
|
join, args := playable(kind)
|
|
base := "FROM favorites f " + join + " WHERE f.user_id = ? AND f.kind = ?"
|
|
where := append(append([]any{}, args...), userID, string(kind))
|
|
var total int
|
|
if err := r.db.QueryRowContext(ctx, "SELECT COUNT(*) "+base, where...).Scan(&total); err != nil {
|
|
return nil, 0, fmt.Errorf("count favourites: %w", err)
|
|
}
|
|
rows, err := r.db.QueryContext(ctx, "SELECT f.item_id "+base+" ORDER BY f.created_at DESC, f.item_id DESC LIMIT ? OFFSET ?",
|
|
append(where, limit, offset)...)
|
|
if err != nil {
|
|
return nil, 0, fmt.Errorf("list favourites: %w", err)
|
|
}
|
|
ids, err := scanIDs(rows)
|
|
return ids, total, err
|
|
}
|
|
|
|
// IDs returns the ids of every favourite of a kind, the newest first.
|
|
func (r *FavoritesRepo) IDs(ctx context.Context, userID int64, kind favorites.Kind) ([]int64, error) {
|
|
join, args := playable(kind)
|
|
rows, err := r.db.QueryContext(ctx,
|
|
"SELECT f.item_id FROM favorites f "+join+" WHERE f.user_id = ? AND f.kind = ? ORDER BY f.created_at DESC, f.item_id DESC",
|
|
append(args, userID, string(kind))...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list favourites: %w", err)
|
|
}
|
|
return scanIDs(rows)
|
|
}
|
|
|
|
// Marks returns when each of the ids that are favourites was marked.
|
|
func (r *FavoritesRepo) Marks(ctx context.Context, userID int64, kind favorites.Kind, ids []int64) (map[int64]time.Time, error) {
|
|
out := map[int64]time.Time{}
|
|
if len(ids) == 0 {
|
|
return out, nil
|
|
}
|
|
rows, err := r.db.QueryContext(ctx, `SELECT item_id, created_at FROM favorites WHERE user_id = ? AND kind = ? AND item_id IN (?)`,
|
|
userID, string(kind), bun.In(ids))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read favourites: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var id, at int64
|
|
if err := rows.Scan(&id, &at); err != nil {
|
|
return nil, err
|
|
}
|
|
out[id] = time.Unix(at, 0)
|
|
}
|
|
return out, rows.Err()
|
|
}
|