boards.js 6.5 KB

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