trelloCreator.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. import moment from 'moment';
  2. import { TAPi18n } from '/imports/i18n';
  3. const DateString = Match.Where(function(dateAsString) {
  4. check(dateAsString, String);
  5. return moment(dateAsString, moment.ISO_8601).isValid();
  6. });
  7. export class TrelloCreator {
  8. constructor(data) {
  9. // we log current date, to use the same timestamp for all our actions.
  10. // this helps to retrieve all elements performed by the same import.
  11. this._nowDate = new Date();
  12. // The object creation dates, indexed by Trello id
  13. // (so we only parse actions once!)
  14. this.createdAt = {
  15. board: null,
  16. cards: {},
  17. lists: {},
  18. };
  19. // The object creator Trello Id, indexed by the object Trello id
  20. // (so we only parse actions once!)
  21. this.createdBy = {
  22. cards: {}, // only cards have a field for that
  23. };
  24. // Map of labels Trello ID => Wekan ID
  25. this.labels = {};
  26. // Default swimlane
  27. this.swimlane = null;
  28. // Map of lists Trello ID => Wekan ID
  29. this.lists = {};
  30. // Map of cards Trello ID => Wekan ID
  31. this.cards = {};
  32. // Map of attachments Wekan ID => Wekan ID
  33. this.attachmentIds = {};
  34. // Map of checklists Wekan ID => Wekan ID
  35. this.checklists = {};
  36. // The comments, indexed by Trello card id (to map when importing cards)
  37. this.comments = {};
  38. // the members, indexed by Trello member id => Wekan user ID
  39. this.members = data.membersMapping ? data.membersMapping : {};
  40. // maps a trelloCardId to an array of trelloAttachments
  41. this.attachments = {};
  42. this.customFields = {};
  43. }
  44. /**
  45. * If dateString is provided,
  46. * return the Date it represents.
  47. * If not, will return the date when it was first called.
  48. * This is useful for us, as we want all import operations to
  49. * have the exact same date for easier later retrieval.
  50. *
  51. * @param {String} dateString a properly formatted Date
  52. */
  53. _now(dateString) {
  54. if (dateString) {
  55. return new Date(dateString);
  56. }
  57. if (!this._nowDate) {
  58. this._nowDate = new Date();
  59. }
  60. return this._nowDate;
  61. }
  62. /**
  63. * if trelloUserId is provided and we have a mapping,
  64. * return it.
  65. * Otherwise return current logged user.
  66. * @param trelloUserId
  67. * @private
  68. */
  69. _user(trelloUserId) {
  70. if (trelloUserId && this.members[trelloUserId]) {
  71. return this.members[trelloUserId];
  72. }
  73. return Meteor.userId();
  74. }
  75. checkActions(trelloActions) {
  76. check(trelloActions, [
  77. Match.ObjectIncluding({
  78. data: Object,
  79. date: DateString,
  80. type: String,
  81. }),
  82. ]);
  83. // XXX we could perform more thorough checks based on action type
  84. }
  85. checkBoard(trelloBoard) {
  86. check(
  87. trelloBoard,
  88. Match.ObjectIncluding({
  89. // closed: Boolean, // issue #3840, should import closed Trello boards
  90. name: String,
  91. prefs: Match.ObjectIncluding({
  92. // XXX refine control by validating 'background' against a list of
  93. // allowed values (is it worth the maintenance?)
  94. background: String,
  95. permissionLevel: Match.Where(value => {
  96. return ['org', 'private', 'public'].indexOf(value) >= 0;
  97. }),
  98. }),
  99. }),
  100. );
  101. }
  102. checkCards(trelloCards) {
  103. check(trelloCards, [
  104. Match.ObjectIncluding({
  105. closed: Boolean,
  106. dateLastActivity: DateString,
  107. desc: String,
  108. idLabels: [String],
  109. idMembers: [String],
  110. name: String,
  111. pos: Number,
  112. }),
  113. ]);
  114. }
  115. checkLabels(trelloLabels) {
  116. check(trelloLabels, [
  117. Match.ObjectIncluding({
  118. // XXX refine control by validating 'color' against a list of allowed
  119. // values (is it worth the maintenance?)
  120. name: String,
  121. }),
  122. ]);
  123. }
  124. checkLists(trelloLists) {
  125. check(trelloLists, [
  126. Match.ObjectIncluding({
  127. closed: Boolean,
  128. name: String,
  129. }),
  130. ]);
  131. }
  132. checkChecklists(trelloChecklists) {
  133. check(trelloChecklists, [
  134. Match.ObjectIncluding({
  135. idBoard: String,
  136. idCard: String,
  137. name: String,
  138. checkItems: [
  139. Match.ObjectIncluding({
  140. state: String,
  141. name: String,
  142. }),
  143. ],
  144. }),
  145. ]);
  146. }
  147. // You must call parseActions before calling this one.
  148. createBoardAndLabels(trelloBoard) {
  149. let color = 'blue';
  150. if (this.getColor(trelloBoard.prefs.background) !== undefined) {
  151. color = this.getColor(trelloBoard.prefs.background);
  152. }
  153. const boardToCreate = {
  154. archived: trelloBoard.closed,
  155. color: color,
  156. // very old boards won't have a creation activity so no creation date
  157. createdAt: this._now(this.createdAt.board),
  158. labels: [],
  159. customFields: [],
  160. members: [
  161. {
  162. userId: Meteor.userId(),
  163. isAdmin: true,
  164. isActive: true,
  165. isNoComments: false,
  166. isCommentOnly: false,
  167. swimlaneId: false,
  168. },
  169. ],
  170. permission: this.getPermission(trelloBoard.prefs.permissionLevel),
  171. slug: getSlug(trelloBoard.name) || 'board',
  172. stars: 0,
  173. title: Boards.uniqueTitle(trelloBoard.name),
  174. };
  175. // now add other members
  176. if (trelloBoard.memberships) {
  177. trelloBoard.memberships.forEach(trelloMembership => {
  178. const trelloId = trelloMembership.idMember;
  179. // do we have a mapping?
  180. if (this.members[trelloId]) {
  181. const wekanId = this.members[trelloId];
  182. // do we already have it in our list?
  183. const wekanMember = boardToCreate.members.find(
  184. wekanMember => wekanMember.userId === wekanId,
  185. );
  186. if (wekanMember) {
  187. // we're already mapped, but maybe with lower rights
  188. if (!wekanMember.isAdmin) {
  189. wekanMember.isAdmin = this.getAdmin(trelloMembership.memberType);
  190. }
  191. } else {
  192. boardToCreate.members.push({
  193. userId: wekanId,
  194. isAdmin: this.getAdmin(trelloMembership.memberType),
  195. isActive: true,
  196. isNoComments: false,
  197. isCommentOnly: false,
  198. swimlaneId: false,
  199. });
  200. }
  201. }
  202. });
  203. }
  204. if (trelloBoard.labels) {
  205. trelloBoard.labels.forEach(label => {
  206. const labelToCreate = {
  207. _id: Random.id(6),
  208. color: label.color ? label.color : 'black',
  209. name: label.name,
  210. };
  211. // We need to remember them by Trello ID, as this is the only ref we have
  212. // when importing cards.
  213. this.labels[label.id] = labelToCreate._id;
  214. boardToCreate.labels.push(labelToCreate);
  215. });
  216. }
  217. const boardId = Boards.direct.insert(boardToCreate);
  218. Boards.direct.update(boardId, { $set: { modifiedAt: this._now() } });
  219. // log activity
  220. Activities.direct.insert({
  221. activityType: 'importBoard',
  222. boardId,
  223. createdAt: this._now(),
  224. source: {
  225. id: trelloBoard.id,
  226. system: 'Trello',
  227. url: trelloBoard.url,
  228. },
  229. // We attribute the import to current user,
  230. // not the author from the original object.
  231. userId: this._user(),
  232. });
  233. if (trelloBoard.customFields) {
  234. trelloBoard.customFields.forEach(field => {
  235. const fieldToCreate = {
  236. // trelloId: field.id,
  237. name: field.name,
  238. showOnCard: field.display.cardFront,
  239. showLabelOnMiniCard: field.display.cardFront,
  240. automaticallyOnCard: true,
  241. alwaysOnCard: false,
  242. type: field.type,
  243. boardIds: [boardId],
  244. settings: {},
  245. };
  246. if (field.type === 'list') {
  247. fieldToCreate.type = 'dropdown';
  248. fieldToCreate.settings = {
  249. dropdownItems: field.options.map(opt => {
  250. return {
  251. _id: opt.id,
  252. name: opt.value.text,
  253. };
  254. }),
  255. };
  256. }
  257. // We need to remember them by Trello ID, as this is the only ref we have
  258. // when importing cards.
  259. this.customFields[field.id] = CustomFields.direct.insert(fieldToCreate);
  260. });
  261. }
  262. return boardId;
  263. }
  264. /**
  265. * Create the Wekan cards corresponding to the supplied Trello cards,
  266. * as well as all linked data: activities, comments, and attachments
  267. * @param trelloCards
  268. * @param boardId
  269. * @returns {Array}
  270. */
  271. createCards(trelloCards, boardId) {
  272. const result = [];
  273. trelloCards.forEach(card => {
  274. const cardToCreate = {
  275. archived: card.closed,
  276. boardId,
  277. // very old boards won't have a creation activity so no creation date
  278. createdAt: this._now(this.createdAt.cards[card.id]),
  279. dateLastActivity: this._now(),
  280. description: card.desc,
  281. listId: this.lists[card.idList],
  282. swimlaneId: this.swimlane,
  283. sort: card.pos,
  284. title: card.name,
  285. // we attribute the card to its creator if available
  286. userId: this._user(this.createdBy.cards[card.id]),
  287. dueAt: card.due ? this._now(card.due) : null,
  288. };
  289. // add labels
  290. if (card.idLabels) {
  291. cardToCreate.labelIds = card.idLabels.map(trelloId => {
  292. return this.labels[trelloId];
  293. });
  294. }
  295. // add members {
  296. if (card.idMembers) {
  297. const wekanMembers = [];
  298. // we can't just map, as some members may not have been mapped
  299. card.idMembers.forEach(trelloId => {
  300. if (this.members[trelloId]) {
  301. const wekanId = this.members[trelloId];
  302. // we may map multiple Trello members to the same wekan user
  303. // in which case we risk adding the same user multiple times
  304. if (!wekanMembers.find(wId => wId === wekanId)) {
  305. wekanMembers.push(wekanId);
  306. }
  307. }
  308. return true;
  309. });
  310. if (wekanMembers.length > 0) {
  311. cardToCreate.members = wekanMembers;
  312. }
  313. }
  314. // add vote
  315. if (card.idMembersVoted) {
  316. // Trello only know's positive votes
  317. const positiveVotes = [];
  318. card.idMembersVoted.forEach(trelloId => {
  319. if (this.members[trelloId]) {
  320. const wekanId = this.members[trelloId];
  321. // we may map multiple Trello members to the same wekan user
  322. // in which case we risk adding the same user multiple times
  323. if (!positiveVotes.find(wId => wId === wekanId)) {
  324. positiveVotes.push(wekanId);
  325. }
  326. }
  327. return true;
  328. });
  329. if (positiveVotes.length > 0) {
  330. cardToCreate.vote = {
  331. question: cardToCreate.title,
  332. public: true,
  333. positive: positiveVotes,
  334. };
  335. }
  336. }
  337. if (card.customFieldItems) {
  338. cardToCreate.customFields = [];
  339. card.customFieldItems.forEach(item => {
  340. const custom = {
  341. _id: this.customFields[item.idCustomField],
  342. };
  343. if (item.idValue) {
  344. custom.value = item.idValue;
  345. } else if (item.value.hasOwnProperty('checked')) {
  346. custom.value = item.value.checked === 'true';
  347. } else if (item.value.hasOwnProperty('text')) {
  348. custom.value = item.value.text;
  349. } else if (item.value.hasOwnProperty('date')) {
  350. custom.value = item.value.date;
  351. } else if (item.value.hasOwnProperty('number')) {
  352. custom.value = item.value.number;
  353. }
  354. cardToCreate.customFields.push(custom);
  355. });
  356. }
  357. // insert card
  358. const cardId = Cards.direct.insert(cardToCreate);
  359. // keep track of Trello id => Wekan id
  360. this.cards[card.id] = cardId;
  361. // log activity
  362. // Activities.direct.insert({
  363. // activityType: 'importCard',
  364. // boardId,
  365. // cardId,
  366. // createdAt: this._now(),
  367. // listId: cardToCreate.listId,
  368. // source: {
  369. // id: card.id,
  370. // system: 'Trello',
  371. // url: card.url,
  372. // },
  373. // // we attribute the import to current user,
  374. // // not the author of the original card
  375. // userId: this._user(),
  376. // });
  377. // add comments
  378. const comments = this.comments[card.id];
  379. if (comments) {
  380. comments.forEach(comment => {
  381. const commentToCreate = {
  382. boardId,
  383. cardId,
  384. createdAt: this._now(comment.date),
  385. text: comment.data.text,
  386. // we attribute the comment to the original author, default to current user
  387. userId: this._user(comment.idMemberCreator),
  388. };
  389. // dateLastActivity will be set from activity insert, no need to
  390. // update it ourselves
  391. const commentId = CardComments.direct.insert(commentToCreate);
  392. // We need to keep adding comment activities this way with Trello
  393. // because it doesn't provide a comment ID
  394. Activities.direct.insert({
  395. activityType: 'addComment',
  396. boardId: commentToCreate.boardId,
  397. cardId: commentToCreate.cardId,
  398. commentId,
  399. createdAt: this._now(comment.date),
  400. // we attribute the addComment (not the import)
  401. // to the original author - it is needed by some UI elements.
  402. userId: commentToCreate.userId,
  403. });
  404. });
  405. }
  406. const attachments = this.attachments[card.id];
  407. const trelloCoverId = card.idAttachmentCover;
  408. if (attachments && Meteor.isServer) {
  409. attachments.forEach(att => {
  410. const self = this;
  411. const opts = {
  412. type: att.type ? att.type : undefined,
  413. userId: self._user(att.userId),
  414. meta: {
  415. boardId,
  416. cardId,
  417. source: 'import',
  418. },
  419. };
  420. const cb = (error, fileObj) => {
  421. if (error) {
  422. throw error;
  423. }
  424. self.attachmentIds[att._id] = fileObj._id;
  425. if (trelloCoverId === att._id) {
  426. Cards.direct.update(cardId, {
  427. $set: { coverId: fileObj._id },
  428. });
  429. }
  430. };
  431. if (att.url) {
  432. Attachment.load(att.url, opts, cb, true);
  433. } else if (att.file) {
  434. Attachment.write(att.file, opts, cb, true);
  435. }
  436. });
  437. if (links.length) {
  438. let desc = cardToCreate.description.trim();
  439. if (desc) {
  440. desc += '\n\n';
  441. }
  442. desc += `## ${TAPi18n.__('links-heading')}\n`;
  443. links.forEach(link => {
  444. desc += `* ${link}\n`;
  445. });
  446. Cards.direct.update(cardId, {
  447. $set: {
  448. description: desc,
  449. },
  450. });
  451. }
  452. }
  453. result.push(cardId);
  454. });
  455. return result;
  456. }
  457. // Create labels if they do not exist and load this.labels.
  458. createLabels(trelloLabels, board) {
  459. trelloLabels.forEach(label => {
  460. const color = label.color;
  461. const name = label.name;
  462. const existingLabel = board.getLabel(name, color);
  463. if (existingLabel) {
  464. this.labels[label.id] = existingLabel._id;
  465. } else {
  466. const idLabelCreated = board.pushLabel(name, color);
  467. this.labels[label.id] = idLabelCreated;
  468. }
  469. });
  470. }
  471. createLists(trelloLists, boardId) {
  472. trelloLists.forEach(list => {
  473. const listToCreate = {
  474. archived: list.closed,
  475. boardId,
  476. // We are being defensing here by providing a default date (now) if the
  477. // creation date wasn't found on the action log. This happen on old
  478. // Trello boards (eg from 2013) that didn't log the 'createList' action
  479. // we require.
  480. createdAt: this._now(this.createdAt.lists[list.id]),
  481. title: list.name,
  482. sort: list.pos,
  483. };
  484. const listId = Lists.direct.insert(listToCreate);
  485. Lists.direct.update(listId, { $set: { updatedAt: this._now() } });
  486. this.lists[list.id] = listId;
  487. // log activity
  488. // Activities.direct.insert({
  489. // activityType: 'importList',
  490. // boardId,
  491. // createdAt: this._now(),
  492. // listId,
  493. // source: {
  494. // id: list.id,
  495. // system: 'Trello',
  496. // },
  497. // // We attribute the import to current user,
  498. // // not the creator of the original object
  499. // userId: this._user(),
  500. // });
  501. });
  502. }
  503. createSwimlanes(boardId) {
  504. const swimlaneToCreate = {
  505. archived: false,
  506. boardId,
  507. // We are being defensing here by providing a default date (now) if the
  508. // creation date wasn't found on the action log. This happen on old
  509. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  510. // we require.
  511. createdAt: this._now(),
  512. title: 'Default',
  513. sort: 1,
  514. };
  515. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  516. Swimlanes.direct.update(swimlaneId, { $set: { updatedAt: this._now() } });
  517. this.swimlane = swimlaneId;
  518. }
  519. createChecklists(trelloChecklists) {
  520. trelloChecklists.forEach(checklist => {
  521. if (this.cards[checklist.idCard]) {
  522. // Create the checklist
  523. const checklistToCreate = {
  524. cardId: this.cards[checklist.idCard],
  525. title: checklist.name,
  526. createdAt: this._now(),
  527. sort: checklist.pos,
  528. };
  529. const checklistId = Checklists.direct.insert(checklistToCreate);
  530. // keep track of Trello id => Wekan id
  531. this.checklists[checklist.id] = checklistId;
  532. // Now add the items to the checklistItems
  533. let counter = 0;
  534. checklist.checkItems.forEach(item => {
  535. counter++;
  536. const checklistItemTocreate = {
  537. _id: checklistId + counter,
  538. title: item.name,
  539. checklistId: this.checklists[checklist.id],
  540. cardId: this.cards[checklist.idCard],
  541. sort: item.pos,
  542. isFinished: item.state === 'complete',
  543. };
  544. ChecklistItems.direct.insert(checklistItemTocreate);
  545. });
  546. }
  547. });
  548. }
  549. getAdmin(trelloMemberType) {
  550. return trelloMemberType === 'admin';
  551. }
  552. getColor(trelloColorCode) {
  553. // trello color name => wekan color
  554. const mapColors = {
  555. blue: 'belize',
  556. orange: 'pumpkin',
  557. green: 'nephritis',
  558. red: 'pomegranate',
  559. purple: 'wisteria',
  560. pink: 'moderatepink',
  561. lime: 'limegreen',
  562. sky: 'strongcyan',
  563. grey: 'midnight',
  564. };
  565. const wekanColor = mapColors[trelloColorCode];
  566. return wekanColor || Boards.simpleSchema()._schema.color.allowedValues[0];
  567. }
  568. getPermission(trelloPermissionCode) {
  569. if (trelloPermissionCode === 'public') {
  570. return 'public';
  571. }
  572. // Wekan does NOT have organization level, so we default both 'private' and
  573. // 'org' to private.
  574. return 'private';
  575. }
  576. parseActions(trelloActions) {
  577. trelloActions.forEach(action => {
  578. if (action.type === 'addAttachmentToCard') {
  579. // We have to be cautious, because the attachment could have been removed later.
  580. // In that case Trello still reports its addition, but removes its 'url' field.
  581. // So we test for that
  582. const trelloAttachment = action.data.attachment;
  583. // We need the idMemberCreator
  584. trelloAttachment.idMemberCreator = action.idMemberCreator;
  585. if (trelloAttachment.url) {
  586. // we cannot actually create the Wekan attachment, because we don't yet
  587. // have the cards to attach it to, so we store it in the instance variable.
  588. const trelloCardId = action.data.card.id;
  589. if (!this.attachments[trelloCardId]) {
  590. this.attachments[trelloCardId] = [];
  591. }
  592. this.attachments[trelloCardId].push(trelloAttachment);
  593. }
  594. } else if (action.type === 'commentCard') {
  595. const id = action.data.card.id;
  596. if (this.comments[id]) {
  597. this.comments[id].push(action);
  598. } else {
  599. this.comments[id] = [action];
  600. }
  601. } else if (action.type === 'createBoard') {
  602. this.createdAt.board = action.date;
  603. } else if (action.type === 'createCard') {
  604. const cardId = action.data.card.id;
  605. this.createdAt.cards[cardId] = action.date;
  606. this.createdBy.cards[cardId] = action.idMemberCreator;
  607. } else if (action.type === 'createList') {
  608. const listId = action.data.list.id;
  609. this.createdAt.lists[listId] = action.date;
  610. }
  611. });
  612. }
  613. importActions(actions, boardId) {
  614. actions.forEach(action => {
  615. switch (action.type) {
  616. // Board related actions
  617. // TODO: addBoardMember, removeBoardMember
  618. case 'createBoard': {
  619. Activities.direct.insert({
  620. userId: this._user(action.idMemberCreator),
  621. type: 'board',
  622. activityTypeId: boardId,
  623. activityType: 'createBoard',
  624. boardId,
  625. createdAt: this._now(action.date),
  626. });
  627. break;
  628. }
  629. // List related activities
  630. // TODO: removeList, archivedList
  631. case 'createList': {
  632. Activities.direct.insert({
  633. userId: this._user(action.idMemberCreator),
  634. type: 'list',
  635. activityType: 'createList',
  636. listId: this.lists[action.data.list.id],
  637. boardId,
  638. createdAt: this._now(action.date),
  639. });
  640. break;
  641. }
  642. // Card related activities
  643. // TODO: archivedCard, restoredCard, joinMember, unjoinMember
  644. case 'createCard': {
  645. Activities.direct.insert({
  646. userId: this._user(action.idMemberCreator),
  647. activityType: 'createCard',
  648. listId: this.lists[action.data.list.id],
  649. cardId: this.cards[action.data.card.id],
  650. boardId,
  651. createdAt: this._now(action.date),
  652. });
  653. break;
  654. }
  655. case 'updateCard': {
  656. if (action.data.old.idList) {
  657. Activities.direct.insert({
  658. userId: this._user(action.idMemberCreator),
  659. oldListId: this.lists[action.data.old.idList],
  660. activityType: 'moveCard',
  661. listId: this.lists[action.data.listAfter.id],
  662. cardId: this.cards[action.data.card.id],
  663. boardId,
  664. createdAt: this._now(action.date),
  665. });
  666. }
  667. break;
  668. }
  669. // Comment related activities
  670. // Trello doesn't export the comment id
  671. // Attachment related activities
  672. case 'addAttachmentToCard': {
  673. Activities.direct.insert({
  674. userId: this._user(action.idMemberCreator),
  675. type: 'card',
  676. activityType: 'addAttachment',
  677. attachmentId: this.attachmentIds[action.data.attachment.id],
  678. cardId: this.cards[action.data.card.id],
  679. boardId,
  680. createdAt: this._now(action.date),
  681. });
  682. break;
  683. }
  684. // Checklist related activities
  685. case 'addChecklistToCard': {
  686. Activities.direct.insert({
  687. userId: this._user(action.idMemberCreator),
  688. activityType: 'addChecklist',
  689. cardId: this.cards[action.data.card.id],
  690. checklistId: this.checklists[action.data.checklist.id],
  691. boardId,
  692. createdAt: this._now(action.date),
  693. });
  694. break;
  695. }
  696. }
  697. // Trello doesn't have an add checklist item action
  698. });
  699. }
  700. check(board) {
  701. try {
  702. // check(data, {
  703. // membersMapping: Match.Optional(Object),
  704. // });
  705. this.checkActions(board.actions);
  706. this.checkBoard(board);
  707. this.checkLabels(board.labels);
  708. this.checkLists(board.lists);
  709. this.checkCards(board.cards);
  710. this.checkChecklists(board.checklists);
  711. } catch (e) {
  712. throw new Meteor.Error('error-json-schema');
  713. }
  714. }
  715. create(board, currentBoardId) {
  716. // TODO : Make isSandstorm variable global
  717. const isSandstorm =
  718. Meteor.settings &&
  719. Meteor.settings.public &&
  720. Meteor.settings.public.sandstorm;
  721. if (isSandstorm && currentBoardId) {
  722. const currentBoard = Boards.findOne(currentBoardId);
  723. currentBoard.archive();
  724. }
  725. this.parseActions(board.actions);
  726. const boardId = this.createBoardAndLabels(board);
  727. this.createLists(board.lists, boardId);
  728. this.createSwimlanes(boardId);
  729. this.createCards(board.cards, boardId);
  730. this.createChecklists(board.checklists);
  731. this.importActions(board.actions, boardId);
  732. // XXX add members
  733. return boardId;
  734. }
  735. }