boards.js 7.5 KB

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