From 2691f033cbd072864cf79e95d131a93449d3c84d Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Wed, 29 Apr 2020 22:38:50 +0200 Subject: Fix creation of card links Without this fix, orphaned card links are created and therefore this leads to problems as described in https://github.com/wekan/wekan/issues/2785. --- models/cards.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'models') diff --git a/models/cards.js b/models/cards.js index 4197f7ab..498140b9 100644 --- a/models/cards.js +++ b/models/cards.js @@ -428,6 +428,21 @@ Cards.helpers({ return _id; }, + link(boardId, swimlaneId, listId) { + // TODO is there a better method to create a deepcopy? + linkCard = JSON.parse(JSON.stringify(this)); + // TODO is this how it is meant to be? + linkCard.linkedId = linkCard.linkedId || linkCard._id; + linkCard.boardId = boardId; + linkCard.swimlaneId = swimlaneId; + linkCard.listId = listId; + linkCard.type = 'cardType-linkedCard'; + delete linkCard._id; + // TODO shall we copy the labels for a linked card?! + delete linkCard.labelIds; + return Cards.insert(linkCard); + }, + list() { return Lists.findOne(this.listId); }, -- cgit v1.2.3-1-g7c22 From 3cc0a93e0ea2399d239923e3a89d49d93a979684 Mon Sep 17 00:00:00 2001 From: Nico Date: Sun, 3 May 2020 00:33:15 +0200 Subject: Card vote options in new fork --- models/cards.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 3 deletions(-) (limited to 'models') diff --git a/models/cards.js b/models/cards.js index 4197f7ab..b0783898 100644 --- a/models/cards.js +++ b/models/cards.js @@ -340,6 +340,10 @@ Cards.attachSchema( type: Boolean, defaultValue: false, }, + 'vote.allowNonBoardMembers': { + type: Boolean, + defaultValue: false, + }, }), ); @@ -347,8 +351,14 @@ Cards.allow({ insert(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - update(userId, doc) { - return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); + + update(userId, doc, fields) { + // Allow board members or logged in users if only vote get's changed + return ( + allowIsBoardMember(userId, Boards.findOne(doc.boardId)) || + (_.isEqual(fields, ['vote', 'modifiedAt', 'dateLastActivity']) && + !!userId) + ); }, remove(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); @@ -1048,6 +1058,29 @@ Cards.helpers({ } }, + getVoteEnd() { + if (this.isLinkedCard()) { + const card = Cards.findOne({ _id: this.linkedId }); + if (card && card.vote) return card.vote.end; + else return null; + } else if (this.isLinkedBoard()) { + const board = Boards.findOne({ _id: this.linkedId }); + if (board && board.vote) return board.vote.end; + else return null; + } else if (this.vote) { + return this.vote.end; + } else { + return null; + } + }, + expiredVote() { + let end = this.getVoteEnd(); + if (end) { + end = moment(end); + return end.isBefore(new Date()); + } + return false; + }, voteMemberPositive() { if (this.vote && this.vote.positive) return Users.find({ _id: { $in: this.vote.positive } }); @@ -1153,6 +1186,26 @@ Cards.helpers({ isTemplateCard() { return this.type === 'template-card'; }, + + votePublic() { + if (this.vote) return this.vote.public; + return null; + }, + voteAllowNonBoardMembers() { + if (this.vote) return this.vote.allowNonBoardMembers; + return null; + }, + voteCountNegative() { + if (this.vote && this.vote.negative) return this.vote.negative.length; + return null; + }, + voteCountPositive() { + if (this.vote && this.vote.positive) return this.vote.positive.length; + return null; + }, + voteCount() { + return this.voteCountPositive() + this.voteCountNegative(); + }, }); Cards.mutations({ @@ -1476,12 +1529,13 @@ Cards.mutations({ }, }; }, - setVoteQuestion(question, publicVote) { + setVoteQuestion(question, publicVote, allowNonBoardMembers) { return { $set: { vote: { question, public: publicVote, + allowNonBoardMembers, positive: [], negative: [], }, @@ -1495,6 +1549,16 @@ Cards.mutations({ }, }; }, + setVoteEnd(end) { + return { + $set: { 'vote.end': end }, + }; + }, + unsetVoteEnd() { + return { + $unset: { 'vote.end': '' }, + }; + }, setVote(userId, forIt) { switch (forIt) { case true: -- cgit v1.2.3-1-g7c22 From ec03bbe260e3a7e1298ac7cf526b41c7d2207e91 Mon Sep 17 00:00:00 2001 From: Nico Date: Sun, 3 May 2020 01:29:28 +0200 Subject: API add boards to json where user is member of --- models/users.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'models') diff --git a/models/users.js b/models/users.js index a1bc5b0f..1a021bb7 100644 --- a/models/users.js +++ b/models/users.js @@ -1240,6 +1240,25 @@ if (Meteor.isServer) { Authentication.checkLoggedIn(req.userId); const data = Meteor.users.findOne({ _id: req.userId }); delete data.services; + + // get all boards where the user is member of + let boards = Boards.find( + { + type: 'board', + 'members.userId': req.userId, + }, + { + fields: { _id: 1, members: 1 }, + }, + ); + boards = boards.map(b => { + const u = b.members.find(m => m.userId === req.userId); + delete u.userId; + u.boardId = b._id; + return u; + }); + + data.boards = boards; JsonRoutes.sendResult(res, { code: 200, data, @@ -1292,9 +1311,29 @@ if (Meteor.isServer) { try { Authentication.checkUserId(req.userId); const id = req.params.userId; + + // get all boards where the user is member of + let boards = Boards.find( + { + type: 'board', + 'members.userId': id, + }, + { + fields: { _id: 1, members: 1 }, + }, + ); + boards = boards.map(b => { + const u = b.members.find(m => m.userId === id); + delete u.userId; + u.boardId = b._id; + return u; + }); + + const user = Meteor.users.findOne({ _id: id }); + user.boards = boards; JsonRoutes.sendResult(res, { code: 200, - data: Meteor.users.findOne({ _id: id }), + data: user, }); } catch (error) { JsonRoutes.sendResult(res, { -- cgit v1.2.3-1-g7c22 From 1742bcd9b15737c5853e9bcd0a6301139498307d Mon Sep 17 00:00:00 2001 From: Bryan Mutai Date: Thu, 7 May 2020 01:29:22 +0300 Subject: add: import board/cards/lists using CSV/TSV --- models/csvCreator.js | 314 +++++++++++++++++++++++++++++++++++++++++++++++++++ models/import.js | 8 +- 2 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 models/csvCreator.js (limited to 'models') diff --git a/models/csvCreator.js b/models/csvCreator.js new file mode 100644 index 00000000..346d2201 --- /dev/null +++ b/models/csvCreator.js @@ -0,0 +1,314 @@ +import Boards from './boards'; + +export class CsvCreator { + constructor(data) { + // date to be used for timestamps during import + this._nowDate = new Date(); + // index to help keep track of what information a column stores + // each row represents a card + this.fieldIndex = {}; + this.lists = {}; + // Map of members using username => wekanid + this.members = data.membersMapping ? data.membersMapping : {}; + this.swimlane = null; + } + + /** + * If dateString is provided, + * return the Date it represents. + * If not, will return the date when it was first called. + * This is useful for us, as we want all import operations to + * have the exact same date for easier later retrieval. + * + * @param {String} dateString a properly formatted Date + */ + _now(dateString) { + if (dateString) { + return new Date(dateString); + } + if (!this._nowDate) { + this._nowDate = new Date(); + } + return this._nowDate; + } + + _user(wekanUserId) { + if (wekanUserId && this.members[wekanUserId]) { + return this.members[wekanUserId]; + } + return Meteor.userId(); + } + + /** + * Map the header row titles to an index to help assign proper values to the cards' fields + * Valid headers (name of card fields): + * title, description, status, owner, member, label, due date, start date, finish date, created at, updated at + * Some header aliases can also be accepted. + * Headers are NOT case-sensitive. + * + * @param {Array} headerRow array from row of headers of imported CSV/TSV for cards + */ + mapHeadertoCardFieldIndex(headerRow) { + const index = {}; + for (let i = 0; i < headerRow.length; i++) { + switch (headerRow[i].trim().toLowerCase()) { + case 'title': + index.title = i; + break; + case 'description': + index.description = i; + break; + case 'stage': + case 'status': + case 'state': + index.stage = i; + break; + case 'owner': + index.owner = i; + break; + case 'members': + case 'member': + index.members = i; + break; + case 'labels': + case 'label': + index.labels = i; + break; + case 'due date': + case 'deadline': + case 'due at': + index.dueAt = i; + break; + case 'start date': + case 'start at': + index.startAt = i; + break; + case 'finish date': + case 'end at': + index.endAt = i; + break; + case 'creation date': + case 'created at': + index.createdAt = i; + break; + case 'update date': + case 'updated at': + case 'modified at': + case 'modified on': + index.modifiedAt = i; + break; + } + } + this.fieldIndex = index; + } + + createBoard(csvData) { + const boardToCreate = { + archived: false, + color: 'belize', + createdAt: this._now(), + labels: [], + members: [ + { + userId: Meteor.userId(), + wekanId: Meteor.userId(), + isActive: true, + isAdmin: true, + isNoComments: false, + isCommentOnly: false, + swimlaneId: false, + }, + ], + modifiedAt: this._now(), + //default is private, should inform user. + permission: 'private', + slug: 'board', + stars: 0, + title: `Imported Board ${this._now()}`, + }; + + // create labels + for (let i = 1; i < csvData.length; i++) { + //get the label column + if (csvData[i][this.fieldIndex.labels]) { + const labelsToCreate = new Set(); + for (const importedLabel of csvData[i][this.fieldIndex.labels].split( + ' ', + )) { + if (importedLabel && importedLabel.length > 0) { + labelsToCreate.add(importedLabel); + } + } + for (const label of labelsToCreate) { + let labelName, labelColor; + if (label.indexOf('-') > -1) { + labelName = label.split('-')[0]; + labelColor = label.split('-')[1]; + } else { + labelName = label; + } + const labelToCreate = { + _id: Random.id(6), + color: labelColor ? labelColor : 'black', + name: labelName, + }; + boardToCreate.labels.push(labelToCreate); + } + } + } + + const boardId = Boards.direct.insert(boardToCreate); + Boards.direct.update(boardId, { + $set: { + modifiedAt: this._now(), + }, + }); + // log activity + Activities.direct.insert({ + activityType: 'importBoard', + boardId, + createdAt: this._now(), + source: { + id: boardId, + system: 'CSV/TSV', + }, + // We attribute the import to current user, + // not the author from the original object. + userId: this._user(), + }); + return boardId; + } + + createSwimlanes(boardId) { + const swimlaneToCreate = { + archived: false, + boardId, + createdAt: this._now(), + title: 'Default', + sort: 1, + }; + const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate); + Swimlanes.direct.update(swimlaneId, { $set: { updatedAt: this._now() } }); + this.swimlane = swimlaneId; + } + + createLists(csvData, boardId) { + let numOfCreatedLists = 0; + for (let i = 1; i < csvData.length; i++) { + const listToCreate = { + archived: false, + boardId, + createdAt: this._now(), + }; + if (csvData[i][this.fieldIndex.stage]) { + const existingList = Lists.find({ + title: csvData[i][this.fieldIndex.stage], + boardId, + }).fetch(); + if (existingList.length > 0) { + continue; + } else { + listToCreate.title = csvData[i][this.fieldIndex.stage]; + } + } else listToCreate.title = `Imported List ${this._now()}`; + + const listId = Lists.direct.insert(listToCreate); + this.lists[csvData[i][this.fieldIndex.stage]] = listId; + numOfCreatedLists++; + Lists.direct.update(listId, { + $set: { + updatedAt: this._now(), + sort: numOfCreatedLists, + }, + }); + } + } + + createCards(csvData, boardId) { + for (let i = 1; i < csvData.length; i++) { + const cardToCreate = { + archived: false, + boardId, + createdAt: csvData[i][this.fieldIndex.createdAt] + ? this._now(new Date(csvData[i][this.fieldIndex.createdAt])) + : null, + dateLastActivity: this._now(), + description: csvData[i][this.fieldIndex.description], + listId: this.lists[csvData[i][this.fieldIndex.stage]], + swimlaneId: this.swimlane, + sort: -1, + title: csvData[i][this.fieldIndex.title], + userId: this._user(), + startAt: csvData[i][this.fieldIndex.startAt] + ? this._now(new Date(csvData[i][this.fieldIndex.startAt])) + : null, + dueAt: csvData[i][this.fieldIndex.dueAt] + ? this._now(new Date(csvData[i][this.fieldIndex.dueAt])) + : null, + endAt: csvData[i][this.fieldIndex.endAt] + ? this._now(new Date(csvData[i][this.fieldIndex.endAt])) + : null, + spentTime: null, + labelIds: [], + modifiedAt: csvData[i][this.fieldIndex.modifiedAt] + ? this._now(new Date(csvData[i][this.fieldIndex.modifiedAt])) + : null, + }; + // add the labels + if (csvData[i][this.fieldIndex.labels]) { + const board = Boards.findOne(boardId); + for (const importedLabel of csvData[i][this.fieldIndex.labels].split( + ' ', + )) { + if (importedLabel && importedLabel.length > 0) { + let labelToApply; + if (importedLabel.indexOf('-') === -1) { + labelToApply = board.getLabel(importedLabel, 'black'); + } else { + labelToApply = board.getLabel( + importedLabel.split('-')[0], + importedLabel.split('-')[1], + ); + } + cardToCreate.labelIds.push(labelToApply._id); + } + } + } + // add the members + if (csvData[i][this.fieldIndex.members]) { + const wekanMembers = []; + for (const importedMember of csvData[i][this.fieldIndex.members].split( + ' ', + )) { + if (this.members[importedMember]) { + const wekanId = this.members[importedMember]; + if (!wekanMembers.find(wId => wId === wekanId)) { + wekanMembers.push(wekanId); + } + } + } + if (wekanMembers.length > 0) { + cardToCreate.members = wekanMembers; + } + } + Cards.direct.insert(cardToCreate); + } + } + + create(board, currentBoardId) { + const isSandstorm = + Meteor.settings && + Meteor.settings.public && + Meteor.settings.public.sandstorm; + if (isSandstorm && currentBoardId) { + const currentBoard = Boards.findOne(currentBoardId); + currentBoard.archive(); + } + this.mapHeadertoCardFieldIndex(board[0]); + const boardId = this.createBoard(board); + this.createLists(board, boardId); + this.createSwimlanes(boardId); + this.createCards(board, boardId); + return boardId; + } +} diff --git a/models/import.js b/models/import.js index fbfb1483..ea18c14f 100644 --- a/models/import.js +++ b/models/import.js @@ -2,21 +2,27 @@ import { TrelloCreator } from './trelloCreator'; import { WekanCreator } from './wekanCreator'; import { Exporter } from './export'; import wekanMembersMapper from './wekanmapper'; +import { CsvCreator } from './csvCreator'; Meteor.methods({ importBoard(board, data, importSource, currentBoard) { - check(board, Object); check(data, Object); check(importSource, String); check(currentBoard, Match.Maybe(String)); let creator; switch (importSource) { case 'trello': + check(board, Object); creator = new TrelloCreator(data); break; case 'wekan': + check(board, Object); creator = new WekanCreator(data); break; + case 'csv': + check(board, Array); + creator = new CsvCreator(data); + break; } // 1. check all parameters are ok from a syntax point of view -- cgit v1.2.3-1-g7c22 From a797abaa36790b95b44c3e9b2b080041512604e6 Mon Sep 17 00:00:00 2001 From: wackazong Date: Fri, 8 May 2020 18:55:37 +0200 Subject: Create card does not allow an empty member list When I create a card via the API I always have the authorId in members, even if I pass an empty string as member list. Workaround: I can empty the member list by passing an empty string in a PUT request. This pull request proposes to not add the authorId to the member list when creating a card and the member list is empty. --- models/cards.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'models') diff --git a/models/cards.js b/models/cards.js index 4197f7ab..a22cf70b 100644 --- a/models/cards.js +++ b/models/cards.js @@ -2156,7 +2156,7 @@ if (Meteor.isServer) { const check = Users.findOne({ _id: req.body.authorId, }); - const members = req.body.members || [req.body.authorId]; + const members = req.body.members; const assignees = req.body.assignees; if (typeof check !== 'undefined') { const id = Cards.direct.insert({ -- cgit v1.2.3-1-g7c22 From a570c4a86157ce4b60e056a4f0583ebc0fe009cf Mon Sep 17 00:00:00 2001 From: Bryan Mutai Date: Sun, 10 May 2020 23:58:15 +0300 Subject: add: export board/cards/lists to CSV/TSV --- models/csvCreator.js | 33 +++--- models/export.js | 240 ++++++++------------------------------ models/exporter.js | 320 +++++++++++++++++++++++++++++++++++++++++++++++++++ models/import.js | 4 +- 4 files changed, 385 insertions(+), 212 deletions(-) create mode 100644 models/exporter.js (limited to 'models') diff --git a/models/csvCreator.js b/models/csvCreator.js index 346d2201..025a3179 100644 --- a/models/csvCreator.js +++ b/models/csvCreator.js @@ -128,10 +128,9 @@ export class CsvCreator { }; // create labels + const labelsToCreate = new Set(); for (let i = 1; i < csvData.length; i++) { - //get the label column if (csvData[i][this.fieldIndex.labels]) { - const labelsToCreate = new Set(); for (const importedLabel of csvData[i][this.fieldIndex.labels].split( ' ', )) { @@ -139,23 +138,23 @@ export class CsvCreator { labelsToCreate.add(importedLabel); } } - for (const label of labelsToCreate) { - let labelName, labelColor; - if (label.indexOf('-') > -1) { - labelName = label.split('-')[0]; - labelColor = label.split('-')[1]; - } else { - labelName = label; - } - const labelToCreate = { - _id: Random.id(6), - color: labelColor ? labelColor : 'black', - name: labelName, - }; - boardToCreate.labels.push(labelToCreate); - } } } + for (const label of labelsToCreate) { + let labelName, labelColor; + if (label.indexOf('-') > -1) { + labelName = label.split('-')[0]; + labelColor = label.split('-')[1]; + } else { + labelName = label; + } + const labelToCreate = { + _id: Random.id(6), + color: labelColor ? labelColor : 'black', + name: labelName, + }; + boardToCreate.labels.push(labelToCreate); + } const boardId = Boards.direct.insert(boardToCreate); Boards.direct.update(boardId, { diff --git a/models/export.js b/models/export.js index 339123c8..c3783679 100644 --- a/models/export.js +++ b/models/export.js @@ -1,3 +1,4 @@ +import { Exporter } from './exporter'; /* global JsonRoutes */ if (Meteor.isServer) { // todo XXX once we have a real API in place, move that route there @@ -7,10 +8,10 @@ if (Meteor.isServer) { // on the client instead of copy/pasting the route path manually between the // client and the server. /** - * @operation export + * @operation exportJson * @tag Boards * - * @summary This route is used to export the board. + * @summary This route is used to export the board to a json file format. * * @description If user is already logged-in, pass loginToken as param * "authToken": '/api/boards/:boardId/export?authToken=:token' @@ -46,199 +47,52 @@ if (Meteor.isServer) { JsonRoutes.sendResult(res, 403); } }); -} - -// exporter maybe is broken since Gridfs introduced, add fs and path - -export class Exporter { - constructor(boardId) { - this._boardId = boardId; - } - - build() { - const fs = Npm.require('fs'); - const os = Npm.require('os'); - const path = Npm.require('path'); - - const byBoard = { boardId: this._boardId }; - const byBoardNoLinked = { - boardId: this._boardId, - linkedId: { $in: ['', null] }, - }; - // we do not want to retrieve boardId in related elements - const noBoardId = { - fields: { - boardId: 0, - }, - }; - const result = { - _format: 'wekan-board-1.0.0', - }; - _.extend( - result, - Boards.findOne(this._boardId, { - fields: { - stars: 0, - }, - }), - ); - result.lists = Lists.find(byBoard, noBoardId).fetch(); - result.cards = Cards.find(byBoardNoLinked, noBoardId).fetch(); - result.swimlanes = Swimlanes.find(byBoard, noBoardId).fetch(); - result.customFields = CustomFields.find( - { boardIds: { $in: [this.boardId] } }, - { fields: { boardId: 0 } }, - ).fetch(); - result.comments = CardComments.find(byBoard, noBoardId).fetch(); - result.activities = Activities.find(byBoard, noBoardId).fetch(); - result.rules = Rules.find(byBoard, noBoardId).fetch(); - result.checklists = []; - result.checklistItems = []; - result.subtaskItems = []; - result.triggers = []; - result.actions = []; - result.cards.forEach(card => { - result.checklists.push( - ...Checklists.find({ - cardId: card._id, - }).fetch(), - ); - result.checklistItems.push( - ...ChecklistItems.find({ - cardId: card._id, - }).fetch(), - ); - result.subtaskItems.push( - ...Cards.find({ - parentId: card._id, - }).fetch(), - ); - }); - result.rules.forEach(rule => { - result.triggers.push( - ...Triggers.find( - { - _id: rule.triggerId, - }, - noBoardId, - ).fetch(), - ); - result.actions.push( - ...Actions.find( - { - _id: rule.actionId, - }, - noBoardId, - ).fetch(), - ); - }); - - // [Old] for attachments we only export IDs and absolute url to original doc - // [New] Encode attachment to base64 - - const getBase64Data = function(doc, callback) { - let buffer = Buffer.allocUnsafe(0); - buffer.fill(0); - - // callback has the form function (err, res) {} - const tmpFile = path.join( - os.tmpdir(), - `tmpexport${process.pid}${Math.random()}`, - ); - const tmpWriteable = fs.createWriteStream(tmpFile); - const readStream = doc.createReadStream(); - readStream.on('data', function(chunk) { - buffer = Buffer.concat([buffer, chunk]); - }); - - readStream.on('error', function(err) { - callback(null, null); - }); - readStream.on('end', function() { - // done - fs.unlink(tmpFile, () => { - //ignored - }); - callback(null, buffer.toString('base64')); + /** + * @operation exportCSV/TSV + * @tag Boards + * + * @summary This route is used to export the board to a CSV or TSV file format. + * + * @description If user is already logged-in, pass loginToken as param + * + * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/ + * for detailed explanations + * + * @param {string} boardId the ID of the board we are exporting + * @param {string} authToken the loginToken + * @param {string} delimiter delimiter to use while building export. Default is comma ',' + */ + Picker.route('/api/boards/:boardId/export/csv', function(params, req, res) { + const boardId = params.boardId; + let user = null; + const loginToken = params.query.authToken; + if (loginToken) { + const hashToken = Accounts._hashLoginToken(loginToken); + user = Meteor.users.findOne({ + 'services.resume.loginTokens.hashedToken': hashToken, }); - readStream.pipe(tmpWriteable); - }; - const getBase64DataSync = Meteor.wrapAsync(getBase64Data); - result.attachments = Attachments.find(byBoard) - .fetch() - .map(attachment => { - let filebase64 = null; - filebase64 = getBase64DataSync(attachment); - - return { - _id: attachment._id, - cardId: attachment.cardId, - //url: FlowRouter.url(attachment.url()), - file: filebase64, - name: attachment.original.name, - type: attachment.original.type, - }; + } else if (!Meteor.settings.public.sandstorm) { + Authentication.checkUserId(req.userId); + user = Users.findOne({ + _id: req.userId, + isAdmin: true, }); - - // we also have to export some user data - as the other elements only - // include id but we have to be careful: - // 1- only exports users that are linked somehow to that board - // 2- do not export any sensitive information - const users = {}; - result.members.forEach(member => { - users[member.userId] = true; - }); - result.lists.forEach(list => { - users[list.userId] = true; - }); - result.cards.forEach(card => { - users[card.userId] = true; - if (card.members) { - card.members.forEach(memberId => { - users[memberId] = true; - }); - } - }); - result.comments.forEach(comment => { - users[comment.userId] = true; - }); - result.activities.forEach(activity => { - users[activity.userId] = true; - }); - result.checklists.forEach(checklist => { - users[checklist.userId] = true; - }); - const byUserIds = { - _id: { - $in: Object.getOwnPropertyNames(users), - }, - }; - // we use whitelist to be sure we do not expose inadvertently - // some secret fields that gets added to User later. - const userFields = { - fields: { - _id: 1, - username: 1, - 'profile.fullname': 1, - 'profile.initials': 1, - 'profile.avatarUrl': 1, - }, - }; - result.users = Users.find(byUserIds, userFields) - .fetch() - .map(user => { - // user avatar is stored as a relative url, we export absolute - if ((user.profile || {}).avatarUrl) { - user.profile.avatarUrl = FlowRouter.url(user.profile.avatarUrl); - } - return user; + } + const exporter = new Exporter(boardId); + if (exporter.canExport(user)) { + body = params.query.delimiter + ? exporter.buildCsv(params.query.delimiter) + : exporter.buildCsv(); + res.writeHead(200, { + 'Content-Length': body[0].length, + 'Content-Type': params.query.delimiter ? 'text/csv' : 'text/tsv', }); - return result; - } - - canExport(user) { - const board = Boards.findOne(this._boardId); - return board && board.isVisibleBy(user); - } + res.write(body[0]); + res.end(); + } else { + res.writeHead(403); + res.end('Permission Error'); + } + }); } diff --git a/models/exporter.js b/models/exporter.js new file mode 100644 index 00000000..910ca9a1 --- /dev/null +++ b/models/exporter.js @@ -0,0 +1,320 @@ +models / exporter.js; +const stringify = require('csv-stringify'); + +// exporter maybe is broken since Gridfs introduced, add fs and path +export class Exporter { + constructor(boardId) { + this._boardId = boardId; + } + + build() { + const fs = Npm.require('fs'); + const os = Npm.require('os'); + const path = Npm.require('path'); + + const byBoard = { boardId: this._boardId }; + const byBoardNoLinked = { + boardId: this._boardId, + linkedId: { $in: ['', null] }, + }; + // we do not want to retrieve boardId in related elements + const noBoardId = { + fields: { + boardId: 0, + }, + }; + const result = { + _format: 'wekan-board-1.0.0', + }; + _.extend( + result, + Boards.findOne(this._boardId, { + fields: { + stars: 0, + }, + }), + ); + result.lists = Lists.find(byBoard, noBoardId).fetch(); + result.cards = Cards.find(byBoardNoLinked, noBoardId).fetch(); + result.swimlanes = Swimlanes.find(byBoard, noBoardId).fetch(); + result.customFields = CustomFields.find( + { boardIds: { $in: [this.boardId] } }, + { fields: { boardId: 0 } }, + ).fetch(); + result.comments = CardComments.find(byBoard, noBoardId).fetch(); + result.activities = Activities.find(byBoard, noBoardId).fetch(); + result.rules = Rules.find(byBoard, noBoardId).fetch(); + result.checklists = []; + result.checklistItems = []; + result.subtaskItems = []; + result.triggers = []; + result.actions = []; + result.cards.forEach(card => { + result.checklists.push( + ...Checklists.find({ + cardId: card._id, + }).fetch(), + ); + result.checklistItems.push( + ...ChecklistItems.find({ + cardId: card._id, + }).fetch(), + ); + result.subtaskItems.push( + ...Cards.find({ + parentId: card._id, + }).fetch(), + ); + }); + result.rules.forEach(rule => { + result.triggers.push( + ...Triggers.find( + { + _id: rule.triggerId, + }, + noBoardId, + ).fetch(), + ); + result.actions.push( + ...Actions.find( + { + _id: rule.actionId, + }, + noBoardId, + ).fetch(), + ); + }); + + // [Old] for attachments we only export IDs and absolute url to original doc + // [New] Encode attachment to base64 + + const getBase64Data = function(doc, callback) { + let buffer = Buffer.allocUnsafe(0); + buffer.fill(0); + + // callback has the form function (err, res) {} + const tmpFile = path.join( + os.tmpdir(), + `tmpexport${process.pid}${Math.random()}`, + ); + const tmpWriteable = fs.createWriteStream(tmpFile); + const readStream = doc.createReadStream(); + readStream.on('data', function(chunk) { + buffer = Buffer.concat([buffer, chunk]); + }); + + readStream.on('error', function() { + callback(null, null); + }); + readStream.on('end', function() { + // done + fs.unlink(tmpFile, () => { + //ignored + }); + + callback(null, buffer.toString('base64')); + }); + readStream.pipe(tmpWriteable); + }; + const getBase64DataSync = Meteor.wrapAsync(getBase64Data); + result.attachments = Attachments.find(byBoard) + .fetch() + .map(attachment => { + let filebase64 = null; + filebase64 = getBase64DataSync(attachment); + + return { + _id: attachment._id, + cardId: attachment.cardId, + //url: FlowRouter.url(attachment.url()), + file: filebase64, + name: attachment.original.name, + type: attachment.original.type, + }; + }); + + // we also have to export some user data - as the other elements only + // include id but we have to be careful: + // 1- only exports users that are linked somehow to that board + // 2- do not export any sensitive information + const users = {}; + result.members.forEach(member => { + users[member.userId] = true; + }); + result.lists.forEach(list => { + users[list.userId] = true; + }); + result.cards.forEach(card => { + users[card.userId] = true; + if (card.members) { + card.members.forEach(memberId => { + users[memberId] = true; + }); + } + }); + result.comments.forEach(comment => { + users[comment.userId] = true; + }); + result.activities.forEach(activity => { + users[activity.userId] = true; + }); + result.checklists.forEach(checklist => { + users[checklist.userId] = true; + }); + const byUserIds = { + _id: { + $in: Object.getOwnPropertyNames(users), + }, + }; + // we use whitelist to be sure we do not expose inadvertently + // some secret fields that gets added to User later. + const userFields = { + fields: { + _id: 1, + username: 1, + 'profile.fullname': 1, + 'profile.initials': 1, + 'profile.avatarUrl': 1, + }, + }; + result.users = Users.find(byUserIds, userFields) + .fetch() + .map(user => { + // user avatar is stored as a relative url, we export absolute + if ((user.profile || {}).avatarUrl) { + user.profile.avatarUrl = FlowRouter.url(user.profile.avatarUrl); + } + return user; + }); + return result; + } + + buildCsv(delimiter = ',') { + const result = this.build(); + const columnHeaders = []; + const cardRows = []; + columnHeaders.push( + 'Title', + 'Description', + 'Status', + 'Swimlane', + 'Owner', + 'Requested by', + 'Assigned by', + 'Members', + 'Assignees', + 'Labels', + 'Start at', + 'Due at', + 'End at', + 'Over time', + 'Spent time (hours)', + 'Created at', + 'Last modified at', + 'Last activity', + 'Vote', + 'Archived', + ); + const stringifier = stringify({ + header: true, + delimiter, + columns: columnHeaders, + }); + + stringifier.on('readable', function() { + let row; + while ((row = stringifier.read())) { + cardRows.push(row); + } + }); + + stringifier.on('error', function(err) { + // eslint-disable-next-line no-console + console.error(err.message); + }); + + result.cards.forEach(card => { + const currentRow = []; + currentRow.push(card.title); + currentRow.push(card.description); + currentRow.push( + result.lists.find(({ _id }) => _id === card.listId).title, + ); + currentRow.push( + result.swimlanes.find(({ _id }) => _id === card.swimlaneId).title, + ); + currentRow.push( + result.users.find(({ _id }) => _id === card.userId).username, + ); + currentRow.push(card.requestedBy ? card.requestedBy : ' '); + currentRow.push(card.assignedBy ? card.assignedBy : ' '); + let usernames = ''; + card.members.forEach(memberId => { + const user = result.users.find(({ _id }) => _id === memberId); + usernames = `${usernames + user.username} `; + }); + currentRow.push(usernames.trim()); + let assignees = ''; + card.assignees.forEach(assigneeId => { + const user = result.users.find(({ _id }) => _id === assigneeId); + assignees = `${assignees + user.username} `; + }); + currentRow.push(assignees.trim()); + let labels = ''; + card.labelIds.forEach(labelId => { + const label = result.labels.find(({ _id }) => _id === labelId); + labels = `${labels + label.name}-${label.color} `; + }); + currentRow.push(labels.trim()); + currentRow.push(card.startAt ? moment(card.startAt).format('LLLL') : ' '); + currentRow.push(card.dueAt ? moment(card.dueAt).format('LLLL') : ' '); + currentRow.push(card.endAt ? moment(card.endAt).format('LLLL') : ' '); + currentRow.push(card.isOvertime ? 'true' : 'false'); + currentRow.push(card.spentTime); + currentRow.push( + card.createdAt ? moment(card.createdAt).format('LLLL') : ' ', + ); + currentRow.push( + card.modifiedAt ? moment(card.modifiedAt).format('LLLL') : ' ', + ); + currentRow.push( + card.dateLastActivity + ? moment(card.dateLastActivity).format('LLLL') + : ' ', + ); + if (card.vote.question) { + let positiveVoters = ''; + let negativeVoters = ''; + card.vote.positive.forEach(userId => { + const user = result.users.find(({ _id }) => _id === userId); + positiveVoters = `${positiveVoters + user.username} `; + }); + card.vote.negative.forEach(userId => { + const user = result.users.find(({ _id }) => _id === userId); + negativeVoters = `${negativeVoters + user.username} `; + }); + const votingResult = `${ + card.vote.public + ? `yes-${ + card.vote.positive.length + }-${positiveVoters.trimRight()}-no-${ + card.vote.negative.length + }-${negativeVoters.trimRight()}` + : `yes-${card.vote.positive.length}-no-${card.vote.negative.length}` + }`; + currentRow.push(`${card.vote.question}-${votingResult}`); + } else { + currentRow.push(' '); + } + currentRow.push(card.archived ? 'true' : 'false'); + stringifier.write(currentRow); + }); + stringifier.end(); + return cardRows; + } + + canExport(user) { + const board = Boards.findOne(this._boardId); + return board && board.isVisibleBy(user); + } +} diff --git a/models/import.js b/models/import.js index ea18c14f..f3cbaa9b 100644 --- a/models/import.js +++ b/models/import.js @@ -1,8 +1,8 @@ import { TrelloCreator } from './trelloCreator'; import { WekanCreator } from './wekanCreator'; -import { Exporter } from './export'; -import wekanMembersMapper from './wekanmapper'; import { CsvCreator } from './csvCreator'; +import { Exporter } from './exporter'; +import wekanMembersMapper from './wekanmapper'; Meteor.methods({ importBoard(board, data, importSource, currentBoard) { -- cgit v1.2.3-1-g7c22 From 8a2509007c21a1056f79d183c71cb9f1b3e0857d Mon Sep 17 00:00:00 2001 From: Lauri Ojansivu Date: Wed, 13 May 2020 05:25:04 +0300 Subject: Fix syntax. Maybe sometime later think about translations. Thanks to xet7 ! --- models/exporter.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'models') diff --git a/models/exporter.js b/models/exporter.js index 910ca9a1..3fa8ca3a 100644 --- a/models/exporter.js +++ b/models/exporter.js @@ -1,4 +1,3 @@ -models / exporter.js; const stringify = require('csv-stringify'); // exporter maybe is broken since Gridfs introduced, add fs and path @@ -215,6 +214,31 @@ export class Exporter { 'Vote', 'Archived', ); + + /* TODO: Try to get translations working. + These currently only bring English translations. + TAPi18n.__('title'), + TAPi18n.__('description'), + TAPi18n.__('status'), + TAPi18n.__('swimlane'), + TAPi18n.__('owner'), + TAPi18n.__('requested-by'), + TAPi18n.__('assigned-by'), + TAPi18n.__('members'), + TAPi18n.__('assignee'), + TAPi18n.__('labels'), + TAPi18n.__('card-start'), + TAPi18n.__('card-due'), + TAPi18n.__('card-end'), + TAPi18n.__('overtime-hours'), + TAPi18n.__('spent-time-hours'), + TAPi18n.__('createdAt'), + TAPi18n.__('last-modified-at'), + TAPi18n.__('last-activity'), + TAPi18n.__('voting'), + TAPi18n.__('archived'), + */ + const stringifier = stringify({ header: true, delimiter, -- cgit v1.2.3-1-g7c22