activities.js 4.2 KB

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