wekanCreator.js 26 KB

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