boards.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // This is the publication used to display the board list. We publish all the
  2. // non-archived boards:
  3. // 1. that the user is a member of
  4. // 2. the user has starred
  5. Meteor.publish('boards', function() {
  6. // Ensure that the user is connected. If it is not, we need to return an empty
  7. // array to tell the client to remove the previously published docs.
  8. if (!Match.test(this.userId, String))
  9. return [];
  10. // Defensive programming to verify that starredBoards has the expected
  11. // format -- since the field is in the `profile` a user can modify it.
  12. const {starredBoards = []} = Users.findOne(this.userId).profile;
  13. check(starredBoards, [String]);
  14. return Boards.find({
  15. archived: false,
  16. $or: [
  17. {
  18. _id: { $in: starredBoards },
  19. permission: 'public',
  20. },
  21. { members: { $elemMatch: { userId: this.userId, isActive: true }}},
  22. ],
  23. }, {
  24. fields: {
  25. _id: 1,
  26. archived: 1,
  27. slug: 1,
  28. title: 1,
  29. description: 1,
  30. color: 1,
  31. members: 1,
  32. permission: 1,
  33. },
  34. });
  35. });
  36. Meteor.publish('archivedBoards', function() {
  37. if (!Match.test(this.userId, String))
  38. return [];
  39. return Boards.find({
  40. archived: true,
  41. members: {
  42. $elemMatch: {
  43. userId: this.userId,
  44. isAdmin: true,
  45. },
  46. },
  47. }, {
  48. fields: {
  49. _id: 1,
  50. archived: 1,
  51. slug: 1,
  52. title: 1,
  53. },
  54. });
  55. });
  56. Meteor.publishRelations('board', function(boardId) {
  57. check(boardId, String);
  58. const thisUserId = this.userId;
  59. this.cursor(Boards.find({
  60. _id: boardId,
  61. archived: false,
  62. // If the board is not public the user has to be a member of it to see
  63. // it.
  64. $or: [
  65. { permission: 'public' },
  66. { members: { $elemMatch: { userId: this.userId, isActive: true }}},
  67. ],
  68. }, { limit: 1 }), function(boardId, board) {
  69. this.cursor(Lists.find({ boardId }));
  70. this.cursor(Swimlanes.find({ boardId }));
  71. this.cursor(Integrations.find({ boardId }));
  72. this.cursor(CustomFields.find({ boardId }, { sort: { name: 1 } }));
  73. // Cards and cards comments
  74. // XXX Originally we were publishing the card documents as a child of the
  75. // list publication defined above using the following selector `{ listId:
  76. // list._id }`. But it was causing a race condition in publish-composite,
  77. // that I documented here:
  78. //
  79. // https://github.com/englue/meteor-publish-composite/issues/29
  80. //
  81. // cottz:publish had a similar problem:
  82. //
  83. // https://github.com/Goluis/cottz-publish/issues/4
  84. //
  85. // The current state of relational publishing in meteor is a bit sad,
  86. // there are a lot of various packages, with various APIs, some of them
  87. // are unmaintained. Fortunately this is something that will be fixed by
  88. // meteor-core at some point:
  89. //
  90. // https://trello.com/c/BGvIwkEa/48-easy-joins-in-subscriptions
  91. //
  92. // And in the meantime our code below works pretty well -- it's not even a
  93. // hack!
  94. this.cursor(Cards.find({ boardId }), function(cardId, card) {
  95. if (card.type === 'cardType-linkedCard') {
  96. const impCardId = card.linkedId;
  97. this.cursor(Cards.find({ _id: impCardId }));
  98. this.cursor(CardComments.find({ cardId: impCardId }));
  99. this.cursor(Activities.find({ cardId: impCardId }));
  100. this.cursor(Attachments.find({ cardId: impCardId }));
  101. this.cursor(Checklists.find({ cardId: impCardId }));
  102. this.cursor(ChecklistItems.find({ cardId: impCardId }));
  103. } else if (card.type === 'cardType-linkedBoard') {
  104. this.cursor(Boards.find({ _id: card.linkedId}));
  105. }
  106. this.cursor(Activities.find({ cardId }));
  107. this.cursor(CardComments.find({ cardId }));
  108. this.cursor(Attachments.find({ cardId }));
  109. this.cursor(Checklists.find({ cardId }));
  110. this.cursor(ChecklistItems.find({ cardId }));
  111. this.cursor(Cards.find({ parentId: cardId }));
  112. });
  113. if (board.members) {
  114. // Board members. This publication also includes former board members that
  115. // aren't members anymore but may have some activities attached to them in
  116. // the history.
  117. const memberIds = _.pluck(board.members, 'userId');
  118. // We omit the current user because the client should already have that data,
  119. // and sending it triggers a subtle bug:
  120. // https://github.com/wefork/wekan/issues/15
  121. this.cursor(Users.find({
  122. _id: { $in: _.without(memberIds, thisUserId)},
  123. }, { fields: {
  124. 'username': 1,
  125. 'profile.fullname': 1,
  126. 'profile.avatarUrl': 1,
  127. }}));
  128. this.cursor(presences.find({ userId: { $in: memberIds } }));
  129. }
  130. });
  131. return this.ready();
  132. });