boards.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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(
  126. CustomFields.find(
  127. { boardIds: { $in: [boardId] } },
  128. { sort: { name: 1 } },
  129. ),
  130. );
  131. // Cards and cards comments
  132. // XXX Originally we were publishing the card documents as a child of the
  133. // list publication defined above using the following selector `{ listId:
  134. // list._id }`. But it was causing a race condition in publish-composite,
  135. // that I documented here:
  136. //
  137. // https://github.com/englue/meteor-publish-composite/issues/29
  138. //
  139. // cottz:publish had a similar problem:
  140. //
  141. // https://github.com/Goluis/cottz-publish/issues/4
  142. //
  143. // The current state of relational publishing in meteor is a bit sad,
  144. // there are a lot of various packages, with various APIs, some of them
  145. // are unmaintained. Fortunately this is something that will be fixed by
  146. // meteor-core at some point:
  147. //
  148. // https://trello.com/c/BGvIwkEa/48-easy-joins-in-subscriptions
  149. //
  150. // And in the meantime our code below works pretty well -- it's not even a
  151. // hack!
  152. // Gather queries and send in bulk
  153. const cardComments = this.join(CardComments);
  154. cardComments.selector = _ids => ({ cardId: _ids });
  155. const attachments = this.join(Attachments);
  156. attachments.selector = _ids => ({ cardId: _ids });
  157. const checklists = this.join(Checklists);
  158. checklists.selector = _ids => ({ cardId: _ids });
  159. const checklistItems = this.join(ChecklistItems);
  160. checklistItems.selector = _ids => ({ cardId: _ids });
  161. const parentCards = this.join(Cards);
  162. parentCards.selector = _ids => ({ parentId: _ids });
  163. const boards = this.join(Boards);
  164. const subCards = this.join(Cards);
  165. subCards.selector = _ids => ({ _id: _ids, archived: isArchived });
  166. this.cursor(
  167. Cards.find({
  168. boardId: { $in: [boardId, board.subtasksDefaultBoardId] },
  169. archived: isArchived,
  170. }),
  171. function(cardId, card) {
  172. if (card.type === 'cardType-linkedCard') {
  173. const impCardId = card.linkedId;
  174. subCards.push(impCardId); // GitHub issue #2688 and #2693
  175. cardComments.push(impCardId);
  176. attachments.push(impCardId);
  177. checklists.push(impCardId);
  178. checklistItems.push(impCardId);
  179. } else if (card.type === 'cardType-linkedBoard') {
  180. boards.push(card.linkedId);
  181. }
  182. cardComments.push(cardId);
  183. attachments.push(cardId);
  184. checklists.push(cardId);
  185. checklistItems.push(cardId);
  186. parentCards.push(cardId);
  187. },
  188. );
  189. // Send bulk queries for all found ids
  190. subCards.send();
  191. cardComments.send();
  192. attachments.send();
  193. checklists.send();
  194. checklistItems.send();
  195. boards.send();
  196. parentCards.send();
  197. if (board.members) {
  198. // Board members. This publication also includes former board members that
  199. // aren't members anymore but may have some activities attached to them in
  200. // the history.
  201. const memberIds = _.pluck(board.members, 'userId');
  202. // We omit the current user because the client should already have that data,
  203. // and sending it triggers a subtle bug:
  204. // https://github.com/wefork/wekan/issues/15
  205. this.cursor(
  206. Users.find(
  207. {
  208. _id: { $in: _.without(memberIds, thisUserId) },
  209. },
  210. {
  211. fields: {
  212. username: 1,
  213. 'profile.fullname': 1,
  214. 'profile.avatarUrl': 1,
  215. 'profile.initials': 1,
  216. },
  217. },
  218. ),
  219. );
  220. this.cursor(presences.find({ userId: { $in: memberIds } }));
  221. }
  222. },
  223. );
  224. return this.ready();
  225. });
  226. Meteor.methods({
  227. copyBoard(boardId, properties) {
  228. check(boardId, String);
  229. check(properties, Object);
  230. const board = Boards.findOne(boardId);
  231. if (board) {
  232. for (const key in properties) {
  233. board[key] = properties[key];
  234. }
  235. return board.copy();
  236. }
  237. return null;
  238. },
  239. });