trelloCreator.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. // Simulating file.attachData on the client generates multiple errors
  409. // - HEAD returns null, which causes exception down the line
  410. // - the template then tries to display the url to the attachment which causes other errors
  411. // so we make it server only, and let UI catch up once it is done, forget about latency comp.
  412. if (attachments && Meteor.isServer) {
  413. attachments.forEach(att => {
  414. // Simulating file.attachData on the client generates multiple errors
  415. // - HEAD returns null, which causes exception down the line
  416. // - the template then tries to display the url to the attachment which causes other errors
  417. // so we make it server only, and let UI catch up once it is done, forget about latency comp.
  418. const self = this;
  419. if (att.url) {
  420. Attachment.load(att.url, (error, fileObj) => {
  421. if (error) {
  422. throw error;
  423. }
  424. fileObj.boardId = boardId;
  425. fileObj.cardId = cardId;
  426. fileObj.userId = self._user(att.userId);
  427. // The field source will only be used to prevent adding
  428. // attachments' related activities automatically
  429. fileObj.source = 'import';
  430. self.attachmentIds[att._id] = fileObj._id;
  431. if (trelloCoverId === att.id) {
  432. Cards.direct.update(cardId, {
  433. $set: { coverId: fileObj._id },
  434. });
  435. }
  436. });
  437. }
  438. });
  439. if (links.length) {
  440. let desc = cardToCreate.description.trim();
  441. if (desc) {
  442. desc += '\n\n';
  443. }
  444. desc += `## ${TAPi18n.__('links-heading')}\n`;
  445. links.forEach(link => {
  446. desc += `* ${link}\n`;
  447. });
  448. Cards.direct.update(cardId, {
  449. $set: {
  450. description: desc,
  451. },
  452. });
  453. }
  454. }
  455. result.push(cardId);
  456. });
  457. return result;
  458. }
  459. // Create labels if they do not exist and load this.labels.
  460. createLabels(trelloLabels, board) {
  461. trelloLabels.forEach(label => {
  462. const color = label.color;
  463. const name = label.name;
  464. const existingLabel = board.getLabel(name, color);
  465. if (existingLabel) {
  466. this.labels[label.id] = existingLabel._id;
  467. } else {
  468. const idLabelCreated = board.pushLabel(name, color);
  469. this.labels[label.id] = idLabelCreated;
  470. }
  471. });
  472. }
  473. createLists(trelloLists, boardId) {
  474. trelloLists.forEach(list => {
  475. const listToCreate = {
  476. archived: list.closed,
  477. boardId,
  478. // We are being defensing here by providing a default date (now) if the
  479. // creation date wasn't found on the action log. This happen on old
  480. // Trello boards (eg from 2013) that didn't log the 'createList' action
  481. // we require.
  482. createdAt: this._now(this.createdAt.lists[list.id]),
  483. title: list.name,
  484. sort: list.pos,
  485. };
  486. const listId = Lists.direct.insert(listToCreate);
  487. Lists.direct.update(listId, { $set: { updatedAt: this._now() } });
  488. this.lists[list.id] = listId;
  489. // log activity
  490. // Activities.direct.insert({
  491. // activityType: 'importList',
  492. // boardId,
  493. // createdAt: this._now(),
  494. // listId,
  495. // source: {
  496. // id: list.id,
  497. // system: 'Trello',
  498. // },
  499. // // We attribute the import to current user,
  500. // // not the creator of the original object
  501. // userId: this._user(),
  502. // });
  503. });
  504. }
  505. createSwimlanes(boardId) {
  506. const swimlaneToCreate = {
  507. archived: false,
  508. boardId,
  509. // We are being defensing here by providing a default date (now) if the
  510. // creation date wasn't found on the action log. This happen on old
  511. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  512. // we require.
  513. createdAt: this._now(),
  514. title: 'Default',
  515. sort: 1,
  516. };
  517. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  518. Swimlanes.direct.update(swimlaneId, { $set: { updatedAt: this._now() } });
  519. this.swimlane = swimlaneId;
  520. }
  521. createChecklists(trelloChecklists) {
  522. trelloChecklists.forEach(checklist => {
  523. if (this.cards[checklist.idCard]) {
  524. // Create the checklist
  525. const checklistToCreate = {
  526. cardId: this.cards[checklist.idCard],
  527. title: checklist.name,
  528. createdAt: this._now(),
  529. sort: checklist.pos,
  530. };
  531. const checklistId = Checklists.direct.insert(checklistToCreate);
  532. // keep track of Trello id => Wekan id
  533. this.checklists[checklist.id] = checklistId;
  534. // Now add the items to the checklistItems
  535. let counter = 0;
  536. checklist.checkItems.forEach(item => {
  537. counter++;
  538. const checklistItemTocreate = {
  539. _id: checklistId + counter,
  540. title: item.name,
  541. checklistId: this.checklists[checklist.id],
  542. cardId: this.cards[checklist.idCard],
  543. sort: item.pos,
  544. isFinished: item.state === 'complete',
  545. };
  546. ChecklistItems.direct.insert(checklistItemTocreate);
  547. });
  548. }
  549. });
  550. }
  551. getAdmin(trelloMemberType) {
  552. return trelloMemberType === 'admin';
  553. }
  554. getColor(trelloColorCode) {
  555. // trello color name => wekan color
  556. const mapColors = {
  557. blue: 'belize',
  558. orange: 'pumpkin',
  559. green: 'nephritis',
  560. red: 'pomegranate',
  561. purple: 'wisteria',
  562. pink: 'moderatepink',
  563. lime: 'limegreen',
  564. sky: 'strongcyan',
  565. grey: 'midnight',
  566. };
  567. const wekanColor = mapColors[trelloColorCode];
  568. return wekanColor || Boards.simpleSchema()._schema.color.allowedValues[0];
  569. }
  570. getPermission(trelloPermissionCode) {
  571. if (trelloPermissionCode === 'public') {
  572. return 'public';
  573. }
  574. // Wekan does NOT have organization level, so we default both 'private' and
  575. // 'org' to private.
  576. return 'private';
  577. }
  578. parseActions(trelloActions) {
  579. trelloActions.forEach(action => {
  580. if (action.type === 'addAttachmentToCard') {
  581. // We have to be cautious, because the attachment could have been removed later.
  582. // In that case Trello still reports its addition, but removes its 'url' field.
  583. // So we test for that
  584. const trelloAttachment = action.data.attachment;
  585. // We need the idMemberCreator
  586. trelloAttachment.idMemberCreator = action.idMemberCreator;
  587. if (trelloAttachment.url) {
  588. // we cannot actually create the Wekan attachment, because we don't yet
  589. // have the cards to attach it to, so we store it in the instance variable.
  590. const trelloCardId = action.data.card.id;
  591. if (!this.attachments[trelloCardId]) {
  592. this.attachments[trelloCardId] = [];
  593. }
  594. this.attachments[trelloCardId].push(trelloAttachment);
  595. }
  596. } else if (action.type === 'commentCard') {
  597. const id = action.data.card.id;
  598. if (this.comments[id]) {
  599. this.comments[id].push(action);
  600. } else {
  601. this.comments[id] = [action];
  602. }
  603. } else if (action.type === 'createBoard') {
  604. this.createdAt.board = action.date;
  605. } else if (action.type === 'createCard') {
  606. const cardId = action.data.card.id;
  607. this.createdAt.cards[cardId] = action.date;
  608. this.createdBy.cards[cardId] = action.idMemberCreator;
  609. } else if (action.type === 'createList') {
  610. const listId = action.data.list.id;
  611. this.createdAt.lists[listId] = action.date;
  612. }
  613. });
  614. }
  615. importActions(actions, boardId) {
  616. actions.forEach(action => {
  617. switch (action.type) {
  618. // Board related actions
  619. // TODO: addBoardMember, removeBoardMember
  620. case 'createBoard': {
  621. Activities.direct.insert({
  622. userId: this._user(action.idMemberCreator),
  623. type: 'board',
  624. activityTypeId: boardId,
  625. activityType: 'createBoard',
  626. boardId,
  627. createdAt: this._now(action.date),
  628. });
  629. break;
  630. }
  631. // List related activities
  632. // TODO: removeList, archivedList
  633. case 'createList': {
  634. Activities.direct.insert({
  635. userId: this._user(action.idMemberCreator),
  636. type: 'list',
  637. activityType: 'createList',
  638. listId: this.lists[action.data.list.id],
  639. boardId,
  640. createdAt: this._now(action.date),
  641. });
  642. break;
  643. }
  644. // Card related activities
  645. // TODO: archivedCard, restoredCard, joinMember, unjoinMember
  646. case 'createCard': {
  647. Activities.direct.insert({
  648. userId: this._user(action.idMemberCreator),
  649. activityType: 'createCard',
  650. listId: this.lists[action.data.list.id],
  651. cardId: this.cards[action.data.card.id],
  652. boardId,
  653. createdAt: this._now(action.date),
  654. });
  655. break;
  656. }
  657. case 'updateCard': {
  658. if (action.data.old.idList) {
  659. Activities.direct.insert({
  660. userId: this._user(action.idMemberCreator),
  661. oldListId: this.lists[action.data.old.idList],
  662. activityType: 'moveCard',
  663. listId: this.lists[action.data.listAfter.id],
  664. cardId: this.cards[action.data.card.id],
  665. boardId,
  666. createdAt: this._now(action.date),
  667. });
  668. }
  669. break;
  670. }
  671. // Comment related activities
  672. // Trello doesn't export the comment id
  673. // Attachment related activities
  674. case 'addAttachmentToCard': {
  675. Activities.direct.insert({
  676. userId: this._user(action.idMemberCreator),
  677. type: 'card',
  678. activityType: 'addAttachment',
  679. attachmentId: this.attachmentIds[action.data.attachment.id],
  680. cardId: this.cards[action.data.card.id],
  681. boardId,
  682. createdAt: this._now(action.date),
  683. });
  684. break;
  685. }
  686. // Checklist related activities
  687. case 'addChecklistToCard': {
  688. Activities.direct.insert({
  689. userId: this._user(action.idMemberCreator),
  690. activityType: 'addChecklist',
  691. cardId: this.cards[action.data.card.id],
  692. checklistId: this.checklists[action.data.checklist.id],
  693. boardId,
  694. createdAt: this._now(action.date),
  695. });
  696. break;
  697. }
  698. }
  699. // Trello doesn't have an add checklist item action
  700. });
  701. }
  702. check(board) {
  703. try {
  704. // check(data, {
  705. // membersMapping: Match.Optional(Object),
  706. // });
  707. this.checkActions(board.actions);
  708. this.checkBoard(board);
  709. this.checkLabels(board.labels);
  710. this.checkLists(board.lists);
  711. this.checkCards(board.cards);
  712. this.checkChecklists(board.checklists);
  713. } catch (e) {
  714. throw new Meteor.Error('error-json-schema');
  715. }
  716. }
  717. create(board, currentBoardId) {
  718. // TODO : Make isSandstorm variable global
  719. const isSandstorm =
  720. Meteor.settings &&
  721. Meteor.settings.public &&
  722. Meteor.settings.public.sandstorm;
  723. if (isSandstorm && currentBoardId) {
  724. const currentBoard = Boards.findOne(currentBoardId);
  725. currentBoard.archive();
  726. }
  727. this.parseActions(board.actions);
  728. const boardId = this.createBoardAndLabels(board);
  729. this.createLists(board.lists, boardId);
  730. this.createSwimlanes(boardId);
  731. this.createCards(board.cards, boardId);
  732. this.createChecklists(board.checklists);
  733. this.importActions(board.actions, boardId);
  734. // XXX add members
  735. return boardId;
  736. }
  737. }