lists.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { ALLOWED_COLORS } from '/config/const';
  3. Lists = new Mongo.Collection('lists');
  4. /**
  5. * A list (column) in the Wekan board.
  6. */
  7. Lists.attachSchema(
  8. new SimpleSchema({
  9. title: {
  10. /**
  11. * the title of the list
  12. */
  13. type: String,
  14. },
  15. starred: {
  16. /**
  17. * if a list is stared
  18. * then we put it on the top
  19. */
  20. type: Boolean,
  21. optional: true,
  22. defaultValue: false,
  23. },
  24. archived: {
  25. /**
  26. * is the list archived
  27. */
  28. type: Boolean,
  29. // eslint-disable-next-line consistent-return
  30. autoValue() {
  31. if (this.isInsert && !this.isSet) {
  32. return false;
  33. }
  34. },
  35. },
  36. archivedAt: {
  37. /**
  38. * latest archiving date
  39. */
  40. type: Date,
  41. optional: true,
  42. },
  43. boardId: {
  44. /**
  45. * the board associated to this list
  46. */
  47. type: String,
  48. },
  49. swimlaneId: {
  50. /**
  51. * the swimlane associated to this list. Optional for backward compatibility
  52. */
  53. type: String,
  54. optional: true,
  55. defaultValue: '',
  56. },
  57. createdAt: {
  58. /**
  59. * creation date
  60. */
  61. type: Date,
  62. // eslint-disable-next-line consistent-return
  63. autoValue() {
  64. if (this.isInsert) {
  65. return new Date();
  66. } else if (this.isUpsert) {
  67. return { $setOnInsert: new Date() };
  68. } else {
  69. this.unset();
  70. }
  71. },
  72. },
  73. sort: {
  74. /**
  75. * is the list sorted
  76. */
  77. type: Number,
  78. decimal: true,
  79. // XXX We should probably provide a default
  80. optional: true,
  81. },
  82. updatedAt: {
  83. /**
  84. * last update of the list
  85. */
  86. type: Date,
  87. optional: true,
  88. // eslint-disable-next-line consistent-return
  89. autoValue() {
  90. if (this.isUpdate || this.isUpsert || this.isInsert) {
  91. return new Date();
  92. } else {
  93. this.unset();
  94. }
  95. },
  96. },
  97. modifiedAt: {
  98. type: Date,
  99. denyUpdate: false,
  100. // eslint-disable-next-line consistent-return
  101. autoValue() {
  102. // this is redundant with updatedAt
  103. /*if (this.isInsert || this.isUpsert || this.isUpdate) {
  104. return new Date();
  105. } else {
  106. this.unset();
  107. }*/
  108. if (!this.isSet) {
  109. return new Date();
  110. }
  111. },
  112. },
  113. wipLimit: {
  114. /**
  115. * WIP object, see below
  116. */
  117. type: Object,
  118. optional: true,
  119. },
  120. 'wipLimit.value': {
  121. /**
  122. * value of the WIP
  123. */
  124. type: Number,
  125. decimal: false,
  126. defaultValue: 1,
  127. },
  128. 'wipLimit.enabled': {
  129. /**
  130. * is the WIP enabled
  131. */
  132. type: Boolean,
  133. defaultValue: false,
  134. },
  135. 'wipLimit.soft': {
  136. /**
  137. * is the WIP a soft or hard requirement
  138. */
  139. type: Boolean,
  140. defaultValue: false,
  141. },
  142. color: {
  143. /**
  144. * the color of the list
  145. */
  146. type: String,
  147. optional: true,
  148. // silver is the default
  149. allowedValues: ALLOWED_COLORS,
  150. },
  151. type: {
  152. /**
  153. * The type of list
  154. */
  155. type: String,
  156. defaultValue: 'list',
  157. },
  158. collapsed: {
  159. /**
  160. * is the list collapsed
  161. */
  162. type: Boolean,
  163. defaultValue: false,
  164. },
  165. }),
  166. );
  167. Lists.allow({
  168. insert(userId, doc) {
  169. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  170. },
  171. update(userId, doc) {
  172. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  173. },
  174. remove(userId, doc) {
  175. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  176. },
  177. fetch: ['boardId'],
  178. });
  179. Lists.helpers({
  180. copy(boardId, swimlaneId) {
  181. const oldId = this._id;
  182. const oldSwimlaneId = this.swimlaneId || null;
  183. this.boardId = boardId;
  184. this.swimlaneId = swimlaneId;
  185. let _id = null;
  186. const existingListWithSameName = ReactiveCache.getList({
  187. boardId,
  188. title: this.title,
  189. archived: false,
  190. });
  191. if (existingListWithSameName) {
  192. _id = existingListWithSameName._id;
  193. } else {
  194. delete this._id;
  195. this.swimlaneId = swimlaneId; // Set the target swimlane for the copied list
  196. _id = Lists.insert(this);
  197. }
  198. // Copy all cards in list
  199. ReactiveCache.getCards({
  200. swimlaneId: oldSwimlaneId,
  201. listId: oldId,
  202. archived: false,
  203. }).forEach(card => {
  204. card.copy(boardId, swimlaneId, _id);
  205. });
  206. },
  207. move(boardId, swimlaneId) {
  208. const boardList = ReactiveCache.getList({
  209. boardId,
  210. title: this.title,
  211. archived: false,
  212. });
  213. let listId;
  214. if (boardList) {
  215. listId = boardList._id;
  216. this.cards().forEach(card => {
  217. card.move(boardId, this._id, boardList._id);
  218. });
  219. } else {
  220. console.log('list.title:', this.title);
  221. console.log('boardList:', boardList);
  222. listId = Lists.insert({
  223. title: this.title,
  224. boardId,
  225. type: this.type,
  226. archived: false,
  227. wipLimit: this.wipLimit,
  228. swimlaneId: swimlaneId, // Set the target swimlane for the moved list
  229. });
  230. }
  231. this.cards(swimlaneId).forEach(card => {
  232. card.move(boardId, swimlaneId, listId);
  233. });
  234. },
  235. cards(swimlaneId) {
  236. const selector = {
  237. listId: this._id,
  238. archived: false,
  239. };
  240. if (swimlaneId) selector.swimlaneId = swimlaneId;
  241. const ret = ReactiveCache.getCards(Filter.mongoSelector(selector), { sort: ['sort'] });
  242. return ret;
  243. },
  244. cardsUnfiltered(swimlaneId) {
  245. const selector = {
  246. listId: this._id,
  247. archived: false,
  248. };
  249. if (swimlaneId) selector.swimlaneId = swimlaneId;
  250. const ret = ReactiveCache.getCards(selector, { sort: ['sort'] });
  251. return ret;
  252. },
  253. allCards() {
  254. const ret = ReactiveCache.getCards({ listId: this._id });
  255. return ret;
  256. },
  257. board() {
  258. return ReactiveCache.getBoard(this.boardId);
  259. },
  260. getWipLimit(option) {
  261. const list = ReactiveCache.getList(this._id);
  262. if (!list.wipLimit) {
  263. // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  264. return 0;
  265. } else if (!option) {
  266. return list.wipLimit;
  267. } else {
  268. return list.wipLimit[option] ? list.wipLimit[option] : 0; // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  269. }
  270. },
  271. colorClass() {
  272. if (this.color) return `list-header-${this.color}`;
  273. return '';
  274. },
  275. isTemplateList() {
  276. return this.type === 'template-list';
  277. },
  278. isStarred() {
  279. return this.starred === true;
  280. },
  281. isCollapsed() {
  282. return this.collapsed === true;
  283. },
  284. absoluteUrl() {
  285. const card = ReactiveCache.getCard({ listId: this._id });
  286. return card && card.absoluteUrl();
  287. },
  288. originRelativeUrl() {
  289. const card = ReactiveCache.getCard({ listId: this._id });
  290. return card && card.originRelativeUrl();
  291. },
  292. remove() {
  293. Lists.remove({ _id: this._id });
  294. },
  295. });
  296. Lists.mutations({
  297. rename(title) {
  298. // Sanitize title on client side as well
  299. if (typeof title === 'string') {
  300. const { sanitizeTitle } = require('../server/lib/inputSanitizer');
  301. const sanitizedTitle = sanitizeTitle(title);
  302. if (process.env.DEBUG === 'true' && sanitizedTitle !== title) {
  303. console.warn('Client-side sanitized list title:', title, '->', sanitizedTitle);
  304. }
  305. return { $set: { title: sanitizedTitle } };
  306. }
  307. return { $set: { title } };
  308. },
  309. star(enable = true) {
  310. return { $set: { starred: !!enable } };
  311. },
  312. collapse(enable = true) {
  313. return { $set: { collapsed: !!enable } };
  314. },
  315. archive() {
  316. if (this.isTemplateList()) {
  317. this.cards().forEach(card => {
  318. return card.archive();
  319. });
  320. }
  321. return { $set: { archived: true, archivedAt: new Date() } };
  322. },
  323. restore() {
  324. if (this.isTemplateList()) {
  325. this.allCards().forEach(card => {
  326. return card.restore();
  327. });
  328. }
  329. return { $set: { archived: false } };
  330. },
  331. toggleSoftLimit(toggle) {
  332. return { $set: { 'wipLimit.soft': toggle } };
  333. },
  334. toggleWipLimit(toggle) {
  335. return { $set: { 'wipLimit.enabled': toggle } };
  336. },
  337. setWipLimit(limit) {
  338. return { $set: { 'wipLimit.value': limit } };
  339. },
  340. setColor(newColor) {
  341. return {
  342. $set: {
  343. color: newColor,
  344. },
  345. };
  346. },
  347. });
  348. Lists.userArchivedLists = userId => {
  349. return ReactiveCache.getLists({
  350. boardId: { $in: Boards.userBoardIds(userId, null) },
  351. archived: true,
  352. })
  353. };
  354. Lists.userArchivedListIds = () => {
  355. return Lists.userArchivedLists().map(list => { return list._id; });
  356. };
  357. Lists.archivedLists = () => {
  358. return ReactiveCache.getLists({ archived: true });
  359. };
  360. Lists.archivedListIds = () => {
  361. return Lists.archivedLists().map(list => {
  362. return list._id;
  363. });
  364. };
  365. Meteor.methods({
  366. applyWipLimit(listId, limit) {
  367. check(listId, String);
  368. check(limit, Number);
  369. if (limit === 0) {
  370. limit = 1;
  371. }
  372. ReactiveCache.getList(listId).setWipLimit(limit);
  373. },
  374. enableWipLimit(listId) {
  375. check(listId, String);
  376. const list = ReactiveCache.getList(listId);
  377. if (list.getWipLimit('value') === 0) {
  378. list.setWipLimit(1);
  379. }
  380. list.toggleWipLimit(!list.getWipLimit('enabled'));
  381. },
  382. enableSoftLimit(listId) {
  383. check(listId, String);
  384. const list = ReactiveCache.getList(listId);
  385. list.toggleSoftLimit(!list.getWipLimit('soft'));
  386. },
  387. myLists() {
  388. // my lists
  389. return _.uniq(
  390. ReactiveCache.getLists(
  391. {
  392. boardId: { $in: Boards.userBoardIds(this.userId) },
  393. archived: false,
  394. },
  395. {
  396. fields: { title: 1 },
  397. },
  398. )
  399. .map(list => {
  400. return list.title;
  401. }),
  402. ).sort();
  403. },
  404. });
  405. Lists.hookOptions.after.update = { fetchPrevious: false };
  406. if (Meteor.isServer) {
  407. Meteor.startup(() => {
  408. Lists._collection.createIndex({ modifiedAt: -1 });
  409. Lists._collection.createIndex({ boardId: 1 });
  410. Lists._collection.createIndex({ archivedAt: -1 });
  411. });
  412. Lists.after.insert((userId, doc) => {
  413. Activities.insert({
  414. userId,
  415. type: 'list',
  416. activityType: 'createList',
  417. boardId: doc.boardId,
  418. listId: doc._id,
  419. // this preserves the name so that the activity can be useful after the
  420. // list is deleted
  421. title: doc.title,
  422. });
  423. });
  424. Lists.before.remove((userId, doc) => {
  425. const cards = ReactiveCache.getCards({ listId: doc._id });
  426. if (cards) {
  427. cards.forEach(card => {
  428. Cards.remove(card._id);
  429. });
  430. }
  431. Activities.insert({
  432. userId,
  433. type: 'list',
  434. activityType: 'removeList',
  435. boardId: doc.boardId,
  436. listId: doc._id,
  437. title: doc.title,
  438. });
  439. });
  440. Lists.after.update((userId, doc, fieldNames) => {
  441. if (fieldNames.includes('title')) {
  442. Activities.insert({
  443. userId,
  444. type: 'list',
  445. activityType: 'changedListTitle',
  446. listId: doc._id,
  447. boardId: doc.boardId,
  448. // this preserves the name so that the activity can be useful after the
  449. // list is deleted
  450. title: doc.title,
  451. });
  452. } else if (doc.archived) {
  453. Activities.insert({
  454. userId,
  455. type: 'list',
  456. activityType: 'archivedList',
  457. listId: doc._id,
  458. boardId: doc.boardId,
  459. // this preserves the name so that the activity can be useful after the
  460. // list is deleted
  461. title: doc.title,
  462. });
  463. } else if (fieldNames.includes('archived')) {
  464. Activities.insert({
  465. userId,
  466. type: 'list',
  467. activityType: 'restoredList',
  468. listId: doc._id,
  469. boardId: doc.boardId,
  470. // this preserves the name so that the activity can be useful after the
  471. // list is deleted
  472. title: doc.title,
  473. });
  474. }
  475. });
  476. }
  477. //LISTS REST API
  478. if (Meteor.isServer) {
  479. /**
  480. * @operation get_all_lists
  481. * @summary Get the list of Lists attached to a board
  482. *
  483. * @param {string} boardId the board ID
  484. * @return_type [{_id: string,
  485. * title: string}]
  486. */
  487. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  488. try {
  489. const paramBoardId = req.params.boardId;
  490. Authentication.checkBoardAccess(req.userId, paramBoardId);
  491. JsonRoutes.sendResult(res, {
  492. code: 200,
  493. data: ReactiveCache.getLists({ boardId: paramBoardId, archived: false }).map(
  494. function(doc) {
  495. return {
  496. _id: doc._id,
  497. title: doc.title,
  498. };
  499. },
  500. ),
  501. });
  502. } catch (error) {
  503. JsonRoutes.sendResult(res, {
  504. code: 200,
  505. data: error,
  506. });
  507. }
  508. });
  509. /**
  510. * @operation get_list
  511. * @summary Get a List attached to a board
  512. *
  513. * @param {string} boardId the board ID
  514. * @param {string} listId the List ID
  515. * @return_type Lists
  516. */
  517. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  518. req,
  519. res,
  520. ) {
  521. try {
  522. const paramBoardId = req.params.boardId;
  523. const paramListId = req.params.listId;
  524. Authentication.checkBoardAccess(req.userId, paramBoardId);
  525. JsonRoutes.sendResult(res, {
  526. code: 200,
  527. data: ReactiveCache.getList({
  528. _id: paramListId,
  529. boardId: paramBoardId,
  530. archived: false,
  531. }),
  532. });
  533. } catch (error) {
  534. JsonRoutes.sendResult(res, {
  535. code: 200,
  536. data: error,
  537. });
  538. }
  539. });
  540. /**
  541. * @operation new_list
  542. * @summary Add a List to a board
  543. *
  544. * @param {string} boardId the board ID
  545. * @param {string} title the title of the List
  546. * @return_type {_id: string}
  547. */
  548. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  549. try {
  550. const paramBoardId = req.params.boardId;
  551. Authentication.checkBoardAccess(req.userId, paramBoardId);
  552. const board = ReactiveCache.getBoard(paramBoardId);
  553. const id = Lists.insert({
  554. title: req.body.title,
  555. boardId: paramBoardId,
  556. sort: board.lists().length,
  557. swimlaneId: req.body.swimlaneId || board.getDefaultSwimline()._id, // Use provided swimlaneId or default
  558. });
  559. JsonRoutes.sendResult(res, {
  560. code: 200,
  561. data: {
  562. _id: id,
  563. },
  564. });
  565. } catch (error) {
  566. JsonRoutes.sendResult(res, {
  567. code: 200,
  568. data: error,
  569. });
  570. }
  571. });
  572. /**
  573. * @operation edit_list
  574. * @summary Edit a List
  575. *
  576. * @description This updates a list on a board.
  577. * You can update the title, color, wipLimit, starred, and collapsed properties.
  578. *
  579. * @param {string} boardId the board ID
  580. * @param {string} listId the ID of the list to update
  581. * @param {string} [title] the new title of the list
  582. * @param {string} [color] the new color of the list
  583. * @param {Object} [wipLimit] the WIP limit configuration
  584. * @param {boolean} [starred] whether the list is starred
  585. * @param {boolean} [collapsed] whether the list is collapsed
  586. * @return_type {_id: string}
  587. */
  588. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId', function(
  589. req,
  590. res,
  591. ) {
  592. try {
  593. const paramBoardId = req.params.boardId;
  594. const paramListId = req.params.listId;
  595. let updated = false;
  596. Authentication.checkBoardAccess(req.userId, paramBoardId);
  597. const list = ReactiveCache.getList({
  598. _id: paramListId,
  599. boardId: paramBoardId,
  600. archived: false,
  601. });
  602. if (!list) {
  603. JsonRoutes.sendResult(res, {
  604. code: 404,
  605. data: { error: 'List not found' },
  606. });
  607. return;
  608. }
  609. // Update title if provided
  610. if (req.body.title) {
  611. const { sanitizeTitle } = require('../server/lib/inputSanitizer');
  612. const newTitle = sanitizeTitle(req.body.title);
  613. if (process.env.DEBUG === 'true' && newTitle !== req.body.title) {
  614. console.warn('Sanitized list title input:', req.body.title, '->', newTitle);
  615. }
  616. Lists.direct.update(
  617. {
  618. _id: paramListId,
  619. boardId: paramBoardId,
  620. archived: false,
  621. },
  622. {
  623. $set: {
  624. title: newTitle,
  625. },
  626. },
  627. );
  628. updated = true;
  629. }
  630. // Update color if provided
  631. if (req.body.color) {
  632. const newColor = req.body.color;
  633. Lists.direct.update(
  634. {
  635. _id: paramListId,
  636. boardId: paramBoardId,
  637. archived: false,
  638. },
  639. {
  640. $set: {
  641. color: newColor,
  642. },
  643. },
  644. );
  645. updated = true;
  646. }
  647. // Update starred status if provided
  648. if (req.body.hasOwnProperty('starred')) {
  649. const newStarred = req.body.starred;
  650. Lists.direct.update(
  651. {
  652. _id: paramListId,
  653. boardId: paramBoardId,
  654. archived: false,
  655. },
  656. {
  657. $set: {
  658. starred: newStarred,
  659. },
  660. },
  661. );
  662. updated = true;
  663. }
  664. // Update collapsed status if provided
  665. if (req.body.hasOwnProperty('collapsed')) {
  666. const newCollapsed = req.body.collapsed;
  667. Lists.direct.update(
  668. {
  669. _id: paramListId,
  670. boardId: paramBoardId,
  671. archived: false,
  672. },
  673. {
  674. $set: {
  675. collapsed: newCollapsed,
  676. },
  677. },
  678. );
  679. updated = true;
  680. }
  681. // Update wipLimit if provided
  682. if (req.body.wipLimit) {
  683. const newWipLimit = req.body.wipLimit;
  684. Lists.direct.update(
  685. {
  686. _id: paramListId,
  687. boardId: paramBoardId,
  688. archived: false,
  689. },
  690. {
  691. $set: {
  692. wipLimit: newWipLimit,
  693. },
  694. },
  695. );
  696. updated = true;
  697. }
  698. // Check if update is true or false
  699. if (!updated) {
  700. JsonRoutes.sendResult(res, {
  701. code: 404,
  702. data: {
  703. message: 'Error',
  704. },
  705. });
  706. return;
  707. }
  708. JsonRoutes.sendResult(res, {
  709. code: 200,
  710. data: {
  711. _id: paramListId,
  712. },
  713. });
  714. } catch (error) {
  715. JsonRoutes.sendResult(res, {
  716. code: 200,
  717. data: error,
  718. });
  719. }
  720. });
  721. /**
  722. * @operation delete_list
  723. * @summary Delete a List
  724. *
  725. * @description This **deletes** a list from a board.
  726. * The list is not put in the recycle bin.
  727. *
  728. * @param {string} boardId the board ID
  729. * @param {string} listId the ID of the list to remove
  730. * @return_type {_id: string}
  731. */
  732. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  733. req,
  734. res,
  735. ) {
  736. try {
  737. const paramBoardId = req.params.boardId;
  738. const paramListId = req.params.listId;
  739. Authentication.checkBoardAccess(req.userId, paramBoardId);
  740. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  741. JsonRoutes.sendResult(res, {
  742. code: 200,
  743. data: {
  744. _id: paramListId,
  745. },
  746. });
  747. } catch (error) {
  748. JsonRoutes.sendResult(res, {
  749. code: 200,
  750. data: error,
  751. });
  752. }
  753. });
  754. }
  755. export default Lists;