From b864a03da8933a2c43d462f8b16362b83aebd140 Mon Sep 17 00:00:00 2001 From: Tariq Soliman Date: Wed, 13 Dec 2023 16:12:00 -0800 Subject: [PATCH] #471 Additional Body Metadata for Draw Webhooks (#472) * #471 Webhook fields 1 * #471 Additional Body Metadata for Draw Webhooks * #471 Add raw_file_description --- .../Webhooks/processes/triggerwebhooks.js | 132 ++++++++++++------ config/js/webhooks.js | 2 +- configuration/webpack.config.js | 4 - private/api/spice/chronos.py | 3 +- 4 files changed, 95 insertions(+), 46 deletions(-) diff --git a/API/Backend/Webhooks/processes/triggerwebhooks.js b/API/Backend/Webhooks/processes/triggerwebhooks.js index 914203fc..2501c3b6 100644 --- a/API/Backend/Webhooks/processes/triggerwebhooks.js +++ b/API/Backend/Webhooks/processes/triggerwebhooks.js @@ -84,16 +84,9 @@ function drawFileUpdate(webhook, payload) { response.send = function (res) { const webhookHeader = JSON.parse(webhook.header); const webhookBody = JSON.parse(webhook.body); - const file_name = res.body?.file?.[0]?.file_name || null; - const file_owner = res.body?.file?.[0]?.file_owner || null; - const geojson = res.body.geojson; - const injectableVariables = { - file_id, - file_name, - file_owner, - geojson, - }; + const file = res.body?.file?.[0] || {}; + const injectableVariables = getInjectableVariables("draw", file, res); // Build the body buildBody(webhookBody, injectableVariables); @@ -108,27 +101,6 @@ function drawFileUpdate(webhook, payload) { getfile(data, response); } -function buildBody(webhookBody, injectableVariables) { - // Fill in the body - for (var i in webhookBody) { - var match = INJECT_REGEX.exec(webhookBody[i]); - // Match for curly braces. If the value contains no curly braces, assume the value is hardcoded so leave the value as is - if (match) { - var variable = match[1]; - if (!injectableVariables[variable]) { - logger( - "error", - "The variable '" + variable + "' is not an injectable variable", - "Webhooks", - null, - "The variable '" + variable + "' is not an injectable variable" - ); - } - webhookBody[i] = injectableVariables[variable]; - } - } -} - function drawFileDelete(webhook, payload) { const file_id = payload.id; const data = { @@ -147,16 +119,9 @@ function drawFileDelete(webhook, payload) { response.send = function (res) { const webhookHeader = JSON.parse(webhook.header); const webhookBody = JSON.parse(webhook.body); - const geojson = res.body.geojson; - const file_name = res.body?.file?.[0]?.file_name || null; - const file_owner = res.body?.file?.[0]?.file_owner || null; - const injectableVariables = { - file_id, - file_name, - file_owner, - geojson, - }; + const file = res.body?.file?.[0] || {}; + const injectableVariables = getInjectableVariables("draw", file, res); // Build the body buildBody(webhookBody, injectableVariables); @@ -171,12 +136,99 @@ function drawFileDelete(webhook, payload) { getfile(data, response); } +function getInjectableVariables(type, file, res) { + const injectableVariables = {}; + switch (type) { + case "draw": + const injectableNames = [ + "id", + "file_owner", + "file_owner_group", + "file_name", + "file_description", + "is_master", + "intent", + "public", + "hidden", + "template", + "publicity_type", + "public_editors", + "created_on", + "updated_on", + ]; + injectableNames.forEach((name) => { + injectableVariables[name] = file[name]; + }); + + const geojson = res.body.geojson; + + injectableVariables.geojson = geojson; + injectableVariables.file_id = injectableVariables.id; + + if (typeof injectableVariables.file_description === "string") { + injectableVariables.raw_file_description = + injectableVariables.file_description; + const tags = injectableVariables.file_description.match(/~#\w+/g) || []; + const uniqueTags = [...tags]; + // remove '#'s + injectableVariables.tags = uniqueTags.map((t) => t.substring(2)) || []; + + const folders = + injectableVariables.file_description.match(/~@\w+/g) || []; + const uniqueFolders = [...folders]; + // remove '@'s + injectableVariables.folders = + uniqueFolders.map((t) => t.substring(2)) || []; + + const efolders = + injectableVariables.file_description.match(/~\^\w+/g) || []; + const uniqueEFolders = [...efolders]; + // remove '^'s + injectableVariables.efolders = + uniqueEFolders.map((t) => t.substring(2)) || []; + + injectableVariables.file_description = + injectableVariables.file_description + .replaceAll(/~#\w+/g, "") + .replaceAll(/~@\w+/g, "") + .replaceAll(/~\^\w+/g, "") + .trimStart() + .trimEnd(); + } + break; + default: + break; + } + return injectableVariables; +} + +function buildBody(webhookBody, injectableVariables) { + // Fill in the body + for (var i in webhookBody) { + var match = INJECT_REGEX.exec(webhookBody[i]); + // Match for curly braces. If the value contains no curly braces, assume the value is hardcoded so leave the value as is + if (match) { + var variable = match[1]; + if (!injectableVariables.hasOwnProperty(variable)) { + logger( + "error", + "The variable '" + variable + "' is not an injectable variable", + "Webhooks", + null, + "The variable '" + variable + "' is not an injectable variable" + ); + } + webhookBody[i] = injectableVariables[variable]; + } + } +} + function buildUrl(url, injectableVariables) { var updatedUrl = url; var match; while (null !== (match = INJECT_REGEX.exec(updatedUrl))) { var variable = match[1]; - if (!injectableVariables[variable]) { + if (!injectableVariables.hasOwnProperty(variable)) { logger( "error", "The variable '" + variable + "' is not an injectable variable", diff --git a/config/js/webhooks.js b/config/js/webhooks.js index de4a3608..1226bc47 100644 --- a/config/js/webhooks.js +++ b/config/js/webhooks.js @@ -126,7 +126,7 @@ function makeWebhookCard(data) { "" + "
  • " + "
    " + - "" + + "" + "
    " + "
    " + "Delete" + diff --git a/configuration/webpack.config.js b/configuration/webpack.config.js index c41500bd..a08eed67 100644 --- a/configuration/webpack.config.js +++ b/configuration/webpack.config.js @@ -42,10 +42,6 @@ crypto.createHash = (algorithm) => crypto_orig_createHash(algorithm === "md4" ? "sha256" : algorithm); // end hack -console.log( - process.env.GENERATE_SOURCEMAP, - typeof process.env.GENERATE_SOURCEMAP -); // Source maps are resource heavy and can cause out of memory issue for large source files. const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP === "true"; // Some apps do not need the benefits of saving a web request, so not inlining the chunk diff --git a/private/api/spice/chronos.py b/private/api/spice/chronos.py index b53f2235..a6599b95 100644 --- a/private/api/spice/chronos.py +++ b/private/api/spice/chronos.py @@ -57,4 +57,5 @@ def chronos(target, fromFormat, fromtype, to, totype, time): try: print(chronos(target, fromFormat, fromtype, to, totype, time)) except: - print(json.dumps({"error": True, "message": 'Error: ' + str(sys.exc_info()[0])})) \ No newline at end of file + print(json.dumps({"error": True, "message": 'Error: ' + str(sys.exc_info()[0])})) + \ No newline at end of file