activities.js 4.9 KB

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