activities.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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() {
  59. return Labels.findOne(this.labelId);
  60. },
  61. });
  62. Activities.before.insert((userId, doc) => {
  63. doc.createdAt = new Date();
  64. });
  65. Activities.after.insert((userId, doc) => {
  66. const activity = Activities._transform(doc);
  67. RulesHelper.executeRules(activity);
  68. });
  69. if (Meteor.isServer) {
  70. // For efficiency create indexes on the date of creation, and on the date of
  71. // creation in conjunction with the card or board id, as corresponding views
  72. // are largely used in the App. See #524.
  73. Meteor.startup(() => {
  74. Activities._collection._ensureIndex({ createdAt: -1 });
  75. Activities._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  76. Activities._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  77. Activities._collection._ensureIndex({ commentId: 1 }, { partialFilterExpression: { commentId: { $exists: true } } });
  78. Activities._collection._ensureIndex({ attachmentId: 1 }, { partialFilterExpression: { attachmentId: { $exists: true } } });
  79. Activities._collection._ensureIndex({ customFieldId: 1 }, { partialFilterExpression: { customFieldId: { $exists: true } } });
  80. Activities._collection._ensureIndex({ labelId: 1 }, { partialFilterExpression: { labelId: { $exists: true } } });
  81. });
  82. Activities.after.insert((userId, doc) => {
  83. const activity = Activities._transform(doc);
  84. let participants = [];
  85. let watchers = [];
  86. let title = 'act-activity-notify';
  87. let board = null;
  88. const description = `act-${activity.activityType}`;
  89. const params = {
  90. activityId: activity._id,
  91. };
  92. if (activity.userId) {
  93. // No need send notification to user of activity
  94. // participants = _.union(participants, [activity.userId]);
  95. params.user = activity.user().getName();
  96. params.userId = activity.userId;
  97. }
  98. if (activity.boardId) {
  99. board = activity.board();
  100. params.board = board.title;
  101. title = 'act-withBoardTitle';
  102. params.url = board.absoluteUrl();
  103. params.boardId = activity.boardId;
  104. }
  105. if (activity.oldBoardId) {
  106. const oldBoard = activity.oldBoard();
  107. watchers = _.union(watchers, oldBoard.watchers || []);
  108. params.oldBoard = oldBoard.title;
  109. params.oldBoardId = activity.oldBoardId;
  110. }
  111. if (activity.memberId) {
  112. participants = _.union(participants, [activity.memberId]);
  113. params.member = activity.member().getName();
  114. }
  115. if (activity.listId) {
  116. const list = activity.list();
  117. watchers = _.union(watchers, list.watchers || []);
  118. params.list = list.title;
  119. params.listId = activity.listId;
  120. }
  121. if (activity.oldListId) {
  122. const oldList = activity.oldList();
  123. watchers = _.union(watchers, oldList.watchers || []);
  124. params.oldList = oldList.title;
  125. params.oldListId = activity.oldListId;
  126. }
  127. if (activity.oldSwimlaneId) {
  128. const oldSwimlane = activity.oldSwimlane();
  129. watchers = _.union(watchers, oldSwimlane.watchers || []);
  130. params.oldSwimlane = oldSwimlane.title;
  131. params.oldSwimlaneId = activity.oldSwimlaneId;
  132. }
  133. if (activity.cardId) {
  134. const card = activity.card();
  135. participants = _.union(participants, [card.userId], card.members || []);
  136. watchers = _.union(watchers, card.watchers || []);
  137. params.card = card.title;
  138. title = 'act-withCardTitle';
  139. params.url = card.absoluteUrl();
  140. params.cardId = activity.cardId;
  141. }
  142. if (activity.swimlaneId) {
  143. const swimlane = activity.swimlane();
  144. params.swimlane = swimlane.title;
  145. params.swimlaneId = activity.swimlaneId;
  146. }
  147. if (activity.commentId) {
  148. const comment = activity.comment();
  149. params.comment = comment.text;
  150. params.commentId = comment._id;
  151. }
  152. if (activity.attachmentId) {
  153. const attachment = activity.attachment();
  154. params.attachment = attachment._id;
  155. }
  156. if (activity.checklistId) {
  157. const checklist = activity.checklist();
  158. params.checklist = checklist.title;
  159. }
  160. if (activity.checklistItemId) {
  161. const checklistItem = activity.checklistItem();
  162. params.checklistItem = checklistItem.title;
  163. }
  164. if (activity.customFieldId) {
  165. const customField = activity.customField();
  166. params.customField = customField.name;
  167. }
  168. if (activity.labelId) {
  169. const label = activity.label();
  170. params.label = label.name;
  171. params.labelId = activity.labelId;
  172. }
  173. if (board) {
  174. const watchingUsers = _.pluck(_.where(board.watchers, {level: 'watching'}), 'userId');
  175. const trackingUsers = _.pluck(_.where(board.watchers, {level: 'tracking'}), 'userId');
  176. watchers = _.union(watchers, watchingUsers, _.intersection(participants, trackingUsers));
  177. }
  178. Notifications.getUsers(watchers).forEach((user) => {
  179. Notifications.notify(user, title, description, params);
  180. });
  181. const integrations = Integrations.find({ boardId: board._id, type: 'outgoing-webhooks', enabled: true, activities: { '$in': [description, 'all'] } }).fetch();
  182. if (integrations.length > 0) {
  183. Meteor.call('outgoingWebhooks', integrations, description, params);
  184. }
  185. });
  186. }