outgoing.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. if (Meteor.isServer) {
  2. const postCatchError = Meteor.wrapAsync((url, options, resolve) => {
  3. HTTP.post(url, options, (err, res) => {
  4. if (err) {
  5. resolve(null, err.response);
  6. } else {
  7. resolve(null, res);
  8. }
  9. });
  10. });
  11. const Lock = {
  12. _lock: {},
  13. _timer: {},
  14. echoDelay: 500, // echo should be happening much faster
  15. normalDelay: 1e3, // normally user typed comment will be much slower
  16. ECHO: 2,
  17. NORMAL: 1,
  18. NULL: 0,
  19. has(id, value) {
  20. const existing = this._lock[id];
  21. let ret = this.NULL;
  22. if (existing) {
  23. ret = existing === value ? this.ECHO : this.NORMAL;
  24. }
  25. return ret;
  26. },
  27. clear(id, delay) {
  28. const previous = this._timer[id];
  29. if (previous) {
  30. Meteor.clearTimeout(previous);
  31. }
  32. this._timer[id] = Meteor.setTimeout(() => this.unset(id), delay);
  33. },
  34. set(id, value) {
  35. const state = this.has(id, value);
  36. let delay = this.normalDelay;
  37. if (state === this.ECHO) {
  38. delay = this.echoDelay;
  39. }
  40. if (!value) {
  41. // user commented, we set a lock
  42. value = 1;
  43. }
  44. this._lock[id] = value;
  45. this.clear(id, delay); // always auto reset the locker after delay
  46. },
  47. unset(id) {
  48. delete this._lock[id];
  49. },
  50. };
  51. const webhooksAtbts = (process.env.WEBHOOKS_ATTRIBUTES &&
  52. process.env.WEBHOOKS_ATTRIBUTES.split(',')) || [
  53. 'cardId',
  54. 'listId',
  55. 'oldListId',
  56. 'boardId',
  57. 'comment',
  58. 'user',
  59. 'card',
  60. 'commentId',
  61. 'swimlaneId',
  62. ];
  63. const responseFunc = data => {
  64. const paramCommentId = data.commentId;
  65. const paramCardId = data.cardId;
  66. const paramBoardId = data.boardId;
  67. const newComment = data.comment;
  68. if (paramCardId && paramBoardId && newComment) {
  69. // only process data with the cardid, boardid and comment text, TODO can expand other functions here to react on returned data
  70. const comment = CardComments.findOne({
  71. _id: paramCommentId,
  72. cardId: paramCardId,
  73. boardId: paramBoardId,
  74. });
  75. const board = Boards.findOne(paramBoardId);
  76. const card = Cards.findOne(paramCardId);
  77. if (board && card) {
  78. if (comment) {
  79. Lock.set(comment._id, newComment);
  80. CardComments.direct.update(comment._id, {
  81. $set: {
  82. text: newComment,
  83. },
  84. });
  85. }
  86. } else {
  87. const userId = data.userId;
  88. if (userId) {
  89. const inserted = CardComments.direct.insert({
  90. text: newComment,
  91. userId,
  92. cardId,
  93. boardId,
  94. });
  95. Lock.set(inserted._id, newComment);
  96. }
  97. }
  98. }
  99. };
  100. Meteor.methods({
  101. outgoingWebhooks(integration, description, params) {
  102. if (Meteor.user()) {
  103. check(integration, Object);
  104. check(description, String);
  105. check(params, Object);
  106. this.unblock();
  107. // label activity did not work yet, see wekan/models/activities.js
  108. const quoteParams = _.clone(params);
  109. const clonedParams = _.clone(params);
  110. [
  111. 'card',
  112. 'list',
  113. 'oldList',
  114. 'board',
  115. 'oldBoard',
  116. 'comment',
  117. 'checklist',
  118. 'swimlane',
  119. 'oldSwimlane',
  120. 'label',
  121. 'attachment',
  122. ].forEach(key => {
  123. if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
  124. });
  125. const userId = params.userId ? params.userId : integrations[0].userId;
  126. const user = Users.findOne(userId);
  127. const text = `${params.user} ${TAPi18n.__(
  128. description,
  129. quoteParams,
  130. user.getLanguage(),
  131. )}\n${params.url}`;
  132. if (text.length === 0) return;
  133. const value = {
  134. text: `${text}`,
  135. };
  136. webhooksAtbts.forEach(key => {
  137. if (params[key]) value[key] = params[key];
  138. });
  139. value.description = description;
  140. //integrations.forEach(integration => {
  141. const is2way = integration.type === Integrations.Const.TWOWAY;
  142. const token = integration.token || '';
  143. const headers = {
  144. 'Content-Type': 'application/json',
  145. };
  146. if (token) headers['X-Wekan-Token'] = token;
  147. const options = {
  148. headers,
  149. data: is2way ? { description, ...clonedParams } : value,
  150. };
  151. if (!Integrations.findOne({ url: integration.url })) return;
  152. const url = integration.url;
  153. if (is2way) {
  154. const cid = params.commentId;
  155. const comment = params.comment;
  156. const lockState = cid && Lock.has(cid, comment);
  157. if (cid && lockState !== Lock.NULL) {
  158. // it's a comment and there is a previous lock
  159. return;
  160. } else if (cid) {
  161. Lock.set(cid, comment); // set a lock here
  162. }
  163. }
  164. const response = postCatchError(url, options);
  165. if (
  166. response &&
  167. response.statusCode &&
  168. response.statusCode >= 200 &&
  169. response.statusCode < 300
  170. ) {
  171. if (is2way) {
  172. const data = response.data; // only an JSON encoded response will be actioned
  173. if (data) {
  174. try {
  175. responseFunc(data);
  176. } catch (e) {
  177. throw new Meteor.Error('error-process-data');
  178. }
  179. }
  180. }
  181. return response; // eslint-disable-line consistent-return
  182. } else {
  183. throw new Meteor.Error('error-invalid-webhook-response');
  184. }
  185. }
  186. },
  187. });
  188. }