boards.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. type: 1,
  34. },
  35. });
  36. });
  37. Meteor.publish('archivedBoards', function() {
  38. if (!Match.test(this.userId, String))
  39. return [];
  40. return Boards.find({
  41. archived: true,
  42. members: {
  43. $elemMatch: {
  44. userId: this.userId,
  45. isAdmin: true,
  46. },
  47. },
  48. }, {
  49. fields: {
  50. _id: 1,
  51. archived: 1,
  52. slug: 1,
  53. title: 1,
  54. },
  55. });
  56. });
  57. // If isArchived = false, this will only return board elements which are not archived.
  58. // If isArchived = true, this will only return board elements which are archived.
  59. Meteor.publishRelations('board', function(boardId, isArchived) {
  60. this.unblock();
  61. check(boardId, String);
  62. check(isArchived, Boolean);
  63. const thisUserId = this.userId;
  64. this.cursor(Boards.find({
  65. _id: boardId,
  66. archived: false,
  67. // If the board is not public the user has to be a member of it to see
  68. // it.
  69. $or: [
  70. { permission: 'public' },
  71. { members: { $elemMatch: { userId: this.userId, isActive: true }}},
  72. ],
  73. // Sort required to ensure oplog usage
  74. }, { limit: 1, sort: { _id: 1 } }), function(boardId, board) {
  75. this.cursor(Lists.find({ boardId, archived: isArchived }));
  76. this.cursor(Swimlanes.find({ boardId, archived: isArchived }));
  77. this.cursor(Integrations.find({ boardId }));
  78. this.cursor(CustomFields.find({ boardIds: {$in: [boardId]} }, { sort: { name: 1 } }));
  79. // Cards and cards comments
  80. // XXX Originally we were publishing the card documents as a child of the
  81. // list publication defined above using the following selector `{ listId:
  82. // list._id }`. But it was causing a race condition in publish-composite,
  83. // that I documented here:
  84. //
  85. // https://github.com/englue/meteor-publish-composite/issues/29
  86. //
  87. // cottz:publish had a similar problem:
  88. //
  89. // https://github.com/Goluis/cottz-publish/issues/4
  90. //
  91. // The current state of relational publishing in meteor is a bit sad,
  92. // there are a lot of various packages, with various APIs, some of them
  93. // are unmaintained. Fortunately this is something that will be fixed by
  94. // meteor-core at some point:
  95. //
  96. // https://trello.com/c/BGvIwkEa/48-easy-joins-in-subscriptions
  97. //
  98. // And in the meantime our code below works pretty well -- it's not even a
  99. // hack!
  100. // Gather queries and send in bulk
  101. const cardComments = this.join(CardComments);
  102. cardComments.selector = (_ids) => ({ cardId: _ids });
  103. const attachments = this.join(Attachments);
  104. attachments.selector = (_ids) => ({ cardId: _ids });
  105. const checklists = this.join(Checklists);
  106. checklists.selector = (_ids) => ({ cardId: _ids });
  107. const checklistItems = this.join(ChecklistItems);
  108. checklistItems.selector = (_ids) => ({ cardId: _ids });
  109. const parentCards = this.join(Cards);
  110. parentCards.selector = (_ids) => ({ parentId: _ids });
  111. const boards = this.join(Boards);
  112. const subCards = this.join(Cards);
  113. subCards.selector = (_ids) => ({ archived: isArchived });
  114. this.cursor(Cards.find({ boardId: {$in: [boardId, board.subtasksDefaultBoardId]}, archived: isArchived }), function(cardId, card) {
  115. if (card.type === 'cardType-linkedCard') {
  116. const impCardId = card.linkedId;
  117. subCards.push(impCardId);
  118. cardComments.push(impCardId);
  119. attachments.push(impCardId);
  120. checklists.push(impCardId);
  121. checklistItems.push(impCardId);
  122. } else if (card.type === 'cardType-linkedBoard') {
  123. boards.push(card.linkedId);
  124. }
  125. cardComments.push(cardId);
  126. attachments.push(cardId);
  127. checklists.push(cardId);
  128. checklistItems.push(cardId);
  129. parentCards.push(cardId);
  130. });
  131. // Send bulk queries for all found ids
  132. subCards.send();
  133. cardComments.send();
  134. attachments.send();
  135. checklists.send();
  136. checklistItems.send();
  137. boards.send();
  138. parentCards.send();
  139. if (board.members) {
  140. // Board members. This publication also includes former board members that
  141. // aren't members anymore but may have some activities attached to them in
  142. // the history.
  143. const memberIds = _.pluck(board.members, 'userId');
  144. // We omit the current user because the client should already have that data,
  145. // and sending it triggers a subtle bug:
  146. // https://github.com/wefork/wekan/issues/15
  147. this.cursor(Users.find({
  148. _id: { $in: _.without(memberIds, thisUserId)},
  149. }, { fields: {
  150. 'username': 1,
  151. 'profile.fullname': 1,
  152. 'profile.avatarUrl': 1,
  153. }}));
  154. this.cursor(presences.find({ userId: { $in: memberIds } }));
  155. }
  156. });
  157. return this.ready();
  158. });