boards.js 11 KB

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