outgoing.js 1.5 KB

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