trelloCreator.js 24 KB

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