update to use queryTags

This commit is contained in:
Gykes 2025-12-03 15:38:10 -08:00
parent fa8d7d1bc8
commit aa0c95fb86

View file

@ -5,22 +5,24 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/stashbox/graphql"
) )
func (c Client) FindTag(ctx context.Context, query string) (*models.ScrapedTag, error) { // QueryTag searches for tags by name or ID.
var id *string // If query is a valid UUID, it searches by ID (returns single result).
var name *string // Otherwise, it searches by name (returns multiple results).
func (c Client) QueryTag(ctx context.Context, query string) ([]*models.ScrapedTag, error) {
_, err := uuid.Parse(query) _, err := uuid.Parse(query)
if err == nil { if err == nil {
// Confirmed the user passed in a Stash ID // Query is a UUID, use findTag for exact match
id = &query return c.findTagByID(ctx, query)
} else {
// Otherwise assume they're searching on a name
name = &query
} }
// Otherwise search by name
return c.queryTagsByName(ctx, query)
}
tag, err := c.client.FindTag(ctx, id, name) func (c Client) findTagByID(ctx context.Context, id string) ([]*models.ScrapedTag, error) {
tag, err := c.client.FindTag(ctx, &id, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -29,8 +31,37 @@ func (c Client) FindTag(ctx context.Context, query string) (*models.ScrapedTag,
return nil, nil return nil, nil
} }
return &models.ScrapedTag{ return []*models.ScrapedTag{{
Name: tag.FindTag.Name, Name: tag.FindTag.Name,
RemoteSiteID: &tag.FindTag.ID, RemoteSiteID: &tag.FindTag.ID,
}, nil }}, nil
}
func (c Client) queryTagsByName(ctx context.Context, name string) ([]*models.ScrapedTag, error) {
input := graphql.TagQueryInput{
Name: &name,
Page: 1,
PerPage: 25,
Direction: graphql.SortDirectionEnumAsc,
Sort: graphql.TagSortEnumName,
}
result, err := c.client.QueryTags(ctx, input)
if err != nil {
return nil, err
}
if result.QueryTags.Tags == nil {
return nil, nil
}
var ret []*models.ScrapedTag
for _, t := range result.QueryTags.Tags {
ret = append(ret, &models.ScrapedTag{
Name: t.Name,
RemoteSiteID: &t.ID,
})
}
return ret, nil
} }