trelloCreator.js 25 KB

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