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
162 lines
4.7 KiB
Go
162 lines
4.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/catalog"
|
|
"git.kmalloc.org/squid/sonda/internal/favorites"
|
|
)
|
|
|
|
// favoriteService is what the handlers need from favorites.Service.
|
|
type favoriteService interface {
|
|
Add(ctx context.Context, userID int64, kind favorites.Kind, id int64) error
|
|
Remove(ctx context.Context, userID int64, kind favorites.Kind, id int64) error
|
|
Page(ctx context.Context, userID int64, kind favorites.Kind, limit, offset int) ([]int64, int, error)
|
|
IDs(ctx context.Context, userID int64) (favorites.All, error)
|
|
Marks(ctx context.Context, userID int64, kind favorites.Kind, ids []int64) (map[int64]time.Time, error)
|
|
}
|
|
|
|
// favoriteHandlers serve the favourites of the signed-in user, who only ever sees their own.
|
|
type favoriteHandlers struct {
|
|
svc favoriteService
|
|
catalog catalogService
|
|
}
|
|
|
|
func (h *favoriteHandlers) kind(c *gin.Context) (favorites.Kind, bool) {
|
|
kind, ok := favorites.ParseKind(c.Param("kind"))
|
|
if !ok {
|
|
writeError(c, http.StatusNotFound, "not_found", "favourites are of artists, albums or tracks")
|
|
}
|
|
return kind, ok
|
|
}
|
|
|
|
func (h *favoriteHandlers) ids(c *gin.Context) {
|
|
all, err := h.svc.IDs(c.Request.Context(), currentUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"artists": all.Artists, "albums": all.Albums, "tracks": all.Tracks})
|
|
}
|
|
|
|
func (h *favoriteHandlers) add(c *gin.Context) {
|
|
kind, ok := h.kind(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
switch err := h.svc.Add(c.Request.Context(), currentUserID(c), kind, id); {
|
|
case errors.Is(err, favorites.ErrNotFound):
|
|
writeError(c, http.StatusNotFound, string(kind)+"_not_found", "no such "+string(kind))
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
default:
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func (h *favoriteHandlers) remove(c *gin.Context) {
|
|
kind, ok := h.kind(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := h.svc.Remove(c.Request.Context(), currentUserID(c), kind, id); err != nil {
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// list is a page of the favourites of a kind, the newest first, in the shape of the catalogue lists.
|
|
func (h *favoriteHandlers) list(c *gin.Context) {
|
|
kind, ok := h.kind(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
limit, ok := queryInt(c, "limit")
|
|
if !ok {
|
|
return
|
|
}
|
|
offset, ok := queryInt(c, "offset")
|
|
if !ok {
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
ids, total, err := h.svc.Page(ctx, currentUserID(c), kind, clampInt(limit), clampInt(offset))
|
|
switch {
|
|
case errors.Is(err, favorites.ErrInvalid):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
return
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
f := catalog.Filter{IDs: ids, Limit: len(ids)}
|
|
if len(ids) == 0 {
|
|
c.JSON(http.StatusOK, gin.H{"items": []any{}, "total": total, "limit": pageLimit(limit), "offset": clampInt(offset)})
|
|
return
|
|
}
|
|
switch kind {
|
|
case favorites.Artist:
|
|
p, err := h.catalog.Artists(ctx, f)
|
|
writeFavorites(c, err, ids, total, limit, offset, byID(p.Items, func(a catalog.Artist) int64 { return a.ID }), toArtistJSON)
|
|
case favorites.Album:
|
|
p, err := h.catalog.Albums(ctx, f)
|
|
writeFavorites(c, err, ids, total, limit, offset, byID(p.Items, func(a catalog.Album) int64 { return a.ID }), toAlbumJSON)
|
|
default:
|
|
p, err := h.catalog.Tracks(ctx, f)
|
|
writeFavorites(c, err, ids, total, limit, offset, byID(p.Items, func(t catalog.Track) int64 { return t.ID }), toTrackJSON)
|
|
}
|
|
}
|
|
|
|
func byID[T any](items []T, id func(T) int64) map[int64]T {
|
|
m := make(map[int64]T, len(items))
|
|
for _, it := range items {
|
|
m[id(it)] = it
|
|
}
|
|
return m
|
|
}
|
|
|
|
// write answers with the items in the order of the ids.
|
|
func writeFavorites[T, J any](c *gin.Context, err error, ids []int64, total int, limit, offset int64, items map[int64]T, conv func(T) J) {
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
out := make([]J, 0, len(ids))
|
|
for _, id := range ids {
|
|
if it, ok := items[id]; ok {
|
|
out = append(out, conv(it))
|
|
}
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"items": out, "total": total, "limit": pageLimit(limit), "offset": clampInt(offset)})
|
|
}
|
|
|
|
// pageLimit is the size of the page the service gave for the limit that was asked.
|
|
func pageLimit(asked int64) int {
|
|
switch n := clampInt(asked); {
|
|
case n == 0:
|
|
return favorites.DefaultLimit
|
|
case n > favorites.MaxLimit:
|
|
return favorites.MaxLimit
|
|
default:
|
|
return n
|
|
}
|
|
}
|