boards.js 6.8 KB

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