f77245bc41
After Lidarr accepts a request the local reconciler imports albums and
tracks as they arrive — but until the request hit 'completed' the
operator had no way to see "is anything happening?" The /requests and
/admin/requests rows now surface a live progress line whenever the
request's matched entity has children in our library.
Backend:
- New CountAlbumsByArtist + CountTracksByArtist sqlc queries.
- requestView gains imported_album_count and imported_track_count.
- New fillProgress helper computes them from the matched entity:
- kind=artist → counts albums + tracks under matched_artist_id
- kind=album → counts tracks under matched_album_id
- kind=track → 1 once matched_track_id is set
N+1 in the list endpoints; acceptable at admin scale.
- handleListRequests, handleGetRequest, and handleListAdminRequests
populate the new fields on every response.
Frontend:
- LidarrRequest TS type extended with the two counters.
- Both the operator's /requests page and the admin /admin/requests
page render an accent-colored line under the row meta when at
least one counter is non-zero, e.g.:
Artist: "5 albums · 47 tracks ingested"
Album: "12 tracks ingested"
Track: "Track ingested"
- Updated test fixtures to include the new required fields.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
169 lines
5.3 KiB
Go
169 lines
5.3 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarr"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrrequests"
|
|
)
|
|
|
|
// validRequestStatuses is the set of allowed values for the ?status= param.
|
|
var validRequestStatuses = map[string]bool{
|
|
"pending": true,
|
|
"approved": true,
|
|
"rejected": true,
|
|
"completed": true,
|
|
"failed": true,
|
|
}
|
|
|
|
// handleListAdminRequests implements GET /api/admin/requests.
|
|
// ?status= defaults to "pending"; ?limit= defaults to 50, max 200.
|
|
func (h *handlers) handleListAdminRequests(w http.ResponseWriter, r *http.Request) {
|
|
status := r.URL.Query().Get("status")
|
|
if status == "" {
|
|
status = "pending"
|
|
}
|
|
if !validRequestStatuses[status] {
|
|
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_status")
|
|
return
|
|
}
|
|
|
|
limitStr := r.URL.Query().Get("limit")
|
|
limit := 50
|
|
if limitStr != "" {
|
|
v, err := strconv.Atoi(limitStr)
|
|
if err != nil || v <= 0 {
|
|
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_limit")
|
|
return
|
|
}
|
|
if v > 200 {
|
|
v = 200
|
|
}
|
|
limit = v
|
|
}
|
|
|
|
rows, err := h.lidarrRequests.ListByStatus(r.Context(), status, int32(limit))
|
|
if err != nil {
|
|
h.logger.Error("admin: list requests", "err", err)
|
|
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
|
return
|
|
}
|
|
|
|
q := dbq.New(h.pool)
|
|
out := make([]requestView, 0, len(rows))
|
|
for _, row := range rows {
|
|
v := requestViewFrom(row)
|
|
fillProgress(r.Context(), q, row, &v)
|
|
out = append(out, v)
|
|
}
|
|
writeJSON(w, http.StatusOK, out)
|
|
}
|
|
|
|
// approveRequestBody is the optional JSON body for POST /api/admin/requests/:id/approve.
|
|
type approveRequestBody struct {
|
|
QualityProfileID int `json:"quality_profile_id"`
|
|
RootFolderPath string `json:"root_folder_path"`
|
|
}
|
|
|
|
// handleApproveRequest implements POST /api/admin/requests/:id/approve.
|
|
func (h *handlers) handleApproveRequest(w http.ResponseWriter, r *http.Request) {
|
|
admin, ok := auth.UserFromContext(r.Context())
|
|
if !ok {
|
|
writeAdminJSONErr(w, http.StatusUnauthorized, "unauthorized")
|
|
return
|
|
}
|
|
|
|
id, ok := parseUUID(chi.URLParam(r, "id"))
|
|
if !ok {
|
|
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
|
|
return
|
|
}
|
|
|
|
var body approveRequestBody
|
|
// Body is entirely optional; ignore decode errors.
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
|
|
row, err := h.lidarrRequests.Approve(r.Context(), id, admin.ID, lidarrrequests.ApproveOverrides{
|
|
QualityProfileID: body.QualityProfileID,
|
|
RootFolderPath: body.RootFolderPath,
|
|
})
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, lidarrrequests.ErrLidarrDisabled):
|
|
writeAdminJSONErr(w, http.StatusServiceUnavailable, "lidarr_disabled")
|
|
case errors.Is(err, lidarrrequests.ErrDefaultsIncomplete):
|
|
writeAdminJSONErr(w, http.StatusBadRequest, "lidarr_defaults_incomplete")
|
|
case errors.Is(err, lidarrrequests.ErrNotFound):
|
|
writeAdminJSONErr(w, http.StatusNotFound, "request_not_found")
|
|
case errors.Is(err, lidarrrequests.ErrNotPending):
|
|
writeAdminJSONErr(w, http.StatusConflict, "request_not_pending")
|
|
case errors.Is(err, lidarr.ErrUnreachable):
|
|
writeAdminJSONErr(w, http.StatusServiceUnavailable, "lidarr_unreachable")
|
|
case errors.Is(err, lidarr.ErrAuthFailed):
|
|
writeAdminJSONErr(w, http.StatusServiceUnavailable, "lidarr_auth_failed")
|
|
case errors.Is(err, lidarr.ErrServerError):
|
|
// Lidarr itself 5xx'd — its response body is in err's wrapped
|
|
// message (see lidarr.readBodySnippet). Logged below for ops.
|
|
h.logger.Error("admin: approve request: lidarr 5xx", "err", err)
|
|
writeAdminJSONErr(w, http.StatusBadGateway, "lidarr_server_error")
|
|
case errors.Is(err, lidarr.ErrLookupFailed):
|
|
// Lidarr returned 4xx — usually "artist already exists" or
|
|
// "no metadata found." Log so ops can see the actual reason.
|
|
h.logger.Warn("admin: approve request: lidarr 4xx", "err", err)
|
|
writeAdminJSONErr(w, http.StatusBadGateway, "lidarr_rejected")
|
|
default:
|
|
h.logger.Error("admin: approve request", "err", err)
|
|
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
|
}
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, requestViewFrom(row))
|
|
}
|
|
|
|
// rejectRequestBody is the optional JSON body for POST /api/admin/requests/:id/reject.
|
|
type rejectRequestBody struct {
|
|
Notes string `json:"notes"`
|
|
}
|
|
|
|
// handleRejectRequest implements POST /api/admin/requests/:id/reject.
|
|
func (h *handlers) handleRejectRequest(w http.ResponseWriter, r *http.Request) {
|
|
admin, ok := auth.UserFromContext(r.Context())
|
|
if !ok {
|
|
writeAdminJSONErr(w, http.StatusUnauthorized, "unauthorized")
|
|
return
|
|
}
|
|
|
|
id, ok := parseUUID(chi.URLParam(r, "id"))
|
|
if !ok {
|
|
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
|
|
return
|
|
}
|
|
|
|
var body rejectRequestBody
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
|
|
row, err := h.lidarrRequests.Reject(r.Context(), id, admin.ID, body.Notes)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, lidarrrequests.ErrNotFound):
|
|
writeAdminJSONErr(w, http.StatusNotFound, "request_not_found")
|
|
case errors.Is(err, lidarrrequests.ErrNotPending):
|
|
writeAdminJSONErr(w, http.StatusConflict, "request_not_pending")
|
|
default:
|
|
h.logger.Error("admin: reject request", "err", err)
|
|
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
|
}
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, requestViewFrom(row))
|
|
}
|