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}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user