wekanCreator.js 29 KB

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