107 lines
3.7 KiB
Go
107 lines
3.7 KiB
Go
package api
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/ponzischeme89/memby/server/internal/store"
|
||
|
|
)
|
||
|
|
|
||
|
|
type radarrProfileOption struct {
|
||
|
|
ID int `json:"id"`
|
||
|
|
Name string `json:"name"`
|
||
|
|
Recommended bool `json:"recommended"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type radarrRequestAdminPolicy struct {
|
||
|
|
QualityProfileID int `json:"qualityProfileId"`
|
||
|
|
SearchImmediately bool `json:"searchImmediately"`
|
||
|
|
Profiles []radarrProfileOption `json:"profiles"`
|
||
|
|
RecommendedID int `json:"recommendedId"`
|
||
|
|
Configured bool `json:"configured"`
|
||
|
|
Error string `json:"error,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) radarrRequestAdminPolicy(ctx context.Context) radarrRequestAdminPolicy {
|
||
|
|
policy, err := s.store.RadarrRequestPolicy(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return radarrRequestAdminPolicy{Profiles: []radarrProfileOption{}, Error: "Could not read the Radarr request policy."}
|
||
|
|
}
|
||
|
|
result := radarrRequestAdminPolicy{QualityProfileID: policy.QualityProfileID, SearchImmediately: policy.SearchImmediately, Profiles: []radarrProfileOption{}}
|
||
|
|
if s.radarr == nil {
|
||
|
|
result.Error = "Radarr is not configured."
|
||
|
|
return result
|
||
|
|
}
|
||
|
|
profiles, err := s.radarr.QualityProfiles(ctx)
|
||
|
|
if err != nil {
|
||
|
|
result.Error = "Could not read Radarr quality profiles."
|
||
|
|
return result
|
||
|
|
}
|
||
|
|
for _, profile := range profiles {
|
||
|
|
recommended := is720pProfile(profile.Name)
|
||
|
|
if recommended && result.RecommendedID == 0 {
|
||
|
|
result.RecommendedID = profile.ID
|
||
|
|
}
|
||
|
|
if profile.ID == policy.QualityProfileID {
|
||
|
|
result.Configured = true
|
||
|
|
}
|
||
|
|
result.Profiles = append(result.Profiles, radarrProfileOption{ID: profile.ID, Name: profile.Name, Recommended: recommended})
|
||
|
|
}
|
||
|
|
if policy.QualityProfileID == 0 && result.RecommendedID != 0 {
|
||
|
|
result.QualityProfileID, result.Configured = result.RecommendedID, true
|
||
|
|
}
|
||
|
|
if !result.Configured && result.Error == "" {
|
||
|
|
result.Error = "Choose an existing Radarr quality profile before accepting movie requests."
|
||
|
|
}
|
||
|
|
return result
|
||
|
|
}
|
||
|
|
|
||
|
|
type radarrRequestPolicyRequest struct {
|
||
|
|
QualityProfileID int `json:"qualityProfileId"`
|
||
|
|
SearchImmediately bool `json:"searchImmediately"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) handleAdminRadarrRequestPolicy(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if r.Method == http.MethodGet {
|
||
|
|
writeJSON(w, http.StatusOK, s.radarrRequestAdminPolicy(r.Context()))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if s.radarr == nil {
|
||
|
|
writeError(w, http.StatusServiceUnavailable, "Radarr is not configured")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var req radarrRequestPolicyRequest
|
||
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&req); err != nil {
|
||
|
|
writeError(w, http.StatusBadRequest, "malformed request body")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if req.QualityProfileID <= 0 {
|
||
|
|
writeError(w, http.StatusBadRequest, "choose a Radarr quality profile")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
profiles, err := s.radarr.QualityProfiles(r.Context())
|
||
|
|
if err != nil {
|
||
|
|
writeError(w, http.StatusBadGateway, "could not validate Radarr quality profiles")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
found := false
|
||
|
|
for _, profile := range profiles {
|
||
|
|
if profile.ID == req.QualityProfileID {
|
||
|
|
found = true
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !found {
|
||
|
|
writeError(w, http.StatusBadRequest, "the selected Radarr quality profile no longer exists")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := s.store.SetRadarrRequestPolicy(r.Context(), store.RadarrRequestPolicy{QualityProfileID: req.QualityProfileID, SearchImmediately: req.SearchImmediately}); err != nil {
|
||
|
|
s.loggerFor(r.Context()).Error("Radarr request policy write failed", "error", err)
|
||
|
|
writeError(w, http.StatusInternalServerError, "could not save Radarr request policy")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.loggerFor(r.Context()).Info("Radarr request policy changed", "quality_profile_id", req.QualityProfileID, "search_immediately", req.SearchImmediately)
|
||
|
|
writeJSON(w, http.StatusOK, s.radarrRequestAdminPolicy(r.Context()))
|
||
|
|
}
|