boards.js 6.3 KB

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