trelloCreator.js 25 KB

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