wekanCreator.js 29 KB

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