activities.js 5.8 KB

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