@@ -632,6 +632,65 @@ function PublishTask.shouldDeletePhotosFromServiceOnDeleteFromCatalog(publishSet
632632 return nil -- Show builtin Lightroom dialog.
633633end
634634
635+ -- ************************************************
636+ function PublishTask .imposeSortOrderOnPublishedCollection (publishSettings , info , remoteIdSequence )
637+ -- This callback is called by Lightroom for smart collections.
638+ -- It allows you to detect published photos that no longer meet the criteria and mark them for
639+ -- deletion.
640+ log :info (" PublishTask.imposeSortOrderOnPublishedCollection" )
641+
642+ local validSequence = {}
643+ local publishedCollection = info .publishedCollection
644+
645+ if not publishedCollection then
646+ return nil
647+ end
648+
649+ -- Check if it is a smart collection
650+ if not publishedCollection :isSmartCollection () then
651+ return nil
652+ end
653+
654+ -- Retrieve photos currently in the smart collection (according to criteria)
655+ local currentPhotos = publishedCollection :getPhotos ()
656+ local currentPhotoIds = {}
657+ for _ , photo in ipairs (currentPhotos ) do
658+ currentPhotoIds [photo .localIdentifier ] = true
659+ end
660+
661+ -- Browse the remoteIds of published photos
662+ -- remoteIdSequence contains the remoteIds in the current order
663+ local publishedPhotos = publishedCollection :getPublishedPhotos ()
664+ local remoteIdToPhoto = {}
665+ for _ , pubPhoto in ipairs (publishedPhotos ) do
666+ local remoteId = pubPhoto :getRemoteId ()
667+ if remoteId then
668+ remoteIdToPhoto [remoteId ] = pubPhoto
669+ end
670+ end
671+
672+ -- Build the valid sequence: only photos that still meet the criteria
673+ for _ , remoteId in ipairs (remoteIdSequence ) do
674+ local pubPhoto = remoteIdToPhoto [remoteId ]
675+ if pubPhoto then
676+ local lrPhoto = pubPhoto :getPhoto ()
677+ if lrPhoto and currentPhotoIds [lrPhoto .localIdentifier ] then
678+ -- The photo still meets the criteria, keep it.
679+ table.insert (validSequence , remoteId )
680+ end
681+ -- If the photo is no longer in currentPhotoIds, it will be marked for deletion because
682+ -- its remoteId will not be in validSequence.
683+ end
684+ end
685+
686+ log :info (" PublishTask.imposeSortOrderOnPublishedCollection - " ..
687+ # remoteIdSequence .. " published, " ..
688+ # validSequence .. " still match criteria, " ..
689+ (# remoteIdSequence - # validSequence ) .. " to delete" )
690+
691+ return validSequence
692+ end
693+
635694-- ************************************************
636695function PublishTask .validatePublishedCollectionName (name )
637696 log :info (" PublishTask.validatePublishedCollectionName" )
0 commit comments