refactor: drop code nothing calls, and the sync log rows no app reads (#8002)
release / go (push) Successful in 1m56s
release / web (push) Successful in 1m18s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m0s
release / android (push) Successful in 4m20s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 25s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 1m56s
release / web (push) Successful in 1m18s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m0s
release / android (push) Successful in 4m20s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 25s
release / Verify release artifacts (tag releases only) (push) Skipped
- library.ProbeDurationMs and IsAudioFile, and db.MigrationsFS, had no callers. - The delta sync logged and hydrated like and playlist changes on every like, playlist edit and duplicate merge, including a GetPlaylistsByIDs query per sync. Android applies only artists, albums and tracks, and the web doesn't sync; likes and playlists reach both through their own endpoints and live events. The like and playlist-entry deletes also went to every caller unscoped, user ids included. They are no longer written or served, and migration 0112 clears the rows already logged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -41,8 +41,7 @@ type syncResponse struct {
|
||||
// has handed out
|
||||
// - 401 / 500 — standard envelope errors
|
||||
func (h *handlers) handleLibrarySync(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := requireUser(w, r)
|
||||
if !ok {
|
||||
if _, ok := requireUser(w, r); !ok {
|
||||
return
|
||||
}
|
||||
ctx := r.Context()
|
||||
@@ -121,7 +120,7 @@ func (h *handlers) handleLibrarySync(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
upserts, err := h.hydrateUpserts(ctx, q, user.ID, upsertIDs)
|
||||
upserts, err := h.hydrateUpserts(ctx, q, upsertIDs)
|
||||
if err != nil {
|
||||
writeErrWithLog(w, h.logger, "library_sync: hydrateUpserts", err)
|
||||
return
|
||||
@@ -141,16 +140,11 @@ func (h *handlers) handleLibrarySync(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// hydrateUpserts loads the current row payloads for each upserted id,
|
||||
// keyed by entity type (string form). Returns json.RawMessage values so
|
||||
// each entity's full sqlc row shape passes through unchanged.
|
||||
//
|
||||
// Per-user entities (likes, playlists, playlist_tracks) are scoped to
|
||||
// userID — the change log is global but each user only sees rows that
|
||||
// concern them.
|
||||
// keyed by entity type (string form). The log covers the shared library
|
||||
// only (artists, albums, tracks), so every caller sees the same rows.
|
||||
func (h *handlers) hydrateUpserts(
|
||||
ctx context.Context,
|
||||
q *dbq.Queries,
|
||||
userID pgtype.UUID,
|
||||
ids map[syncpkg.EntityType][]string,
|
||||
) (map[string][]json.RawMessage, error) {
|
||||
out := map[string][]json.RawMessage{}
|
||||
@@ -197,73 +191,9 @@ func (h *handlers) hydrateUpserts(
|
||||
}
|
||||
}
|
||||
|
||||
userIDStr := syncpkg.FormatUUID(userID)
|
||||
|
||||
if rows := ids[syncpkg.EntityLikeTrack]; len(rows) > 0 {
|
||||
out["like_track"] = scopedLikeRows(rows, userIDStr, "track_id")
|
||||
}
|
||||
if rows := ids[syncpkg.EntityLikeAlbum]; len(rows) > 0 {
|
||||
out["like_album"] = scopedLikeRows(rows, userIDStr, "album_id")
|
||||
}
|
||||
if rows := ids[syncpkg.EntityLikeArtist]; len(rows) > 0 {
|
||||
out["like_artist"] = scopedLikeRows(rows, userIDStr, "artist_id")
|
||||
}
|
||||
|
||||
if rows := ids[syncpkg.EntityPlaylist]; len(rows) > 0 {
|
||||
uuids := stringsToUUIDs(rows)
|
||||
playlists, err := q.GetPlaylistsByIDs(ctx, uuids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, p := range playlists {
|
||||
// Filter: only return playlists owned by this user OR public ones
|
||||
if syncpkg.FormatUUID(p.UserID) != userIDStr && !p.IsPublic {
|
||||
continue
|
||||
}
|
||||
b, _ := json.Marshal(toPlaylistSyncView(p))
|
||||
out["playlist"] = append(out["playlist"], b)
|
||||
}
|
||||
}
|
||||
if rows := ids[syncpkg.EntityPlaylistTrack]; len(rows) > 0 {
|
||||
// playlist_track ids are "<playlist>:<track>" — we don't know
|
||||
// playlist ownership here without a JOIN. Pass through; client
|
||||
// reconciles against its locally-cached playlists.
|
||||
var msgs []json.RawMessage
|
||||
for _, id := range rows {
|
||||
parts := splitOnce(id, ":")
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
b, _ := json.Marshal(map[string]string{
|
||||
"playlist_id": parts[0],
|
||||
"track_id": parts[1],
|
||||
})
|
||||
msgs = append(msgs, b)
|
||||
}
|
||||
out["playlist_track"] = msgs
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// scopedLikeRows filters composite-key like rows to those owned by userID
|
||||
// and emits {user_id, <entityKey>} JSON shapes. entityKey is "track_id",
|
||||
// "album_id", or "artist_id" depending on the like table.
|
||||
func scopedLikeRows(rows []string, userIDStr, entityKey string) []json.RawMessage {
|
||||
var msgs []json.RawMessage
|
||||
for _, id := range rows {
|
||||
parts := splitOnce(id, ":")
|
||||
if len(parts) != 2 || parts[0] != userIDStr {
|
||||
continue
|
||||
}
|
||||
b, _ := json.Marshal(map[string]string{
|
||||
"user_id": parts[0],
|
||||
entityKey: parts[1],
|
||||
})
|
||||
msgs = append(msgs, b)
|
||||
}
|
||||
return msgs
|
||||
}
|
||||
|
||||
// stringsToUUIDs parses each string as a pgtype.UUID. Invalid entries
|
||||
// are skipped silently — they can't have come from a valid scan/mutation.
|
||||
func stringsToUUIDs(strs []string) []pgtype.UUID {
|
||||
@@ -277,14 +207,3 @@ func stringsToUUIDs(strs []string) []pgtype.UUID {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// splitOnce returns [before, after] split at the first occurrence of sep,
|
||||
// or [s] if sep doesn't appear. Used for composite-key parsing.
|
||||
func splitOnce(s, sep string) []string {
|
||||
for i := 0; i+len(sep) <= len(s); i++ {
|
||||
if s[i:i+len(sep)] == sep {
|
||||
return []string{s[:i], s[i+len(sep):]}
|
||||
}
|
||||
}
|
||||
return []string{s}
|
||||
}
|
||||
|
||||
@@ -146,66 +146,8 @@ func TestLibrarySync_DeleteIsReflected(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibrarySync_LikeRowScopedToUser(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
u := seedUser(t, pool, "alice", "x", false)
|
||||
other := seedUser(t, pool, "bob", "x", false)
|
||||
|
||||
// Two like_track rows: one for u, one for other. u's call should
|
||||
// see only their own row.
|
||||
ctx := context.Background()
|
||||
tx, _ := pool.Begin(ctx)
|
||||
_ = sync.LogChange(ctx, tx, sync.EntityLikeTrack,
|
||||
sync.EncodeLikeID(sync.FormatUUID(u.ID), "11111111-1111-1111-1111-111111111111"),
|
||||
sync.OpUpsert)
|
||||
_ = sync.LogChange(ctx, tx, sync.EntityLikeTrack,
|
||||
sync.EncodeLikeID(sync.FormatUUID(other.ID), "22222222-2222-2222-2222-222222222222"),
|
||||
sync.OpUpsert)
|
||||
_ = tx.Commit(ctx)
|
||||
|
||||
w := callLibrarySync(h, u, "0")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body = %s", w.Code, w.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Upserts map[string][]json.RawMessage `json:"upserts"`
|
||||
}
|
||||
if err := json.NewDecoder(w.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
got := body.Upserts["like_track"]
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("expected 1 like row scoped to alice, got %d", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// Pure unit tests below — run even with -short.
|
||||
|
||||
func TestSplitOnce(t *testing.T) {
|
||||
cases := []struct {
|
||||
in, sep string
|
||||
want []string
|
||||
}{
|
||||
{"a:b", ":", []string{"a", "b"}},
|
||||
{"a:b:c", ":", []string{"a", "b:c"}},
|
||||
{"abc", ":", []string{"abc"}},
|
||||
{":x", ":", []string{"", "x"}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := splitOnce(c.in, c.sep)
|
||||
if len(got) != len(c.want) {
|
||||
t.Errorf("splitOnce(%q,%q): len got=%d want=%d", c.in, c.sep, len(got), len(c.want))
|
||||
continue
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != c.want[i] {
|
||||
t.Errorf("splitOnce(%q,%q)[%d] = %q want %q", c.in, c.sep, i, got[i], c.want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A server restored from an older backup has handed out fewer cursors than an
|
||||
// app remembers; the app is told to reset rather than wait for ids it would
|
||||
// then skip. A compacted log is not that: the cursor the server handed out
|
||||
|
||||
@@ -124,29 +124,3 @@ func toTrackSyncView(t dbq.Track, g library.ReplayGain) trackSyncView {
|
||||
TrackPeak: g.TrackPeak,
|
||||
}
|
||||
}
|
||||
|
||||
type playlistSyncView struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
IsPublic bool `json:"is_public"`
|
||||
CoverPath *string `json:"cover_path"`
|
||||
TrackCount int32 `json:"track_count"`
|
||||
DurationSec int32 `json:"duration_sec"`
|
||||
SystemVariant *string `json:"system_variant"`
|
||||
}
|
||||
|
||||
func toPlaylistSyncView(p dbq.Playlist) playlistSyncView {
|
||||
return playlistSyncView{
|
||||
ID: syncpkg.FormatUUID(p.ID),
|
||||
UserID: syncpkg.FormatUUID(p.UserID),
|
||||
Name: p.Name,
|
||||
Description: p.Description,
|
||||
IsPublic: p.IsPublic,
|
||||
CoverPath: p.CoverPath,
|
||||
TrackCount: p.TrackCount,
|
||||
DurationSec: p.DurationSec,
|
||||
SystemVariant: p.SystemVariant,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,27 +123,3 @@ func TestSyncViews_CarryReplayGain(t *testing.T) {
|
||||
t.Errorf("unmeasured track_gain = %v (present %v), want an explicit null", v, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaylistSyncView_WireKeys(t *testing.T) {
|
||||
variant := "discover"
|
||||
p := dbq.Playlist{
|
||||
ID: validUUID, UserID: validUUID, Name: "Test",
|
||||
IsPublic: true, TrackCount: 5, DurationSec: 600,
|
||||
SystemVariant: &variant,
|
||||
}
|
||||
b, err := json.Marshal(toPlaylistSyncView(p))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertJSONKeys(t, "playlist", b, []string{
|
||||
"id", "user_id", "name", "description",
|
||||
"is_public", "cover_path", "track_count",
|
||||
"duration_sec", "system_variant",
|
||||
})
|
||||
// Sanity: system_variant carries the value, not just present.
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(b, &m)
|
||||
if got := m["system_variant"]; got != "discover" {
|
||||
t.Errorf("system_variant: want 'discover', got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,21 +11,8 @@ import (
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
||||
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
||||
)
|
||||
|
||||
// logLikeChange writes a library_changes row for a like / unlike. Best-
|
||||
// effort: a failure here means the next sync may miss the row, but the
|
||||
// HTTP response still succeeds. Cheap consequence — the user's other
|
||||
// device sees the like once it manually pulls, or once a later like/unlike
|
||||
// on the same entity emits a fresh log row.
|
||||
func (h *handlers) logLikeChange(r *http.Request, et syncpkg.EntityType, userID, entityID pgtype.UUID, op syncpkg.Op) {
|
||||
if err := syncpkg.LogChange(r.Context(), h.pool, et,
|
||||
syncpkg.EncodeLikeID(syncpkg.FormatUUID(userID), syncpkg.FormatUUID(entityID)), op); err != nil {
|
||||
h.logger.Warn("api: LogChange like", "kind", et, "user", userID, "entity", entityID, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
type likedIDsResponse struct {
|
||||
TrackIDs []string `json:"track_ids"`
|
||||
AlbumIDs []string `json:"album_ids"`
|
||||
@@ -63,7 +50,6 @@ func (h *handlers) handleLikeTrack(w http.ResponseWriter, r *http.Request) {
|
||||
// The like is on the song: every copy it reached changes on
|
||||
// the client too.
|
||||
for _, t := range withTrack(id, liked) {
|
||||
h.logLikeChange(r, syncpkg.EntityLikeTrack, user.ID, t, syncpkg.OpUpsert)
|
||||
h.publishLikeEvent(user.ID, t, "track", true)
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
@@ -102,7 +88,6 @@ func (h *handlers) handleUnlikeTrack(w http.ResponseWriter, r *http.Request) {
|
||||
// Don't fail the response — soft-delete is best-effort.
|
||||
}
|
||||
for _, t := range withTrack(id, unliked) {
|
||||
h.logLikeChange(r, syncpkg.EntityLikeTrack, user.ID, t, syncpkg.OpDelete)
|
||||
h.publishLikeEvent(user.ID, t, "track", false)
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
@@ -132,7 +117,6 @@ func (h *handlers) handleLikeAlbum(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, apierror.InternalMsg("insert failed", err))
|
||||
return
|
||||
}
|
||||
h.logLikeChange(r, syncpkg.EntityLikeAlbum, user.ID, id, syncpkg.OpUpsert)
|
||||
h.publishLikeEvent(user.ID, id, "album", true)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -151,7 +135,6 @@ func (h *handlers) handleUnlikeAlbum(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, apierror.InternalMsg("delete failed", err))
|
||||
return
|
||||
}
|
||||
h.logLikeChange(r, syncpkg.EntityLikeAlbum, user.ID, id, syncpkg.OpDelete)
|
||||
h.publishLikeEvent(user.ID, id, "album", false)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -180,7 +163,6 @@ func (h *handlers) handleLikeArtist(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, apierror.InternalMsg("insert failed", err))
|
||||
return
|
||||
}
|
||||
h.logLikeChange(r, syncpkg.EntityLikeArtist, user.ID, id, syncpkg.OpUpsert)
|
||||
h.publishLikeEvent(user.ID, id, "artist", true)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -199,7 +181,6 @@ func (h *handlers) handleUnlikeArtist(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, apierror.InternalMsg("delete failed", err))
|
||||
return
|
||||
}
|
||||
h.logLikeChange(r, syncpkg.EntityLikeArtist, user.ID, id, syncpkg.OpDelete)
|
||||
h.publishLikeEvent(user.ID, id, "artist", false)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user