boards.js 6.2 KB

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