wekanCreator.js 25 KB

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