outgoing.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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', 'boardId'].forEach((key) => {
  27. if (params[key]) value[key] = params[key];
  28. });
  29. value.description = description;
  30. const options = {
  31. headers: {
  32. // 'Content-Type': 'application/json',
  33. // 'X-Wekan-Activities-Token': 'Random.Id()',
  34. },
  35. data: value,
  36. };
  37. integrations.forEach((integration) => {
  38. const response = postCatchError(integration.url, options);
  39. if (response && response.statusCode && response.statusCode === 200) {
  40. return true; // eslint-disable-line consistent-return
  41. } else {
  42. throw new Meteor.Error('error-invalid-webhook-response');
  43. }
  44. });
  45. },
  46. });