activities.js 3.9 KB

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