activities.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. });
  53. Activities.after.insert((userId, doc) => {
  54. const activity = Activities._transform(doc);
  55. let participants = [];
  56. let watchers = [];
  57. let title = 'act-activity-notify';
  58. let board = null;
  59. const description = `act-${activity.activityType}`;
  60. const params = {
  61. activityId: activity._id,
  62. };
  63. if (activity.userId) {
  64. // No need send notification to user of activity
  65. // participants = _.union(participants, [activity.userId]);
  66. params.user = activity.user().getName();
  67. }
  68. if (activity.boardId) {
  69. board = activity.board();
  70. params.board = board.title;
  71. title = 'act-withBoardTitle';
  72. params.url = board.absoluteUrl();
  73. }
  74. if (activity.memberId) {
  75. participants = _.union(participants, [activity.memberId]);
  76. params.member = activity.member().getName();
  77. }
  78. if (activity.listId) {
  79. const list = activity.list();
  80. watchers = _.union(watchers, list.watchers || []);
  81. params.list = list.title;
  82. }
  83. if (activity.oldListId) {
  84. const oldList = activity.oldList();
  85. watchers = _.union(watchers, oldList.watchers || []);
  86. params.oldList = oldList.title;
  87. }
  88. if (activity.cardId) {
  89. const card = activity.card();
  90. participants = _.union(participants, [card.userId], card.members || []);
  91. watchers = _.union(watchers, card.watchers || []);
  92. params.card = card.title;
  93. title = 'act-withCardTitle';
  94. params.url = card.absoluteUrl();
  95. }
  96. if (activity.commentId) {
  97. const comment = activity.comment();
  98. params.comment = comment.text;
  99. }
  100. if (activity.attachmentId) {
  101. const attachment = activity.attachment();
  102. params.attachment = attachment._id;
  103. }
  104. if (activity.checklistId) {
  105. const checklist = activity.checklist();
  106. params.checklist = checklist.title;
  107. }
  108. if (board) {
  109. const watchingUsers = _.pluck(_.where(board.watchers, {level: 'watching'}), 'userId');
  110. const trackingUsers = _.pluck(_.where(board.watchers, {level: 'tracking'}), 'userId');
  111. const mutedUsers = _.pluck(_.where(board.watchers, {level: 'muted'}), 'userId');
  112. switch(board.getWatchDefault()) {
  113. case 'muted':
  114. participants = _.intersection(participants, trackingUsers);
  115. watchers = _.intersection(watchers, trackingUsers);
  116. break;
  117. case 'tracking':
  118. participants = _.difference(participants, mutedUsers);
  119. watchers = _.difference(watchers, mutedUsers);
  120. break;
  121. }
  122. watchers = _.union(watchers, watchingUsers || []);
  123. }
  124. Notifications.getUsers(participants, watchers).forEach((user) => {
  125. Notifications.notify(user, title, description, params);
  126. });
  127. });
  128. }