activities.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Activities don't need a schema because they are always set from the a trusted
  2. // environment - the server - and there is no risk that a user change the logic
  3. // we use with this collection. Moreover using a schema for this collection
  4. // would be difficult (different activities have different fields) and wouldn't
  5. // bring any direct advantage.
  6. //
  7. // XXX The activities API is not so nice and need some functionalities. For
  8. // instance if a user archive a card, and un-archive it a few seconds later we
  9. // should remove both activities assuming it was an error the user decided to
  10. // revert.
  11. Activities = new Mongo.Collection('activities');
  12. Activities.helpers({
  13. board() {
  14. return Boards.findOne(this.boardId);
  15. },
  16. oldBoard() {
  17. return Boards.findOne(this.oldBoardId);
  18. },
  19. user() {
  20. return Users.findOne(this.userId);
  21. },
  22. member() {
  23. return Users.findOne(this.memberId);
  24. },
  25. list() {
  26. return Lists.findOne(this.listId);
  27. },
  28. swimlane() {
  29. return Swimlanes.findOne(this.swimlaneId);
  30. },
  31. oldSwimlane() {
  32. return Swimlanes.findOne(this.oldSwimlaneId);
  33. },
  34. oldList() {
  35. return Lists.findOne(this.oldListId);
  36. },
  37. card() {
  38. return Cards.findOne(this.cardId);
  39. },
  40. comment() {
  41. return CardComments.findOne(this.commentId);
  42. },
  43. attachment() {
  44. return Attachments.findOne(this.attachmentId);
  45. },
  46. checklist() {
  47. return Checklists.findOne(this.checklistId);
  48. },
  49. checklistItem() {
  50. return ChecklistItems.findOne(this.checklistItemId);
  51. },
  52. subtasks() {
  53. return Cards.findOne(this.subtaskId);
  54. },
  55. customField() {
  56. return CustomFields.findOne(this.customFieldId);
  57. },
  58. // Label activity did not work yet, unable to edit labels when tried this.
  59. //label() {
  60. // return Cards.findOne(this.labelId);
  61. //},
  62. });
  63. Activities.before.insert((userId, doc) => {
  64. doc.createdAt = new Date();
  65. });
  66. Activities.after.insert((userId, doc) => {
  67. const activity = Activities._transform(doc);
  68. RulesHelper.executeRules(activity);
  69. });
  70. Activities.before.update((userId, doc, fieldNames, modifier, options) => {
  71. modifier.$set = modifier.$set || {};
  72. modifier.$set.modifiedAt = Date.now();
  73. });
  74. if (Meteor.isServer) {
  75. // For efficiency create indexes on the date of creation, and on the date of
  76. // creation in conjunction with the card or board id, as corresponding views
  77. // are largely used in the App. See #524.
  78. Meteor.startup(() => {
  79. Activities._collection._ensureIndex({ createdAt: -1 });
  80. Activities._collection._ensureIndex({ modifiedAt: -1 });
  81. Activities._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  82. Activities._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  83. Activities._collection._ensureIndex(
  84. { commentId: 1 },
  85. { partialFilterExpression: { commentId: { $exists: true } } }
  86. );
  87. Activities._collection._ensureIndex(
  88. { attachmentId: 1 },
  89. { partialFilterExpression: { attachmentId: { $exists: true } } }
  90. );
  91. Activities._collection._ensureIndex(
  92. { customFieldId: 1 },
  93. { partialFilterExpression: { customFieldId: { $exists: true } } }
  94. );
  95. // Label activity did not work yet, unable to edit labels when tried this.
  96. //Activities._collection._dropIndex({ labelId: 1 }, { "indexKey": -1 });
  97. //Activities._collection._dropIndex({ labelId: 1 }, { partialFilterExpression: { labelId: { $exists: true } } });
  98. });
  99. Activities.after.insert((userId, doc) => {
  100. const activity = Activities._transform(doc);
  101. let participants = [];
  102. let watchers = [];
  103. let title = 'act-activity-notify';
  104. let board = null;
  105. const description = `act-${activity.activityType}`;
  106. const params = {
  107. activityId: activity._id,
  108. };
  109. if (activity.userId) {
  110. // No need send notification to user of activity
  111. // participants = _.union(participants, [activity.userId]);
  112. params.user = activity.user().getName();
  113. params.userId = activity.userId;
  114. }
  115. if (activity.boardId) {
  116. board = activity.board();
  117. params.board = board.title;
  118. title = 'act-withBoardTitle';
  119. params.url = board.absoluteUrl();
  120. params.boardId = activity.boardId;
  121. }
  122. if (activity.oldBoardId) {
  123. const oldBoard = activity.oldBoard();
  124. if (oldBoard) {
  125. watchers = _.union(watchers, oldBoard.watchers || []);
  126. params.oldBoard = oldBoard.title;
  127. params.oldBoardId = activity.oldBoardId;
  128. }
  129. }
  130. if (activity.memberId) {
  131. participants = _.union(participants, [activity.memberId]);
  132. params.member = activity.member().getName();
  133. }
  134. if (activity.listId) {
  135. const list = activity.list();
  136. watchers = _.union(watchers, list.watchers || []);
  137. params.list = list.title;
  138. params.listId = activity.listId;
  139. }
  140. if (activity.oldListId) {
  141. const oldList = activity.oldList();
  142. if (oldList) {
  143. watchers = _.union(watchers, oldList.watchers || []);
  144. params.oldList = oldList.title;
  145. params.oldListId = activity.oldListId;
  146. }
  147. }
  148. if (activity.oldSwimlaneId) {
  149. const oldSwimlane = activity.oldSwimlane();
  150. if (oldSwimlane) {
  151. watchers = _.union(watchers, oldSwimlane.watchers || []);
  152. params.oldSwimlane = oldSwimlane.title;
  153. params.oldSwimlaneId = activity.oldSwimlaneId;
  154. }
  155. }
  156. if (activity.cardId) {
  157. const card = activity.card();
  158. participants = _.union(participants, [card.userId], card.members || []);
  159. watchers = _.union(watchers, card.watchers || []);
  160. params.card = card.title;
  161. title = 'act-withCardTitle';
  162. params.url = card.absoluteUrl();
  163. params.cardId = activity.cardId;
  164. }
  165. if (activity.swimlaneId) {
  166. const swimlane = activity.swimlane();
  167. params.swimlane = swimlane.title;
  168. params.swimlaneId = activity.swimlaneId;
  169. }
  170. if (activity.commentId) {
  171. const comment = activity.comment();
  172. params.comment = comment.text;
  173. params.commentId = comment._id;
  174. }
  175. if (activity.attachmentId) {
  176. const attachment = activity.attachment();
  177. params.attachment = attachment.original.name;
  178. params.attachmentId = attachment._id;
  179. }
  180. if (activity.checklistId) {
  181. const checklist = activity.checklist();
  182. params.checklist = checklist.title;
  183. }
  184. if (activity.checklistItemId) {
  185. const checklistItem = activity.checklistItem();
  186. params.checklistItem = checklistItem.title;
  187. }
  188. if (activity.customFieldId) {
  189. const customField = activity.customField();
  190. params.customField = customField.name;
  191. params.customFieldValue = customField.text;
  192. }
  193. // Label activity did not work yet, unable to edit labels when tried this.
  194. //if (activity.labelId) {
  195. // const label = activity.label();
  196. // params.label = label.name;
  197. // params.labelId = activity.labelId;
  198. //}
  199. if (board) {
  200. const watchingUsers = _.pluck(
  201. _.where(board.watchers, { level: 'watching' }),
  202. 'userId'
  203. );
  204. const trackingUsers = _.pluck(
  205. _.where(board.watchers, { level: 'tracking' }),
  206. 'userId'
  207. );
  208. watchers = _.union(
  209. watchers,
  210. watchingUsers,
  211. _.intersection(participants, trackingUsers)
  212. );
  213. }
  214. Notifications.getUsers(watchers).forEach((user) => {
  215. Notifications.notify(user, title, description, params);
  216. });
  217. const integrations = Integrations.find({
  218. boardId: board._id,
  219. type: 'outgoing-webhooks',
  220. enabled: true,
  221. activities: { $in: [description, 'all'] },
  222. }).fetch();
  223. if (integrations.length > 0) {
  224. Meteor.call('outgoingWebhooks', integrations, description, params);
  225. }
  226. });
  227. }
  228. export default Activities;