outgoing.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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(integration, description, params) {
  12. check(integration, Object);
  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 user = Users.findOne(integration.userId);
  20. const text = `${params.user} ${TAPi18n.__(description, quoteParams, user.getLanguage())}\n${params.url}`;
  21. if (text.length === 0) return;
  22. const value = {
  23. text: `${text}`,
  24. };
  25. const options = {
  26. headers: {
  27. // 'Content-Type': 'application/json',
  28. // 'X-Wekan-Activities-Token': 'Random.Id()',
  29. },
  30. data: value,
  31. };
  32. const response = postCatchError(integration.url, options);
  33. if (response && response.statusCode && response.statusCode === 200) {
  34. return true; // eslint-disable-line consistent-return
  35. } else {
  36. throw new Meteor.Error('error-invalid-webhook-response');
  37. }
  38. },
  39. });