outgoing.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const postCatchError = Meteor.wrapAsync((url, options, resolve) => {
  2. HTTP.post(url, options, (err, res) => {
  3. if (err) {
  4. resolve(null, err.response);
  5. } else {
  6. resolve(null, res);
  7. }
  8. });
  9. });
  10. const webhooksAtbts = ( (process.env.WEBHOOKS_ATTRIBUTES && process.env.WEBHOOKS_ATTRIBUTES.split(',') ) || ['cardId', 'listId', 'oldListId', 'boardId', 'comment', 'user', 'card', 'commentId', 'swimlaneId']);
  11. Meteor.methods({
  12. outgoingWebhooks(integrations, description, params) {
  13. check(integrations, Array);
  14. check(description, String);
  15. check(params, Object);
  16. // label activity did not work yet, see wekan/models/activities.js
  17. const quoteParams = _.clone(params);
  18. ['card', 'list', 'oldList', 'board', 'oldBoard', 'comment', 'checklist', 'swimlane', 'oldSwimlane'].forEach((key) => {
  19. if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
  20. });
  21. const userId = (params.userId) ? params.userId : integrations[0].userId;
  22. const user = Users.findOne(userId);
  23. const text = `${params.user} ${TAPi18n.__(description, quoteParams, user.getLanguage())}\n${params.url}`;
  24. if (text.length === 0) return;
  25. const value = {
  26. text: `${text}`,
  27. };
  28. webhooksAtbts.forEach((key) => {
  29. if (params[key]) value[key] = params[key];
  30. });
  31. value.description = description;
  32. const options = {
  33. headers: {
  34. // 'Content-Type': 'application/json',
  35. // 'X-Wekan-Activities-Token': 'Random.Id()',
  36. },
  37. data: value,
  38. };
  39. integrations.forEach((integration) => {
  40. const response = postCatchError(integration.url, options);
  41. if (response && response.statusCode && response.statusCode === 200) {
  42. return true; // eslint-disable-line consistent-return
  43. } else {
  44. throw new Meteor.Error('error-invalid-webhook-response');
  45. }
  46. });
  47. },
  48. });