boards.js 9.7 KB

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