Fix Sonarr TV request policy and bump gateway to 0.1.40

This commit is contained in:
ponzischeme89
2026-08-14 10:30:25 +12:00
parent 5e2ed3d12e
commit 19f08e6293
11 changed files with 366 additions and 29 deletions
+35 -16
View File
@@ -84,7 +84,31 @@ type RootFolder struct {
}
type QualityProfile struct {
ID int `json:"id"`
ID int `json:"id"`
Name string `json:"name"`
}
// RequestOptions are Memby's deliberate request policy. They must be supplied by the
// gateway: letting Sonarr choose a profile or search flag reintroduces unsafe defaults.
type RequestOptions struct {
QualityProfileID int
SearchImmediately bool
}
func (c *Client) RootFolders(ctx context.Context) ([]RootFolder, error) {
var roots []RootFolder
if err := c.get(ctx, "/api/v3/rootfolder", &roots); err != nil {
return nil, err
}
return roots, nil
}
func (c *Client) QualityProfiles(ctx context.Context) ([]QualityProfile, error) {
var profiles []QualityProfile
if err := c.get(ctx, "/api/v3/qualityprofile", &profiles); err != nil {
return nil, err
}
return profiles, nil
}
type EpisodeFile struct {
@@ -168,23 +192,18 @@ func (c *Client) Lookup(ctx context.Context, term string) ([]Series, error) {
return series, nil
}
// AddRequested adds a series, monitors its seasons and asks Sonarr to search for missing
// episodes immediately. An unmonitored catalogue row does not fulfil a media request.
func (c *Client) AddRequested(ctx context.Context, series Series) (Series, error) {
var roots []RootFolder
if err := c.get(ctx, "/api/v3/rootfolder", &roots); err != nil {
return Series{}, err
// AddRequested adds a monitored series using the supplied request policy. It never reads
// Sonarr's first quality profile, because that is commonly "Any".
func (c *Client) AddRequested(ctx context.Context, series Series, rootFolder string, options RequestOptions) (Series, error) {
if strings.TrimSpace(rootFolder) == "" {
return Series{}, fmt.Errorf("sonarr: request root folder is required")
}
var profiles []QualityProfile
if err := c.get(ctx, "/api/v3/qualityprofile", &profiles); err != nil {
return Series{}, err
}
if len(roots) == 0 || len(profiles) == 0 {
return Series{}, fmt.Errorf("sonarr: no root folder or quality profile configured")
if options.QualityProfileID <= 0 {
return Series{}, fmt.Errorf("sonarr: request quality profile is required")
}
series.ID = 0
series.RootFolderPath = roots[0].Path
series.QualityProfileID = profiles[0].ID
series.RootFolderPath = rootFolder
series.QualityProfileID = options.QualityProfileID
series.Monitored = true
series.SeasonFolder = true
for i := range series.Seasons {
@@ -193,7 +212,7 @@ func (c *Client) AddRequested(ctx context.Context, series Series) (Series, error
body := struct {
Series
AddOptions map[string]bool `json:"addOptions"`
}{Series: series, AddOptions: map[string]bool{"searchForMissingEpisodes": true}}
}{Series: series, AddOptions: map[string]bool{"searchForMissingEpisodes": options.SearchImmediately}}
var added Series
if err := c.post(ctx, "/api/v3/series", body, &added); err != nil {
return Series{}, err
+4 -8
View File
@@ -46,13 +46,9 @@ func TestCalendarUsesV3APIAndKeepsKeyInHeader(t *testing.T) {
}
}
func TestAddRequestedUsesDefaultsAndStartsSearch(t *testing.T) {
func TestAddRequestedUsesExplicitPolicyWithoutSearching(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v3/rootfolder":
_, _ = w.Write([]byte(`[{"path":"/tv"}]`))
case "/api/v3/qualityprofile":
_, _ = w.Write([]byte(`[{"id":3}]`))
case "/api/v3/series":
var body map[string]any
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
@@ -67,8 +63,8 @@ func TestAddRequestedUsesDefaultsAndStartsSearch(t *testing.T) {
t.Errorf("season was not monitored: %#v", body)
}
options := body["addOptions"].(map[string]any)
if options["searchForMissingEpisodes"] != true {
t.Errorf("episode search was not enabled: %#v", body)
if options["searchForMissingEpisodes"] != false {
t.Errorf("episode search was unexpectedly enabled: %#v", body)
}
_, _ = w.Write([]byte(`{"id":8,"tvdbId":44,"title":"Severance"}`))
default:
@@ -81,7 +77,7 @@ func TestAddRequestedUsesDefaultsAndStartsSearch(t *testing.T) {
context.Background(), Series{
TVDBID: 44, Title: "Severance",
Seasons: []Season{{SeasonNumber: 1}},
},
}, "/tv", RequestOptions{QualityProfileID: 3, SearchImmediately: false},
)
if err != nil {
t.Fatal(err)