import.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. const DateString = Match.Where(function (dateAsString) {
  2. check(dateAsString, String);
  3. return moment(dateAsString, moment.ISO_8601).isValid();
  4. });
  5. class TrelloCreator {
  6. constructor() {
  7. // The object creation dates, indexed by Trello id (so we only parse actions
  8. // once!)
  9. this.createdAt = {
  10. board: null,
  11. cards: {},
  12. lists: {},
  13. };
  14. // Map of labels Trello ID => Wekan ID
  15. this.labels = {};
  16. // Map of lists Trello ID => Wekan ID
  17. this.lists = {};
  18. // The comments, indexed by Trello card id (to map when importing cards)
  19. this.comments = {};
  20. }
  21. checkActions(trelloActions) {
  22. check(trelloActions, [Match.ObjectIncluding({
  23. data: Object,
  24. date: DateString,
  25. type: String,
  26. })]);
  27. // XXX we could perform more thorough checks based on action type
  28. }
  29. checkBoard(trelloBoard) {
  30. check(trelloBoard, Match.ObjectIncluding({
  31. closed: Boolean,
  32. name: String,
  33. prefs: Match.ObjectIncluding({
  34. // XXX refine control by validating 'background' against a list of
  35. // allowed values (is it worth the maintenance?)
  36. background: String,
  37. permissionLevel: Match.Where((value) => {
  38. return ['org', 'private', 'public'].indexOf(value)>= 0;
  39. }),
  40. }),
  41. }));
  42. }
  43. checkCards(trelloCards) {
  44. check(trelloCards, [Match.ObjectIncluding({
  45. closed: Boolean,
  46. dateLastActivity: DateString,
  47. desc: String,
  48. idLabels: [String],
  49. idMembers: [String],
  50. name: String,
  51. pos: Number,
  52. })]);
  53. }
  54. checkLabels(trelloLabels) {
  55. check(trelloLabels, [Match.ObjectIncluding({
  56. // XXX refine control by validating 'color' against a list of allowed
  57. // values (is it worth the maintenance?)
  58. color: String,
  59. name: String,
  60. })]);
  61. }
  62. checkLists(trelloLists) {
  63. check(trelloLists, [Match.ObjectIncluding({
  64. closed: Boolean,
  65. name: String,
  66. })]);
  67. }
  68. // You must call parseActions before calling this one.
  69. createBoardAndLabels(trelloBoard) {
  70. const createdAt = this.createdAt.board;
  71. const boardToCreate = {
  72. archived: trelloBoard.closed,
  73. color: this.getColor(trelloBoard.prefs.background),
  74. createdAt,
  75. labels: [],
  76. members: [{
  77. userId: Meteor.userId(),
  78. isAdmin: true,
  79. isActive: true,
  80. }],
  81. permission: this.getPermission(trelloBoard.prefs.permissionLevel),
  82. slug: getSlug(trelloBoard.name) || 'board',
  83. stars: 0,
  84. title: trelloBoard.name,
  85. };
  86. trelloBoard.labels.forEach((label) => {
  87. const labelToCreate = {
  88. _id: Random.id(6),
  89. color: label.color,
  90. name: label.name,
  91. };
  92. // We need to remember them by Trello ID, as this is the only ref we have
  93. // when importing cards.
  94. this.labels[label.id] = labelToCreate._id;
  95. boardToCreate.labels.push(labelToCreate);
  96. });
  97. const now = new Date();
  98. const boardId = Boards.direct.insert(boardToCreate);
  99. Boards.direct.update(boardId, {$set: {modifiedAt: now}});
  100. // log activity
  101. Activities.direct.insert({
  102. activityType: 'importBoard',
  103. boardId,
  104. createdAt: now,
  105. source: {
  106. id: trelloBoard.id,
  107. system: 'Trello',
  108. url: trelloBoard.url,
  109. },
  110. // We attribute the import to current user, not the one from the original
  111. // object.
  112. userId: Meteor.userId(),
  113. });
  114. return boardId;
  115. }
  116. // Create labels if they do not exist and load this.labels.
  117. createLabels(trelloLabels, board) {
  118. trelloLabels.forEach((label) => {
  119. const color = label.color;
  120. const name = label.name;
  121. const existingLabel = board.getLabel(name, color);
  122. if (existingLabel) {
  123. this.labels[label.id] = existingLabel._id;
  124. } else {
  125. const idLabelCreated = board.pushLabel(name, color);
  126. this.labels[label.id] = idLabelCreated;
  127. }
  128. });
  129. }
  130. createLists(trelloLists, boardId) {
  131. trelloLists.forEach((list) => {
  132. const listToCreate = {
  133. archived: list.closed,
  134. boardId,
  135. // We are being defensing here by providing a default date (now) if the
  136. // creation date wasn't found on the action log. This happen on old
  137. // Trello boards (eg from 2013) that didn't log the 'createList' action
  138. // we require.
  139. createdAt: new Date(this.createdAt.lists[list.id] || Date.now()),
  140. title: list.name,
  141. userId: Meteor.userId(),
  142. };
  143. const listId = Lists.direct.insert(listToCreate);
  144. const now = new Date();
  145. Lists.direct.update(listId, {$set: {'updatedAt': now}});
  146. this.lists[list.id] = listId;
  147. // log activity
  148. Activities.direct.insert({
  149. activityType: 'importList',
  150. boardId,
  151. createdAt: now,
  152. listId,
  153. source: {
  154. id: list.id,
  155. system: 'Trello',
  156. },
  157. // We attribute the import to current user, not the one from the
  158. // original object
  159. userId: Meteor.userId(),
  160. });
  161. });
  162. }
  163. createCardsAndComments(trelloCards, boardId) {
  164. const result = [];
  165. trelloCards.forEach((card) => {
  166. const cardToCreate = {
  167. archived: card.closed,
  168. boardId,
  169. createdAt: new Date(this.createdAt.cards[card.id] || Date.now()),
  170. dateLastActivity: new Date(),
  171. description: card.desc,
  172. listId: this.lists[card.idList],
  173. sort: card.pos,
  174. title: card.name,
  175. // XXX use the original user?
  176. userId: Meteor.userId(),
  177. };
  178. // add labels
  179. if (card.idLabels) {
  180. cardToCreate.labelIds = card.idLabels.map((trelloId) => {
  181. return this.labels[trelloId];
  182. });
  183. }
  184. // insert card
  185. const cardId = Cards.direct.insert(cardToCreate);
  186. // log activity
  187. Activities.direct.insert({
  188. activityType: 'importCard',
  189. boardId,
  190. cardId,
  191. createdAt: new Date(),
  192. listId: cardToCreate.listId,
  193. source: {
  194. id: card.id,
  195. system: 'Trello',
  196. url: card.url,
  197. },
  198. // we attribute the import to current user, not the one from the
  199. // original card
  200. userId: Meteor.userId(),
  201. });
  202. // add comments
  203. const comments = this.comments[card.id];
  204. if (comments) {
  205. comments.forEach((comment) => {
  206. const commentToCreate = {
  207. boardId,
  208. cardId,
  209. createdAt: comment.date,
  210. text: comment.data.text,
  211. // XXX use the original comment user instead
  212. userId: Meteor.userId(),
  213. };
  214. // dateLastActivity will be set from activity insert, no need to
  215. // update it ourselves
  216. const commentId = CardComments.direct.insert(commentToCreate);
  217. Activities.direct.insert({
  218. activityType: 'addComment',
  219. boardId: commentToCreate.boardId,
  220. cardId: commentToCreate.cardId,
  221. commentId,
  222. createdAt: commentToCreate.createdAt,
  223. userId: commentToCreate.userId,
  224. });
  225. });
  226. }
  227. // XXX add attachments
  228. result.push(cardId);
  229. });
  230. return result;
  231. }
  232. getColor(trelloColorCode) {
  233. // trello color name => wekan color
  234. const mapColors = {
  235. 'blue': 'belize',
  236. 'orange': 'pumpkin',
  237. 'green': 'nephritis',
  238. 'red': 'pomegranate',
  239. 'purple': 'wisteria',
  240. 'pink': 'pomegranate',
  241. 'lime': 'nephritis',
  242. 'sky': 'belize',
  243. 'grey': 'midnight',
  244. };
  245. const wekanColor = mapColors[trelloColorCode];
  246. return wekanColor || Boards.simpleSchema()._schema.color.allowedValues[0];
  247. }
  248. getPermission(trelloPermissionCode) {
  249. if (trelloPermissionCode === 'public') {
  250. return 'public';
  251. }
  252. // Wekan does NOT have organization level, so we default both 'private' and
  253. // 'org' to private.
  254. return 'private';
  255. }
  256. parseActions(trelloActions) {
  257. trelloActions.forEach((action) => {
  258. switch (action.type) {
  259. case 'createBoard':
  260. this.createdAt.board = action.date;
  261. break;
  262. case 'createCard':
  263. const cardId = action.data.card.id;
  264. this.createdAt.cards[cardId] = action.date;
  265. break;
  266. case 'createList':
  267. const listId = action.data.list.id;
  268. this.createdAt.lists[listId] = action.date;
  269. break;
  270. case 'commentCard':
  271. const id = action.data.card.id;
  272. if (this.comments[id]) {
  273. this.comments[id].push(action);
  274. } else {
  275. this.comments[id] = [action];
  276. }
  277. break;
  278. default:
  279. // do nothing
  280. break;
  281. }
  282. });
  283. }
  284. }
  285. Meteor.methods({
  286. importTrelloBoard(trelloBoard, data) {
  287. const trelloCreator = new TrelloCreator();
  288. // 1. check all parameters are ok from a syntax point of view
  289. try {
  290. // we don't use additional data - this should be an empty object
  291. check(data, {});
  292. trelloCreator.checkActions(trelloBoard.actions);
  293. trelloCreator.checkBoard(trelloBoard);
  294. trelloCreator.checkLabels(trelloBoard.labels);
  295. trelloCreator.checkLists(trelloBoard.lists);
  296. trelloCreator.checkCards(trelloBoard.cards);
  297. } catch (e) {
  298. throw new Meteor.Error('error-json-schema');
  299. }
  300. // 2. check parameters are ok from a business point of view (exist &
  301. // authorized) nothing to check, everyone can import boards in their account
  302. // 3. create all elements
  303. trelloCreator.parseActions(trelloBoard.actions);
  304. const boardId = trelloCreator.createBoardAndLabels(trelloBoard);
  305. trelloCreator.createLists(trelloBoard.lists, boardId);
  306. trelloCreator.createCardsAndComments(trelloBoard.cards, boardId);
  307. // XXX add members
  308. return boardId;
  309. },
  310. importTrelloCard(trelloCard, data) {
  311. const trelloCreator = new TrelloCreator();
  312. // 1. check parameters are ok from a syntax point of view
  313. try {
  314. check(data, {
  315. listId: String,
  316. sortIndex: Number,
  317. });
  318. trelloCreator.checkCards([trelloCard]);
  319. trelloCreator.checkLabels(trelloCard.labels);
  320. trelloCreator.checkActions(trelloCard.actions);
  321. } catch(e) {
  322. throw new Meteor.Error('error-json-schema');
  323. }
  324. // 2. check parameters are ok from a business point of view (exist &
  325. // authorized)
  326. const list = Lists.findOne(data.listId);
  327. if (!list) {
  328. throw new Meteor.Error('error-list-doesNotExist');
  329. }
  330. if (Meteor.isServer) {
  331. if (!allowIsBoardMember(Meteor.userId(), Boards.findOne(list.boardId))) {
  332. throw new Meteor.Error('error-board-notAMember');
  333. }
  334. }
  335. // 3. create all elements
  336. trelloCreator.lists[trelloCard.idList] = data.listId;
  337. trelloCreator.parseActions(trelloCard.actions);
  338. const board = list.board();
  339. trelloCreator.createLabels(trelloCard.labels, board);
  340. const cardIds = trelloCreator.createCardsAndComments([trelloCard], board._id);
  341. return cardIds[0];
  342. },
  343. });