boards.js 11 KB

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