boards.js 11 KB

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