Update gallery when adding image via scan (#3802)

This commit is contained in:
WithoutPants 2023-06-06 13:25:11 +10:00 committed by GitHub
parent e22291d912
commit 0c999080c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,6 +36,7 @@ type GalleryFinderCreator interface {
FindByFileID(ctx context.Context, fileID file.ID) ([]*models.Gallery, error)
FindByFolderID(ctx context.Context, folderID file.FolderID) ([]*models.Gallery, error)
Create(ctx context.Context, newObject *models.Gallery, fileIDs []file.ID) error
UpdatePartial(ctx context.Context, id int, updatedGallery models.GalleryPartial) (*models.Gallery, error)
}
type ScanConfig interface {
@ -117,10 +118,16 @@ func (h *ScanHandler) Handle(ctx context.Context, f file.File, oldFile file.File
logger.Infof("%s doesn't exist. Creating new image...", f.Base().Path)
if _, err := h.associateGallery(ctx, newImage, imageFile); err != nil {
g, err := h.getGalleryToAssociate(ctx, newImage, f)
if err != nil {
return err
}
if g != nil {
newImage.GalleryIDs.Add(g.ID)
logger.Infof("Adding %s to gallery %s", f.Base().Path, g.Path)
}
if err := h.CreatorUpdater.Create(ctx, &models.ImageCreateInput{
Image: newImage,
FileIDs: []file.ID{imageFile.ID},
@ -128,6 +135,15 @@ func (h *ScanHandler) Handle(ctx context.Context, f file.File, oldFile file.File
return fmt.Errorf("creating new image: %w", err)
}
// update the gallery updated at timestamp if applicable
if g != nil {
if _, err := h.GalleryFinder.UpdatePartial(ctx, g.ID, models.GalleryPartial{
UpdatedAt: models.NewOptionalTime(time.Now()),
}); err != nil {
return fmt.Errorf("updating gallery updated at timestamp: %w", err)
}
}
h.PluginCache.RegisterPostHooks(ctx, newImage.ID, plugin.ImageCreatePost, nil, nil)
existing = []*models.Image{newImage}
@ -172,16 +188,18 @@ func (h *ScanHandler) associateExisting(ctx context.Context, existing []*models.
}
// associate with gallery if applicable
changed, err := h.associateGallery(ctx, i, f)
g, err := h.getGalleryToAssociate(ctx, i, f)
if err != nil {
return err
}
var galleryIDs *models.UpdateIDs
if changed {
changed := false
if g != nil {
changed = true
galleryIDs = &models.UpdateIDs{
IDs: i.GalleryIDs.List(),
Mode: models.RelationshipUpdateModeSet,
IDs: []int{g.ID},
Mode: models.RelationshipUpdateModeAdd,
}
}
@ -203,6 +221,14 @@ func (h *ScanHandler) associateExisting(ctx context.Context, existing []*models.
}); err != nil {
return fmt.Errorf("updating image: %w", err)
}
if g != nil {
if _, err := h.GalleryFinder.UpdatePartial(ctx, g.ID, models.GalleryPartial{
UpdatedAt: models.NewOptionalTime(time.Now()),
}); err != nil {
return fmt.Errorf("updating gallery updated at timestamp: %w", err)
}
}
}
if changed || updateExisting {
@ -331,22 +357,19 @@ func (h *ScanHandler) getOrCreateGallery(ctx context.Context, f file.File) (*mod
return nil, nil
}
func (h *ScanHandler) associateGallery(ctx context.Context, newImage *models.Image, f file.File) (bool, error) {
func (h *ScanHandler) getGalleryToAssociate(ctx context.Context, newImage *models.Image, f file.File) (*models.Gallery, error) {
g, err := h.getOrCreateGallery(ctx, f)
if err != nil {
return false, err
return nil, err
}
if err := newImage.LoadGalleryIDs(ctx, h.CreatorUpdater); err != nil {
return false, err
return nil, err
}
ret := false
if g != nil && !intslice.IntInclude(newImage.GalleryIDs.List(), g.ID) {
ret = true
newImage.GalleryIDs.Add(g.ID)
logger.Infof("Adding %s to gallery %s", f.Base().Path, g.Path)
return g, nil
}
return ret, nil
return nil, nil
}