trelloCreator.js 25 KB

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