boards.js 11 KB

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