activities.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. user() {
  17. return Users.findOne(this.userId);
  18. },
  19. member() {
  20. return Users.findOne(this.memberId);
  21. },
  22. list() {
  23. return Lists.findOne(this.listId);
  24. },
  25. swimlane() {
  26. return Swimlanes.findOne(this.swimlaneId);
  27. },
  28. oldList() {
  29. return Lists.findOne(this.oldListId);
  30. },
  31. card() {
  32. return Cards.findOne(this.cardId);
  33. },
  34. comment() {
  35. return CardComments.findOne(this.commentId);
  36. },
  37. attachment() {
  38. return Attachments.findOne(this.attachmentId);
  39. },
  40. checklist() {
  41. return Checklists.findOne(this.checklistId);
  42. },
  43. checklistItem() {
  44. return ChecklistItems.findOne(this.checklistItemId);
  45. },
  46. subtasks() {
  47. return Cards.findOne(this.subtaskId);
  48. },
  49. customField() {
  50. return CustomFields.findOne(this.customFieldId);
  51. },
  52. });
  53. Activities.before.insert((userId, doc) => {
  54. doc.createdAt = new Date();
  55. });
  56. Activities.after.insert((userId, doc) => {
  57. const activity = Activities._transform(doc);
  58. RulesHelper.executeRules(activity);
  59. });
  60. if (Meteor.isServer) {
  61. // For efficiency create indexes on the date of creation, and on the date of
  62. // creation in conjunction with the card or board id, as corresponding views
  63. // are largely used in the App. See #524.
  64. Meteor.startup(() => {
  65. Activities._collection._ensureIndex({ createdAt: -1 });
  66. Activities._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  67. Activities._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  68. Activities._collection._ensureIndex({ commentId: 1 }, { partialFilterExpression: { commentId: { $exists: true } } });
  69. Activities._collection._ensureIndex({ attachmentId: 1 }, { partialFilterExpression: { attachmentId: { $exists: true } } });
  70. Activities._collection._ensureIndex({ customFieldId: 1 }, { partialFilterExpression: { customFieldId: { $exists: true } } });
  71. });
  72. Activities.after.insert((userId, doc) => {
  73. const activity = Activities._transform(doc);
  74. let participants = [];
  75. let watchers = [];
  76. let title = 'act-activity-notify';
  77. let board = null;
  78. const description = `act-${activity.activityType}`;
  79. const params = {
  80. activityId: activity._id,
  81. };
  82. if (activity.userId) {
  83. // No need send notification to user of activity
  84. // participants = _.union(participants, [activity.userId]);
  85. params.user = activity.user().getName();
  86. params.userId = activity.userId;
  87. }
  88. if (activity.boardId) {
  89. board = activity.board();
  90. params.board = board.title;
  91. title = 'act-withBoardTitle';
  92. params.url = board.absoluteUrl();
  93. params.boardId = activity.boardId;
  94. }
  95. if (activity.memberId) {
  96. participants = _.union(participants, [activity.memberId]);
  97. params.member = activity.member().getName();
  98. }
  99. if (activity.listId) {
  100. const list = activity.list();
  101. watchers = _.union(watchers, list.watchers || []);
  102. params.list = list.title;
  103. params.listId = activity.listId;
  104. }
  105. if (activity.oldListId) {
  106. const oldList = activity.oldList();
  107. watchers = _.union(watchers, oldList.watchers || []);
  108. params.oldList = oldList.title;
  109. params.oldListId = activity.oldListId;
  110. }
  111. if (activity.cardId) {
  112. const card = activity.card();
  113. participants = _.union(participants, [card.userId], card.members || []);
  114. watchers = _.union(watchers, card.watchers || []);
  115. params.card = card.title;
  116. title = 'act-withCardTitle';
  117. params.url = card.absoluteUrl();
  118. params.cardId = activity.cardId;
  119. }
  120. if (activity.swimlaneId) {
  121. params.swimlaneId = activity.swimlaneId;
  122. }
  123. if (activity.commentId) {
  124. const comment = activity.comment();
  125. params.comment = comment.text;
  126. params.commentId = comment._id;
  127. }
  128. if (activity.attachmentId) {
  129. const attachment = activity.attachment();
  130. params.attachment = attachment._id;
  131. }
  132. if (activity.checklistId) {
  133. const checklist = activity.checklist();
  134. params.checklist = checklist.title;
  135. }
  136. if (activity.checklistItemId) {
  137. const checklistItem = activity.checklistItem();
  138. params.checklistItem = checklistItem.title;
  139. }
  140. if (activity.customFieldId) {
  141. const customField = activity.customField();
  142. params.customField = customField.name;
  143. }
  144. if (board) {
  145. const watchingUsers = _.pluck(_.where(board.watchers, {level: 'watching'}), 'userId');
  146. const trackingUsers = _.pluck(_.where(board.watchers, {level: 'tracking'}), 'userId');
  147. const mutedUsers = _.pluck(_.where(board.watchers, {level: 'muted'}), 'userId');
  148. switch(board.getWatchDefault()) {
  149. case 'muted':
  150. participants = _.intersection(participants, trackingUsers);
  151. watchers = _.intersection(watchers, trackingUsers);
  152. break;
  153. case 'tracking':
  154. participants = _.difference(participants, mutedUsers);
  155. watchers = _.difference(watchers, mutedUsers);
  156. break;
  157. }
  158. watchers = _.union(watchers, watchingUsers || []);
  159. }
  160. Notifications.getUsers(participants, watchers).forEach((user) => {
  161. Notifications.notify(user, title, description, params);
  162. });
  163. const integrations = Integrations.find({ boardId: board._id, type: 'outgoing-webhooks', enabled: true, activities: { '$in': [description, 'all'] } }).fetch();
  164. if (integrations.length > 0) {
  165. Meteor.call('outgoingWebhooks', integrations, description, params);
  166. }
  167. });
  168. }