outgoing.js 5.9 KB

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