summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSam X. Chen <sam.xi.chen@gmail.com>2019-08-29 22:07:40 -0400
committerSam X. Chen <sam.xi.chen@gmail.com>2019-08-29 22:07:40 -0400
commitdd0682328bc26bbe852fb19a85131e4017c547b0 (patch)
treee9171a1703b96820aefb38e7bfb3f03defbee12f /server
parent3f0600fed70512f87dc20fe039695d1681a73d39 (diff)
downloadwekan-dd0682328bc26bbe852fb19a85131e4017c547b0.tar.gz
wekan-dd0682328bc26bbe852fb19a85131e4017c547b0.tar.bz2
wekan-dd0682328bc26bbe852fb19a85131e4017c547b0.zip
Add Feature: enable two-way webhooks - stage two
Diffstat (limited to 'server')
-rw-r--r--server/notifications/outgoing.js97
1 files changed, 80 insertions, 17 deletions
diff --git a/server/notifications/outgoing.js b/server/notifications/outgoing.js
index 85d54968..850b3acd 100644
--- a/server/notifications/outgoing.js
+++ b/server/notifications/outgoing.js
@@ -8,6 +8,19 @@ const postCatchError = Meteor.wrapAsync((url, options, resolve) => {
});
});
+const Lock = {
+ _lock: {},
+ has(id) {
+ return !!this._lock[id];
+ },
+ set(id) {
+ this._lock[id] = 1;
+ },
+ unset(id) {
+ delete this._lock[id];
+ },
+};
+
const webhooksAtbts = (process.env.WEBHOOKS_ATTRIBUTES &&
process.env.WEBHOOKS_ATTRIBUTES.split(',')) || [
'cardId',
@@ -20,15 +33,44 @@ const webhooksAtbts = (process.env.WEBHOOKS_ATTRIBUTES &&
'commentId',
'swimlaneId',
];
-
+const responseFunc = 'reactOnHookResponse';
Meteor.methods({
- outgoingWebhooks(integrations, description, params) {
- check(integrations, Array);
+ [responseFunc](data) {
+ check(data, Object);
+ const paramCommentId = data.commentId;
+ const paramCardId = data.cardId;
+ const paramBoardId = data.boardId;
+ const newComment = data.comment;
+ if (paramCardId && paramBoardId && newComment) { // only process data with the cardid, boardid and comment text, TODO can expand other functions here to react on returned data
+ const comment = CardComments.findOne({
+ _id: paramCommentId,
+ cardId: paramCardId,
+ boardId: paramBoardId,
+ });
+ if (comment) {
+ CardComments.update(comment._id, {
+ $set: {
+ text: newComment,
+ },
+ });
+ } else {
+ CardComments.insert({
+ text: newComment,
+ cardId,
+ boardId,
+ });
+ }
+ }
+ },
+ outgoingWebhooks(integration, description, params) {
+ check(integration, Object);
check(description, String);
check(params, Object);
+ this.unblock();
// label activity did not work yet, see wekan/models/activities.js
const quoteParams = _.clone(params);
+ const clonedParams = _.clone(params);
[
'card',
'list',
@@ -63,23 +105,44 @@ Meteor.methods({
if (params[key]) value[key] = params[key];
});
value.description = description;
-
+ //integrations.forEach(integration => {
+ const is2way = integration.type === Integrations.Const.TWOWAY;
+ const token = integration.token || '';
+ const headers = {
+ 'Content-Type': 'application/json',
+ };
+ if (token) headers['X-Wekan-Token'] = token;
const options = {
- headers: {
- // 'Content-Type': 'application/json',
- // 'X-Wekan-Activities-Token': 'Random.Id()',
- },
- data: value,
+ headers,
+ data: is2way ? clonedParams : value,
};
+ const url = integration.url;
+ const response = postCatchError(url, options);
- integrations.forEach(integration => {
- const response = postCatchError(integration.url, options);
-
- if (response && response.statusCode && response.statusCode === 200) {
- return true; // eslint-disable-line consistent-return
- } else {
- throw new Meteor.Error('error-invalid-webhook-response');
+ if (response && response.statusCode && response.statusCode === 200) {
+ if (is2way) {
+ const cid = params.commentId;
+ const tooSoon = Lock.has(cid); // if an activity happens to fast, notification shouldn't fire with the same id
+ if (!tooSoon) {
+ let clearNotification = () => {};
+ if (cid) {
+ Lock.set(cid);
+ const clearNotificationFlagTimeout = 1000;
+ clearNotification = () => Lock.unset(cid);
+ Meteor.setTimeout(clearNotification, clearNotificationFlagTimeout);
+ }
+ const data = response.data; // only an JSON encoded response will be actioned
+ if (data) {
+ Meteor.call(responseFunc, data, () => {
+ clearNotification();
+ });
+ }
+ }
}
- });
+ return response; // eslint-disable-line consistent-return
+ } else {
+ throw new Meteor.Error('error-invalid-webhook-response');
+ }
+ //});
},
});