Skip to content

Commit

Permalink
#471 Additional Body Metadata for Draw Webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman committed Dec 14, 2023
1 parent b0a9fee commit 43c65a1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 46 deletions.
130 changes: 90 additions & 40 deletions API/Backend/Webhooks/processes/triggerwebhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = {
Expand All @@ -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);
Expand All @@ -171,12 +136,97 @@ 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") {
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",
Expand Down
2 changes: 1 addition & 1 deletion config/js/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function makeWebhookCard(data) {
"</li>" +
"<li class='row'>" +
"<div class='inject-label input-field col s9' id='webhookUrlEl'>" +
"<label style='top: 0px; font-size: 14px;'>Valid injectable variables for URL and Body fields: {created_on}, {file_description}, {file_id}, {file_name}, {file_owner}, {file_owner_group}, {hidden}, {intent}, {is_master}, {public}, {public_editors}, {publicity_type}, {template}, {updated_on}, {geojson}</label>" +
"<label style='top: 0px; font-size: 14px;'>Valid injectable variables for URL and Body fields: {created_on}, {efolders}, {file_description}, {file_id}, {file_name}, {file_owner}, {file_owner_group}, {folders}, {geojson}, {hidden}, {intent}, {is_master}, {public}, {public_editors}, {publicity_type}, {tags}, {template}, {updated_on}</label>" +
"</div>" +
"<div class='col s3 push-s1' id='deleteWebhook_" + webhooksCounter +"'>" +
"<a class='btn waves-effect' style='color: black; background: white;'>Delete<i class='mdi mdi-delete mdi-24px' style='float: right; margin-top: 1px;'></i></a>" +
Expand Down
4 changes: 0 additions & 4 deletions configuration/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion private/api/spice/chronos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])}))
print(json.dumps({"error": True, "message": 'Error: ' + str(sys.exc_info()[0])}))

0 comments on commit 43c65a1

Please sign in to comment.