wekanCreator.js 27 KB

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