outgoing.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 &&
  11. process.env.WEBHOOKS_ATTRIBUTES.split(',')) || [
  12. 'cardId',
  13. 'listId',
  14. 'oldListId',
  15. 'boardId',
  16. 'comment',
  17. 'user',
  18. 'card',
  19. 'commentId',
  20. 'swimlaneId',
  21. ];
  22. Meteor.methods({
  23. outgoingWebhooks(integrations, description, params) {
  24. check(integrations, Array);
  25. check(description, String);
  26. check(params, Object);
  27. // label activity did not work yet, see wekan/models/activities.js
  28. const quoteParams = _.clone(params);
  29. [
  30. 'card',
  31. 'list',
  32. 'oldList',
  33. 'board',
  34. 'oldBoard',
  35. 'comment',
  36. 'checklist',
  37. 'swimlane',
  38. 'oldSwimlane',
  39. 'label',
  40. 'attachment',
  41. ].forEach(key => {
  42. if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
  43. });
  44. const userId = params.userId ? params.userId : integrations[0].userId;
  45. const user = Users.findOne(userId);
  46. const text = `${params.user} ${TAPi18n.__(
  47. description,
  48. quoteParams,
  49. user.getLanguage(),
  50. )}\n${params.url}`;
  51. if (text.length === 0) return;
  52. const value = {
  53. text: `${text}`,
  54. };
  55. webhooksAtbts.forEach(key => {
  56. if (params[key]) value[key] = params[key];
  57. });
  58. value.description = description;
  59. const options = {
  60. headers: {
  61. // 'Content-Type': 'application/json',
  62. // 'X-Wekan-Activities-Token': 'Random.Id()',
  63. },
  64. data: value,
  65. };
  66. integrations.forEach(integration => {
  67. const response = postCatchError(integration.url, options);
  68. if (response && response.statusCode && response.statusCode === 200) {
  69. return true; // eslint-disable-line consistent-return
  70. } else {
  71. throw new Meteor.Error('error-invalid-webhook-response');
  72. }
  73. });
  74. },
  75. });