Skip to content

Commit

Permalink
Add multiple tags and task to markerToSceneTag (#325)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
DuctTape42 and DuctTape42 authored Jun 5, 2024
1 parent 7fc9138 commit 74259d5
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 13 deletions.
137 changes: 124 additions & 13 deletions plugins/markerTagToScene/markerTagToScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 [];
Expand Down
12 changes: 12 additions & 0 deletions plugins/markerTagToScene/markerTagToScene.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 74259d5

Please sign in to comment.