feat(subsonic): wire contextual_likes capture/soft-delete into star/unstar
handleStar's track branch calls playevents.CaptureContextualLikeIfPlaying when the underlying LikeTrack actually inserted a row. handleUnstar calls SoftDeleteContextualLikes after every track-id unstar. Same helpers as the api surface — single source of truth. mediaHandlers struct gains a logger field threaded through Mount.
This commit is contained in:
@@ -57,7 +57,7 @@ func testScrobblePool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track) {
|
|||||||
func newScrobbleHandlers(pool *pgxpool.Pool) *mediaHandlers {
|
func newScrobbleHandlers(pool *pgxpool.Pool) *mediaHandlers {
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
|
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
|
||||||
return newMediaHandlers(pool, w)
|
return newMediaHandlers(pool, w, logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleScrobble_SubmissionFalseInsertsOpenPlayEvent(t *testing.T) {
|
func TestHandleScrobble_SubmissionFalseInsertsOpenPlayEvent(t *testing.T) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
||||||
)
|
)
|
||||||
|
|
||||||
// handleStar implements /rest/star. Accepts any combination of id (track),
|
// handleStar implements /rest/star. Accepts any combination of id (track),
|
||||||
@@ -38,10 +39,14 @@ func (m *mediaHandlers) handleStar(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if trackID.Valid {
|
if trackID.Valid {
|
||||||
if _, err := q.LikeTrack(r.Context(), dbq.LikeTrackParams{UserID: user.ID, TrackID: trackID}); err != nil {
|
rows, err := q.LikeTrack(r.Context(), dbq.LikeTrackParams{UserID: user.ID, TrackID: trackID})
|
||||||
|
if err != nil {
|
||||||
WriteFail(w, r, ErrGeneric, "Could not star track")
|
WriteFail(w, r, ErrGeneric, "Could not star track")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if rows == 1 {
|
||||||
|
_ = playevents.CaptureContextualLikeIfPlaying(r.Context(), q, user.ID, trackID, m.logger)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if albumID.Valid {
|
if albumID.Valid {
|
||||||
if err := q.LikeAlbum(r.Context(), dbq.LikeAlbumParams{UserID: user.ID, AlbumID: albumID}); err != nil {
|
if err := q.LikeAlbum(r.Context(), dbq.LikeAlbumParams{UserID: user.ID, AlbumID: albumID}); err != nil {
|
||||||
@@ -71,6 +76,7 @@ func (m *mediaHandlers) handleUnstar(w http.ResponseWriter, r *http.Request) {
|
|||||||
q := dbq.New(m.pool)
|
q := dbq.New(m.pool)
|
||||||
if t, ok := parseUUID(params.Get("id")); ok {
|
if t, ok := parseUUID(params.Get("id")); ok {
|
||||||
_ = q.UnlikeTrack(r.Context(), dbq.UnlikeTrackParams{UserID: user.ID, TrackID: t})
|
_ = q.UnlikeTrack(r.Context(), dbq.UnlikeTrackParams{UserID: user.ID, TrackID: t})
|
||||||
|
_ = playevents.SoftDeleteContextualLikes(r.Context(), q, user.ID, t)
|
||||||
}
|
}
|
||||||
if a, ok := parseUUID(params.Get("albumId")); ok {
|
if a, ok := parseUUID(params.Get("albumId")); ok {
|
||||||
_ = q.UnlikeAlbum(r.Context(), dbq.UnlikeAlbumParams{UserID: user.ID, AlbumID: a})
|
_ = q.UnlikeAlbum(r.Context(), dbq.UnlikeAlbumParams{UserID: user.ID, AlbumID: a})
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ func testStarPool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track, dbq.Album,
|
|||||||
func newStarHandlers(pool *pgxpool.Pool) *mediaHandlers {
|
func newStarHandlers(pool *pgxpool.Pool) *mediaHandlers {
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
|
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
|
||||||
return newMediaHandlers(pool, w)
|
return newMediaHandlers(pool, w, logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleStar_TrackIDInsertsRow(t *testing.T) {
|
func TestHandleStar_TrackIDInsertsRow(t *testing.T) {
|
||||||
@@ -249,3 +249,49 @@ func TestHandleGetStarred2_CrossUserIsolation(t *testing.T) {
|
|||||||
t.Errorf("bob's starred songs should be empty, got %d", len(songs))
|
t.Errorf("bob's starred songs should be empty, got %d", len(songs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandleStar_DuringNowPlaying_WritesContextualLike(t *testing.T) {
|
||||||
|
pool, user, track1, _, _ := testStarPool(t)
|
||||||
|
// Add a second track so star refers to a different track than the one playing.
|
||||||
|
q := dbq.New(pool)
|
||||||
|
a, _ := q.UpsertArtist(context.Background(), dbq.UpsertArtistParams{Name: "Y", SortName: "Y"})
|
||||||
|
al, _ := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{Title: "Y", SortTitle: "Y", ArtistID: a.ID})
|
||||||
|
track2, _ := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
|
||||||
|
Title: "Star Me", AlbumID: al.ID, ArtistID: a.ID, FilePath: "/tmp/t2.flac", DurationMs: 100_000,
|
||||||
|
})
|
||||||
|
|
||||||
|
m := newStarHandlers(pool)
|
||||||
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
|
m.logger = logger
|
||||||
|
|
||||||
|
// Start now-playing for track1 (creates open play_event with vector).
|
||||||
|
q1 := url.Values{}
|
||||||
|
q1.Set("id", uuidToID(track1.ID))
|
||||||
|
q1.Set("submission", "false")
|
||||||
|
scrobReq := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q1.Encode(), nil)
|
||||||
|
scrobCtx := context.WithValue(scrobReq.Context(), userCtxKey, user)
|
||||||
|
scrobResp := httptest.NewRecorder()
|
||||||
|
m.handleScrobble(scrobResp, scrobReq.WithContext(scrobCtx))
|
||||||
|
if scrobResp.Code != http.StatusOK {
|
||||||
|
t.Fatalf("scrobble: %d", scrobResp.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Star track2.
|
||||||
|
q2 := url.Values{}
|
||||||
|
q2.Set("id", uuidToID(track2.ID))
|
||||||
|
starReq := httptest.NewRequest(http.MethodGet, "/rest/star?"+q2.Encode(), nil)
|
||||||
|
starCtx := context.WithValue(starReq.Context(), userCtxKey, user)
|
||||||
|
starResp := httptest.NewRecorder()
|
||||||
|
m.handleStar(starResp, starReq.WithContext(starCtx))
|
||||||
|
if starResp.Code != http.StatusOK {
|
||||||
|
t.Fatalf("star: %d", starResp.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify contextual_likes row.
|
||||||
|
var count int
|
||||||
|
_ = pool.QueryRow(context.Background(),
|
||||||
|
"SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND track_id=$2", user.ID, track2.ID).Scan(&count)
|
||||||
|
if count != 1 {
|
||||||
|
t.Errorf("contextual_likes count = %d, want 1", count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -24,10 +25,11 @@ import (
|
|||||||
type mediaHandlers struct {
|
type mediaHandlers struct {
|
||||||
pool *pgxpool.Pool
|
pool *pgxpool.Pool
|
||||||
events *playevents.Writer
|
events *playevents.Writer
|
||||||
|
logger *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMediaHandlers(pool *pgxpool.Pool, events *playevents.Writer) *mediaHandlers {
|
func newMediaHandlers(pool *pgxpool.Pool, events *playevents.Writer, logger *slog.Logger) *mediaHandlers {
|
||||||
return &mediaHandlers{pool: pool, events: events}
|
return &mediaHandlers{pool: pool, events: events, logger: logger}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleStream serves the raw track bytes. http.ServeContent gives us Range,
|
// handleStream serves the raw track bytes. http.ServeContent gives us Range,
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import (
|
|||||||
// either way.
|
// either way.
|
||||||
func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, cfg Config, events *playevents.Writer) {
|
func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, cfg Config, events *playevents.Writer) {
|
||||||
b := &browseHandlers{pool: pool}
|
b := &browseHandlers{pool: pool}
|
||||||
m := newMediaHandlers(pool, events)
|
m := newMediaHandlers(pool, events, logger)
|
||||||
r.Route("/rest", func(sub chi.Router) {
|
r.Route("/rest", func(sub chi.Router) {
|
||||||
sub.Use(Middleware(pool, cfg))
|
sub.Use(Middleware(pool, cfg))
|
||||||
register(sub, "/ping", handlePing)
|
register(sub, "/ping", handlePing)
|
||||||
@@ -52,7 +52,6 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, cfg Config, ev
|
|||||||
sub.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
sub.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
||||||
WriteFail(w, r, ErrGeneric, "Method not implemented")
|
WriteFail(w, r, ErrGeneric, "Method not implemented")
|
||||||
})
|
})
|
||||||
_ = logger
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user