boards.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. var starredBoards = Users.findOne(this.userId).profile.starredBoards || [];
  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. slug: 1,
  24. title: 1,
  25. color: 1,
  26. permission: 1
  27. }
  28. });
  29. });
  30. Meteor.publishComposite('board', function(boardId, slug) {
  31. check(boardId, String);
  32. check(slug, String);
  33. return {
  34. find: function() {
  35. return Boards.find({
  36. _id: boardId,
  37. slug: slug,
  38. archived: false,
  39. // If the board is not public the user has to be a member of it to see
  40. // it.
  41. $or: [
  42. { permission: 'public' },
  43. { 'members.userId': this.userId }
  44. ]
  45. }, { limit: 1 });
  46. },
  47. children: [
  48. // Lists
  49. {
  50. find: function(board) {
  51. return Lists.find({
  52. boardId: board._id
  53. });
  54. }
  55. },
  56. // Cards and cards comments
  57. // XXX Originally we were publishing the card documents as a child of the
  58. // list publication defined above using the following selector `{ listId:
  59. // list._id }`. But it was causing a race condition in publish-composite,
  60. // that I documented here:
  61. //
  62. // https://github.com/englue/meteor-publish-composite/issues/29
  63. //
  64. // I then tried to replace publish-composite by cottz:publish, but it had
  65. // a similar problem:
  66. //
  67. // https://github.com/Goluis/cottz-publish/issues/4
  68. // https://github.com/libreboard/libreboard/pull/78
  69. //
  70. // The current state of relational publishing in meteor is a bit sad,
  71. // there are a lot of various packages, with various APIs, some of them
  72. // are unmaintained. Fortunately this is something that will be fixed by
  73. // meteor-core at some point:
  74. //
  75. // https://trello.com/c/BGvIwkEa/48-easy-joins-in-subscriptions
  76. //
  77. // And in the meantime our code below works pretty well -- it's not even a
  78. // hack!
  79. {
  80. find: function(board) {
  81. return Cards.find({
  82. boardId: board._id
  83. });
  84. },
  85. children: [
  86. // comments
  87. {
  88. find: function(card) {
  89. return CardComments.find({
  90. cardId: card._id
  91. });
  92. }
  93. },
  94. // Attachments
  95. {
  96. find: function(card) {
  97. return Attachments.find({
  98. cardId: card._id
  99. });
  100. }
  101. }
  102. ]
  103. },
  104. // Board members. This publication also includes former board members that
  105. // aren't members anymore but may have some activities attached to them in
  106. // the history.
  107. {
  108. find: function(board) {
  109. return Users.find({
  110. _id: { $in: _.pluck(board.members, 'userId') }
  111. });
  112. },
  113. // Presence indicators
  114. children: [{
  115. find: function(user) {
  116. return Presences.find({userId: user._id});
  117. }
  118. }]
  119. }
  120. ]
  121. };
  122. });