outgoing.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 = CardComments.findOne({
  76. _id: paramCommentId,
  77. cardId: paramCardId,
  78. boardId: paramBoardId,
  79. });
  80. const board = ReactiveCache.getBoard(paramBoardId);
  81. const card = Cards.findOne(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 (Meteor.user()) {
  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 = Users.findOne(userId);
  133. const text = `${params.user} ${TAPi18n.__(
  134. description,
  135. quoteParams,
  136. user.getLanguage(),
  137. )}\n${params.url}`;
  138. if (text.length === 0) return;
  139. const value = {
  140. text: `${text}`,
  141. };
  142. webhooksAtbts.forEach(key => {
  143. if (params[key]) value[key] = params[key];
  144. });
  145. value.description = description;
  146. //integrations.forEach(integration => {
  147. const is2way = integration.type === Integrations.Const.TWOWAY;
  148. const token = integration.token || '';
  149. const headers = {
  150. 'Content-Type': 'application/json',
  151. };
  152. if (token) headers['X-Wekan-Token'] = token;
  153. const options = {
  154. headers,
  155. data: is2way ? { description, ...clonedParams } : value,
  156. };
  157. if (!Integrations.findOne({ url: integration.url })) return;
  158. const url = integration.url;
  159. if (is2way) {
  160. const cid = params.commentId;
  161. const comment = params.comment;
  162. const lockState = cid && Lock.has(cid, comment);
  163. if (cid && lockState !== Lock.NULL) {
  164. // it's a comment and there is a previous lock
  165. return;
  166. } else if (cid) {
  167. Lock.set(cid, comment); // set a lock here
  168. }
  169. }
  170. const response = postCatchError(url, options);
  171. if (
  172. response &&
  173. response.statusCode &&
  174. response.statusCode >= 200 &&
  175. response.statusCode < 300
  176. ) {
  177. if (is2way) {
  178. const data = response.data; // only an JSON encoded response will be actioned
  179. if (data) {
  180. try {
  181. responseFunc(data);
  182. } catch (e) {
  183. throw new Meteor.Error('error-process-data');
  184. }
  185. }
  186. }
  187. return response; // eslint-disable-line consistent-return
  188. } else {
  189. throw new Meteor.Error('error-invalid-webhook-response');
  190. }
  191. }
  192. },
  193. });
  194. }