diff --git a/plugins/markerTagToScene/markerTagToScene.js b/plugins/markerTagToScene/markerTagToScene.js index 8aca037d..24a135f6 100644 --- a/plugins/markerTagToScene/markerTagToScene.js +++ b/plugins/markerTagToScene/markerTagToScene.js @@ -4,37 +4,139 @@ function ok() { }; } -function main() { - var hookContext = input.Args.hookContext; - var opInput = hookContext.input; - var primaryTagID = opInput.primary_tag_id; - var sceneID = opInput.scene_id; +function includes(haystack, needle) { + for (var i = 0; i < haystack.length; ++i) { + if (haystack[i] == needle) return true; + } + return false; +} + +function mapSceneTagsToIds(sceneTags) { + var ret = []; + for (var i = 0; i < sceneTags.length; ++i) { + ret.push(sceneTags[i].id); + } + return ret; +} + +function shouldHandleAllTags() { + var query = + "query Query {\ + configuration {\ + plugins\ + }\ +}"; + + var result = gql.Do(query); + //log.Info("Config is " + JSON.stringify(result.configuration)); + if (!result.configuration) { + throw "Unable to get library paths"; + } + if (result.configuration.plugins.hasOwnProperty("markerTagToScene")) { + //log.Info("allTags is " + result.configuration.plugins.markerTagToScene.allTags); + return !!result.configuration.plugins.markerTagToScene.allTags; + } else { + //log.Info("all tags wasn't found. defaulting to false"); + return false; + } +} + +function processMarker(marker, shouldHandleAllTags) { + log.Debug("processMarker (allTags = " + shouldHandleAllTags + ") " + marker); + var primaryTagID = marker.primary_tag_id; + var sceneID = marker.scene_id; + + var tagIDsToCheck = []; + if (primaryTagID != null) tagIDsToCheck.push(primaryTagID); + + if (shouldHandleAllTags && marker.tag_ids != null) + tagIDsToCheck = tagIDsToCheck.concat(marker.tag_ids); // we can't currently find scene markers. If it's not in the input // then just return - if (!primaryTagID || !sceneID) { + if (tagIDsToCheck.length == 0) { // just return return ok(); } // get the existing scene tags - var sceneTags = getSceneTags(sceneID); - var tagIDs = []; - for (var i = 0; i < sceneTags.length; ++i) { - var tagID = sceneTags[i].id; - if (tagID == primaryTagID) { - log.Debug("primary tag already exists on scene"); - return; - } + var sceneTags = mapSceneTagsToIds(getSceneTags(sceneID)); + var newTags = []; + for (var i = 0; i < tagIDsToCheck.length; ++i) { + var tag = tagIDsToCheck[i]; + if (!includes(sceneTags, tag)) newTags.push(tag); + } - tagIDs.push(tagID); + if (newTags.length == 0) { + // All tags were present; don't do anything + log.Debug("All tags were already present on scene " + sceneID); + return ok(); } + var tagIDs = sceneTags.concat(newTags); + setSceneTags(sceneID, tagIDs); + log.Info("adding tags " + newTags + " to scene " + sceneID); +} - // set the tag on the scene if not present - tagIDs.push(primaryTagID); +function main() { + //log.Info(input); + if (input.args.mode == "processMarkers") { + allTags = shouldHandleAllTags(); + log.Trace("Mode is processMarkers, allTags is " + allTags); + allMarkers = getAllMarkers(); + //The markers come back as {primary_tag: { id: 600 } } + //but processMarker (because of the hook) expects 'primary_tag_id', so transform it here + log.Info( + "markerTagToScene has " + allMarkers.length + " markers to process" + ); + for (var i = 0; i < allMarkers.length; ++i) { + marker = allMarkers[i]; + var sceneMarker = {}; + sceneMarker.id = marker.id; + sceneMarker.scene_id = marker.scene.id; + sceneMarker.primary_tag_id = marker.primary_tag.id; + tag_ids = []; + for (j = 0; j < marker.tags.length; ++j) { + tag_ids.push(marker.tags[j].id); + } + sceneMarker.tag_ids = tag_ids; + //log.Info(sceneMarker); + processMarker(sceneMarker, allTags); + log.Progress(i / allMarkers.length); + } + log.Progress("Finished processing markers"); + } else if (input.args.mode == "hook") { + log.Info("Mode is hook"); + processMarker(input.Args.hookContext.input, shouldHandleAllTags()); + } else { + log.Error("Unknown mode"); + } +} - setSceneTags(sceneID, tagIDs); - log.Info("added primary tag " + primaryTagID + " to scene " + sceneID); +function getAllMarkers() { + var query = + "\ +query Query($filter: FindFilterType) {\ + findSceneMarkers (filter: $filter) {\ + scene_markers {\ + id,\ + primary_tag {\ + id\ + }\ + tags {\ + id\ + }\ + scene {\ + id\ + }\ + }\ + }\ +}"; + var variables = { filter: { per_page: -1 } }; + var result = gql.Do(query, variables); + var findSceneMarkers = result.findSceneMarkers; + if (findSceneMarkers) { + return findSceneMarkers.scene_markers; + } } function getSceneTags(sceneID) { diff --git a/plugins/markerTagToScene/markerTagToScene.yml b/plugins/markerTagToScene/markerTagToScene.yml index f5973e55..a0097d81 100644 --- a/plugins/markerTagToScene/markerTagToScene.yml +++ b/plugins/markerTagToScene/markerTagToScene.yml @@ -6,9 +6,21 @@ version: 1.0 exec: - markerTagToScene.js interface: js +settings: + allTags: + displayName: All Tags + description: Add all scene tags instead of just the primary scene tag. + type: BOOLEAN hooks: - name: Update scene with scene marker tag description: Adds primary tag of Scene Marker to the Scene on marker create/update. triggeredBy: - SceneMarker.Create.Post - SceneMarker.Update.Post + defaultArgs: + mode: hook +tasks: + - name: Process all markers + description: Add tags from all markers to scenes + defaultArgs: + mode: processMarkers