boards.js 6.5 KB

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