2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2021-04-16 06:06:35 +00:00
|
|
|
"fmt"
|
2019-08-21 02:51:30 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/hash/md5"
|
2019-02-14 23:42:52 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2021-04-16 06:06:35 +00:00
|
|
|
"github.com/stashapp/stash/pkg/performer"
|
2021-06-11 07:24:58 +00:00
|
|
|
"github.com/stashapp/stash/pkg/plugin"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
|
2019-02-14 23:42:52 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2021-06-11 07:24:58 +00:00
|
|
|
func (r *mutationResolver) getPerformer(ctx context.Context, id int) (ret *models.Performer, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.Performer().Find(id)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
func (r *mutationResolver) PerformerCreate(ctx context.Context, input models.PerformerCreateInput) (*models.Performer, error) {
|
2019-08-21 02:51:30 +00:00
|
|
|
// generate checksum from performer name rather than image
|
2022-03-17 00:33:59 +00:00
|
|
|
checksum := md5.FromString(input.Name)
|
2019-08-21 02:51:30 +00:00
|
|
|
|
|
|
|
var imageData []byte
|
|
|
|
var err error
|
|
|
|
|
2020-08-11 23:19:27 +00:00
|
|
|
if input.Image != nil {
|
Toward better context handling (#1835)
* Use the request context
The code uses context.Background() in a flow where there is a
http.Request. Use the requests context instead.
* Use a true context in the plugin example
Let AddTag/RemoveTag take a context and use that context throughout
the example.
* Avoid the use of context.Background
Prefer context.TODO over context.Background deep in the call chain.
This marks the site as something which we need to context-handle
later, and also makes it clear to the reader that the context is
sort-of temporary in the code base.
While here, be consistent in handling the `act` variable in each
branch of the if .. { .. } .. check.
* Prefer context.TODO over context.Background
For the different scraping operations here, there is a context
higher up the call chain, which we ought to use. Mark the call-sites
as TODO for now, so we can come back later on a sweep of which parts
can be context-lifted.
* Thread context upwards
Initialization requires context for transactions. Thread the context
upward the call chain.
At the intialization call, add a context.TODO since we can't break this
yet. The singleton assumption prevents us from pulling it up into main for
now.
* make tasks context-aware
Change the task interface to understand contexts.
Pass the context down in some of the branches where it is needed.
* Make QueryStashBoxScene context-aware
This call naturally sits inside the request-context. Use it.
* Introduce a context in the JS plugin code
This allows us to use a context for HTTP calls inside the system.
Mark the context with a TODO at top level for now.
* Nitpick error formatting
Use %v rather than %s for error interfaces.
Do not begin an error strong with a capital letter.
* Avoid the use of http.Get in FFMPEG download chain
Since http.Get has no context, it isn't possible to break out or have
policy induced. The call will block until the GET completes. Rewrite
to use a http Request and provide a context.
Thread the context through the call chain for now. provide
context.TODO() at the top level of the initialization chain.
* Make getRemoteCDPWSAddress aware of contexts
Eliminate a call to http.Get and replace it with a context-aware
variant.
Push the context upwards in the call chain, but plug it before the
scraper interface so we don't have to rewrite said interface yet.
Plugged with context.TODO()
* Scraper: make the getImage function context-aware
Use a context, and pass it upwards. Plug it with context.TODO()
up the chain before the rewrite gets too much out of hand for now.
Minor tweaks along the way, remove a call to context.Background()
deep in the call chain.
* Make NOTIFY request context-aware
The call sits inside a Request-handler. So it's natural to use the
requests context as the context for the outgoing HTTP request.
* Use a context in the url scraper code
We are sitting in code which has a context, so utilize it for the
request as well.
* Use a context when checking versions
When we check the version of stash on Github, use a context. Thread
the context up to the initialization routine of the HTTP/GraphQL
server and plug it with a context.TODO() for now.
This paves the way for providing a context to the HTTP server code in a
future patch.
* Make utils func ReadImage context-aware
In almost all of the cases, there is a context in the call chain which
is a natural use. This is true for all the GraphQL mutations.
The exception is in task_stash_box_tag, so plug that task with
context.TODO() for now.
* Make stash-box get context-aware
Thread a context through the call chain until we hit the Client API.
Plug it with context.TODO() there for now.
* Enable the noctx linter
The code is now free of any uncontexted HTTP request. This means we
pass the noctx linter, and we can enable it in the code base.
2021-10-14 04:32:41 +00:00
|
|
|
imageData, err = utils.ProcessImageInput(ctx, *input.Image)
|
2019-08-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate a new performer from the input
|
|
|
|
currentTime := time.Now()
|
|
|
|
newPerformer := models.Performer{
|
2019-02-14 22:53:32 +00:00
|
|
|
Checksum: checksum,
|
|
|
|
CreatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
|
|
|
UpdatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-10-21 00:27:16 +00:00
|
|
|
newPerformer.Name = sql.NullString{String: input.Name, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
if input.URL != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.URL = sql.NullString{String: *input.URL, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-03-31 22:36:38 +00:00
|
|
|
if input.Gender != nil {
|
|
|
|
newPerformer.Gender = sql.NullString{String: input.Gender.String(), Valid: true}
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
if input.Birthdate != nil {
|
2019-03-05 01:14:52 +00:00
|
|
|
newPerformer.Birthdate = models.SQLiteDate{String: *input.Birthdate, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Ethnicity != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Ethnicity = sql.NullString{String: *input.Ethnicity, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Country != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Country = sql.NullString{String: *input.Country, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.EyeColor != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.EyeColor = sql.NullString{String: *input.EyeColor, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Height != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Height = sql.NullString{String: *input.Height, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Measurements != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Measurements = sql.NullString{String: *input.Measurements, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.FakeTits != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.FakeTits = sql.NullString{String: *input.FakeTits, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.CareerLength != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.CareerLength = sql.NullString{String: *input.CareerLength, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Tattoos != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Tattoos = sql.NullString{String: *input.Tattoos, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Piercings != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Piercings = sql.NullString{String: *input.Piercings, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Aliases != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Aliases = sql.NullString{String: *input.Aliases, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Twitter != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Twitter = sql.NullString{String: *input.Twitter, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Instagram != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Instagram = sql.NullString{String: *input.Instagram, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if input.Favorite != nil {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Favorite = sql.NullBool{Bool: *input.Favorite, Valid: true}
|
2019-02-11 11:06:33 +00:00
|
|
|
} else {
|
2019-02-14 22:53:32 +00:00
|
|
|
newPerformer.Favorite = sql.NullBool{Bool: false, Valid: true}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-04-26 03:48:32 +00:00
|
|
|
if input.Rating != nil {
|
|
|
|
newPerformer.Rating = sql.NullInt64{Int64: int64(*input.Rating), Valid: true}
|
|
|
|
} else {
|
|
|
|
newPerformer.Rating = sql.NullInt64{Valid: false}
|
|
|
|
}
|
2021-04-16 06:06:35 +00:00
|
|
|
if input.Details != nil {
|
|
|
|
newPerformer.Details = sql.NullString{String: *input.Details, Valid: true}
|
|
|
|
}
|
|
|
|
if input.DeathDate != nil {
|
|
|
|
newPerformer.DeathDate = models.SQLiteDate{String: *input.DeathDate, Valid: true}
|
|
|
|
}
|
|
|
|
if input.HairColor != nil {
|
|
|
|
newPerformer.HairColor = sql.NullString{String: *input.HairColor, Valid: true}
|
|
|
|
}
|
|
|
|
if input.Weight != nil {
|
|
|
|
weight := int64(*input.Weight)
|
|
|
|
newPerformer.Weight = sql.NullInt64{Int64: weight, Valid: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := performer.ValidateDeathDate(nil, input.Birthdate, input.DeathDate); err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
// Start the transaction and save the performer
|
2021-01-18 01:23:20 +00:00
|
|
|
var performer *models.Performer
|
|
|
|
if err := r.withTxn(ctx, func(repo models.Repository) error {
|
|
|
|
qb := repo.Performer()
|
2020-10-24 03:31:39 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
performer, err = qb.Create(newPerformer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-22 23:19:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 01:25:51 +00:00
|
|
|
if len(input.TagIds) > 0 {
|
|
|
|
if err := r.updatePerformerTags(qb, performer.ID, input.TagIds); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
// update image table
|
|
|
|
if len(imageData) > 0 {
|
|
|
|
if err := qb.UpdateImage(performer.ID, imageData); err != nil {
|
|
|
|
return err
|
2020-10-24 03:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
// Save the stash_ids
|
|
|
|
if input.StashIds != nil {
|
|
|
|
stashIDJoins := models.StashIDsFromInput(input.StashIds)
|
|
|
|
if err := qb.UpdateStashIDs(performer.ID, stashIDJoins); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-24 03:31:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:24:58 +00:00
|
|
|
r.hookExecutor.ExecutePostHooks(ctx, performer.ID, plugin.PerformerCreatePost, input, nil)
|
|
|
|
return r.getPerformer(ctx, performer.ID)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) PerformerUpdate(ctx context.Context, input models.PerformerUpdateInput) (*models.Performer, error) {
|
|
|
|
// Populate performer from the input
|
|
|
|
performerID, _ := strconv.Atoi(input.ID)
|
2020-12-04 01:42:56 +00:00
|
|
|
updatedPerformer := models.PerformerPartial{
|
2019-02-14 22:53:32 +00:00
|
|
|
ID: performerID,
|
2020-12-04 01:42:56 +00:00
|
|
|
UpdatedAt: &models.SQLiteTimestamp{Timestamp: time.Now()},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-12-04 01:42:56 +00:00
|
|
|
|
|
|
|
translator := changesetTranslator{
|
|
|
|
inputMap: getUpdateInputMap(ctx),
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:19:19 +00:00
|
|
|
var imageData []byte
|
|
|
|
var err error
|
2020-12-04 01:42:56 +00:00
|
|
|
imageIncluded := translator.hasField("image")
|
2019-02-09 12:30:49 +00:00
|
|
|
if input.Image != nil {
|
Toward better context handling (#1835)
* Use the request context
The code uses context.Background() in a flow where there is a
http.Request. Use the requests context instead.
* Use a true context in the plugin example
Let AddTag/RemoveTag take a context and use that context throughout
the example.
* Avoid the use of context.Background
Prefer context.TODO over context.Background deep in the call chain.
This marks the site as something which we need to context-handle
later, and also makes it clear to the reader that the context is
sort-of temporary in the code base.
While here, be consistent in handling the `act` variable in each
branch of the if .. { .. } .. check.
* Prefer context.TODO over context.Background
For the different scraping operations here, there is a context
higher up the call chain, which we ought to use. Mark the call-sites
as TODO for now, so we can come back later on a sweep of which parts
can be context-lifted.
* Thread context upwards
Initialization requires context for transactions. Thread the context
upward the call chain.
At the intialization call, add a context.TODO since we can't break this
yet. The singleton assumption prevents us from pulling it up into main for
now.
* make tasks context-aware
Change the task interface to understand contexts.
Pass the context down in some of the branches where it is needed.
* Make QueryStashBoxScene context-aware
This call naturally sits inside the request-context. Use it.
* Introduce a context in the JS plugin code
This allows us to use a context for HTTP calls inside the system.
Mark the context with a TODO at top level for now.
* Nitpick error formatting
Use %v rather than %s for error interfaces.
Do not begin an error strong with a capital letter.
* Avoid the use of http.Get in FFMPEG download chain
Since http.Get has no context, it isn't possible to break out or have
policy induced. The call will block until the GET completes. Rewrite
to use a http Request and provide a context.
Thread the context through the call chain for now. provide
context.TODO() at the top level of the initialization chain.
* Make getRemoteCDPWSAddress aware of contexts
Eliminate a call to http.Get and replace it with a context-aware
variant.
Push the context upwards in the call chain, but plug it before the
scraper interface so we don't have to rewrite said interface yet.
Plugged with context.TODO()
* Scraper: make the getImage function context-aware
Use a context, and pass it upwards. Plug it with context.TODO()
up the chain before the rewrite gets too much out of hand for now.
Minor tweaks along the way, remove a call to context.Background()
deep in the call chain.
* Make NOTIFY request context-aware
The call sits inside a Request-handler. So it's natural to use the
requests context as the context for the outgoing HTTP request.
* Use a context in the url scraper code
We are sitting in code which has a context, so utilize it for the
request as well.
* Use a context when checking versions
When we check the version of stash on Github, use a context. Thread
the context up to the initialization routine of the HTTP/GraphQL
server and plug it with a context.TODO() for now.
This paves the way for providing a context to the HTTP server code in a
future patch.
* Make utils func ReadImage context-aware
In almost all of the cases, there is a context in the call chain which
is a natural use. This is true for all the GraphQL mutations.
The exception is in task_stash_box_tag, so plug that task with
context.TODO() for now.
* Make stash-box get context-aware
Thread a context through the call chain until we hit the Client API.
Plug it with context.TODO() there for now.
* Enable the noctx linter
The code is now free of any uncontexted HTTP request. This means we
pass the noctx linter, and we can enable it in the code base.
2021-10-14 04:32:41 +00:00
|
|
|
imageData, err = utils.ProcessImageInput(ctx, *input.Image)
|
2019-02-09 12:30:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 01:42:56 +00:00
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
if input.Name != nil {
|
2019-08-22 02:49:09 +00:00
|
|
|
// generate checksum from performer name rather than image
|
2022-03-17 00:33:59 +00:00
|
|
|
checksum := md5.FromString(*input.Name)
|
2019-08-22 02:49:09 +00:00
|
|
|
|
2020-12-04 01:42:56 +00:00
|
|
|
updatedPerformer.Name = &sql.NullString{String: *input.Name, Valid: true}
|
|
|
|
updatedPerformer.Checksum = &checksum
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2020-12-04 01:42:56 +00:00
|
|
|
|
|
|
|
updatedPerformer.URL = translator.nullString(input.URL, "url")
|
|
|
|
|
|
|
|
if translator.hasField("gender") {
|
|
|
|
if input.Gender != nil {
|
|
|
|
updatedPerformer.Gender = &sql.NullString{String: input.Gender.String(), Valid: true}
|
|
|
|
} else {
|
|
|
|
updatedPerformer.Gender = &sql.NullString{String: "", Valid: false}
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 01:42:56 +00:00
|
|
|
updatedPerformer.Birthdate = translator.sqliteDate(input.Birthdate, "birthdate")
|
|
|
|
updatedPerformer.Country = translator.nullString(input.Country, "country")
|
|
|
|
updatedPerformer.EyeColor = translator.nullString(input.EyeColor, "eye_color")
|
|
|
|
updatedPerformer.Measurements = translator.nullString(input.Measurements, "measurements")
|
|
|
|
updatedPerformer.Height = translator.nullString(input.Height, "height")
|
|
|
|
updatedPerformer.Ethnicity = translator.nullString(input.Ethnicity, "ethnicity")
|
|
|
|
updatedPerformer.FakeTits = translator.nullString(input.FakeTits, "fake_tits")
|
|
|
|
updatedPerformer.CareerLength = translator.nullString(input.CareerLength, "career_length")
|
|
|
|
updatedPerformer.Tattoos = translator.nullString(input.Tattoos, "tattoos")
|
|
|
|
updatedPerformer.Piercings = translator.nullString(input.Piercings, "piercings")
|
|
|
|
updatedPerformer.Aliases = translator.nullString(input.Aliases, "aliases")
|
|
|
|
updatedPerformer.Twitter = translator.nullString(input.Twitter, "twitter")
|
|
|
|
updatedPerformer.Instagram = translator.nullString(input.Instagram, "instagram")
|
|
|
|
updatedPerformer.Favorite = translator.nullBool(input.Favorite, "favorite")
|
2021-04-26 03:48:32 +00:00
|
|
|
updatedPerformer.Rating = translator.nullInt64(input.Rating, "rating")
|
2021-04-16 06:06:35 +00:00
|
|
|
updatedPerformer.Details = translator.nullString(input.Details, "details")
|
|
|
|
updatedPerformer.DeathDate = translator.sqliteDate(input.DeathDate, "death_date")
|
|
|
|
updatedPerformer.HairColor = translator.nullString(input.HairColor, "hair_color")
|
|
|
|
updatedPerformer.Weight = translator.nullInt64(input.Weight, "weight")
|
2020-12-04 01:42:56 +00:00
|
|
|
|
2021-04-16 06:06:35 +00:00
|
|
|
// Start the transaction and save the p
|
|
|
|
var p *models.Performer
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withTxn(ctx, func(repo models.Repository) error {
|
|
|
|
qb := repo.Performer()
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-04-16 06:06:35 +00:00
|
|
|
// need to get existing performer
|
|
|
|
existing, err := qb.Find(updatedPerformer.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing == nil {
|
|
|
|
return fmt.Errorf("performer with id %d not found", updatedPerformer.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := performer.ValidateDeathDate(existing, input.Birthdate, input.DeathDate); err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err = qb.Update(updatedPerformer)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-22 23:19:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 01:25:51 +00:00
|
|
|
// Save the tags
|
|
|
|
if translator.hasField("tag_ids") {
|
2021-04-16 06:06:35 +00:00
|
|
|
if err := r.updatePerformerTags(qb, p.ID, input.TagIds); err != nil {
|
2021-03-10 01:25:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
// update image table
|
|
|
|
if len(imageData) > 0 {
|
2021-04-16 06:06:35 +00:00
|
|
|
if err := qb.UpdateImage(p.ID, imageData); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if imageIncluded {
|
|
|
|
// must be unsetting
|
2021-04-16 06:06:35 +00:00
|
|
|
if err := qb.DestroyImage(p.ID); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return err
|
2020-10-24 03:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
// Save the stash_ids
|
|
|
|
if translator.hasField("stash_ids") {
|
|
|
|
stashIDJoins := models.StashIDsFromInput(input.StashIds)
|
|
|
|
if err := qb.UpdateStashIDs(performerID, stashIDJoins); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-24 03:31:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:24:58 +00:00
|
|
|
r.hookExecutor.ExecutePostHooks(ctx, p.ID, plugin.PerformerUpdatePost, input, translator.getFields())
|
|
|
|
return r.getPerformer(ctx, p.ID)
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|
2019-08-14 21:40:51 +00:00
|
|
|
|
2021-03-10 01:25:51 +00:00
|
|
|
func (r *mutationResolver) updatePerformerTags(qb models.PerformerReaderWriter, performerID int, tagsIDs []string) error {
|
2022-03-17 00:33:59 +00:00
|
|
|
ids, err := stringslice.StringSliceToIntSlice(tagsIDs)
|
2021-03-10 01:25:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return qb.UpdateTags(performerID, ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) BulkPerformerUpdate(ctx context.Context, input models.BulkPerformerUpdateInput) ([]*models.Performer, error) {
|
2022-03-17 00:33:59 +00:00
|
|
|
performerIDs, err := stringslice.StringSliceToIntSlice(input.Ids)
|
2021-03-10 01:25:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate performer from the input
|
|
|
|
updatedTime := time.Now()
|
|
|
|
|
|
|
|
translator := changesetTranslator{
|
|
|
|
inputMap: getUpdateInputMap(ctx),
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedPerformer := models.PerformerPartial{
|
|
|
|
UpdatedAt: &models.SQLiteTimestamp{Timestamp: updatedTime},
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedPerformer.URL = translator.nullString(input.URL, "url")
|
|
|
|
updatedPerformer.Birthdate = translator.sqliteDate(input.Birthdate, "birthdate")
|
|
|
|
updatedPerformer.Ethnicity = translator.nullString(input.Ethnicity, "ethnicity")
|
|
|
|
updatedPerformer.Country = translator.nullString(input.Country, "country")
|
|
|
|
updatedPerformer.EyeColor = translator.nullString(input.EyeColor, "eye_color")
|
|
|
|
updatedPerformer.Height = translator.nullString(input.Height, "height")
|
|
|
|
updatedPerformer.Measurements = translator.nullString(input.Measurements, "measurements")
|
|
|
|
updatedPerformer.FakeTits = translator.nullString(input.FakeTits, "fake_tits")
|
|
|
|
updatedPerformer.CareerLength = translator.nullString(input.CareerLength, "career_length")
|
|
|
|
updatedPerformer.Tattoos = translator.nullString(input.Tattoos, "tattoos")
|
|
|
|
updatedPerformer.Piercings = translator.nullString(input.Piercings, "piercings")
|
|
|
|
updatedPerformer.Aliases = translator.nullString(input.Aliases, "aliases")
|
|
|
|
updatedPerformer.Twitter = translator.nullString(input.Twitter, "twitter")
|
|
|
|
updatedPerformer.Instagram = translator.nullString(input.Instagram, "instagram")
|
|
|
|
updatedPerformer.Favorite = translator.nullBool(input.Favorite, "favorite")
|
2021-04-26 03:48:32 +00:00
|
|
|
updatedPerformer.Rating = translator.nullInt64(input.Rating, "rating")
|
2021-04-16 06:06:35 +00:00
|
|
|
updatedPerformer.Details = translator.nullString(input.Details, "details")
|
|
|
|
updatedPerformer.DeathDate = translator.sqliteDate(input.DeathDate, "death_date")
|
|
|
|
updatedPerformer.HairColor = translator.nullString(input.HairColor, "hair_color")
|
|
|
|
updatedPerformer.Weight = translator.nullInt64(input.Weight, "weight")
|
2021-03-10 01:25:51 +00:00
|
|
|
|
|
|
|
if translator.hasField("gender") {
|
|
|
|
if input.Gender != nil {
|
|
|
|
updatedPerformer.Gender = &sql.NullString{String: input.Gender.String(), Valid: true}
|
|
|
|
} else {
|
|
|
|
updatedPerformer.Gender = &sql.NullString{String: "", Valid: false}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := []*models.Performer{}
|
|
|
|
|
|
|
|
// Start the transaction and save the scene marker
|
|
|
|
if err := r.withTxn(ctx, func(repo models.Repository) error {
|
|
|
|
qb := repo.Performer()
|
|
|
|
|
|
|
|
for _, performerID := range performerIDs {
|
|
|
|
updatedPerformer.ID = performerID
|
|
|
|
|
2021-04-16 06:06:35 +00:00
|
|
|
// need to get existing performer
|
|
|
|
existing, err := qb.Find(performerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing == nil {
|
|
|
|
return fmt.Errorf("performer with id %d not found", performerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := performer.ValidateDeathDate(existing, input.Birthdate, input.DeathDate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 01:25:51 +00:00
|
|
|
performer, err := qb.Update(updatedPerformer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, performer)
|
|
|
|
|
|
|
|
// Save the tags
|
|
|
|
if translator.hasField("tag_ids") {
|
|
|
|
tagIDs, err := adjustTagIDs(qb, performerID, *input.TagIds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := qb.UpdateTags(performerID, tagIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:24:58 +00:00
|
|
|
// execute post hooks outside of txn
|
|
|
|
var newRet []*models.Performer
|
|
|
|
for _, performer := range ret {
|
|
|
|
r.hookExecutor.ExecutePostHooks(ctx, performer.ID, plugin.ImageUpdatePost, input, translator.getFields())
|
|
|
|
|
|
|
|
performer, err = r.getPerformer(ctx, performer.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newRet = append(newRet, performer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRet, nil
|
2021-03-10 01:25:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 21:40:51 +00:00
|
|
|
func (r *mutationResolver) PerformerDestroy(ctx context.Context, input models.PerformerDestroyInput) (bool, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
id, err := strconv.Atoi(input.ID)
|
|
|
|
if err != nil {
|
2019-08-14 21:40:51 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if err := r.withTxn(ctx, func(repo models.Repository) error {
|
|
|
|
return repo.Performer().Destroy(id)
|
|
|
|
}); err != nil {
|
2019-08-14 21:40:51 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2021-06-11 07:24:58 +00:00
|
|
|
|
|
|
|
r.hookExecutor.ExecutePostHooks(ctx, id, plugin.PerformerDestroyPost, input, nil)
|
|
|
|
|
2019-08-14 21:40:51 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2021-01-13 00:57:53 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *mutationResolver) PerformersDestroy(ctx context.Context, performerIDs []string) (bool, error) {
|
2022-03-17 00:33:59 +00:00
|
|
|
ids, err := stringslice.StringSliceToIntSlice(performerIDs)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2021-01-13 00:57:53 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if err := r.withTxn(ctx, func(repo models.Repository) error {
|
|
|
|
qb := repo.Performer()
|
|
|
|
for _, id := range ids {
|
|
|
|
if err := qb.Destroy(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-01-13 00:57:53 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2021-06-11 07:24:58 +00:00
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
r.hookExecutor.ExecutePostHooks(ctx, id, plugin.PerformerDestroyPost, performerIDs, nil)
|
|
|
|
}
|
|
|
|
|
2021-01-13 00:57:53 +00:00
|
|
|
return true, nil
|
|
|
|
}
|