export.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. Meteor.methods({
  2. exportBoard(boardId) {
  3. check(boardId, String);
  4. const board = Boards.findOne(boardId);
  5. if(board.isVisibleByUser()) {
  6. const exporter = new Exporter(boardId);
  7. return exporter.build();
  8. } else {
  9. throw new Meteor.Error('error-board-notAMember');
  10. }
  11. }
  12. });
  13. class Exporter {
  14. constructor(boardId) {
  15. this._boardId = boardId;
  16. }
  17. build() {
  18. const byBoard = {boardId: this._boardId};
  19. const fields = {fields: {boardId: 0}};
  20. const result = Boards.findOne(this._boardId);
  21. result.lists = Lists.find(byBoard, fields).fetch();
  22. result.cards = Cards.find(byBoard, fields).fetch();
  23. result.comments = CardComments.find(byBoard, fields).fetch();
  24. result.activities = Activities.find(byBoard, fields).fetch();
  25. // we also have to export some user data - as the other elements only include id
  26. // but we have to be careful:
  27. // 1- only exports users that are linked somehow to that board
  28. // 2- do not export any sensitive information
  29. const users = {};
  30. result.members.forEach((member) => {users[member.userId] = true;});
  31. result.lists.forEach((list) => {users[list.userId] = true;});
  32. result.cards.forEach((card) => {
  33. users[card.userId] = true;
  34. if (card.members) {
  35. card.members.forEach((memberId) => {users[memberId] = true;});
  36. }
  37. });
  38. result.comments.forEach((comment) => {users[comment.userId] = true;});
  39. result.activities.forEach((activity) => {users[activity.userId] = true;});
  40. const byUserIds = {_id: {$in: Object.getOwnPropertyNames(users)}};
  41. // we use whitelist to be sure we do not expose inadvertently
  42. // some secret fields that gets added to User later.
  43. const userFields = {fields: {
  44. _id: 1,
  45. username: 1,
  46. 'profile.fullname': 1,
  47. 'profile.initials': 1,
  48. 'profile.avatarUrl': 1,
  49. }};
  50. result.users = Users.find(byUserIds, userFields).fetch();
  51. //return JSON.stringify(result);
  52. return result;
  53. }
  54. }