From 49398e5aa3d0b6b8d8925a6ef8b8de8fce0c7be8 Mon Sep 17 00:00:00 2001 From: Sean Dick Date: Mon, 11 Nov 2013 13:21:26 -0800 Subject: [PATCH] fixed the sets behaviour for the lightroom plugin -- renaming is contingent on #60547544 being deployed --- .gitignore | 1 + StippleAPI.lua | 87 +++++++++++++++++++++++++++++--- StippleExportServiceProvider.lua | 49 +++++++++--------- StipplePublishSupport.lua | 23 +++++++-- StippleUser.lua | 6 ++- 5 files changed, 128 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 485dee6..76ff3c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +.DS_STORE diff --git a/StippleAPI.lua b/StippleAPI.lua index d69a312..6839a86 100644 --- a/StippleAPI.lua +++ b/StippleAPI.lua @@ -156,7 +156,7 @@ end -------------------------------------------------------------------------------- -function StippleAPI.callRestMethod( propertyTable, params ) +function StippleAPI.getRestMethod( propertyTable, params ) local apiKey = StippleAPI.getApiKeyAndSecret() if not params.api_key then @@ -239,11 +239,66 @@ end -------------------------------------------------------------------------------- +function StippleAPI.postRestMethod( propertyTable, params ) + assert( type( params ) == 'table', 'StippleAPI.postRestMethod: params must be a table' ) + local apiKey = StippleAPI.getApiKeyAndSecret() + local url = string.format( urlBase .. '/api/v1/%s', assert( params.url ) ) + params.url = nil + + if not params.api_key then + params.api_key = apiKey + end + + local suppressError = params.suppressError + local suppressErrorCodes = params.suppressErrorCodes + local skipAuthToken = params.skipAuthToken + + params.suppressError = nil + params.suppressErrorCodes = nil + params.skipAuthToken = nil + local chunks = {} + + for key, value in pairs(params) do + if (type(value) == 'table') then + for attribute, val in pairs(value) do + if (type(attribute) == 'number') then + attribute = '' + end + table.insert(chunks, { name = key .. '[' .. attribute .. ']', value = val }) + end + else + table.insert(chunks, {name = key, value = value }) + end + end + + local response, hdrs = LrHttp.postMultipart( url, chunks ) -- Post it and wait for confirmation. + + if not response then + if hdrs and hdrs.error then + LrErrors.throwUserError( formatError( hdrs.error.nativeCode ) ) + end + end + + local json = JSON:decode(response) + if tonumber(json.status) == 200 then + return json + else + return json + end +end + +-------------------------------------------------------------------------------- + function StippleAPI.uploadPhoto( propertyTable, params ) assert( type( params ) == 'table', 'StippleAPI.uploadPhoto: params must be a table' ) local apiKey = StippleAPI.getApiKeyAndSecret() - local postUrl = params.id and urlBase .. '/api/v1/photos/update' or urlBase .. '/api/v1/photos/upload/' + local postUrl = '' + if not not params.id then + postUrl = urlBase .. '/api/v1/photos/update' + else + postUrl = urlBase .. '/api/v1/photos/upload/' + end local originalParams = params.id and table.shallowcopy( params ) logger:info( 'uploading photo', params.filePath ) @@ -281,7 +336,7 @@ function StippleAPI.uploadPhoto( propertyTable, params ) local json = JSON:decode(response) if tonumber(json.status) == 200 then - return json.data.photo.id + return tostring(json.data.photo.id) elseif params.id and json.error and tonumber(hdrs.error.nativeCode) == 422 then -- Photo is missing. Most likely, the user deleted it outside of Lightroom. Just repost it. @@ -299,7 +354,7 @@ end -------------------------------------------------------------------------------- function StippleAPI.openAuthUrl() - local response = StippleAPI.callRestMethod( nil, { url = 'users/me' } ) + local response = StippleAPI.getRestMethod( nil, { url = 'users/me' } ) return response.data end @@ -337,13 +392,26 @@ end -------------------------------------------------------------------------------- function StippleAPI.createOrUpdatePhotoset( propertyTable, params ) - return true + local response = {} + if params.photosetId then + StippleAPI.postRestMethod(nil, { url = "sets/update", id = params.photosetId, set = { name = params.title } }) + return params.photosetId, StippleAPI.constructPhotosetURL(propertyTable, params.photosetId) + end + + response = StippleAPI.postRestMethod(nil, { url = "sets/create", set = params }) + + return tostring(response.data.set.id), StippleAPI.constructPhotosetURL(propertyTable, tostring(response.data.set.id)) end -------------------------------------------------------------------------------- function StippleAPI.listPhotosFromPhotoset( propertyTable, params ) - return nil + local response, ids = {}, {} + response = StippleAPI.getRestMethod(nil, {url = 'sets/' .. params.photosetId}) + for _,photo in pairs(response.data.set.photos) do + table.insert(ids,photo.id) + end + return ids end -------------------------------------------------------------------------------- @@ -355,18 +423,23 @@ end -------------------------------------------------------------------------------- function StippleAPI.addPhotosToSet( propertyTable, params ) + local data = {} + data = StippleAPI.postRestMethod(nil, { url = 'sets/add', id = params.photosetId, photo_ids = params.photoId }) return true end -------------------------------------------------------------------------------- -function StippleAPI.deletePhoto( propertyTable, params ) +function StippleAPI.deletePhotoFromPhotoset( propertyTable, params ) + local data = {} + data = StippleAPI.postRestMethod(nil, { url = 'sets/remove', id = params.photosetId, photo_ids = params.photoId }) return true end -------------------------------------------------------------------------------- function StippleAPI.deletePhotoset( propertyTable, params ) + data = StippleAPI.postRestMethod(nil, {url = 'sets/delete', id = params.photosetId}) return true end diff --git a/StippleExportServiceProvider.lua b/StippleExportServiceProvider.lua index f41ac11..bc7fd07 100644 --- a/StippleExportServiceProvider.lua +++ b/StippleExportServiceProvider.lua @@ -553,7 +553,7 @@ function exportServiceProvider.processRenderedPhotos( functionContext, exportCon progressScope:setPortionComplete( ( i - 1 ) / nPhotos ) -- Update progress scope. local photo = rendition.photo -- Get next photo. - local stipplePhotoId = stipplePhotoIdsForRenditions[rendition] -- See if we previously uploaded this photo. + local stipplePhotoId = rendition.publishedPhotoId -- See if we previously uploaded this photo. if not rendition.wasSkipped then local success, pathOrMessage = rendition:waitForRender() -- Update progress scope again once we've got rendered photo. @@ -598,18 +598,18 @@ function exportServiceProvider.processRenderedPhotos( functionContext, exportCon } ) - if didReplace then - -- The replace call used by StippleAPI.uploadPhoto ignores all of the metadata that is passed - -- in above. We have to manually upload that info after the fact in this case. - if exportSettings.titleRepublishBehavior == 'replace' then - --StippleAPI.callRestMethod( exportSettings, { - --method = 'stipple.photos.setMeta', - --photo_id = stipplePhotoId, - --title = title or '', - --description = description or '', - --}) - end - end + -- if didReplace then + -- -- The replace call used by StippleAPI.uploadPhoto ignores all of the metadata that is passed + -- -- in above. We have to manually upload that info after the fact in this case. + -- if exportSettings.titleRepublishBehavior == 'replace' then + -- --StippleAPI.callRestMethod( exportSettings, { + -- --method = 'stipple.photos.setMeta', + -- --photo_id = stipplePhotoId, + -- --title = title or '', + -- --description = description or '', + -- --}) + -- end + -- end -- When done with photo, delete temp file. There is a cleanup step that happens later, -- but this will help manage space in the event of a large upload. @@ -623,13 +623,12 @@ function exportServiceProvider.processRenderedPhotos( functionContext, exportCon if not isDefaultCollection then -- Create or update this photoset. photosetUrl = 'https://stipple.com' - - --photosetId, photosetUrl = StippleAPI.createOrUpdatePhotoset(exportSettings, { - --photosetId = photosetId, - --title = publishedCollectionInfo.name, - --description = ??, - --primary_photo_id = uploadedPhotoIds[ 1 ], - --}) + photosetId, photosetUrl = StippleAPI.createOrUpdatePhotoset(exportSettings, { + photosetId = photosetId, + name = publishedCollectionInfo.name, + description = '', + primary_photo_id = uploadedPhotoIds[ 1 ], + }) else photosetUrl = StippleAPI.constructPhotostreamURL( exportSettings ) -- Photostream: find the URL. end @@ -646,10 +645,10 @@ function exportServiceProvider.processRenderedPhotos( functionContext, exportCon }) -- Add the uploaded photos to the correct photoset. - -- StippleAPI.addPhotosToSet(exportSettings, { - -- photoId = stipplePhotoId, - -- photosetId = photosetId, - -- }) + StippleAPI.addPhotosToSet(exportSettings, { + photoId = stipplePhotoId, + photosetId = photosetId, + }) else photoUrl = StippleAPI.constructPhotoURL(exportSettings, { id = stipplePhotoId, @@ -673,7 +672,7 @@ function exportServiceProvider.processRenderedPhotos( functionContext, exportCon if #uploadedPhotoIds > 0 then if (not isDefaultCollection) then - --exportSession:recordRemoteCollectionId( photosetId ) + exportSession:recordRemoteCollectionId( photosetId ) end -- Set up some additional metadata for this collection. diff --git a/StipplePublishSupport.lua b/StipplePublishSupport.lua index 3fab2a1..c091ee2 100644 --- a/StipplePublishSupport.lua +++ b/StipplePublishSupport.lua @@ -1,5 +1,8 @@ -- Lightroom SDK local LrDialogs = import 'LrDialogs' +local LrApplication = import 'LrApplication' +local LrTasks = import 'LrTasks' +local catalog = LrApplication.activeCatalog() -- Stipple plug-in require 'StippleAPI' @@ -503,10 +506,22 @@ end -- @param localCollectionId (number) The local identifier for the collection for which -- photos are being removed. -function publishServiceProvider.deletePhotosFromPublishedCollection( publishSettings, arrayOfPhotoIds, deletedCallback ) - for i, photoId in ipairs( arrayOfPhotoIds ) do - StippleAPI.deletePhoto( publishSettings, { photoId = photoId, suppressErrorCodes = { [ 1 ] = true } } ) -- If Stipple says photo not found, ignore that. - deletedCallback( photoId ) +function publishServiceProvider.deletePhotosFromPublishedCollection( publishSettings, arrayOfPhotoIds, deletedCallback, localId ) + local set = {} + + local function removePhotos(setId) + for i, photoId in ipairs( arrayOfPhotoIds ) do + StippleAPI.deletePhotoFromPhotoset( publishSettings, { photoId = photoId, photosetId = setId, suppressErrorCodes = { [ 1 ] = true } } ) -- If Stipple says photo not found, ignore that. + end + end + + LrTasks.startAsyncTask(function() + set = catalog:getPublishedCollectionByLocalIdentifier(localId) + removePhotos(set:getRemoteId()) + end) + + for _,id in ipairs( arrayOfPhotoIds ) do + deletedCallback( id ) end end diff --git a/StippleUser.lua b/StippleUser.lua index 817c50b..2fb903e 100644 --- a/StippleUser.lua +++ b/StippleUser.lua @@ -65,8 +65,9 @@ function StippleUser.login( propertyTable ) if propertyTable.LR_editingExistingPublishConnection then if auth.user and propertyTable.nsid ~= auth.user.id then - LrDialogs.message( LOC "$$$/Stipple/CantChangeUserID=You can not change Stipple accounts on an existing publish connection. Please log in again with the account you used when you first created this connection." ) - return + -- propertyTable.nsid = auth.user.id + -- LrDialogs.message( LOC "$$$/Stipple/CantChangeUserID=You can not change Stipple accounts on an existing publish connection. Please log in again with the account you used when you first created this connection." ) + -- return end end @@ -117,6 +118,7 @@ function StippleUser.verifyLogin(propertyTable) propertyTable.validAccount = true end else + propertyTable.LR_editingExistingPublishConnection = true notLoggedIn(propertyTable) end