cards.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. Cards = new Mongo.Collection('cards');
  2. // XXX To improve pub/sub performances a card document should include a
  3. // de-normalized number of comments so we don't have to publish the whole list
  4. // of comments just to display the number of them in the board view.
  5. Cards.attachSchema(new SimpleSchema({
  6. title: {
  7. type: String,
  8. },
  9. archived: {
  10. type: Boolean,
  11. autoValue() { // eslint-disable-line consistent-return
  12. if (this.isInsert && !this.isSet) {
  13. return false;
  14. }
  15. },
  16. },
  17. listId: {
  18. type: String,
  19. },
  20. // The system could work without this `boardId` information (we could deduce
  21. // the board identifier from the card), but it would make the system more
  22. // difficult to manage and less efficient.
  23. boardId: {
  24. type: String,
  25. },
  26. coverId: {
  27. type: String,
  28. optional: true,
  29. },
  30. createdAt: {
  31. type: Date,
  32. autoValue() { // eslint-disable-line consistent-return
  33. if (this.isInsert) {
  34. return new Date();
  35. } else {
  36. this.unset();
  37. }
  38. },
  39. },
  40. dateLastActivity: {
  41. type: Date,
  42. autoValue() {
  43. return new Date();
  44. },
  45. },
  46. description: {
  47. type: String,
  48. optional: true,
  49. },
  50. labelIds: {
  51. type: [String],
  52. optional: true,
  53. },
  54. members: {
  55. type: [String],
  56. optional: true,
  57. },
  58. startAt: {
  59. type: Date,
  60. optional: true,
  61. },
  62. dueAt: {
  63. type: Date,
  64. optional: true,
  65. },
  66. // XXX Should probably be called `authorId`. Is it even needed since we have
  67. // the `members` field?
  68. userId: {
  69. type: String,
  70. autoValue() { // eslint-disable-line consistent-return
  71. if (this.isInsert && !this.isSet) {
  72. return this.userId;
  73. }
  74. },
  75. },
  76. sort: {
  77. type: Number,
  78. decimal: true,
  79. },
  80. }));
  81. Cards.allow({
  82. insert(userId, doc) {
  83. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  84. },
  85. update(userId, doc) {
  86. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  87. },
  88. remove(userId, doc) {
  89. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  90. },
  91. fetch: ['boardId'],
  92. });
  93. Cards.helpers({
  94. list() {
  95. return Lists.findOne(this.listId);
  96. },
  97. board() {
  98. return Boards.findOne(this.boardId);
  99. },
  100. labels() {
  101. const boardLabels = this.board().labels;
  102. const cardLabels = _.filter(boardLabels, (label) => {
  103. return _.contains(this.labelIds, label._id);
  104. });
  105. return cardLabels;
  106. },
  107. hasLabel(labelId) {
  108. return _.contains(this.labelIds, labelId);
  109. },
  110. user() {
  111. return Users.findOne(this.userId);
  112. },
  113. isAssigned(memberId) {
  114. return _.contains(this.members, memberId);
  115. },
  116. activities() {
  117. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  118. },
  119. comments() {
  120. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  121. },
  122. attachments() {
  123. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  124. },
  125. cover() {
  126. const cover = Attachments.findOne(this.coverId);
  127. // if we return a cover before it is fully stored, we will get errors when we try to display it
  128. // todo XXX we could return a default "upload pending" image in the meantime?
  129. return cover && cover.url() && cover;
  130. },
  131. checklists() {
  132. return Checklists.find({cardId: this._id}, {sort: {createdAt: 1}});
  133. },
  134. checklistItemCount() {
  135. const checklists = this.checklists().fetch();
  136. return checklists.map((checklist) => {
  137. return checklist.itemCount();
  138. }).reduce((prev, next) => {
  139. return prev + next;
  140. }, 0);
  141. },
  142. checklistFinishedCount() {
  143. const checklists = this.checklists().fetch();
  144. return checklists.map((checklist) => {
  145. return checklist.finishedCount();
  146. }).reduce((prev, next) => {
  147. return prev + next;
  148. }, 0);
  149. },
  150. checklistFinished() {
  151. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  152. },
  153. hasChecklist() {
  154. return this.checklistItemCount() !== 0;
  155. },
  156. absoluteUrl() {
  157. const board = this.board();
  158. return FlowRouter.url('card', {
  159. boardId: board._id,
  160. slug: board.slug,
  161. cardId: this._id,
  162. });
  163. },
  164. });
  165. Cards.mutations({
  166. archive() {
  167. return {$set: {archived: true}};
  168. },
  169. restore() {
  170. return {$set: {archived: false}};
  171. },
  172. setTitle(title) {
  173. return {$set: {title}};
  174. },
  175. setDescription(description) {
  176. return {$set: {description}};
  177. },
  178. move(listId, sortIndex) {
  179. const mutatedFields = {listId};
  180. if (sortIndex) {
  181. mutatedFields.sort = sortIndex;
  182. }
  183. return {$set: mutatedFields};
  184. },
  185. addLabel(labelId) {
  186. return {$addToSet: {labelIds: labelId}};
  187. },
  188. removeLabel(labelId) {
  189. return {$pull: {labelIds: labelId}};
  190. },
  191. toggleLabel(labelId) {
  192. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  193. return this.removeLabel(labelId);
  194. } else {
  195. return this.addLabel(labelId);
  196. }
  197. },
  198. assignMember(memberId) {
  199. return {$addToSet: {members: memberId}};
  200. },
  201. unassignMember(memberId) {
  202. return {$pull: {members: memberId}};
  203. },
  204. toggleMember(memberId) {
  205. if (this.members && this.members.indexOf(memberId) > -1) {
  206. return this.unassignMember(memberId);
  207. } else {
  208. return this.assignMember(memberId);
  209. }
  210. },
  211. setCover(coverId) {
  212. return {$set: {coverId}};
  213. },
  214. unsetCover() {
  215. return {$unset: {coverId: ''}};
  216. },
  217. setStart(startAt) {
  218. return {$set: {startAt}};
  219. },
  220. unsetStart() {
  221. return {$unset: {startAt: ''}};
  222. },
  223. setDue(dueAt) {
  224. return {$set: {dueAt}};
  225. },
  226. unsetDue() {
  227. return {$unset: {dueAt: ''}};
  228. },
  229. });
  230. //FUNCTIONS FOR creation of Activities
  231. function cardMove(userId, doc, fieldNames, oldListId) {
  232. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  233. Activities.insert({
  234. userId,
  235. oldListId,
  236. activityType: 'moveCard',
  237. listId: doc.listId,
  238. boardId: doc.boardId,
  239. cardId: doc._id,
  240. });
  241. }
  242. }
  243. function cardState(userId, doc, fieldNames) {
  244. if (_.contains(fieldNames, 'archived')) {
  245. if (doc.archived) {
  246. Activities.insert({
  247. userId,
  248. activityType: 'archivedCard',
  249. boardId: doc.boardId,
  250. listId: doc.listId,
  251. cardId: doc._id,
  252. });
  253. } else {
  254. Activities.insert({
  255. userId,
  256. activityType: 'restoredCard',
  257. boardId: doc.boardId,
  258. listId: doc.listId,
  259. cardId: doc._id,
  260. });
  261. }
  262. }
  263. }
  264. function cardMembers(userId, doc, fieldNames, modifier) {
  265. if (!_.contains(fieldNames, 'members'))
  266. return;
  267. let memberId;
  268. // Say hello to the new member
  269. if (modifier.$addToSet && modifier.$addToSet.members) {
  270. memberId = modifier.$addToSet.members;
  271. if (!_.contains(doc.members, memberId)) {
  272. Activities.insert({
  273. userId,
  274. memberId,
  275. activityType: 'joinMember',
  276. boardId: doc.boardId,
  277. cardId: doc._id,
  278. });
  279. }
  280. }
  281. // Say goodbye to the former member
  282. if (modifier.$pull && modifier.$pull.members) {
  283. memberId = modifier.$pull.members;
  284. // Check that the former member is member of the card
  285. if (_.contains(doc.members, memberId)) {
  286. Activities.insert({
  287. userId,
  288. memberId,
  289. activityType: 'unjoinMember',
  290. boardId: doc.boardId,
  291. cardId: doc._id,
  292. });
  293. }
  294. }
  295. }
  296. function cardCreation(userId, doc) {
  297. Activities.insert({
  298. userId,
  299. activityType: 'createCard',
  300. boardId: doc.boardId,
  301. listId: doc.listId,
  302. cardId: doc._id,
  303. });
  304. }
  305. function cardRemover(userId, doc) {
  306. Activities.remove({
  307. cardId: doc._id,
  308. });
  309. Checklists.remove({
  310. cardId: doc._id,
  311. });
  312. CardComments.remove({
  313. cardId: doc._id,
  314. });
  315. Attachments.remove({
  316. cardId: doc._id,
  317. });
  318. }
  319. if (Meteor.isServer) {
  320. // Cards are often fetched within a board, so we create an index to make these
  321. // queries more efficient.
  322. Meteor.startup(() => {
  323. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  324. });
  325. Cards.after.insert((userId, doc) => {
  326. cardCreation(userId, doc);
  327. });
  328. // New activity for card (un)archivage
  329. Cards.after.update((userId, doc, fieldNames) => {
  330. cardState(userId, doc, fieldNames);
  331. });
  332. //New activity for card moves
  333. Cards.after.update(function (userId, doc, fieldNames) {
  334. const oldListId = this.previous.listId;
  335. cardMove(userId, doc, fieldNames, oldListId);
  336. });
  337. // Add a new activity if we add or remove a member to the card
  338. Cards.before.update((userId, doc, fieldNames, modifier) => {
  339. cardMembers(userId, doc, fieldNames, modifier);
  340. });
  341. // Remove all activities associated with a card if we remove the card
  342. // Remove also card_comments / checklists / attachments
  343. Cards.after.remove((userId, doc) => {
  344. cardRemover(userId, doc);
  345. });
  346. }
  347. //LISTS REST API
  348. if (Meteor.isServer) {
  349. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  350. const paramBoardId = req.params.boardId;
  351. const paramListId = req.params.listId;
  352. Authentication.checkBoardAccess(req.userId, paramBoardId);
  353. JsonRoutes.sendResult(res, {
  354. code: 200,
  355. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  356. return {
  357. _id: doc._id,
  358. title: doc.title,
  359. description: doc.description,
  360. };
  361. }),
  362. });
  363. });
  364. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  365. const paramBoardId = req.params.boardId;
  366. const paramListId = req.params.listId;
  367. const paramCardId = req.params.cardId;
  368. Authentication.checkBoardAccess(req.userId, paramBoardId);
  369. JsonRoutes.sendResult(res, {
  370. code: 200,
  371. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  372. });
  373. });
  374. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  375. Authentication.checkUserId(req.userId);
  376. const paramBoardId = req.params.boardId;
  377. const paramListId = req.params.listId;
  378. const check = Users.findOne({_id: req.body.authorId});
  379. if (typeof check !== 'undefined') {
  380. const id = Cards.direct.insert({
  381. title: req.body.title,
  382. boardId: paramBoardId,
  383. listId: paramListId,
  384. description: req.body.description,
  385. userId: req.body.authorId,
  386. sort: 0,
  387. members: [req.body.authorId],
  388. });
  389. JsonRoutes.sendResult(res, {
  390. code: 200,
  391. data: {
  392. _id: id,
  393. },
  394. });
  395. const card = Cards.findOne({_id:id});
  396. cardCreation(req.body.authorId, card);
  397. } else {
  398. JsonRoutes.sendResult(res, {
  399. code: 401,
  400. });
  401. }
  402. });
  403. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  404. Authentication.checkUserId(req.userId);
  405. const paramBoardId = req.params.boardId;
  406. const paramCardId = req.params.cardId;
  407. const paramListId = req.params.listId;
  408. if (req.body.hasOwnProperty('title')) {
  409. const newTitle = req.body.title;
  410. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  411. {$set: {title: newTitle}});
  412. }
  413. if (req.body.hasOwnProperty('listId')) {
  414. const newParamListId = req.body.listId;
  415. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  416. {$set: {listId: newParamListId}});
  417. const card = Cards.findOne({_id: paramCardId} );
  418. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  419. }
  420. if (req.body.hasOwnProperty('description')) {
  421. const newDescription = req.body.description;
  422. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  423. {$set: {description: newDescription}});
  424. }
  425. JsonRoutes.sendResult(res, {
  426. code: 200,
  427. data: {
  428. _id: paramCardId,
  429. },
  430. });
  431. });
  432. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  433. Authentication.checkUserId(req.userId);
  434. const paramBoardId = req.params.boardId;
  435. const paramListId = req.params.listId;
  436. const paramCardId = req.params.cardId;
  437. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  438. const card = Cards.find({_id: paramCardId} );
  439. cardRemover(req.body.authorId, card);
  440. JsonRoutes.sendResult(res, {
  441. code: 200,
  442. data: {
  443. _id: paramCardId,
  444. },
  445. });
  446. });
  447. }