wekanCreator.js 25 KB

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