2015-12-16 21:54:35 +01:00
|
|
|
/* global JsonRoutes */
|
2017-04-27 20:49:24 +03:00
|
|
|
if (Meteor.isServer) {
|
2015-12-17 13:11:33 +01:00
|
|
|
// todo XXX once we have a real API in place, move that route there
|
2015-12-17 23:57:28 +01:00
|
|
|
// todo XXX also share the route definition between the client and the server
|
2016-01-05 13:37:15 +01:00
|
|
|
// so that we could use something like
|
|
|
|
// `ApiRoutes.path('boards/export', boardId)``
|
|
|
|
// on the client instead of copy/pasting the route path manually between the
|
|
|
|
// client and the server.
|
2015-12-17 13:11:33 +01:00
|
|
|
/*
|
|
|
|
* This route is used to export the board FROM THE APPLICATION.
|
2015-12-17 23:57:28 +01:00
|
|
|
* If user is already logged-in, pass loginToken as param "authToken":
|
|
|
|
* '/api/boards/:boardId?authToken=:token'
|
2015-12-17 13:11:33 +01:00
|
|
|
*
|
|
|
|
* See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
|
|
|
|
* for detailed explanations
|
|
|
|
*/
|
2017-04-27 20:49:24 +03:00
|
|
|
// JsonRoutes.add('get', '/api/boards/:boardId', function (req, res) {
|
|
|
|
// const boardId = req.params.boardId;
|
|
|
|
// let user = null;
|
|
|
|
// // todo XXX for real API, first look for token in Authentication: header
|
|
|
|
// // then fallback to parameter
|
|
|
|
// const loginToken = req.query.authToken;
|
|
|
|
// if (loginToken) {
|
|
|
|
// const hashToken = Accounts._hashLoginToken(loginToken);
|
|
|
|
// user = Meteor.users.findOne({
|
|
|
|
// 'services.resume.loginTokens.hashedToken': hashToken,
|
|
|
|
// });
|
|
|
|
// }
|
2015-12-16 21:54:35 +01:00
|
|
|
|
2017-04-27 20:49:24 +03:00
|
|
|
// const exporter = new Exporter(boardId);
|
|
|
|
// if(exporter.canExport(user)) {
|
|
|
|
// JsonRoutes.sendResult(res, 200, exporter.build());
|
|
|
|
// } else {
|
|
|
|
// // we could send an explicit error message, but on the other hand the only
|
|
|
|
// // way to get there is by hacking the UI so let's keep it raw.
|
|
|
|
// JsonRoutes.sendResult(res, 403);
|
|
|
|
// }
|
|
|
|
// });
|
2015-12-16 21:54:35 +01:00
|
|
|
}
|
2015-12-13 20:02:34 +01:00
|
|
|
|
2015-12-09 00:35:45 +01:00
|
|
|
class Exporter {
|
|
|
|
constructor(boardId) {
|
|
|
|
this._boardId = boardId;
|
|
|
|
}
|
|
|
|
|
|
|
|
build() {
|
2017-04-27 20:49:24 +03:00
|
|
|
const byBoard = { boardId: this._boardId };
|
2015-12-16 16:30:48 +01:00
|
|
|
// we do not want to retrieve boardId in related elements
|
2017-04-27 20:49:24 +03:00
|
|
|
const noBoardId = { fields: { boardId: 0 } };
|
2015-12-17 11:58:55 +01:00
|
|
|
const result = {
|
|
|
|
_format: 'wekan-board-1.0.0',
|
|
|
|
};
|
2017-04-27 20:49:24 +03:00
|
|
|
_.extend(result, Boards.findOne(this._boardId, { fields: { stars: 0 } }));
|
2015-12-16 16:30:48 +01:00
|
|
|
result.lists = Lists.find(byBoard, noBoardId).fetch();
|
|
|
|
result.cards = Cards.find(byBoard, noBoardId).fetch();
|
|
|
|
result.comments = CardComments.find(byBoard, noBoardId).fetch();
|
|
|
|
result.activities = Activities.find(byBoard, noBoardId).fetch();
|
2015-12-17 11:58:55 +01:00
|
|
|
// for attachments we only export IDs and absolute url to original doc
|
2016-01-05 13:37:15 +01:00
|
|
|
result.attachments = Attachments.find(byBoard).fetch().map((attachment) => {
|
|
|
|
return {
|
|
|
|
_id: attachment._id,
|
|
|
|
cardId: attachment.cardId,
|
|
|
|
url: FlowRouter.url(attachment.url()),
|
|
|
|
};
|
|
|
|
});
|
2015-12-09 00:35:45 +01:00
|
|
|
|
2016-01-05 13:37:15 +01:00
|
|
|
// we also have to export some user data - as the other elements only
|
|
|
|
// include id but we have to be careful:
|
2015-12-09 00:35:45 +01:00
|
|
|
// 1- only exports users that are linked somehow to that board
|
|
|
|
// 2- do not export any sensitive information
|
|
|
|
const users = {};
|
2017-04-27 20:49:24 +03:00
|
|
|
result.members.forEach((member) => { users[member.userId] = true; });
|
|
|
|
result.lists.forEach((list) => { users[list.userId] = true; });
|
2015-12-09 00:35:45 +01:00
|
|
|
result.cards.forEach((card) => {
|
|
|
|
users[card.userId] = true;
|
|
|
|
if (card.members) {
|
2017-04-27 20:49:24 +03:00
|
|
|
card.members.forEach((memberId) => { users[memberId] = true; });
|
2015-12-09 00:35:45 +01:00
|
|
|
}
|
|
|
|
});
|
2017-04-27 20:49:24 +03:00
|
|
|
result.comments.forEach((comment) => { users[comment.userId] = true; });
|
|
|
|
result.activities.forEach((activity) => { users[activity.userId] = true; });
|
|
|
|
const byUserIds = { _id: { $in: Object.getOwnPropertyNames(users) } };
|
2015-12-09 00:35:45 +01:00
|
|
|
// we use whitelist to be sure we do not expose inadvertently
|
|
|
|
// some secret fields that gets added to User later.
|
2017-04-27 20:49:24 +03:00
|
|
|
const userFields = {
|
|
|
|
fields: {
|
|
|
|
_id: 1,
|
|
|
|
username: 1,
|
|
|
|
'profile.fullname': 1,
|
|
|
|
'profile.initials': 1,
|
|
|
|
'profile.avatarUrl': 1,
|
|
|
|
},
|
|
|
|
};
|
2015-12-17 11:58:55 +01:00
|
|
|
result.users = Users.find(byUserIds, userFields).fetch().map((user) => {
|
|
|
|
// user avatar is stored as a relative url, we export absolute
|
2017-04-27 20:49:24 +03:00
|
|
|
if (user.profile.avatarUrl) {
|
2016-01-05 13:37:15 +01:00
|
|
|
user.profile.avatarUrl = FlowRouter.url(user.profile.avatarUrl);
|
2015-12-17 11:58:55 +01:00
|
|
|
}
|
|
|
|
return user;
|
|
|
|
});
|
2015-12-09 00:35:45 +01:00
|
|
|
return result;
|
|
|
|
}
|
2015-12-16 21:54:35 +01:00
|
|
|
|
|
|
|
canExport(user) {
|
|
|
|
const board = Boards.findOne(this._boardId);
|
|
|
|
return board && board.isVisibleBy(user);
|
|
|
|
}
|
2015-12-09 00:35:45 +01:00
|
|
|
}
|