From 74259d59f5752ea95636ad9200c11b606a100eba Mon Sep 17 00:00:00 2001 From: DuctTape42 <110079000+DuctTape42@users.noreply.github.com> Date: Wed, 5 Jun 2024 07:08:02 -0700 Subject: [PATCH] Add multiple tags and task to markerToSceneTag (#325) * Add the ability to add all tags and not just the primary tag to the scene Add config to control above behavior Add a task to run over all markers in database * Run prettier on markerTagToScene.js * Fix markerTagToScene for v0.26 changes * Update markerTagToScene.js for new JS engine --------- Co-authored-by: DuctTape42 --- plugins/markerTagToScene/markerTagToScene.js | 137 ++++++++++++++++-- plugins/markerTagToScene/markerTagToScene.yml | 12 ++ 2 files changed, 136 insertions(+), 13 deletions(-) diff --git a/plugins/markerTagToScene/markerTagToScene.js b/plugins/markerTagToScene/markerTagToScene.js index 2010672f..c256adf9 100644 --- a/plugins/markerTagToScene/markerTagToScene.js +++ b/plugins/markerTagToScene/markerTagToScene.js @@ -4,28 +4,139 @@ function ok() { }; } -function main() { - var hookContext = input.Args.hookContext; - var opInput = hookContext.Input; - var primaryTagID = opInput.PrimaryTagID; - var sceneID = opInput.SceneID; +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 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); + } + + 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); +} - // Combine all tags from scene and the new marker - var allTags = [...new Set([...sceneTags, primaryTagID, ...opInput.TagIds])]; - var newTags = allTags.filter((t) => sceneTags.includes(t)); +function main() { + log.Info(JSON.stringify(input)); + if (input.Args.mode == "processMarkers") { + var allTags = shouldHandleAllTags(); + log.Trace("Mode is processMarkers, allTags is " + allTags); + var 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) { + var marker = allMarkers[i]; + var sceneMarker = {}; + sceneMarker.id = marker.id; + sceneMarker.scene_id = marker.scene.id; + sceneMarker.primary_tag_id = marker.primary_tag.id; + var tag_ids = []; + for (var 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, allTags); - log.Debug("added new tags " + newTags.join(", ") + " 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) { @@ -46,7 +157,7 @@ query findScene($id: ID) {\ var result = gql.Do(query, variables); var findScene = result.findScene; if (findScene) { - return findScene.tags.map((t) => t.id); + return findScene.tags; } return []; 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