boards.js 8.5 KB

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