wekanCreator.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. // set color
  471. if (swimlane.color) {
  472. swimlaneToCreate.color = swimlane.color;
  473. }
  474. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  475. Swimlanes.direct.update(swimlaneId, {
  476. $set: {
  477. 'updatedAt': this._now(),
  478. },
  479. });
  480. this.swimlanes[swimlane._id] = swimlaneId;
  481. });
  482. }
  483. createChecklists(wekanChecklists) {
  484. const result = [];
  485. wekanChecklists.forEach((checklist, checklistIndex) => {
  486. // Create the checklist
  487. const checklistToCreate = {
  488. cardId: this.cards[checklist.cardId],
  489. title: checklist.title,
  490. createdAt: checklist.createdAt,
  491. sort: checklist.sort ? checklist.sort : checklistIndex,
  492. };
  493. const checklistId = Checklists.direct.insert(checklistToCreate);
  494. this.checklists[checklist._id] = checklistId;
  495. result.push(checklistId);
  496. });
  497. return result;
  498. }
  499. createTriggers(wekanTriggers, boardId) {
  500. wekanTriggers.forEach((trigger) => {
  501. if (trigger.hasOwnProperty('labelId')) {
  502. trigger.labelId = this.labels[trigger.labelId];
  503. }
  504. if (trigger.hasOwnProperty('memberId')) {
  505. trigger.memberId = this.members[trigger.memberId];
  506. }
  507. trigger.boardId = boardId;
  508. const oldId = trigger._id;
  509. delete trigger._id;
  510. this.triggers[oldId] = Triggers.direct.insert(trigger);
  511. });
  512. }
  513. createActions(wekanActions, boardId) {
  514. wekanActions.forEach((action) => {
  515. if (action.hasOwnProperty('labelId')) {
  516. action.labelId = this.labels[action.labelId];
  517. }
  518. if (action.hasOwnProperty('memberId')) {
  519. action.memberId = this.members[action.memberId];
  520. }
  521. action.boardId = boardId;
  522. const oldId = action._id;
  523. delete action._id;
  524. this.actions[oldId] = Actions.direct.insert(action);
  525. });
  526. }
  527. createRules(wekanRules, boardId) {
  528. wekanRules.forEach((rule) => {
  529. // Create the rule
  530. rule.boardId = boardId;
  531. rule.triggerId = this.triggers[rule.triggerId];
  532. rule.actionId = this.actions[rule.actionId];
  533. delete rule._id;
  534. Rules.direct.insert(rule);
  535. });
  536. }
  537. createChecklistItems(wekanChecklistItems) {
  538. wekanChecklistItems.forEach((checklistitem, checklistitemIndex) => {
  539. // Create the checklistItem
  540. const checklistItemTocreate = {
  541. title: checklistitem.title,
  542. checklistId: this.checklists[checklistitem.checklistId],
  543. cardId: this.cards[checklistitem.cardId],
  544. sort: checklistitem.sort ? checklistitem.sort : checklistitemIndex,
  545. isFinished: checklistitem.isFinished,
  546. };
  547. const checklistItemId = ChecklistItems.direct.insert(checklistItemTocreate);
  548. this.checklistItems[checklistitem._id] = checklistItemId;
  549. });
  550. }
  551. parseActivities(wekanBoard) {
  552. wekanBoard.activities.forEach((activity) => {
  553. switch (activity.activityType) {
  554. case 'addAttachment':
  555. {
  556. // We have to be cautious, because the attachment could have been removed later.
  557. // In that case Wekan still reports its addition, but removes its 'url' field.
  558. // So we test for that
  559. const wekanAttachment = wekanBoard.attachments.filter((attachment) => {
  560. return attachment._id === activity.attachmentId;
  561. })[0];
  562. if (typeof wekanAttachment !== 'undefined' && wekanAttachment) {
  563. if (wekanAttachment.url || wekanAttachment.file) {
  564. // we cannot actually create the Wekan attachment, because we don't yet
  565. // have the cards to attach it to, so we store it in the instance variable.
  566. const wekanCardId = activity.cardId;
  567. if (!this.attachments[wekanCardId]) {
  568. this.attachments[wekanCardId] = [];
  569. }
  570. this.attachments[wekanCardId].push(wekanAttachment);
  571. }
  572. }
  573. break;
  574. }
  575. case 'addComment':
  576. {
  577. const wekanComment = wekanBoard.comments.filter((comment) => {
  578. return comment._id === activity.commentId;
  579. })[0];
  580. const id = activity.cardId;
  581. if (!this.comments[id]) {
  582. this.comments[id] = [];
  583. }
  584. this.comments[id].push(wekanComment);
  585. break;
  586. }
  587. case 'createBoard':
  588. {
  589. this.createdAt.board = activity.createdAt;
  590. break;
  591. }
  592. case 'createCard':
  593. {
  594. const cardId = activity.cardId;
  595. this.createdAt.cards[cardId] = activity.createdAt;
  596. this.createdBy.cards[cardId] = activity.userId;
  597. break;
  598. }
  599. case 'createList':
  600. {
  601. const listId = activity.listId;
  602. this.createdAt.lists[listId] = activity.createdAt;
  603. break;
  604. }
  605. case 'createSwimlane':
  606. {
  607. const swimlaneId = activity.swimlaneId;
  608. this.createdAt.swimlanes[swimlaneId] = activity.createdAt;
  609. break;
  610. }
  611. }
  612. });
  613. }
  614. importActivities(activities, boardId) {
  615. activities.forEach((activity) => {
  616. switch (activity.activityType) {
  617. // Board related activities
  618. // TODO: addBoardMember, removeBoardMember
  619. case 'createBoard':
  620. {
  621. Activities.direct.insert({
  622. userId: this._user(activity.userId),
  623. type: 'board',
  624. activityTypeId: boardId,
  625. activityType: activity.activityType,
  626. boardId,
  627. createdAt: this._now(activity.createdAt),
  628. });
  629. break;
  630. }
  631. // List related activities
  632. // TODO: removeList, archivedList
  633. case 'createList':
  634. {
  635. Activities.direct.insert({
  636. userId: this._user(activity.userId),
  637. type: 'list',
  638. activityType: activity.activityType,
  639. listId: this.lists[activity.listId],
  640. boardId,
  641. createdAt: this._now(activity.createdAt),
  642. });
  643. break;
  644. }
  645. // Card related activities
  646. // TODO: archivedCard, restoredCard, joinMember, unjoinMember
  647. case 'createCard':
  648. {
  649. Activities.direct.insert({
  650. userId: this._user(activity.userId),
  651. activityType: activity.activityType,
  652. listId: this.lists[activity.listId],
  653. cardId: this.cards[activity.cardId],
  654. boardId,
  655. createdAt: this._now(activity.createdAt),
  656. });
  657. break;
  658. }
  659. case 'moveCard':
  660. {
  661. Activities.direct.insert({
  662. userId: this._user(activity.userId),
  663. oldListId: this.lists[activity.oldListId],
  664. activityType: activity.activityType,
  665. listId: this.lists[activity.listId],
  666. cardId: this.cards[activity.cardId],
  667. boardId,
  668. createdAt: this._now(activity.createdAt),
  669. });
  670. break;
  671. }
  672. // Comment related activities
  673. case 'addComment':
  674. {
  675. Activities.direct.insert({
  676. userId: this._user(activity.userId),
  677. activityType: activity.activityType,
  678. cardId: this.cards[activity.cardId],
  679. commentId: this.commentIds[activity.commentId],
  680. boardId,
  681. createdAt: this._now(activity.createdAt),
  682. });
  683. break;
  684. }
  685. // Attachment related activities
  686. case 'addAttachment':
  687. {
  688. Activities.direct.insert({
  689. userId: this._user(activity.userId),
  690. type: 'card',
  691. activityType: activity.activityType,
  692. attachmentId: this.attachmentIds[activity.attachmentId],
  693. cardId: this.cards[activity.cardId],
  694. boardId,
  695. createdAt: this._now(activity.createdAt),
  696. });
  697. break;
  698. }
  699. // Checklist related activities
  700. case 'addChecklist':
  701. {
  702. Activities.direct.insert({
  703. userId: this._user(activity.userId),
  704. activityType: activity.activityType,
  705. cardId: this.cards[activity.cardId],
  706. checklistId: this.checklists[activity.checklistId],
  707. boardId,
  708. createdAt: this._now(activity.createdAt),
  709. });
  710. break;
  711. }
  712. case 'addChecklistItem':
  713. {
  714. Activities.direct.insert({
  715. userId: this._user(activity.userId),
  716. activityType: activity.activityType,
  717. cardId: this.cards[activity.cardId],
  718. checklistId: this.checklists[activity.checklistId],
  719. checklistItemId: activity.checklistItemId.replace(
  720. activity.checklistId,
  721. this.checklists[activity.checklistId]),
  722. boardId,
  723. createdAt: this._now(activity.createdAt),
  724. });
  725. break;
  726. }
  727. }
  728. });
  729. }
  730. //check(board) {
  731. check() {
  732. //try {
  733. // check(data, {
  734. // membersMapping: Match.Optional(Object),
  735. // });
  736. // this.checkActivities(board.activities);
  737. // this.checkBoard(board);
  738. // this.checkLabels(board.labels);
  739. // this.checkLists(board.lists);
  740. // this.checkSwimlanes(board.swimlanes);
  741. // this.checkCards(board.cards);
  742. //this.checkChecklists(board.checklists);
  743. // this.checkRules(board.rules);
  744. // this.checkActions(board.actions);
  745. //this.checkTriggers(board.triggers);
  746. //this.checkChecklistItems(board.checklistItems);
  747. //} catch (e) {
  748. // throw new Meteor.Error('error-json-schema');
  749. // }
  750. }
  751. create(board, currentBoardId) {
  752. // TODO : Make isSandstorm variable global
  753. const isSandstorm = Meteor.settings && Meteor.settings.public &&
  754. Meteor.settings.public.sandstorm;
  755. if (isSandstorm && currentBoardId) {
  756. const currentBoard = Boards.findOne(currentBoardId);
  757. currentBoard.archive();
  758. }
  759. this.parseActivities(board);
  760. const boardId = this.createBoardAndLabels(board);
  761. this.createLists(board.lists, boardId);
  762. this.createSwimlanes(board.swimlanes, boardId);
  763. this.createCards(board.cards, boardId);
  764. this.createChecklists(board.checklists);
  765. this.createChecklistItems(board.checklistItems);
  766. this.importActivities(board.activities, boardId);
  767. this.createTriggers(board.triggers, boardId);
  768. this.createActions(board.actions, boardId);
  769. this.createRules(board.rules, boardId);
  770. // XXX add members
  771. return boardId;
  772. }
  773. }