2021-04-26 02:51:31 +00:00
|
|
|
package autotag
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
|
|
|
|
2022-03-09 01:01:56 +00:00
|
|
|
"github.com/stashapp/stash/pkg/match"
|
2021-04-26 02:51:31 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
"github.com/stashapp/stash/pkg/scene"
|
2022-08-12 02:21:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil/intslice"
|
2021-04-26 02:51:31 +00:00
|
|
|
)
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
type ScenePerformerUpdater interface {
|
|
|
|
models.PerformerIDLoader
|
|
|
|
scene.PartialUpdater
|
|
|
|
}
|
|
|
|
|
|
|
|
type SceneTagUpdater interface {
|
|
|
|
models.TagIDLoader
|
|
|
|
scene.PartialUpdater
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:01:56 +00:00
|
|
|
func getSceneFileTagger(s *models.Scene, cache *match.Cache) tagger {
|
2021-04-26 02:51:31 +00:00
|
|
|
return tagger{
|
2022-03-09 01:01:56 +00:00
|
|
|
ID: s.ID,
|
|
|
|
Type: "scene",
|
|
|
|
Name: s.GetTitle(),
|
2022-07-13 06:30:54 +00:00
|
|
|
Path: s.Path(),
|
2022-03-09 01:01:56 +00:00
|
|
|
cache: cache,
|
2021-04-26 02:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScenePerformers tags the provided scene with performers whose name matches the scene's path.
|
2022-08-12 02:21:46 +00:00
|
|
|
func ScenePerformers(ctx context.Context, s *models.Scene, rw ScenePerformerUpdater, performerReader match.PerformerAutoTagQueryer, cache *match.Cache) error {
|
2022-03-09 01:01:56 +00:00
|
|
|
t := getSceneFileTagger(s, cache)
|
2021-04-26 02:51:31 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
return t.tagPerformers(ctx, performerReader, func(subjectID, otherID int) (bool, error) {
|
2022-08-12 02:21:46 +00:00
|
|
|
if err := s.LoadPerformerIDs(ctx, rw); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
existing := s.PerformerIDs.List()
|
|
|
|
|
|
|
|
if intslice.IntInclude(existing, otherID) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scene.AddPerformer(ctx, rw, s, otherID); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
2021-04-26 02:51:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SceneStudios tags the provided scene with the first studio whose name matches the scene's path.
|
|
|
|
//
|
|
|
|
// Scenes will not be tagged if studio is already set.
|
2022-05-19 07:49:32 +00:00
|
|
|
func SceneStudios(ctx context.Context, s *models.Scene, rw SceneFinderUpdater, studioReader match.StudioAutoTagQueryer, cache *match.Cache) error {
|
2022-07-13 06:30:54 +00:00
|
|
|
if s.StudioID != nil {
|
2021-04-26 02:51:31 +00:00
|
|
|
// don't modify
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:01:56 +00:00
|
|
|
t := getSceneFileTagger(s, cache)
|
2021-04-26 02:51:31 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
return t.tagStudios(ctx, studioReader, func(subjectID, otherID int) (bool, error) {
|
2022-07-13 06:30:54 +00:00
|
|
|
return addSceneStudio(ctx, rw, s, otherID)
|
2021-04-26 02:51:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SceneTags tags the provided scene with tags whose name matches the scene's path.
|
2022-08-12 02:21:46 +00:00
|
|
|
func SceneTags(ctx context.Context, s *models.Scene, rw SceneTagUpdater, tagReader match.TagAutoTagQueryer, cache *match.Cache) error {
|
2022-03-09 01:01:56 +00:00
|
|
|
t := getSceneFileTagger(s, cache)
|
2021-04-26 02:51:31 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
return t.tagTags(ctx, tagReader, func(subjectID, otherID int) (bool, error) {
|
2022-08-12 02:21:46 +00:00
|
|
|
if err := s.LoadTagIDs(ctx, rw); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
existing := s.TagIDs.List()
|
|
|
|
|
|
|
|
if intslice.IntInclude(existing, otherID) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scene.AddTag(ctx, rw, s, otherID); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
2021-04-26 02:51:31 +00:00
|
|
|
})
|
|
|
|
}
|