activities.js 4.6 KB

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