Add Feature: enable two-way webhooks - stage two

This commit is contained in:
Sam X. Chen 2019-08-29 22:07:40 -04:00
parent 3f0600fed7
commit dd0682328b
4 changed files with 103 additions and 26 deletions

View file

@ -184,10 +184,11 @@ if (Meteor.isServer) {
// it's person at himself, ignore it? // it's person at himself, ignore it?
continue; continue;
} }
const user = Users.findOne(username) || Users.findOne({ username }); const atUser =
const uid = user && user._id; Users.findOne(username) || Users.findOne({ username });
const uid = atUser && atUser._id;
params.atUsername = username; params.atUsername = username;
params.atEmails = user.emails; params.atEmails = atUser.emails;
if (board.hasMember(uid)) { if (board.hasMember(uid)) {
title = 'act-atUserComment'; title = 'act-atUserComment';
watchers = _.union(watchers, [uid]); watchers = _.union(watchers, [uid]);
@ -268,13 +269,23 @@ if (Meteor.isServer) {
}); });
const integrations = Integrations.find({ const integrations = Integrations.find({
boardId: board._id, boardId: { $in: [board._id, Integrations.Const.GLOBAL_WEBHOOK_ID] },
type: 'outgoing-webhooks', // type: 'outgoing-webhooks', // all types
enabled: true, enabled: true,
activities: { $in: [description, 'all'] }, activities: { $in: [description, 'all'] },
}).fetch(); }).fetch();
if (integrations.length > 0) { if (integrations.length > 0) {
Meteor.call('outgoingWebhooks', integrations, description, params); integrations.forEach(integration => {
Meteor.call(
'outgoingWebhooks',
integration,
description,
params,
() => {
return;
},
);
});
} }
}); });
} }

View file

@ -90,7 +90,11 @@ Integrations.attachSchema(
); );
Integrations.Const = { Integrations.Const = {
GLOBAL_WEBHOOK_ID: '_global', GLOBAL_WEBHOOK_ID: '_global',
WEBHOOK_TYPES: ['outgoing-webhooks', 'bidirectional-webhooks'], ONEWAY: 'outgoing-webhooks',
TWOWAY: 'bidirectional-webhooks',
get WEBHOOK_TYPES() {
return [this.ONEWAY, this.TWOWAY];
},
}; };
const permissionHelper = { const permissionHelper = {
allow(userId, doc) { allow(userId, doc) {

View file

@ -147,7 +147,6 @@ if (Meteor.isServer) {
}:${doc.mailServer.port}/`; }:${doc.mailServer.port}/`;
} }
Accounts.emailTemplates.from = doc.mailServer.from; Accounts.emailTemplates.from = doc.mailServer.from;
console.log('Settings saved:', Accounts.emailTemplates);
} }
}); });

View file

@ -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 && const webhooksAtbts = (process.env.WEBHOOKS_ATTRIBUTES &&
process.env.WEBHOOKS_ATTRIBUTES.split(',')) || [ process.env.WEBHOOKS_ATTRIBUTES.split(',')) || [
'cardId', 'cardId',
@ -20,15 +33,44 @@ const webhooksAtbts = (process.env.WEBHOOKS_ATTRIBUTES &&
'commentId', 'commentId',
'swimlaneId', 'swimlaneId',
]; ];
const responseFunc = 'reactOnHookResponse';
Meteor.methods({ Meteor.methods({
outgoingWebhooks(integrations, description, params) { [responseFunc](data) {
check(integrations, Array); 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(description, String);
check(params, Object); check(params, Object);
this.unblock();
// label activity did not work yet, see wekan/models/activities.js // label activity did not work yet, see wekan/models/activities.js
const quoteParams = _.clone(params); const quoteParams = _.clone(params);
const clonedParams = _.clone(params);
[ [
'card', 'card',
'list', 'list',
@ -63,23 +105,44 @@ Meteor.methods({
if (params[key]) value[key] = params[key]; if (params[key]) value[key] = params[key];
}); });
value.description = description; value.description = description;
//integrations.forEach(integration => {
const options = { const is2way = integration.type === Integrations.Const.TWOWAY;
headers: { const token = integration.token || '';
// 'Content-Type': 'application/json', const headers = {
// 'X-Wekan-Activities-Token': 'Random.Id()', 'Content-Type': 'application/json',
},
data: value,
}; };
if (token) headers['X-Wekan-Token'] = token;
const options = {
headers,
data: is2way ? clonedParams : value,
};
const url = integration.url;
const response = postCatchError(url, options);
integrations.forEach(integration => { if (response && response.statusCode && response.statusCode === 200) {
const response = postCatchError(integration.url, options); if (is2way) {
const cid = params.commentId;
if (response && response.statusCode && response.statusCode === 200) { const tooSoon = Lock.has(cid); // if an activity happens to fast, notification shouldn't fire with the same id
return true; // eslint-disable-line consistent-return if (!tooSoon) {
} else { let clearNotification = () => {};
throw new Meteor.Error('error-invalid-webhook-response'); 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');
}
//});
}, },
}); });