boards.js 6.9 KB

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