wekanCreator.js 25 KB

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