boards.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Meteor.publish('boards', function() {
  6. // Ensure that the user is connected. If it is not, we need to return an empty
  7. // array to tell the client to remove the previously published docs.
  8. if (!Match.test(this.userId, String))
  9. return [];
  10. // Defensive programming to verify that starredBoards has the expected
  11. // format -- since the field is in the `profile` a user can modify it.
  12. const {starredBoards = []} = Users.findOne(this.userId).profile;
  13. check(starredBoards, [String]);
  14. return Boards.find({
  15. archived: false,
  16. $or: [
  17. { 'members.userId': this.userId },
  18. { _id: { $in: starredBoards } },
  19. ],
  20. }, {
  21. fields: {
  22. _id: 1,
  23. archived: 1,
  24. slug: 1,
  25. title: 1,
  26. description: 1,
  27. color: 1,
  28. members: 1,
  29. permission: 1,
  30. },
  31. });
  32. });
  33. Meteor.publish('archivedBoards', function() {
  34. if (!Match.test(this.userId, String))
  35. return [];
  36. return Boards.find({
  37. archived: true,
  38. members: {
  39. $elemMatch: {
  40. userId: this.userId,
  41. isAdmin: true,
  42. },
  43. },
  44. }, {
  45. fields: {
  46. _id: 1,
  47. archived: 1,
  48. slug: 1,
  49. title: 1,
  50. },
  51. });
  52. });
  53. Meteor.publishComposite('board', function(boardId) {
  54. check(boardId, String);
  55. return {
  56. find() {
  57. return Boards.find({
  58. _id: boardId,
  59. archived: false,
  60. // If the board is not public the user has to be a member of it to see
  61. // it.
  62. $or: [
  63. { permission: 'public' },
  64. { 'members.userId': this.userId },
  65. ],
  66. }, { limit: 1 });
  67. },
  68. children: [
  69. // Lists
  70. {
  71. find(board) {
  72. return Lists.find({
  73. boardId: board._id,
  74. });
  75. },
  76. },
  77. // Cards and cards comments
  78. // XXX Originally we were publishing the card documents as a child of the
  79. // list publication defined above using the following selector `{ listId:
  80. // list._id }`. But it was causing a race condition in publish-composite,
  81. // that I documented here:
  82. //
  83. // https://github.com/englue/meteor-publish-composite/issues/29
  84. //
  85. // I then tried to replace publish-composite by cottz:publish, but it had
  86. // a similar problem:
  87. //
  88. // https://github.com/Goluis/cottz-publish/issues/4
  89. // https://github.com/wekan/wekan/pull/78
  90. //
  91. // The current state of relational publishing in meteor is a bit sad,
  92. // there are a lot of various packages, with various APIs, some of them
  93. // are unmaintained. Fortunately this is something that will be fixed by
  94. // meteor-core at some point:
  95. //
  96. // https://trello.com/c/BGvIwkEa/48-easy-joins-in-subscriptions
  97. //
  98. // And in the meantime our code below works pretty well -- it's not even a
  99. // hack!
  100. {
  101. find(board) {
  102. return Cards.find({
  103. boardId: board._id,
  104. });
  105. },
  106. children: [
  107. // comments
  108. {
  109. find(card) {
  110. return CardComments.find({
  111. cardId: card._id,
  112. });
  113. },
  114. },
  115. // Attachments
  116. {
  117. find(card) {
  118. return Attachments.find({
  119. cardId: card._id,
  120. });
  121. },
  122. },
  123. ],
  124. },
  125. // Board members. This publication also includes former board members that
  126. // aren't members anymore but may have some activities attached to them in
  127. // the history.
  128. {
  129. find(board) {
  130. return Users.find({
  131. _id: { $in: _.pluck(board.members, 'userId') },
  132. });
  133. },
  134. // Presence indicators
  135. children: [{
  136. find(user) {
  137. return presences.find({userId: user._id});
  138. },
  139. }],
  140. },
  141. ],
  142. };
  143. });