trelloCreator.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import moment from 'moment/min/moment-with-locales';
  3. import { TAPi18n } from '/imports/i18n';
  4. const DateString = Match.Where(function(dateAsString) {
  5. check(dateAsString, String);
  6. return moment(dateAsString, moment.ISO_8601).isValid();
  7. });
  8. export class TrelloCreator {
  9. constructor(data) {
  10. // we log current date, to use the same timestamp for all our actions.
  11. // this helps to retrieve all elements performed by the same import.
  12. this._nowDate = new Date();
  13. // The object creation dates, indexed by Trello id
  14. // (so we only parse actions once!)
  15. this.createdAt = {
  16. board: null,
  17. cards: {},
  18. lists: {},
  19. };
  20. // The object creator Trello Id, indexed by the object Trello id
  21. // (so we only parse actions once!)
  22. this.createdBy = {
  23. cards: {}, // only cards have a field for that
  24. };
  25. // Map of labels Trello ID => Wekan ID
  26. this.labels = {};
  27. // Default swimlane
  28. this.swimlane = null;
  29. // Map of lists Trello ID => Wekan ID
  30. this.lists = {};
  31. // Map of cards Trello ID => Wekan ID
  32. this.cards = {};
  33. // Map of attachments Wekan ID => Wekan ID
  34. this.attachmentIds = {};
  35. // Map of checklists Wekan ID => Wekan ID
  36. this.checklists = {};
  37. // The comments, indexed by Trello card id (to map when importing cards)
  38. this.comments = {};
  39. // the members, indexed by Trello member id => Wekan user ID
  40. this.members = data.membersMapping ? data.membersMapping : {};
  41. // maps a trelloCardId to an array of trelloAttachments
  42. this.attachments = {};
  43. this.customFields = {};
  44. }
  45. /**
  46. * If dateString is provided,
  47. * return the Date it represents.
  48. * If not, will return the date when it was first called.
  49. * This is useful for us, as we want all import operations to
  50. * have the exact same date for easier later retrieval.
  51. *
  52. * @param {String} dateString a properly formatted Date
  53. */
  54. _now(dateString) {
  55. if (dateString) {
  56. return new Date(dateString);
  57. }
  58. if (!this._nowDate) {
  59. this._nowDate = new Date();
  60. }
  61. return this._nowDate;
  62. }
  63. /**
  64. * if trelloUserId is provided and we have a mapping,
  65. * return it.
  66. * Otherwise return current logged user.
  67. * @param trelloUserId
  68. * @private
  69. */
  70. _user(trelloUserId) {
  71. if (trelloUserId && this.members[trelloUserId]) {
  72. return this.members[trelloUserId];
  73. }
  74. return Meteor.userId();
  75. }
  76. checkActions(trelloActions) {
  77. check(trelloActions, [
  78. Match.ObjectIncluding({
  79. data: Object,
  80. date: DateString,
  81. type: String,
  82. }),
  83. ]);
  84. // XXX we could perform more thorough checks based on action type
  85. }
  86. checkBoard(trelloBoard) {
  87. check(
  88. trelloBoard,
  89. Match.ObjectIncluding({
  90. // closed: Boolean, // issue #3840, should import closed Trello boards
  91. name: String,
  92. prefs: Match.ObjectIncluding({
  93. // XXX refine control by validating 'background' against a list of
  94. // allowed values (is it worth the maintenance?)
  95. background: String,
  96. permissionLevel: Match.Where(value => {
  97. return ['org', 'private', 'public'].indexOf(value) >= 0;
  98. }),
  99. }),
  100. }),
  101. );
  102. }
  103. checkCards(trelloCards) {
  104. check(trelloCards, [
  105. Match.ObjectIncluding({
  106. closed: Boolean,
  107. dateLastActivity: DateString,
  108. desc: String,
  109. idLabels: [String],
  110. idMembers: [String],
  111. name: String,
  112. pos: Number,
  113. }),
  114. ]);
  115. }
  116. checkLabels(trelloLabels) {
  117. check(trelloLabels, [
  118. Match.ObjectIncluding({
  119. // XXX refine control by validating 'color' against a list of allowed
  120. // values (is it worth the maintenance?)
  121. name: String,
  122. }),
  123. ]);
  124. }
  125. checkLists(trelloLists) {
  126. check(trelloLists, [
  127. Match.ObjectIncluding({
  128. closed: Boolean,
  129. name: String,
  130. }),
  131. ]);
  132. }
  133. checkChecklists(trelloChecklists) {
  134. check(trelloChecklists, [
  135. Match.ObjectIncluding({
  136. idBoard: String,
  137. idCard: String,
  138. name: String,
  139. checkItems: [
  140. Match.ObjectIncluding({
  141. state: String,
  142. name: String,
  143. }),
  144. ],
  145. }),
  146. ]);
  147. }
  148. // You must call parseActions before calling this one.
  149. createBoardAndLabels(trelloBoard) {
  150. let color = 'blue';
  151. if (this.getColor(trelloBoard.prefs.background) !== undefined) {
  152. color = this.getColor(trelloBoard.prefs.background);
  153. }
  154. const boardToCreate = {
  155. archived: trelloBoard.closed,
  156. color: color,
  157. // very old boards won't have a creation activity so no creation date
  158. createdAt: this._now(this.createdAt.board),
  159. labels: [],
  160. customFields: [],
  161. members: [
  162. {
  163. userId: Meteor.userId(),
  164. isAdmin: true,
  165. isActive: true,
  166. isNoComments: false,
  167. isCommentOnly: false,
  168. swimlaneId: false,
  169. },
  170. ],
  171. permission: this.getPermission(trelloBoard.prefs.permissionLevel),
  172. slug: getSlug(trelloBoard.name) || 'board',
  173. stars: 0,
  174. title: Boards.uniqueTitle(trelloBoard.name),
  175. };
  176. // now add other members
  177. if (trelloBoard.memberships) {
  178. trelloBoard.memberships.forEach(trelloMembership => {
  179. const trelloId = trelloMembership.idMember;
  180. // do we have a mapping?
  181. if (this.members[trelloId]) {
  182. const wekanId = this.members[trelloId];
  183. // do we already have it in our list?
  184. const wekanMember = boardToCreate.members.find(
  185. wekanMember => wekanMember.userId === wekanId,
  186. );
  187. if (wekanMember) {
  188. // we're already mapped, but maybe with lower rights
  189. if (!wekanMember.isAdmin) {
  190. wekanMember.isAdmin = this.getAdmin(trelloMembership.memberType);
  191. }
  192. } else {
  193. boardToCreate.members.push({
  194. userId: wekanId,
  195. isAdmin: this.getAdmin(trelloMembership.memberType),
  196. isActive: true,
  197. isNoComments: false,
  198. isCommentOnly: false,
  199. swimlaneId: false,
  200. });
  201. }
  202. }
  203. });
  204. }
  205. if (trelloBoard.labels) {
  206. trelloBoard.labels.forEach(label => {
  207. const labelToCreate = {
  208. _id: Random.id(6),
  209. color: label.color ? label.color : 'black',
  210. name: label.name,
  211. };
  212. // We need to remember them by Trello ID, as this is the only ref we have
  213. // when importing cards.
  214. this.labels[label.id] = labelToCreate._id;
  215. boardToCreate.labels.push(labelToCreate);
  216. });
  217. }
  218. const boardId = Boards.direct.insert(boardToCreate);
  219. Boards.direct.update(boardId, { $set: { modifiedAt: this._now() } });
  220. // log activity
  221. Activities.direct.insert({
  222. activityType: 'importBoard',
  223. boardId,
  224. createdAt: this._now(),
  225. source: {
  226. id: trelloBoard.id,
  227. system: 'Trello',
  228. url: trelloBoard.url,
  229. },
  230. // We attribute the import to current user,
  231. // not the author from the original object.
  232. userId: this._user(),
  233. });
  234. if (trelloBoard.customFields) {
  235. trelloBoard.customFields.forEach(field => {
  236. const fieldToCreate = {
  237. // trelloId: field.id,
  238. name: field.name,
  239. showOnCard: field.display.cardFront,
  240. showLabelOnMiniCard: field.display.cardFront,
  241. automaticallyOnCard: true,
  242. alwaysOnCard: false,
  243. type: field.type,
  244. boardIds: [boardId],
  245. settings: {},
  246. };
  247. if (field.type === 'list') {
  248. fieldToCreate.type = 'dropdown';
  249. fieldToCreate.settings = {
  250. dropdownItems: field.options.map(opt => {
  251. return {
  252. _id: opt.id,
  253. name: opt.value.text,
  254. };
  255. }),
  256. };
  257. }
  258. // We need to remember them by Trello ID, as this is the only ref we have
  259. // when importing cards.
  260. this.customFields[field.id] = CustomFields.direct.insert(fieldToCreate);
  261. });
  262. }
  263. return boardId;
  264. }
  265. /**
  266. * Create the Wekan cards corresponding to the supplied Trello cards,
  267. * as well as all linked data: activities, comments, and attachments
  268. * @param trelloCards
  269. * @param boardId
  270. * @returns {Array}
  271. */
  272. createCards(trelloCards, boardId) {
  273. const result = [];
  274. trelloCards.forEach(card => {
  275. const cardToCreate = {
  276. archived: card.closed,
  277. boardId,
  278. // very old boards won't have a creation activity so no creation date
  279. createdAt: this._now(this.createdAt.cards[card.id]),
  280. dateLastActivity: this._now(),
  281. description: card.desc,
  282. listId: this.lists[card.idList],
  283. swimlaneId: this.swimlane,
  284. sort: card.pos,
  285. title: card.name,
  286. // we attribute the card to its creator if available
  287. userId: this._user(this.createdBy.cards[card.id]),
  288. dueAt: card.due ? this._now(card.due) : null,
  289. };
  290. // add labels
  291. if (card.idLabels) {
  292. cardToCreate.labelIds = card.idLabels.map(trelloId => {
  293. return this.labels[trelloId];
  294. });
  295. }
  296. // add members {
  297. if (card.idMembers) {
  298. const wekanMembers = [];
  299. // we can't just map, as some members may not have been mapped
  300. card.idMembers.forEach(trelloId => {
  301. if (this.members[trelloId]) {
  302. const wekanId = this.members[trelloId];
  303. // we may map multiple Trello members to the same wekan user
  304. // in which case we risk adding the same user multiple times
  305. if (!wekanMembers.find(wId => wId === wekanId)) {
  306. wekanMembers.push(wekanId);
  307. }
  308. }
  309. return true;
  310. });
  311. if (wekanMembers.length > 0) {
  312. cardToCreate.members = wekanMembers;
  313. }
  314. }
  315. // add vote
  316. if (card.idMembersVoted) {
  317. // Trello only know's positive votes
  318. const positiveVotes = [];
  319. card.idMembersVoted.forEach(trelloId => {
  320. if (this.members[trelloId]) {
  321. const wekanId = this.members[trelloId];
  322. // we may map multiple Trello members to the same wekan user
  323. // in which case we risk adding the same user multiple times
  324. if (!positiveVotes.find(wId => wId === wekanId)) {
  325. positiveVotes.push(wekanId);
  326. }
  327. }
  328. return true;
  329. });
  330. if (positiveVotes.length > 0) {
  331. cardToCreate.vote = {
  332. question: cardToCreate.title,
  333. public: true,
  334. positive: positiveVotes,
  335. };
  336. }
  337. }
  338. if (card.customFieldItems) {
  339. cardToCreate.customFields = [];
  340. card.customFieldItems.forEach(item => {
  341. const custom = {
  342. _id: this.customFields[item.idCustomField],
  343. };
  344. if (item.idValue) {
  345. custom.value = item.idValue;
  346. } else if (item.value.hasOwnProperty('checked')) {
  347. custom.value = item.value.checked === 'true';
  348. } else if (item.value.hasOwnProperty('text')) {
  349. custom.value = item.value.text;
  350. } else if (item.value.hasOwnProperty('date')) {
  351. custom.value = item.value.date;
  352. } else if (item.value.hasOwnProperty('number')) {
  353. custom.value = item.value.number;
  354. }
  355. cardToCreate.customFields.push(custom);
  356. });
  357. }
  358. // insert card
  359. const cardId = Cards.direct.insert(cardToCreate);
  360. // keep track of Trello id => Wekan id
  361. this.cards[card.id] = cardId;
  362. // log activity
  363. // Activities.direct.insert({
  364. // activityType: 'importCard',
  365. // boardId,
  366. // cardId,
  367. // createdAt: this._now(),
  368. // listId: cardToCreate.listId,
  369. // source: {
  370. // id: card.id,
  371. // system: 'Trello',
  372. // url: card.url,
  373. // },
  374. // // we attribute the import to current user,
  375. // // not the author of the original card
  376. // userId: this._user(),
  377. // });
  378. // add comments
  379. const comments = this.comments[card.id];
  380. if (comments) {
  381. comments.forEach(comment => {
  382. const commentToCreate = {
  383. boardId,
  384. cardId,
  385. createdAt: this._now(comment.date),
  386. text: comment.data.text,
  387. // we attribute the comment to the original author, default to current user
  388. userId: this._user(comment.idMemberCreator),
  389. };
  390. // dateLastActivity will be set from activity insert, no need to
  391. // update it ourselves
  392. const commentId = CardComments.direct.insert(commentToCreate);
  393. // We need to keep adding comment activities this way with Trello
  394. // because it doesn't provide a comment ID
  395. Activities.direct.insert({
  396. activityType: 'addComment',
  397. boardId: commentToCreate.boardId,
  398. cardId: commentToCreate.cardId,
  399. commentId,
  400. createdAt: this._now(comment.date),
  401. // we attribute the addComment (not the import)
  402. // to the original author - it is needed by some UI elements.
  403. userId: commentToCreate.userId,
  404. });
  405. });
  406. }
  407. const attachments = this.attachments[card.id];
  408. const trelloCoverId = card.idAttachmentCover;
  409. if (attachments && Meteor.isServer) {
  410. attachments.forEach(att => {
  411. const self = this;
  412. const opts = {
  413. type: att.type ? att.type : undefined,
  414. userId: self._user(att.userId),
  415. meta: {
  416. boardId,
  417. cardId,
  418. source: 'import',
  419. },
  420. };
  421. const cb = (error, fileObj) => {
  422. if (error) {
  423. throw error;
  424. }
  425. self.attachmentIds[att._id] = fileObj._id;
  426. if (trelloCoverId === att._id) {
  427. Cards.direct.update(cardId, {
  428. $set: { coverId: fileObj._id },
  429. });
  430. }
  431. };
  432. if (att.url) {
  433. Attachments.load(att.url, opts, cb, true);
  434. } else if (att.file) {
  435. Attachments.insert(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 = ReactiveCache.getBoard(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. }