boards.js 11 KB

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