import.js 10 KB

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