export.js 1.8 KB

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