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"
|
2023-11-01 21:58:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil"
|
2021-04-26 02:51:31 +00:00
|
|
|
)
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
type SceneFinderUpdater interface {
|
|
|
|
models.SceneQueryer
|
|
|
|
models.SceneUpdater
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
type ScenePerformerUpdater interface {
|
|
|
|
models.PerformerIDLoader
|
2023-09-01 00:39:29 +00:00
|
|
|
models.SceneUpdater
|
2022-08-12 02:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SceneTagUpdater interface {
|
|
|
|
models.TagIDLoader
|
2023-09-01 00:39:29 +00:00
|
|
|
models.SceneUpdater
|
2022-08-12 02:21:46 +00:00
|
|
|
}
|
|
|
|
|
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",
|
2022-09-02 01:18:37 +00:00
|
|
|
Name: s.DisplayName(),
|
2022-09-01 07:54:34 +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.
|
2023-09-01 00:39:29 +00:00
|
|
|
func ScenePerformers(ctx context.Context, s *models.Scene, rw ScenePerformerUpdater, performerReader models.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()
|
|
|
|
|
2023-11-01 21:58:32 +00:00
|
|
|
if sliceutil.Contains(existing, otherID) {
|
2022-08-12 02:21:46 +00:00
|
|
|
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.
|
2023-09-01 00:39:29 +00:00
|
|
|
func SceneStudios(ctx context.Context, s *models.Scene, rw SceneFinderUpdater, studioReader models.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.
|
2023-09-01 00:39:29 +00:00
|
|
|
func SceneTags(ctx context.Context, s *models.Scene, rw SceneTagUpdater, tagReader models.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()
|
|
|
|
|
2023-11-01 21:58:32 +00:00
|
|
|
if sliceutil.Contains(existing, otherID) {
|
2022-08-12 02:21:46 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|