fix: polish player controls and remove fake track stats

This commit is contained in:
2026-04-03 02:27:24 +03:00
parent d7e21956db
commit db6e2818c1
8 changed files with 253 additions and 39 deletions

View File

@@ -171,11 +171,17 @@ func (a app) me(w http.ResponseWriter, r *http.Request) {
}
func (a app) home(w http.ResponseWriter, r *http.Request) {
user := currentUserFromContext(r)
home, err := a.library.Home(r.Context())
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load home"})
return
}
home.RecentTracks, err = a.library.PopulateTrackStats(r.Context(), user.ID, home.RecentTracks)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load home"})
return
}
writeJSON(w, http.StatusOK, home)
}
@@ -195,6 +201,11 @@ func (a app) recentlyPlayed(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load recent tracks"})
return
}
items, err = a.library.PopulateTrackStats(r.Context(), user.ID, items)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load recent tracks"})
return
}
writeJSON(w, http.StatusOK, map[string]any{"items": items})
}
@@ -221,6 +232,7 @@ func (a app) albums(w http.ResponseWriter, r *http.Request) {
}
func (a app) albumByID(w http.ResponseWriter, r *http.Request) {
user := currentUserFromContext(r)
item, err := a.library.AlbumByID(r.Context(), chi.URLParam(r, "id"))
if err != nil {
if errors.Is(err, library.ErrNotFound) {
@@ -230,19 +242,31 @@ func (a app) albumByID(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load album"})
return
}
item.Tracks, err = a.library.PopulateTrackStats(r.Context(), user.ID, item.Tracks)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load album"})
return
}
writeJSON(w, http.StatusOK, item)
}
func (a app) tracks(w http.ResponseWriter, r *http.Request) {
user := currentUserFromContext(r)
items, err := a.library.Tracks(r.Context(), 200)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load tracks"})
return
}
items, err = a.library.PopulateTrackStats(r.Context(), user.ID, items)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load tracks"})
return
}
writeJSON(w, http.StatusOK, map[string]any{"items": items})
}
func (a app) trackByID(w http.ResponseWriter, r *http.Request) {
user := currentUserFromContext(r)
item, err := a.library.TrackByID(r.Context(), chi.URLParam(r, "id"))
if err != nil {
if errors.Is(err, library.ErrNotFound) {
@@ -252,6 +276,14 @@ func (a app) trackByID(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load track"})
return
}
enriched, err := a.library.PopulateTrackStats(r.Context(), user.ID, []library.Track{item})
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load track"})
return
}
if len(enriched) > 0 {
item = enriched[0]
}
writeJSON(w, http.StatusOK, item)
}