activities.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. params.userId = activity.userId;
  73. }
  74. if (activity.boardId) {
  75. board = activity.board();
  76. params.board = board.title;
  77. title = 'act-withBoardTitle';
  78. params.url = board.absoluteUrl();
  79. params.boardId = activity.boardId;
  80. }
  81. if (activity.memberId) {
  82. participants = _.union(participants, [activity.memberId]);
  83. params.member = activity.member().getName();
  84. }
  85. if (activity.listId) {
  86. const list = activity.list();
  87. watchers = _.union(watchers, list.watchers || []);
  88. params.list = list.title;
  89. params.listId = activity.listId;
  90. }
  91. if (activity.oldListId) {
  92. const oldList = activity.oldList();
  93. watchers = _.union(watchers, oldList.watchers || []);
  94. params.oldList = oldList.title;
  95. params.oldListId = activity.oldListId;
  96. }
  97. if (activity.cardId) {
  98. const card = activity.card();
  99. participants = _.union(participants, [card.userId], card.members || []);
  100. watchers = _.union(watchers, card.watchers || []);
  101. params.card = card.title;
  102. title = 'act-withCardTitle';
  103. params.url = card.absoluteUrl();
  104. params.cardId = activity.cardId;
  105. }
  106. if (activity.commentId) {
  107. const comment = activity.comment();
  108. params.comment = comment.text;
  109. }
  110. if (activity.attachmentId) {
  111. const attachment = activity.attachment();
  112. params.attachment = attachment._id;
  113. }
  114. if (activity.checklistId) {
  115. const checklist = activity.checklist();
  116. params.checklist = checklist.title;
  117. }
  118. if (board) {
  119. const watchingUsers = _.pluck(_.where(board.watchers, {level: 'watching'}), 'userId');
  120. const trackingUsers = _.pluck(_.where(board.watchers, {level: 'tracking'}), 'userId');
  121. const mutedUsers = _.pluck(_.where(board.watchers, {level: 'muted'}), 'userId');
  122. switch(board.getWatchDefault()) {
  123. case 'muted':
  124. participants = _.intersection(participants, trackingUsers);
  125. watchers = _.intersection(watchers, trackingUsers);
  126. break;
  127. case 'tracking':
  128. participants = _.difference(participants, mutedUsers);
  129. watchers = _.difference(watchers, mutedUsers);
  130. break;
  131. }
  132. watchers = _.union(watchers, watchingUsers || []);
  133. }
  134. Notifications.getUsers(participants, watchers).forEach((user) => {
  135. Notifications.notify(user, title, description, params);
  136. });
  137. const integration = Integrations.findOne({ boardId: board._id, type: 'outgoing-webhooks', enabled: true });
  138. if (integration) {
  139. Meteor.call('outgoingWebhooks', integration, description, params);
  140. }
  141. });
  142. }