wekanCreator.js 30 KB

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