wekan/server/notifications/outgoing.js

86 lines
2 KiB
JavaScript
Raw Normal View History

2017-07-09 15:02:17 +09:00
const postCatchError = Meteor.wrapAsync((url, options, resolve) => {
HTTP.post(url, options, (err, res) => {
if (err) {
resolve(null, err.response);
} else {
resolve(null, res);
}
});
});
2019-06-28 12:52:09 -05:00
const webhooksAtbts = (process.env.WEBHOOKS_ATTRIBUTES &&
process.env.WEBHOOKS_ATTRIBUTES.split(',')) || [
'cardId',
'listId',
'oldListId',
'boardId',
'comment',
'user',
'card',
'commentId',
'swimlaneId',
];
2017-07-09 15:02:17 +09:00
Meteor.methods({
outgoingWebhooks(integrations, description, params) {
check(integrations, Array);
2017-07-09 15:02:17 +09:00
check(description, String);
check(params, Object);
// label activity did not work yet, see wekan/models/activities.js
2017-07-09 15:02:17 +09:00
const quoteParams = _.clone(params);
2019-06-28 12:52:09 -05:00
[
'card',
'list',
'oldList',
'board',
'oldBoard',
'comment',
'checklist',
'swimlane',
'oldSwimlane',
'label',
'attachment',
].forEach(key => {
2017-07-09 15:02:17 +09:00
if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
});
2019-06-28 12:52:09 -05:00
const userId = params.userId ? params.userId : integrations[0].userId;
const user = Users.findOne(userId);
2019-06-28 12:52:09 -05:00
const text = `${params.user} ${TAPi18n.__(
description,
quoteParams,
user.getLanguage(),
)}\n${params.url}`;
2017-07-09 15:02:17 +09:00
if (text.length === 0) return;
const value = {
text: `${text}`,
};
2019-06-28 12:52:09 -05:00
webhooksAtbts.forEach(key => {
2017-08-15 11:08:16 -03:00
if (params[key]) value[key] = params[key];
});
value.description = description;
2017-08-15 11:08:16 -03:00
2017-07-09 15:02:17 +09:00
const options = {
headers: {
// 'Content-Type': 'application/json',
// 'X-Wekan-Activities-Token': 'Random.Id()',
},
data: value,
};
2019-06-28 12:52:09 -05:00
integrations.forEach(integration => {
const response = postCatchError(integration.url, options);
2017-07-09 15:02:17 +09:00
if (response && response.statusCode && response.statusCode === 200) {
return true; // eslint-disable-line consistent-return
} else {
throw new Meteor.Error('error-invalid-webhook-response');
}
});
2017-07-09 15:02:17 +09:00
},
});