outgoing.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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']);
  11. Meteor.methods({
  12. outgoingWebhooks(integrations, description, params) {
  13. check(integrations, Array);
  14. check(description, String);
  15. check(params, Object);
  16. const quoteParams = _.clone(params);
  17. ['card', 'list', 'oldList', 'board', 'comment'].forEach((key) => {
  18. if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
  19. });
  20. const userId = (params.userId) ? params.userId : integrations[0].userId;
  21. const user = Users.findOne(userId);
  22. const text = `${params.user} ${TAPi18n.__(description, quoteParams, user.getLanguage())}\n${params.url}`;
  23. if (text.length === 0) return;
  24. const value = {
  25. text: `${text}`,
  26. };
  27. webhooksAtbts.forEach((key) => {
  28. if (params[key]) value[key] = params[key];
  29. });
  30. value.description = description;
  31. const options = {
  32. headers: {
  33. // 'Content-Type': 'application/json',
  34. // 'X-Wekan-Activities-Token': 'Random.Id()',
  35. },
  36. data: value,
  37. };
  38. integrations.forEach((integration) => {
  39. const response = postCatchError(integration.url, options);
  40. if (response && response.statusCode && response.statusCode === 200) {
  41. return true; // eslint-disable-line consistent-return
  42. } else {
  43. throw new Meteor.Error('error-invalid-webhook-response');
  44. }
  45. });
  46. },
  47. });