sonda/internal/api/http/handlers_suggestions.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

120 lines
3.9 KiB
Go

package httpapi
import (
"context"
"errors"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"git.kmalloc.org/squid/sonda/internal/affinity"
"git.kmalloc.org/squid/sonda/internal/feedback"
)
// listeningSuggestionService is what the handlers need from affinity.Service.
type listeningSuggestionService interface {
Suggest(ctx context.Context, userID int64, limit int) (affinity.Suggestions, error)
AddToWishlist(ctx context.Context, userID int64, name string) (int64, error)
}
type listeningSuggestionHandlers struct{ svc listeningSuggestionService }
type ownedReasonJSON struct {
Name string `json:"name"`
Score float64 `json:"score"`
}
type listeningSuggestionJSON struct {
Name string `json:"name"`
ArtistID *int64 `json:"artist_id"`
Score float64 `json:"score"`
Plays int `json:"plays"`
LastPlayedAt time.Time `json:"last_played_at"`
Wishlisted bool `json:"wishlisted"`
Because []ownedReasonJSON `json:"because"`
}
type listeningSuggestionsJSON struct {
Library []listeningSuggestionJSON `json:"library"`
Missing []listeningSuggestionJSON `json:"missing"`
Built struct {
At time.Time `json:"at"`
Plays int `json:"plays"`
Sessions int `json:"sessions"`
Artists int `json:"artists"`
Links int `json:"links"`
} `json:"built"`
}
func toListeningSuggestions(in []affinity.Suggestion) []listeningSuggestionJSON {
out := make([]listeningSuggestionJSON, 0, len(in))
for _, s := range in {
j := listeningSuggestionJSON{Name: s.Name, Score: s.Score, Plays: s.Plays, LastPlayedAt: s.LastPlayed.UTC(), Wishlisted: s.Wishlisted, Because: make([]ownedReasonJSON, 0, len(s.Because))}
if s.ArtistID != 0 {
id := s.ArtistID
j.ArtistID = &id
}
for _, r := range s.Because {
j.Because = append(j.Because, ownedReasonJSON{Name: r.Name, Score: r.Score})
}
out = append(out, j)
}
return out
}
// list suggests artists from how the signed-in person listens.
func (h *listeningSuggestionHandlers) list(c *gin.Context) {
limit, ok := queryInt(c, "limit")
if !ok {
return
}
got, err := h.svc.Suggest(c.Request.Context(), currentUserID(c), clampInt(limit))
switch {
case errors.Is(err, affinity.ErrNotAllowed):
writeError(c, http.StatusBadRequest, "listening_not_allowed", err.Error())
return
case errors.Is(err, affinity.ErrInvalidLimit):
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
return
case err != nil:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
return
}
out := listeningSuggestionsJSON{Library: toListeningSuggestions(got.Library), Missing: toListeningSuggestions(got.Missing)}
out.Built.At, out.Built.Plays, out.Built.Sessions = got.Built.BuiltAt.UTC(), got.Built.Plays, got.Built.Sessions
out.Built.Artists, out.Built.Links = got.Built.Artists, got.Built.Links
c.JSON(http.StatusOK, out)
}
type wishRequest struct {
Name string `json:"name"`
}
// wish puts an artist the person has played on their wishlist, by name.
func (h *listeningSuggestionHandlers) wish(c *gin.Context) {
var req wishRequest
if !bindJSON(c, &req) {
return
}
if strings.TrimSpace(req.Name) == "" {
writeError(c, http.StatusBadRequest, "invalid_input", "name is required")
return
}
id, err := h.svc.AddToWishlist(c.Request.Context(), currentUserID(c), req.Name)
switch {
case err == nil:
c.JSON(http.StatusOK, gin.H{"artist_id": id})
case errors.Is(err, affinity.ErrNotAllowed):
writeError(c, http.StatusBadRequest, "listening_not_allowed", err.Error())
case errors.Is(err, affinity.ErrUnknownArtist):
writeError(c, http.StatusNotFound, "artist_not_played", err.Error())
case errors.Is(err, feedback.ErrUserNotFound):
writeError(c, http.StatusUnauthorized, "unauthorized", "sign in again")
default:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
}
}