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