wekanCreator.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. file.attachData(att.url, function(error) {
  400. file.boardId = boardId;
  401. file.cardId = cardId;
  402. file.userId = self._user(att.userId);
  403. // The field source will only be used to prevent adding
  404. // attachments' related activities automatically
  405. file.source = 'import';
  406. if (error) {
  407. throw error;
  408. } else {
  409. const wekanAtt = Attachments.insert(file, () => {
  410. // we do nothing
  411. });
  412. self.attachmentIds[att._id] = wekanAtt._id;
  413. //
  414. if (wekanCoverId === att._id) {
  415. Cards.direct.update(cardId, {
  416. $set: {
  417. coverId: wekanAtt._id,
  418. },
  419. });
  420. }
  421. }
  422. });
  423. } else if (att.file) {
  424. file.attachData(
  425. new Buffer(att.file, 'base64'),
  426. {
  427. type: att.type,
  428. },
  429. error => {
  430. file.name(att.name);
  431. file.boardId = boardId;
  432. file.cardId = cardId;
  433. file.userId = self._user(att.userId);
  434. // The field source will only be used to prevent adding
  435. // attachments' related activities automatically
  436. file.source = 'import';
  437. if (error) {
  438. throw error;
  439. } else {
  440. const wekanAtt = Attachments.insert(file, () => {
  441. // we do nothing
  442. });
  443. this.attachmentIds[att._id] = wekanAtt._id;
  444. //
  445. if (wekanCoverId === att._id) {
  446. Cards.direct.update(cardId, {
  447. $set: {
  448. coverId: wekanAtt._id,
  449. },
  450. });
  451. }
  452. }
  453. },
  454. );
  455. }
  456. }
  457. // todo XXX set cover - if need be
  458. });
  459. }
  460. result.push(cardId);
  461. });
  462. return result;
  463. }
  464. // Create labels if they do not exist and load this.labels.
  465. createLabels(wekanLabels, board) {
  466. wekanLabels.forEach(label => {
  467. const color = label.color;
  468. const name = label.name;
  469. const existingLabel = board.getLabel(name, color);
  470. if (existingLabel) {
  471. this.labels[label.id] = existingLabel._id;
  472. } else {
  473. const idLabelCreated = board.pushLabel(name, color);
  474. this.labels[label.id] = idLabelCreated;
  475. }
  476. });
  477. }
  478. createLists(wekanLists, boardId) {
  479. wekanLists.forEach((list, listIndex) => {
  480. const listToCreate = {
  481. archived: list.archived,
  482. boardId,
  483. // We are being defensing here by providing a default date (now) if the
  484. // creation date wasn't found on the action log. This happen on old
  485. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  486. // we require.
  487. createdAt: this._now(this.createdAt.lists[list.id]),
  488. title: list.title,
  489. sort: list.sort ? list.sort : listIndex,
  490. };
  491. const listId = Lists.direct.insert(listToCreate);
  492. Lists.direct.update(listId, {
  493. $set: {
  494. updatedAt: this._now(),
  495. },
  496. });
  497. this.lists[list._id] = listId;
  498. // // log activity
  499. // Activities.direct.insert({
  500. // activityType: 'importList',
  501. // boardId,
  502. // createdAt: this._now(),
  503. // listId,
  504. // source: {
  505. // id: list._id,
  506. // system: 'Wekan',
  507. // },
  508. // // We attribute the import to current user,
  509. // // not the creator of the original object
  510. // userId: this._user(),
  511. // });
  512. });
  513. }
  514. createSwimlanes(wekanSwimlanes, boardId) {
  515. wekanSwimlanes.forEach((swimlane, swimlaneIndex) => {
  516. const swimlaneToCreate = {
  517. archived: swimlane.archived,
  518. boardId,
  519. // We are being defensing here by providing a default date (now) if the
  520. // creation date wasn't found on the action log. This happen on old
  521. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  522. // we require.
  523. createdAt: this._now(this.createdAt.swimlanes[swimlane._id]),
  524. title: swimlane.title,
  525. sort: swimlane.sort ? swimlane.sort : swimlaneIndex,
  526. };
  527. // set color
  528. if (swimlane.color) {
  529. swimlaneToCreate.color = swimlane.color;
  530. }
  531. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  532. Swimlanes.direct.update(swimlaneId, {
  533. $set: {
  534. updatedAt: this._now(),
  535. },
  536. });
  537. this.swimlanes[swimlane._id] = swimlaneId;
  538. });
  539. }
  540. createChecklists(wekanChecklists) {
  541. const result = [];
  542. wekanChecklists.forEach((checklist, checklistIndex) => {
  543. // Create the checklist
  544. const checklistToCreate = {
  545. cardId: this.cards[checklist.cardId],
  546. title: checklist.title,
  547. createdAt: checklist.createdAt,
  548. sort: checklist.sort ? checklist.sort : checklistIndex,
  549. };
  550. const checklistId = Checklists.direct.insert(checklistToCreate);
  551. this.checklists[checklist._id] = checklistId;
  552. result.push(checklistId);
  553. });
  554. return result;
  555. }
  556. createTriggers(wekanTriggers, boardId) {
  557. wekanTriggers.forEach(trigger => {
  558. if (trigger.hasOwnProperty('labelId')) {
  559. trigger.labelId = this.labels[trigger.labelId];
  560. }
  561. if (trigger.hasOwnProperty('memberId')) {
  562. trigger.memberId = this.members[trigger.memberId];
  563. }
  564. trigger.boardId = boardId;
  565. const oldId = trigger._id;
  566. delete trigger._id;
  567. this.triggers[oldId] = Triggers.direct.insert(trigger);
  568. });
  569. }
  570. createActions(wekanActions, boardId) {
  571. wekanActions.forEach(action => {
  572. if (action.hasOwnProperty('labelId')) {
  573. action.labelId = this.labels[action.labelId];
  574. }
  575. if (action.hasOwnProperty('memberId')) {
  576. action.memberId = this.members[action.memberId];
  577. }
  578. action.boardId = boardId;
  579. const oldId = action._id;
  580. delete action._id;
  581. this.actions[oldId] = Actions.direct.insert(action);
  582. });
  583. }
  584. createRules(wekanRules, boardId) {
  585. wekanRules.forEach(rule => {
  586. // Create the rule
  587. rule.boardId = boardId;
  588. rule.triggerId = this.triggers[rule.triggerId];
  589. rule.actionId = this.actions[rule.actionId];
  590. delete rule._id;
  591. Rules.direct.insert(rule);
  592. });
  593. }
  594. createChecklistItems(wekanChecklistItems) {
  595. wekanChecklistItems.forEach((checklistitem, checklistitemIndex) => {
  596. // Create the checklistItem
  597. const checklistItemTocreate = {
  598. title: checklistitem.title,
  599. checklistId: this.checklists[checklistitem.checklistId],
  600. cardId: this.cards[checklistitem.cardId],
  601. sort: checklistitem.sort ? checklistitem.sort : checklistitemIndex,
  602. isFinished: checklistitem.isFinished,
  603. };
  604. const checklistItemId = ChecklistItems.direct.insert(
  605. checklistItemTocreate,
  606. );
  607. this.checklistItems[checklistitem._id] = checklistItemId;
  608. });
  609. }
  610. parseActivities(wekanBoard) {
  611. wekanBoard.activities.forEach(activity => {
  612. switch (activity.activityType) {
  613. case 'addAttachment': {
  614. // We have to be cautious, because the attachment could have been removed later.
  615. // In that case Wekan still reports its addition, but removes its 'url' field.
  616. // So we test for that
  617. const wekanAttachment = wekanBoard.attachments.filter(attachment => {
  618. return attachment._id === activity.attachmentId;
  619. })[0];
  620. if (typeof wekanAttachment !== 'undefined' && wekanAttachment) {
  621. if (wekanAttachment.url || wekanAttachment.file) {
  622. // we cannot actually create the Wekan attachment, because we don't yet
  623. // have the cards to attach it to, so we store it in the instance variable.
  624. const wekanCardId = activity.cardId;
  625. if (!this.attachments[wekanCardId]) {
  626. this.attachments[wekanCardId] = [];
  627. }
  628. this.attachments[wekanCardId].push(wekanAttachment);
  629. }
  630. }
  631. break;
  632. }
  633. case 'addComment': {
  634. const wekanComment = wekanBoard.comments.filter(comment => {
  635. return comment._id === activity.commentId;
  636. })[0];
  637. const id = activity.cardId;
  638. if (!this.comments[id]) {
  639. this.comments[id] = [];
  640. }
  641. this.comments[id].push(wekanComment);
  642. break;
  643. }
  644. case 'createBoard': {
  645. this.createdAt.board = activity.createdAt;
  646. break;
  647. }
  648. case 'createCard': {
  649. const cardId = activity.cardId;
  650. this.createdAt.cards[cardId] = activity.createdAt;
  651. this.createdBy.cards[cardId] = activity.userId;
  652. break;
  653. }
  654. case 'createList': {
  655. const listId = activity.listId;
  656. this.createdAt.lists[listId] = activity.createdAt;
  657. break;
  658. }
  659. case 'createSwimlane': {
  660. const swimlaneId = activity.swimlaneId;
  661. this.createdAt.swimlanes[swimlaneId] = activity.createdAt;
  662. break;
  663. }
  664. }
  665. });
  666. }
  667. importActivities(activities, boardId) {
  668. activities.forEach(activity => {
  669. switch (activity.activityType) {
  670. // Board related activities
  671. // TODO: addBoardMember, removeBoardMember
  672. case 'createBoard': {
  673. Activities.direct.insert({
  674. userId: this._user(activity.userId),
  675. type: 'board',
  676. activityTypeId: boardId,
  677. activityType: activity.activityType,
  678. boardId,
  679. createdAt: this._now(activity.createdAt),
  680. });
  681. break;
  682. }
  683. // List related activities
  684. // TODO: removeList, archivedList
  685. case 'createList': {
  686. Activities.direct.insert({
  687. userId: this._user(activity.userId),
  688. type: 'list',
  689. activityType: activity.activityType,
  690. listId: this.lists[activity.listId],
  691. boardId,
  692. createdAt: this._now(activity.createdAt),
  693. });
  694. break;
  695. }
  696. // Card related activities
  697. // TODO: archivedCard, restoredCard, joinMember, unjoinMember
  698. case 'createCard': {
  699. Activities.direct.insert({
  700. userId: this._user(activity.userId),
  701. activityType: activity.activityType,
  702. listId: this.lists[activity.listId],
  703. cardId: this.cards[activity.cardId],
  704. boardId,
  705. createdAt: this._now(activity.createdAt),
  706. });
  707. break;
  708. }
  709. case 'moveCard': {
  710. Activities.direct.insert({
  711. userId: this._user(activity.userId),
  712. oldListId: this.lists[activity.oldListId],
  713. activityType: activity.activityType,
  714. listId: this.lists[activity.listId],
  715. cardId: this.cards[activity.cardId],
  716. boardId,
  717. createdAt: this._now(activity.createdAt),
  718. });
  719. break;
  720. }
  721. // Comment related activities
  722. case 'addComment': {
  723. Activities.direct.insert({
  724. userId: this._user(activity.userId),
  725. activityType: activity.activityType,
  726. cardId: this.cards[activity.cardId],
  727. commentId: this.commentIds[activity.commentId],
  728. boardId,
  729. createdAt: this._now(activity.createdAt),
  730. });
  731. break;
  732. }
  733. // Attachment related activities
  734. case 'addAttachment': {
  735. Activities.direct.insert({
  736. userId: this._user(activity.userId),
  737. type: 'card',
  738. activityType: activity.activityType,
  739. attachmentId: this.attachmentIds[activity.attachmentId],
  740. cardId: this.cards[activity.cardId],
  741. boardId,
  742. createdAt: this._now(activity.createdAt),
  743. });
  744. break;
  745. }
  746. // Checklist related activities
  747. case 'addChecklist': {
  748. Activities.direct.insert({
  749. userId: this._user(activity.userId),
  750. activityType: activity.activityType,
  751. cardId: this.cards[activity.cardId],
  752. checklistId: this.checklists[activity.checklistId],
  753. boardId,
  754. createdAt: this._now(activity.createdAt),
  755. });
  756. break;
  757. }
  758. case 'addChecklistItem': {
  759. Activities.direct.insert({
  760. userId: this._user(activity.userId),
  761. activityType: activity.activityType,
  762. cardId: this.cards[activity.cardId],
  763. checklistId: this.checklists[activity.checklistId],
  764. checklistItemId: activity.checklistItemId.replace(
  765. activity.checklistId,
  766. this.checklists[activity.checklistId],
  767. ),
  768. boardId,
  769. createdAt: this._now(activity.createdAt),
  770. });
  771. break;
  772. }
  773. }
  774. });
  775. }
  776. //check(board) {
  777. check() {
  778. //try {
  779. // check(data, {
  780. // membersMapping: Match.Optional(Object),
  781. // });
  782. // this.checkActivities(board.activities);
  783. // this.checkBoard(board);
  784. // this.checkLabels(board.labels);
  785. // this.checkLists(board.lists);
  786. // this.checkSwimlanes(board.swimlanes);
  787. // this.checkCards(board.cards);
  788. //this.checkChecklists(board.checklists);
  789. // this.checkRules(board.rules);
  790. // this.checkActions(board.actions);
  791. //this.checkTriggers(board.triggers);
  792. //this.checkChecklistItems(board.checklistItems);
  793. //} catch (e) {
  794. // throw new Meteor.Error('error-json-schema');
  795. // }
  796. }
  797. create(board, currentBoardId) {
  798. // TODO : Make isSandstorm variable global
  799. const isSandstorm =
  800. Meteor.settings &&
  801. Meteor.settings.public &&
  802. Meteor.settings.public.sandstorm;
  803. if (isSandstorm && currentBoardId) {
  804. const currentBoard = Boards.findOne(currentBoardId);
  805. currentBoard.archive();
  806. }
  807. this.parseActivities(board);
  808. const boardId = this.createBoardAndLabels(board);
  809. this.createLists(board.lists, boardId);
  810. this.createSwimlanes(board.swimlanes, boardId);
  811. this.createCards(board.cards, boardId);
  812. this.createChecklists(board.checklists);
  813. this.createChecklistItems(board.checklistItems);
  814. this.importActivities(board.activities, boardId);
  815. this.createTriggers(board.triggers, boardId);
  816. this.createActions(board.actions, boardId);
  817. this.createRules(board.rules, boardId);
  818. // XXX add members
  819. return boardId;
  820. }
  821. }