Skip to content

Commit

Permalink
Correct Stash box endpoint inputs (#4924)
Browse files Browse the repository at this point in the history
* Use stashbox endpoint instead of index
* Update UI to not use deprecated fields
  • Loading branch information
WithoutPants authored Jun 11, 2024
1 parent 94a978d commit ed057c9
Show file tree
Hide file tree
Showing 17 changed files with 234 additions and 185 deletions.
16 changes: 11 additions & 5 deletions graphql/schema/types/scraper.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ input ScraperSourceInput {
stash_box_index: Int @deprecated(reason: "use stash_box_endpoint")
"Stash-box endpoint"
stash_box_endpoint: String
"Scraper ID to scrape with. Should be unset if stash_box_index is set"
"Scraper ID to scrape with. Should be unset if stash_box_endpoint/stash_box_index is set"
scraper_id: ID
}

Expand All @@ -137,7 +137,7 @@ type ScraperSource {
stash_box_index: Int @deprecated(reason: "use stash_box_endpoint")
"Stash-box endpoint"
stash_box_endpoint: String
"Scraper ID to scrape with. Should be unset if stash_box_index is set"
"Scraper ID to scrape with. Should be unset if stash_box_endpoint/stash_box_index is set"
scraper_id: ID
}

Expand Down Expand Up @@ -196,7 +196,9 @@ input ScrapeSingleMovieInput {

input StashBoxSceneQueryInput {
"Index of the configured stash-box instance to use"
stash_box_index: Int!
stash_box_index: Int @deprecated(reason: "use stash_box_endpoint")
"Endpoint of the stash-box instance to use"
stash_box_endpoint: String
"Instructs query by scene fingerprints"
scene_ids: [ID!]
"Query by query string"
Expand All @@ -205,7 +207,9 @@ input StashBoxSceneQueryInput {

input StashBoxPerformerQueryInput {
"Index of the configured stash-box instance to use"
stash_box_index: Int!
stash_box_index: Int @deprecated(reason: "use stash_box_endpoint")
"Endpoint of the stash-box instance to use"
stash_box_endpoint: String
"Instructs query by scene fingerprints"
performer_ids: [ID!]
"Query by query string"
Expand All @@ -226,7 +230,9 @@ type StashBoxFingerprint {
"If neither ids nor names are set, tag all items"
input StashBoxBatchTagInput {
"Stash endpoint to use for the tagging"
endpoint: Int!
endpoint: Int @deprecated(reason: "use stash_box_endpoint")
"Endpoint of the stash-box instance to use"
stash_box_endpoint: String
"Fields to exclude when executing the tagging"
exclude_fields: [String!]
"Refresh items already tagged by StashBox if true. Only tag items with no StashBox tagging if false"
Expand Down
6 changes: 4 additions & 2 deletions graphql/schema/types/stash-box.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ input StashIDInput {

input StashBoxFingerprintSubmissionInput {
scene_ids: [String!]!
stash_box_index: Int!
stash_box_index: Int @deprecated(reason: "use stash_box_endpoint")
stash_box_endpoint: String
}

input StashBoxDraftSubmissionInput {
id: String!
stash_box_index: Int!
stash_box_index: Int @deprecated(reason: "use stash_box_endpoint")
stash_box_endpoint: String
}
50 changes: 27 additions & 23 deletions internal/api/resolver_mutation_stash_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,46 @@ import (
"strconv"

"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/internal/manager/config"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/scraper/stashbox"
)

func (r *mutationResolver) SubmitStashBoxFingerprints(ctx context.Context, input StashBoxFingerprintSubmissionInput) (bool, error) {
boxes := config.GetInstance().GetStashBoxes()

if input.StashBoxIndex < 0 || input.StashBoxIndex >= len(boxes) {
return false, fmt.Errorf("invalid stash_box_index %d", input.StashBoxIndex)
b, err := resolveStashBox(input.StashBoxIndex, input.StashBoxEndpoint)
if err != nil {
return false, err
}

client := stashbox.NewClient(*boxes[input.StashBoxIndex], r.stashboxRepository())

return client.SubmitStashBoxFingerprints(ctx, input.SceneIds, boxes[input.StashBoxIndex].Endpoint)
client := r.newStashBoxClient(*b)
return client.SubmitStashBoxFingerprints(ctx, input.SceneIds)
}

func (r *mutationResolver) StashBoxBatchPerformerTag(ctx context.Context, input manager.StashBoxBatchTagInput) (string, error) {
jobID := manager.GetInstance().StashBoxBatchPerformerTag(ctx, input)
b, err := resolveStashBoxBatchTagInput(input.Endpoint, input.StashBoxEndpoint)
if err != nil {
return "", err
}

jobID := manager.GetInstance().StashBoxBatchPerformerTag(ctx, b, input)
return strconv.Itoa(jobID), nil
}

func (r *mutationResolver) StashBoxBatchStudioTag(ctx context.Context, input manager.StashBoxBatchTagInput) (string, error) {
jobID := manager.GetInstance().StashBoxBatchStudioTag(ctx, input)
b, err := resolveStashBoxBatchTagInput(input.Endpoint, input.StashBoxEndpoint)
if err != nil {
return "", err
}

jobID := manager.GetInstance().StashBoxBatchStudioTag(ctx, b, input)
return strconv.Itoa(jobID), nil
}

func (r *mutationResolver) SubmitStashBoxSceneDraft(ctx context.Context, input StashBoxDraftSubmissionInput) (*string, error) {
boxes := config.GetInstance().GetStashBoxes()

if input.StashBoxIndex < 0 || input.StashBoxIndex >= len(boxes) {
return nil, fmt.Errorf("invalid stash_box_index %d", input.StashBoxIndex)
b, err := resolveStashBox(input.StashBoxIndex, input.StashBoxEndpoint)
if err != nil {
return nil, err
}

client := stashbox.NewClient(*boxes[input.StashBoxIndex], r.stashboxRepository())
client := r.newStashBoxClient(*b)

id, err := strconv.Atoi(input.ID)
if err != nil {
Expand Down Expand Up @@ -68,21 +73,20 @@ func (r *mutationResolver) SubmitStashBoxSceneDraft(ctx context.Context, input S
return fmt.Errorf("loading scene URLs: %w", err)
}

res, err = client.SubmitSceneDraft(ctx, scene, boxes[input.StashBoxIndex].Endpoint, cover)
res, err = client.SubmitSceneDraft(ctx, scene, cover)
return err
})

return res, err
}

func (r *mutationResolver) SubmitStashBoxPerformerDraft(ctx context.Context, input StashBoxDraftSubmissionInput) (*string, error) {
boxes := config.GetInstance().GetStashBoxes()

if input.StashBoxIndex < 0 || input.StashBoxIndex >= len(boxes) {
return nil, fmt.Errorf("invalid stash_box_index %d", input.StashBoxIndex)
b, err := resolveStashBox(input.StashBoxIndex, input.StashBoxEndpoint)
if err != nil {
return nil, err
}

client := stashbox.NewClient(*boxes[input.StashBoxIndex], r.stashboxRepository())
client := r.newStashBoxClient(*b)

id, err := strconv.Atoi(input.ID)
if err != nil {
Expand All @@ -101,7 +105,7 @@ func (r *mutationResolver) SubmitStashBoxPerformerDraft(ctx context.Context, inp
return fmt.Errorf("performer with id %d not found", id)
}

res, err = client.SubmitPerformerDraft(ctx, performer, boxes[input.StashBoxIndex].Endpoint)
res, err = client.SubmitPerformerDraft(ctx, performer)
return err
})

Expand Down
46 changes: 21 additions & 25 deletions internal/api/resolver_query_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/internal/manager/config"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scraper"
Expand Down Expand Up @@ -190,18 +189,6 @@ func (r *queryResolver) ScrapeMovieURL(ctx context.Context, url string) (*models
return marshalScrapedMovie(content)
}

func (r *queryResolver) getStashBoxClient(index int) (*stashbox.Client, error) {
boxes := config.GetInstance().GetStashBoxes()

if index < 0 || index >= len(boxes) {
return nil, fmt.Errorf("%w: invalid stash_box_index %d", ErrInput, index)
}

return stashbox.NewClient(*boxes[index], r.stashboxRepository()), nil
}

// FIXME - in the following resolvers, we're processing the deprecated field and not processing the new endpoint input

func (r *queryResolver) ScrapeSingleScene(ctx context.Context, source scraper.Source, input ScrapeSingleSceneInput) ([]*scraper.ScrapedScene, error) {
var ret []*scraper.ScrapedScene

Expand Down Expand Up @@ -245,12 +232,14 @@ func (r *queryResolver) ScrapeSingleScene(ctx context.Context, source scraper.So
if err != nil {
return nil, err
}
case source.StashBoxIndex != nil:
client, err := r.getStashBoxClient(*source.StashBoxIndex)
case source.StashBoxIndex != nil || source.StashBoxEndpoint != nil:
b, err := resolveStashBox(source.StashBoxIndex, source.StashBoxEndpoint)
if err != nil {
return nil, err
}

client := r.newStashBoxClient(*b)

switch {
case input.SceneID != nil:
ret, err = client.FindStashBoxSceneByFingerprints(ctx, sceneID)
Expand All @@ -275,12 +264,14 @@ func (r *queryResolver) ScrapeSingleScene(ctx context.Context, source scraper.So
func (r *queryResolver) ScrapeMultiScenes(ctx context.Context, source scraper.Source, input ScrapeMultiScenesInput) ([][]*scraper.ScrapedScene, error) {
if source.ScraperID != nil {
return nil, ErrNotImplemented
} else if source.StashBoxIndex != nil {
client, err := r.getStashBoxClient(*source.StashBoxIndex)
} else if source.StashBoxIndex != nil || source.StashBoxEndpoint != nil {
b, err := resolveStashBox(source.StashBoxIndex, source.StashBoxEndpoint)
if err != nil {
return nil, err
}

client := r.newStashBoxClient(*b)

sceneIDs, err := stringslice.StringSliceToIntSlice(input.SceneIds)
if err != nil {
return nil, err
Expand All @@ -293,12 +284,14 @@ func (r *queryResolver) ScrapeMultiScenes(ctx context.Context, source scraper.So
}

func (r *queryResolver) ScrapeSingleStudio(ctx context.Context, source scraper.Source, input ScrapeSingleStudioInput) ([]*models.ScrapedStudio, error) {
if source.StashBoxIndex != nil {
client, err := r.getStashBoxClient(*source.StashBoxIndex)
if source.StashBoxIndex != nil || source.StashBoxEndpoint != nil {
b, err := resolveStashBox(source.StashBoxIndex, source.StashBoxEndpoint)
if err != nil {
return nil, err
}

client := r.newStashBoxClient(*b)

var ret []*models.ScrapedStudio
out, err := client.FindStashBoxStudio(ctx, *input.Query)

Expand Down Expand Up @@ -346,13 +339,14 @@ func (r *queryResolver) ScrapeSinglePerformer(ctx context.Context, source scrape
default:
return nil, ErrNotImplemented
}
// FIXME - we're relying on a deprecated field and not processing the endpoint input
case source.StashBoxIndex != nil:
client, err := r.getStashBoxClient(*source.StashBoxIndex)
case source.StashBoxIndex != nil || source.StashBoxEndpoint != nil:
b, err := resolveStashBox(source.StashBoxIndex, source.StashBoxEndpoint)
if err != nil {
return nil, err
}

client := r.newStashBoxClient(*b)

var res []*stashbox.StashBoxPerformerQueryResult
switch {
case input.PerformerID != nil:
Expand Down Expand Up @@ -382,12 +376,14 @@ func (r *queryResolver) ScrapeSinglePerformer(ctx context.Context, source scrape
func (r *queryResolver) ScrapeMultiPerformers(ctx context.Context, source scraper.Source, input ScrapeMultiPerformersInput) ([][]*models.ScrapedPerformer, error) {
if source.ScraperID != nil {
return nil, ErrNotImplemented
} else if source.StashBoxIndex != nil {
client, err := r.getStashBoxClient(*source.StashBoxIndex)
} else if source.StashBoxIndex != nil || source.StashBoxEndpoint != nil {
b, err := resolveStashBox(source.StashBoxIndex, source.StashBoxEndpoint)
if err != nil {
return nil, err
}

client := r.newStashBoxClient(*b)

return client.FindStashBoxPerformersByPerformerNames(ctx, input.PerformerIds)
}

Expand All @@ -397,7 +393,7 @@ func (r *queryResolver) ScrapeMultiPerformers(ctx context.Context, source scrape
func (r *queryResolver) ScrapeSingleGallery(ctx context.Context, source scraper.Source, input ScrapeSingleGalleryInput) ([]*scraper.ScrapedGallery, error) {
var ret []*scraper.ScrapedGallery

if source.StashBoxIndex != nil {
if source.StashBoxIndex != nil || source.StashBoxEndpoint != nil {
return nil, ErrNotSupported
}

Expand Down
45 changes: 45 additions & 0 deletions internal/api/stash_box.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package api

import (
"fmt"
"strings"

"github.com/stashapp/stash/internal/manager/config"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scraper/stashbox"
)

func (r *Resolver) newStashBoxClient(box models.StashBox) *stashbox.Client {
return stashbox.NewClient(box, r.stashboxRepository())
}

func resolveStashBoxFn(indexField, endpointField string) func(index *int, endpoint *string) (*models.StashBox, error) {
return func(index *int, endpoint *string) (*models.StashBox, error) {
boxes := config.GetInstance().GetStashBoxes()

// prefer endpoint over index
if endpoint != nil {
for _, box := range boxes {
if strings.EqualFold(*endpoint, box.Endpoint) {
return box, nil
}
}
return nil, fmt.Errorf("stash box not found")
}

if index != nil {
if *index < 0 || *index >= len(boxes) {
return nil, fmt.Errorf("invalid %s %d", indexField, index)
}

return boxes[*index], nil
}

return nil, fmt.Errorf("%s not provided", endpointField)
}
}

var (
resolveStashBox = resolveStashBoxFn("stash_box_index", "stash_box_endpoint")
resolveStashBoxBatchTagInput = resolveStashBoxFn("endpoint", "stash_box_endpoint")
)
21 changes: 5 additions & 16 deletions internal/manager/manager_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,9 @@ func (s *Manager) MigrateHash(ctx context.Context) int {

// If neither ids nor names are set, tag all items
type StashBoxBatchTagInput struct {
// Stash endpoint to use for the tagging
Endpoint int `json:"endpoint"`
// Stash endpoint to use for the tagging - deprecated - use StashBoxEndpoint
Endpoint *int `json:"endpoint"`
StashBoxEndpoint *string `json:"stash_box_endpoint"`
// Fields to exclude when executing the tagging
ExcludeFields []string `json:"exclude_fields"`
// Refresh items already tagged by StashBox if true. Only tag items with no StashBox tagging if false
Expand All @@ -388,16 +389,10 @@ type StashBoxBatchTagInput struct {
PerformerNames []string `json:"performer_names"`
}

func (s *Manager) StashBoxBatchPerformerTag(ctx context.Context, input StashBoxBatchTagInput) int {
func (s *Manager) StashBoxBatchPerformerTag(ctx context.Context, box *models.StashBox, input StashBoxBatchTagInput) int {
j := job.MakeJobExec(func(ctx context.Context, progress *job.Progress) error {
logger.Infof("Initiating stash-box batch performer tag")

boxes := config.GetInstance().GetStashBoxes()
if input.Endpoint < 0 || input.Endpoint >= len(boxes) {
return fmt.Errorf("invalid stash_box_index %d", input.Endpoint)
}
box := boxes[input.Endpoint]

var tasks []StashBoxBatchTagTask

// The gocritic linter wants to turn this ifElseChain into a switch.
Expand Down Expand Up @@ -526,16 +521,10 @@ func (s *Manager) StashBoxBatchPerformerTag(ctx context.Context, input StashBoxB
return s.JobManager.Add(ctx, "Batch stash-box performer tag...", j)
}

func (s *Manager) StashBoxBatchStudioTag(ctx context.Context, input StashBoxBatchTagInput) int {
func (s *Manager) StashBoxBatchStudioTag(ctx context.Context, box *models.StashBox, input StashBoxBatchTagInput) int {
j := job.MakeJobExec(func(ctx context.Context, progress *job.Progress) error {
logger.Infof("Initiating stash-box batch studio tag")

boxes := config.GetInstance().GetStashBoxes()
if input.Endpoint < 0 || input.Endpoint >= len(boxes) {
return fmt.Errorf("invalid stash_box_index %d", input.Endpoint)
}
box := boxes[input.Endpoint]

var tasks []StashBoxBatchTagTask

// The gocritic linter wants to turn this ifElseChain into a switch.
Expand Down
Loading

0 comments on commit ed057c9

Please sign in to comment.