outgoing.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 'customField',
  63. 'customFieldValue',
  64. 'attachmentId',
  65. ];
  66. const responseFunc = data => {
  67. const paramCommentId = data.commentId;
  68. const paramCardId = data.cardId;
  69. const paramBoardId = data.boardId;
  70. const newComment = data.comment;
  71. if (paramCardId && paramBoardId && newComment) {
  72. // only process data with the cardid, boardid and comment text, TODO can expand other functions here to react on returned data
  73. const comment = CardComments.findOne({
  74. _id: paramCommentId,
  75. cardId: paramCardId,
  76. boardId: paramBoardId,
  77. });
  78. const board = Boards.findOne(paramBoardId);
  79. const card = Cards.findOne(paramCardId);
  80. if (board && card) {
  81. if (comment) {
  82. Lock.set(comment._id, newComment);
  83. CardComments.direct.update(comment._id, {
  84. $set: {
  85. text: newComment,
  86. },
  87. });
  88. }
  89. } else {
  90. const userId = data.userId;
  91. if (userId) {
  92. const inserted = CardComments.direct.insert({
  93. text: newComment,
  94. userId,
  95. cardId,
  96. boardId,
  97. });
  98. Lock.set(inserted._id, newComment);
  99. }
  100. }
  101. }
  102. };
  103. Meteor.methods({
  104. outgoingWebhooks(integration, description, params) {
  105. if (Meteor.user()) {
  106. check(integration, Object);
  107. check(description, String);
  108. check(params, Object);
  109. this.unblock();
  110. // label activity did not work yet, see wekan/models/activities.js
  111. const quoteParams = _.clone(params);
  112. const clonedParams = _.clone(params);
  113. [
  114. 'card',
  115. 'list',
  116. 'oldList',
  117. 'board',
  118. 'oldBoard',
  119. 'comment',
  120. 'checklist',
  121. 'swimlane',
  122. 'oldSwimlane',
  123. 'label',
  124. 'attachment',
  125. 'attachmentId',
  126. ].forEach(key => {
  127. if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
  128. });
  129. const userId = params.userId ? params.userId : integrations[0].userId;
  130. const user = Users.findOne(userId);
  131. const text = `${params.user} ${TAPi18n.__(
  132. description,
  133. quoteParams,
  134. user.getLanguage(),
  135. )}\n${params.url}`;
  136. if (text.length === 0) return;
  137. const value = {
  138. text: `${text}`,
  139. };
  140. webhooksAtbts.forEach(key => {
  141. if (params[key]) value[key] = params[key];
  142. });
  143. value.description = description;
  144. //integrations.forEach(integration => {
  145. const is2way = integration.type === Integrations.Const.TWOWAY;
  146. const token = integration.token || '';
  147. const headers = {
  148. 'Content-Type': 'application/json',
  149. };
  150. if (token) headers['X-Wekan-Token'] = token;
  151. const options = {
  152. headers,
  153. data: is2way ? { description, ...clonedParams } : value,
  154. };
  155. if (!Integrations.findOne({ url: integration.url })) return;
  156. const url = integration.url;
  157. if (is2way) {
  158. const cid = params.commentId;
  159. const comment = params.comment;
  160. const lockState = cid && Lock.has(cid, comment);
  161. if (cid && lockState !== Lock.NULL) {
  162. // it's a comment and there is a previous lock
  163. return;
  164. } else if (cid) {
  165. Lock.set(cid, comment); // set a lock here
  166. }
  167. }
  168. const response = postCatchError(url, options);
  169. if (
  170. response &&
  171. response.statusCode &&
  172. response.statusCode >= 200 &&
  173. response.statusCode < 300
  174. ) {
  175. if (is2way) {
  176. const data = response.data; // only an JSON encoded response will be actioned
  177. if (data) {
  178. try {
  179. responseFunc(data);
  180. } catch (e) {
  181. throw new Meteor.Error('error-process-data');
  182. }
  183. }
  184. }
  185. return response; // eslint-disable-line consistent-return
  186. } else {
  187. throw new Meteor.Error('error-invalid-webhook-response');
  188. }
  189. }
  190. },
  191. });
  192. }