wekanCreator.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. // Standalone Export has modifiedAt missing, adding modifiedAt to fix it
  233. modifiedAt: this._now(boardToImport.modifiedAt),
  234. permission: boardToImport.permission,
  235. slug: getSlug(boardToImport.title) || 'board',
  236. stars: 0,
  237. title: boardToImport.title,
  238. };
  239. // now add other members
  240. if (boardToImport.members) {
  241. boardToImport.members.forEach(wekanMember => {
  242. // do we already have it in our list?
  243. if (
  244. !boardToCreate.members.some(
  245. member => member.wekanId === wekanMember.wekanId,
  246. )
  247. )
  248. boardToCreate.members.push({
  249. ...wekanMember,
  250. userId: wekanMember.wekanId,
  251. });
  252. });
  253. }
  254. boardToImport.labels.forEach(label => {
  255. const labelToCreate = {
  256. _id: Random.id(6),
  257. color: label.color,
  258. name: label.name,
  259. };
  260. // We need to remember them by Wekan ID, as this is the only ref we have
  261. // when importing cards.
  262. this.labels[label._id] = labelToCreate._id;
  263. boardToCreate.labels.push(labelToCreate);
  264. });
  265. const boardId = Boards.direct.insert(boardToCreate);
  266. Boards.direct.update(boardId, {
  267. $set: {
  268. modifiedAt: this._now(),
  269. },
  270. });
  271. // log activity
  272. Activities.direct.insert({
  273. activityType: 'importBoard',
  274. boardId,
  275. createdAt: this._now(),
  276. source: {
  277. id: boardToImport.id,
  278. system: 'Wekan',
  279. },
  280. // We attribute the import to current user,
  281. // not the author from the original object.
  282. userId: this._user(),
  283. });
  284. return boardId;
  285. }
  286. /**
  287. * Create the Wekan cards corresponding to the supplied Wekan cards,
  288. * as well as all linked data: activities, comments, and attachments
  289. * @param wekanCards
  290. * @param boardId
  291. * @returns {Array}
  292. */
  293. createCards(wekanCards, boardId) {
  294. const result = [];
  295. wekanCards.forEach(card => {
  296. const cardToCreate = {
  297. archived: card.archived,
  298. boardId,
  299. // very old boards won't have a creation activity so no creation date
  300. createdAt: this._now(this.createdAt.cards[card._id]),
  301. dateLastActivity: this._now(),
  302. description: card.description,
  303. listId: this.lists[card.listId],
  304. swimlaneId: this.swimlanes[card.swimlaneId],
  305. sort: card.sort,
  306. title: card.title,
  307. // we attribute the card to its creator if available
  308. userId: this._user(this.createdBy.cards[card._id]),
  309. isOvertime: card.isOvertime || false,
  310. startAt: card.startAt ? this._now(card.startAt) : null,
  311. dueAt: card.dueAt ? this._now(card.dueAt) : null,
  312. spentTime: card.spentTime || null,
  313. };
  314. // add labels
  315. if (card.labelIds) {
  316. cardToCreate.labelIds = card.labelIds.map(wekanId => {
  317. return this.labels[wekanId];
  318. });
  319. }
  320. // add members {
  321. if (card.members) {
  322. const wekanMembers = [];
  323. // we can't just map, as some members may not have been mapped
  324. card.members.forEach(sourceMemberId => {
  325. if (this.members[sourceMemberId]) {
  326. const wekanId = this.members[sourceMemberId];
  327. // we may map multiple Wekan members to the same wekan user
  328. // in which case we risk adding the same user multiple times
  329. if (!wekanMembers.find(wId => wId === wekanId)) {
  330. wekanMembers.push(wekanId);
  331. }
  332. }
  333. return true;
  334. });
  335. if (wekanMembers.length > 0) {
  336. cardToCreate.members = wekanMembers;
  337. }
  338. }
  339. // add assignees
  340. if (card.assignees) {
  341. const wekanAssignees = [];
  342. // we can't just map, as some members may not have been mapped
  343. card.assignees.forEach(sourceMemberId => {
  344. if (this.members[sourceMemberId]) {
  345. const wekanId = this.members[sourceMemberId];
  346. // we may map multiple Wekan members to the same wekan user
  347. // in which case we risk adding the same user multiple times
  348. if (!wekanAssignees.find(wId => wId === wekanId)) {
  349. wekanAssignees.push(wekanId);
  350. }
  351. }
  352. return true;
  353. });
  354. if (wekanAssignees.length > 0) {
  355. cardToCreate.assignees = wekanAssignees;
  356. }
  357. }
  358. // set color
  359. if (card.color) {
  360. cardToCreate.color = card.color;
  361. }
  362. // add custom fields
  363. if (card.customFields) {
  364. cardToCreate.customFields = card.customFields.map(field => {
  365. return {
  366. _id: this.customFields[field._id],
  367. value: field.value,
  368. };
  369. });
  370. }
  371. // insert card
  372. const cardId = Cards.direct.insert(cardToCreate);
  373. // keep track of Wekan id => Wekan id
  374. this.cards[card._id] = cardId;
  375. // // log activity
  376. // Activities.direct.insert({
  377. // activityType: 'importCard',
  378. // boardId,
  379. // cardId,
  380. // createdAt: this._now(),
  381. // listId: cardToCreate.listId,
  382. // source: {
  383. // id: card._id,
  384. // system: 'Wekan',
  385. // },
  386. // // we attribute the import to current user,
  387. // // not the author of the original card
  388. // userId: this._user(),
  389. // });
  390. // add comments
  391. const comments = this.comments[card._id];
  392. if (comments) {
  393. comments.forEach(comment => {
  394. const commentToCreate = {
  395. boardId,
  396. cardId,
  397. createdAt: this._now(comment.createdAt),
  398. text: comment.text,
  399. // we attribute the comment to the original author, default to current user
  400. userId: this._user(comment.userId),
  401. };
  402. // dateLastActivity will be set from activity insert, no need to
  403. // update it ourselves
  404. const commentId = CardComments.direct.insert(commentToCreate);
  405. this.commentIds[comment._id] = commentId;
  406. // Activities.direct.insert({
  407. // activityType: 'addComment',
  408. // boardId: commentToCreate.boardId,
  409. // cardId: commentToCreate.cardId,
  410. // commentId,
  411. // createdAt: this._now(commentToCreate.createdAt),
  412. // // we attribute the addComment (not the import)
  413. // // to the original author - it is needed by some UI elements.
  414. // userId: commentToCreate.userId,
  415. // });
  416. });
  417. }
  418. const attachments = this.attachments[card._id];
  419. const wekanCoverId = card.coverId;
  420. if (attachments) {
  421. attachments.forEach(att => {
  422. const file = new FS.File();
  423. // Simulating file.attachData on the client generates multiple errors
  424. // - HEAD returns null, which causes exception down the line
  425. // - the template then tries to display the url to the attachment which causes other errors
  426. // so we make it server only, and let UI catch up once it is done, forget about latency comp.
  427. const self = this;
  428. if (Meteor.isServer) {
  429. if (att.url) {
  430. file.attachData(att.url, function(error) {
  431. file.boardId = boardId;
  432. file.cardId = cardId;
  433. file.userId = self._user(att.userId);
  434. // The field source will only be used to prevent adding
  435. // attachments' related activities automatically
  436. file.source = 'import';
  437. if (error) {
  438. throw error;
  439. } else {
  440. const wekanAtt = Attachments.insert(file, () => {
  441. // we do nothing
  442. });
  443. self.attachmentIds[att._id] = wekanAtt._id;
  444. //
  445. if (wekanCoverId === att._id) {
  446. Cards.direct.update(cardId, {
  447. $set: {
  448. coverId: wekanAtt._id,
  449. },
  450. });
  451. }
  452. }
  453. });
  454. } else if (att.file) {
  455. file.attachData(
  456. Buffer.from(att.file, 'base64'),
  457. {
  458. type: att.type,
  459. },
  460. error => {
  461. file.name(att.name);
  462. file.boardId = boardId;
  463. file.cardId = cardId;
  464. file.userId = self._user(att.userId);
  465. // The field source will only be used to prevent adding
  466. // attachments' related activities automatically
  467. file.source = 'import';
  468. if (error) {
  469. throw error;
  470. } else {
  471. const wekanAtt = Attachments.insert(file, () => {
  472. // we do nothing
  473. });
  474. this.attachmentIds[att._id] = wekanAtt._id;
  475. //
  476. if (wekanCoverId === att._id) {
  477. Cards.direct.update(cardId, {
  478. $set: {
  479. coverId: wekanAtt._id,
  480. },
  481. });
  482. }
  483. }
  484. },
  485. );
  486. }
  487. }
  488. // todo XXX set cover - if need be
  489. });
  490. }
  491. result.push(cardId);
  492. });
  493. return result;
  494. }
  495. /**
  496. * Create the Wekan custom fields corresponding to the supplied Wekan
  497. * custom fields.
  498. * @param wekanCustomFields
  499. * @param boardId
  500. */
  501. createCustomFields(wekanCustomFields, boardId) {
  502. wekanCustomFields.forEach((field, fieldIndex) => {
  503. const fieldToCreate = {
  504. boardIds: [boardId],
  505. name: field.name,
  506. type: field.type,
  507. settings: field.settings,
  508. showOnCard: field.showOnCard,
  509. showLabelOnMiniCard: field.showLabelOnMiniCard,
  510. automaticallyOnCard: field.automaticallyOnCard,
  511. //use date "now" if now created at date is provided (e.g. for very old boards)
  512. createdAt: this._now(this.createdAt.customFields[field._id]),
  513. modifiedAt: field.modifiedAt,
  514. };
  515. //insert copy of custom field
  516. const fieldId = CustomFields.direct.insert(fieldToCreate);
  517. //set modified date to now
  518. CustomFields.direct.update(fieldId, {
  519. $set: {
  520. modifiedAt: this._now(),
  521. },
  522. });
  523. //store mapping of old id to new id
  524. this.customFields[field._id] = fieldId;
  525. });
  526. }
  527. // Create labels if they do not exist and load this.labels.
  528. createLabels(wekanLabels, board) {
  529. wekanLabels.forEach(label => {
  530. const color = label.color;
  531. const name = label.name;
  532. const existingLabel = board.getLabel(name, color);
  533. if (existingLabel) {
  534. this.labels[label.id] = existingLabel._id;
  535. } else {
  536. const idLabelCreated = board.pushLabel(name, color);
  537. this.labels[label.id] = idLabelCreated;
  538. }
  539. });
  540. }
  541. createLists(wekanLists, boardId) {
  542. wekanLists.forEach((list, listIndex) => {
  543. const listToCreate = {
  544. archived: list.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.lists[list.id]),
  551. title: list.title,
  552. sort: list.sort ? list.sort : listIndex,
  553. };
  554. const listId = Lists.direct.insert(listToCreate);
  555. Lists.direct.update(listId, {
  556. $set: {
  557. updatedAt: this._now(),
  558. },
  559. });
  560. this.lists[list._id] = listId;
  561. // // log activity
  562. // Activities.direct.insert({
  563. // activityType: 'importList',
  564. // boardId,
  565. // createdAt: this._now(),
  566. // listId,
  567. // source: {
  568. // id: list._id,
  569. // system: 'Wekan',
  570. // },
  571. // // We attribute the import to current user,
  572. // // not the creator of the original object
  573. // userId: this._user(),
  574. // });
  575. });
  576. }
  577. createSwimlanes(wekanSwimlanes, boardId) {
  578. wekanSwimlanes.forEach((swimlane, swimlaneIndex) => {
  579. const swimlaneToCreate = {
  580. archived: swimlane.archived,
  581. boardId,
  582. // We are being defensing here by providing a default date (now) if the
  583. // creation date wasn't found on the action log. This happen on old
  584. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  585. // we require.
  586. createdAt: this._now(this.createdAt.swimlanes[swimlane._id]),
  587. title: swimlane.title,
  588. sort: swimlane.sort ? swimlane.sort : swimlaneIndex,
  589. };
  590. // set color
  591. if (swimlane.color) {
  592. swimlaneToCreate.color = swimlane.color;
  593. }
  594. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  595. Swimlanes.direct.update(swimlaneId, {
  596. $set: {
  597. updatedAt: this._now(),
  598. },
  599. });
  600. this.swimlanes[swimlane._id] = swimlaneId;
  601. });
  602. }
  603. createChecklists(wekanChecklists) {
  604. const result = [];
  605. wekanChecklists.forEach((checklist, checklistIndex) => {
  606. // Create the checklist
  607. const checklistToCreate = {
  608. cardId: this.cards[checklist.cardId],
  609. title: checklist.title,
  610. createdAt: checklist.createdAt,
  611. sort: checklist.sort ? checklist.sort : checklistIndex,
  612. };
  613. const checklistId = Checklists.direct.insert(checklistToCreate);
  614. this.checklists[checklist._id] = checklistId;
  615. result.push(checklistId);
  616. });
  617. return result;
  618. }
  619. createTriggers(wekanTriggers, boardId) {
  620. wekanTriggers.forEach(trigger => {
  621. if (trigger.hasOwnProperty('labelId')) {
  622. trigger.labelId = this.labels[trigger.labelId];
  623. }
  624. if (trigger.hasOwnProperty('memberId')) {
  625. trigger.memberId = this.members[trigger.memberId];
  626. }
  627. trigger.boardId = boardId;
  628. const oldId = trigger._id;
  629. delete trigger._id;
  630. this.triggers[oldId] = Triggers.direct.insert(trigger);
  631. });
  632. }
  633. createActions(wekanActions, boardId) {
  634. wekanActions.forEach(action => {
  635. if (action.hasOwnProperty('labelId')) {
  636. action.labelId = this.labels[action.labelId];
  637. }
  638. if (action.hasOwnProperty('memberId')) {
  639. action.memberId = this.members[action.memberId];
  640. }
  641. action.boardId = boardId;
  642. const oldId = action._id;
  643. delete action._id;
  644. this.actions[oldId] = Actions.direct.insert(action);
  645. });
  646. }
  647. createRules(wekanRules, boardId) {
  648. wekanRules.forEach(rule => {
  649. // Create the rule
  650. rule.boardId = boardId;
  651. rule.triggerId = this.triggers[rule.triggerId];
  652. rule.actionId = this.actions[rule.actionId];
  653. delete rule._id;
  654. Rules.direct.insert(rule);
  655. });
  656. }
  657. createChecklistItems(wekanChecklistItems) {
  658. wekanChecklistItems.forEach((checklistitem, checklistitemIndex) => {
  659. // Create the checklistItem
  660. const checklistItemTocreate = {
  661. title: checklistitem.title,
  662. checklistId: this.checklists[checklistitem.checklistId],
  663. cardId: this.cards[checklistitem.cardId],
  664. sort: checklistitem.sort ? checklistitem.sort : checklistitemIndex,
  665. isFinished: checklistitem.isFinished,
  666. };
  667. const checklistItemId = ChecklistItems.direct.insert(
  668. checklistItemTocreate,
  669. );
  670. this.checklistItems[checklistitem._id] = checklistItemId;
  671. });
  672. }
  673. parseActivities(wekanBoard) {
  674. wekanBoard.activities.forEach(activity => {
  675. switch (activity.activityType) {
  676. case 'addAttachment': {
  677. // We have to be cautious, because the attachment could have been removed later.
  678. // In that case Wekan still reports its addition, but removes its 'url' field.
  679. // So we test for that
  680. const wekanAttachment = wekanBoard.attachments.filter(attachment => {
  681. return attachment._id === activity.attachmentId;
  682. })[0];
  683. if (typeof wekanAttachment !== 'undefined' && wekanAttachment) {
  684. if (wekanAttachment.url || wekanAttachment.file) {
  685. // we cannot actually create the Wekan attachment, because we don't yet
  686. // have the cards to attach it to, so we store it in the instance variable.
  687. const wekanCardId = activity.cardId;
  688. if (!this.attachments[wekanCardId]) {
  689. this.attachments[wekanCardId] = [];
  690. }
  691. this.attachments[wekanCardId].push(wekanAttachment);
  692. }
  693. }
  694. break;
  695. }
  696. case 'addComment': {
  697. const wekanComment = wekanBoard.comments.filter(comment => {
  698. return comment._id === activity.commentId;
  699. })[0];
  700. const id = activity.cardId;
  701. if (!this.comments[id]) {
  702. this.comments[id] = [];
  703. }
  704. this.comments[id].push(wekanComment);
  705. break;
  706. }
  707. case 'createBoard': {
  708. this.createdAt.board = activity.createdAt;
  709. break;
  710. }
  711. case 'createCard': {
  712. const cardId = activity.cardId;
  713. this.createdAt.cards[cardId] = activity.createdAt;
  714. this.createdBy.cards[cardId] = activity.userId;
  715. break;
  716. }
  717. case 'createList': {
  718. const listId = activity.listId;
  719. this.createdAt.lists[listId] = activity.createdAt;
  720. break;
  721. }
  722. case 'createSwimlane': {
  723. const swimlaneId = activity.swimlaneId;
  724. this.createdAt.swimlanes[swimlaneId] = activity.createdAt;
  725. break;
  726. }
  727. case 'createCustomField': {
  728. const customFieldId = activity.customFieldId;
  729. this.createdAt.customFields[customFieldId] = activity.createdAt;
  730. break;
  731. }
  732. }
  733. });
  734. }
  735. importActivities(activities, boardId) {
  736. activities.forEach(activity => {
  737. switch (activity.activityType) {
  738. // Board related activities
  739. // TODO: addBoardMember, removeBoardMember
  740. case 'createBoard': {
  741. Activities.direct.insert({
  742. userId: this._user(activity.userId),
  743. type: 'board',
  744. activityTypeId: boardId,
  745. activityType: activity.activityType,
  746. boardId,
  747. createdAt: this._now(activity.createdAt),
  748. });
  749. break;
  750. }
  751. // List related activities
  752. // TODO: removeList, archivedList
  753. case 'createList': {
  754. Activities.direct.insert({
  755. userId: this._user(activity.userId),
  756. type: 'list',
  757. activityType: activity.activityType,
  758. listId: this.lists[activity.listId],
  759. boardId,
  760. createdAt: this._now(activity.createdAt),
  761. });
  762. break;
  763. }
  764. // Card related activities
  765. // TODO: archivedCard, restoredCard, joinMember, unjoinMember
  766. case 'createCard': {
  767. Activities.direct.insert({
  768. userId: this._user(activity.userId),
  769. activityType: activity.activityType,
  770. listId: this.lists[activity.listId],
  771. cardId: this.cards[activity.cardId],
  772. boardId,
  773. createdAt: this._now(activity.createdAt),
  774. });
  775. break;
  776. }
  777. case 'moveCard': {
  778. Activities.direct.insert({
  779. userId: this._user(activity.userId),
  780. oldListId: this.lists[activity.oldListId],
  781. activityType: activity.activityType,
  782. listId: this.lists[activity.listId],
  783. cardId: this.cards[activity.cardId],
  784. boardId,
  785. createdAt: this._now(activity.createdAt),
  786. });
  787. break;
  788. }
  789. // Comment related activities
  790. case 'addComment': {
  791. Activities.direct.insert({
  792. userId: this._user(activity.userId),
  793. activityType: activity.activityType,
  794. cardId: this.cards[activity.cardId],
  795. commentId: this.commentIds[activity.commentId],
  796. boardId,
  797. createdAt: this._now(activity.createdAt),
  798. });
  799. break;
  800. }
  801. // Attachment related activities
  802. case 'addAttachment': {
  803. Activities.direct.insert({
  804. userId: this._user(activity.userId),
  805. type: 'card',
  806. activityType: activity.activityType,
  807. attachmentId: this.attachmentIds[activity.attachmentId],
  808. cardId: this.cards[activity.cardId],
  809. boardId,
  810. createdAt: this._now(activity.createdAt),
  811. });
  812. break;
  813. }
  814. // Checklist related activities
  815. case 'addChecklist': {
  816. Activities.direct.insert({
  817. userId: this._user(activity.userId),
  818. activityType: activity.activityType,
  819. cardId: this.cards[activity.cardId],
  820. checklistId: this.checklists[activity.checklistId],
  821. boardId,
  822. createdAt: this._now(activity.createdAt),
  823. });
  824. break;
  825. }
  826. case 'addChecklistItem': {
  827. Activities.direct.insert({
  828. userId: this._user(activity.userId),
  829. activityType: activity.activityType,
  830. cardId: this.cards[activity.cardId],
  831. checklistId: this.checklists[activity.checklistId],
  832. checklistItemId: activity.checklistItemId.replace(
  833. activity.checklistId,
  834. this.checklists[activity.checklistId],
  835. ),
  836. boardId,
  837. createdAt: this._now(activity.createdAt),
  838. });
  839. break;
  840. }
  841. }
  842. });
  843. }
  844. //check(board) {
  845. check() {
  846. //try {
  847. // check(data, {
  848. // membersMapping: Match.Optional(Object),
  849. // });
  850. // this.checkActivities(board.activities);
  851. // this.checkBoard(board);
  852. // this.checkLabels(board.labels);
  853. // this.checkLists(board.lists);
  854. // this.checkSwimlanes(board.swimlanes);
  855. // this.checkCards(board.cards);
  856. //this.checkChecklists(board.checklists);
  857. // this.checkRules(board.rules);
  858. // this.checkActions(board.actions);
  859. //this.checkTriggers(board.triggers);
  860. //this.checkChecklistItems(board.checklistItems);
  861. //} catch (e) {
  862. // throw new Meteor.Error('error-json-schema');
  863. // }
  864. }
  865. create(board, currentBoardId) {
  866. // TODO : Make isSandstorm variable global
  867. const isSandstorm =
  868. Meteor.settings &&
  869. Meteor.settings.public &&
  870. Meteor.settings.public.sandstorm;
  871. if (isSandstorm && currentBoardId) {
  872. const currentBoard = Boards.findOne(currentBoardId);
  873. currentBoard.archive();
  874. }
  875. this.parseActivities(board);
  876. const boardId = this.createBoardAndLabels(board);
  877. this.createLists(board.lists, boardId);
  878. this.createSwimlanes(board.swimlanes, boardId);
  879. this.createCustomFields(board.customFields, boardId);
  880. this.createCards(board.cards, boardId);
  881. this.createChecklists(board.checklists);
  882. this.createChecklistItems(board.checklistItems);
  883. this.importActivities(board.activities, boardId);
  884. this.createTriggers(board.triggers, boardId);
  885. this.createActions(board.actions, boardId);
  886. this.createRules(board.rules, boardId);
  887. // XXX add members
  888. return boardId;
  889. }
  890. }