boards.js 7.7 KB

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