Fix Sonarr TV request policy and bump gateway to 0.1.40
This commit is contained in:
@@ -59,6 +59,8 @@ func (s *Server) adminRoutes() http.Handler {
|
||||
mux.Handle("POST /admin/api/deployment-alert", s.adminAuth(s.handleAdminDeploymentAlert))
|
||||
mux.Handle("POST /admin/api/update-policy", s.adminAuth(s.handleAdminUpdatePolicy))
|
||||
mux.Handle("POST /admin/api/request-policy", s.adminAuth(s.handleAdminRequestPolicy))
|
||||
mux.Handle("GET /admin/api/sonarr-request-policy", s.adminAuth(s.handleAdminSonarrRequestPolicy))
|
||||
mux.Handle("POST /admin/api/sonarr-request-policy", s.adminAuth(s.handleAdminSonarrRequestPolicy))
|
||||
mux.Handle("POST /admin/api/playback-policy", s.adminAuth(s.handleAdminPlaybackPolicy))
|
||||
mux.Handle("GET /admin/api/hero/search", s.adminAuth(s.handleAdminHeroSearch))
|
||||
mux.Handle("POST /admin/api/hero-policy", s.adminAuth(s.handleAdminHeroPolicy))
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/ponzischeme89/memby/server/internal/store"
|
||||
)
|
||||
|
||||
type sonarrProfileOption struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Recommended bool `json:"recommended"`
|
||||
}
|
||||
|
||||
type sonarrRequestAdminPolicy struct {
|
||||
QualityProfileID int `json:"qualityProfileId"`
|
||||
SearchImmediately bool `json:"searchImmediately"`
|
||||
Profiles []sonarrProfileOption `json:"profiles"`
|
||||
RecommendedID int `json:"recommendedId"`
|
||||
Configured bool `json:"configured"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) sonarrRequestAdminPolicy(ctx context.Context) sonarrRequestAdminPolicy {
|
||||
policy, err := s.store.SonarrRequestPolicy(ctx)
|
||||
if err != nil {
|
||||
return sonarrRequestAdminPolicy{Profiles: []sonarrProfileOption{}, Error: "Could not read the Sonarr request policy."}
|
||||
}
|
||||
result := sonarrRequestAdminPolicy{QualityProfileID: policy.QualityProfileID, SearchImmediately: policy.SearchImmediately, Profiles: []sonarrProfileOption{}}
|
||||
if s.sonarr == nil {
|
||||
result.Error = "Sonarr is not configured."
|
||||
return result
|
||||
}
|
||||
profiles, err := s.sonarr.QualityProfiles(ctx)
|
||||
if err != nil {
|
||||
result.Error = "Could not read Sonarr 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, sonarrProfileOption{ID: profile.ID, Name: profile.Name, Recommended: recommended})
|
||||
}
|
||||
if policy.QualityProfileID == 0 && result.RecommendedID != 0 {
|
||||
result.QualityProfileID = result.RecommendedID
|
||||
result.Configured = true
|
||||
}
|
||||
if !result.Configured && result.Error == "" {
|
||||
result.Error = "Choose an existing Sonarr quality profile before accepting TV requests."
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type sonarrRequestPolicyRequest struct {
|
||||
QualityProfileID int `json:"qualityProfileId"`
|
||||
SearchImmediately bool `json:"searchImmediately"`
|
||||
}
|
||||
|
||||
func (s *Server) handleAdminSonarrRequestPolicy(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
writeJSON(w, http.StatusOK, s.sonarrRequestAdminPolicy(r.Context()))
|
||||
return
|
||||
}
|
||||
if s.sonarr == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "Sonarr is not configured")
|
||||
return
|
||||
}
|
||||
var req sonarrRequestPolicyRequest
|
||||
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 Sonarr quality profile")
|
||||
return
|
||||
}
|
||||
profiles, err := s.sonarr.QualityProfiles(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadGateway, "could not validate Sonarr 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 Sonarr quality profile no longer exists")
|
||||
return
|
||||
}
|
||||
if err := s.store.SetSonarrRequestPolicy(r.Context(), store.SonarrRequestPolicy{QualityProfileID: req.QualityProfileID, SearchImmediately: req.SearchImmediately}); err != nil {
|
||||
s.loggerFor(r.Context()).Error("Sonarr request policy write failed", "error", err)
|
||||
writeError(w, http.StatusInternalServerError, "could not save Sonarr request policy")
|
||||
return
|
||||
}
|
||||
s.loggerFor(r.Context()).Info("Sonarr request policy changed", "quality_profile_id", req.QualityProfileID, "search_immediately", req.SearchImmediately)
|
||||
writeJSON(w, http.StatusOK, s.sonarrRequestAdminPolicy(r.Context()))
|
||||
}
|
||||
|
||||
func is720pProfile(name string) bool { return strings.EqualFold(strings.TrimSpace(name), "720p") }
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ponzischeme89/memby/server/internal/adminevents"
|
||||
"github.com/ponzischeme89/memby/server/internal/radarr"
|
||||
"github.com/ponzischeme89/memby/server/internal/sonarr"
|
||||
"github.com/ponzischeme89/memby/server/internal/store"
|
||||
@@ -302,13 +303,22 @@ func (s *Server) handleRequest(w http.ResponseWriter, r *http.Request, sess stor
|
||||
return
|
||||
}
|
||||
req.Title = show.Title
|
||||
added, err := s.sonarr.AddRequested(r.Context(), show)
|
||||
requestOptions, rootFolder, profileName, err := s.sonarrRequestOptions(r.Context())
|
||||
if err != nil {
|
||||
s.logSonarrRequest(r.Context(), sess, req, show, 0, "failed", "", 0, "", false, err)
|
||||
s.publishSonarrRequestConfigurationProblem(r.Context(), err)
|
||||
writeError(w, http.StatusServiceUnavailable, "TV requests are unavailable: "+err.Error())
|
||||
return
|
||||
}
|
||||
added, err := s.sonarr.AddRequested(r.Context(), show, rootFolder, requestOptions)
|
||||
if err != nil {
|
||||
s.logSonarrRequest(r.Context(), sess, req, show, 0, "failed", profileName, requestOptions.QualityProfileID, rootFolder, requestOptions.SearchImmediately, err)
|
||||
s.logMediaRequest(r.Context(), req, "failed", err)
|
||||
s.writeRequestUpstreamError(r.Context(), w, err, "could not request that series")
|
||||
return
|
||||
}
|
||||
req.Title = added.Title
|
||||
s.logSonarrRequest(r.Context(), sess, req, added, added.ID, "successful", profileName, requestOptions.QualityProfileID, rootFolder, requestOptions.SearchImmediately, nil)
|
||||
s.recordMediaRequest(r.Context(), sess, req, added.Year,
|
||||
sonarrCoverURL(added.Images, "poster"))
|
||||
s.logMediaRequest(r.Context(), req, "successful", nil)
|
||||
@@ -378,3 +388,80 @@ func (s *Server) writeRequestUpstreamError(
|
||||
s.loggerFor(ctx).Error(message, "error", err)
|
||||
writeError(w, http.StatusBadGateway, message)
|
||||
}
|
||||
|
||||
// sonarrRequestOptions validates every component before a POST can reach Sonarr. A missing
|
||||
// configured profile is an error, not permission to fall back to Sonarr's "Any" profile.
|
||||
func (s *Server) sonarrRequestOptions(ctx context.Context) (sonarr.RequestOptions, string, string, error) {
|
||||
if s.sonarr == nil {
|
||||
return sonarr.RequestOptions{}, "", "", errors.New("Sonarr integration is unavailable")
|
||||
}
|
||||
policy, err := s.store.SonarrRequestPolicy(ctx)
|
||||
if err != nil {
|
||||
return sonarr.RequestOptions{}, "", "", errors.New("could not read the Sonarr request policy")
|
||||
}
|
||||
roots, err := s.sonarr.RootFolders(ctx)
|
||||
if err != nil {
|
||||
return sonarr.RequestOptions{}, "", "", errors.New("could not validate Sonarr root folders")
|
||||
}
|
||||
if len(roots) == 0 || strings.TrimSpace(roots[0].Path) == "" {
|
||||
return sonarr.RequestOptions{}, "", "", errors.New("Sonarr has no valid root folder")
|
||||
}
|
||||
profiles, err := s.sonarr.QualityProfiles(ctx)
|
||||
if err != nil {
|
||||
return sonarr.RequestOptions{}, "", "", errors.New("could not validate Sonarr quality profiles")
|
||||
}
|
||||
profileID := policy.QualityProfileID
|
||||
if profileID == 0 {
|
||||
for _, profile := range profiles {
|
||||
if is720pProfile(profile.Name) {
|
||||
profileID = profile.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
if profileID == 0 {
|
||||
return sonarr.RequestOptions{}, "", "", errors.New("no request quality profile is configured and Sonarr has no 720p profile")
|
||||
}
|
||||
// First-run policy is the safe 720p recommendation. Persist its id immediately so
|
||||
// later profile renames or deletion are caught as configuration errors instead of
|
||||
// becoming a fresh name-based selection.
|
||||
policy.QualityProfileID = profileID
|
||||
if err := s.store.SetSonarrRequestPolicy(ctx, policy); err != nil {
|
||||
return sonarr.RequestOptions{}, "", "", errors.New("could not save the default Sonarr request quality profile")
|
||||
}
|
||||
}
|
||||
for _, profile := range profiles {
|
||||
if profile.ID == profileID {
|
||||
return sonarr.RequestOptions{QualityProfileID: profile.ID, SearchImmediately: policy.SearchImmediately}, roots[0].Path, profile.Name, nil
|
||||
}
|
||||
}
|
||||
return sonarr.RequestOptions{}, "", "", errors.New("the configured Sonarr request quality profile no longer exists")
|
||||
}
|
||||
|
||||
func (s *Server) logSonarrRequest(
|
||||
ctx context.Context, sess store.Session, req requestPayload, series sonarr.Series, seriesID int,
|
||||
outcome, profileName string, profileID int, rootFolder string, searchImmediately bool, err error,
|
||||
) {
|
||||
fields := []any{
|
||||
"user", clientLogValue(sess.Username), "user_id", sess.EmbyUserID,
|
||||
"title", clientLogValue(series.Title), "tvdb_id", series.TVDBID,
|
||||
"sonarr_series_id", seriesID, "quality_profile", profileName,
|
||||
"quality_profile_id", profileID, "monitoring_strategy", "all",
|
||||
"root_folder", rootFolder, "search_immediately", searchImmediately,
|
||||
"outcome", outcome,
|
||||
}
|
||||
if err != nil {
|
||||
fields = append(fields, "sonarr_result", err.Error())
|
||||
s.loggerFor(ctx).Warn("Sonarr TV request", fields...)
|
||||
return
|
||||
}
|
||||
fields = append(fields, "sonarr_result", "created")
|
||||
s.loggerFor(ctx).Info("Sonarr TV request", fields...)
|
||||
}
|
||||
|
||||
func (s *Server) publishSonarrRequestConfigurationProblem(ctx context.Context, err error) {
|
||||
s.publishAdmin(ctx, adminevents.Event{
|
||||
Type: "sonarr.request_configuration", Severity: adminevents.SeverityError,
|
||||
Title: "Sonarr TV requests need attention", Summary: err.Error(), Actor: "memby-server",
|
||||
Link: "/admin/integrations", Metadata: adminevents.Meta(map[string]any{"error": err.Error()}),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user