boards.js 11 KB

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