2
0

activities.js 5.4 KB

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