cards.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. if (Meteor.isServer) {
  231. // Cards are often fetched within a board, so we create an index to make these
  232. // queries more efficient.
  233. Meteor.startup(() => {
  234. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  235. });
  236. Cards.after.insert((userId, doc) => {
  237. Activities.insert({
  238. userId,
  239. activityType: 'createCard',
  240. boardId: doc.boardId,
  241. listId: doc.listId,
  242. cardId: doc._id,
  243. });
  244. });
  245. // New activity for card (un)archivage
  246. Cards.after.update((userId, doc, fieldNames) => {
  247. if (_.contains(fieldNames, 'archived')) {
  248. if (doc.archived) {
  249. Activities.insert({
  250. userId,
  251. activityType: 'archivedCard',
  252. boardId: doc.boardId,
  253. listId: doc.listId,
  254. cardId: doc._id,
  255. });
  256. } else {
  257. Activities.insert({
  258. userId,
  259. activityType: 'restoredCard',
  260. boardId: doc.boardId,
  261. listId: doc.listId,
  262. cardId: doc._id,
  263. });
  264. }
  265. }
  266. });
  267. // New activity for card moves
  268. Cards.after.update(function(userId, doc, fieldNames) {
  269. const oldListId = this.previous.listId;
  270. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  271. Activities.insert({
  272. userId,
  273. oldListId,
  274. activityType: 'moveCard',
  275. listId: doc.listId,
  276. boardId: doc.boardId,
  277. cardId: doc._id,
  278. });
  279. }
  280. });
  281. // Add a new activity if we add or remove a member to the card
  282. Cards.before.update((userId, doc, fieldNames, modifier) => {
  283. if (!_.contains(fieldNames, 'members'))
  284. return;
  285. let memberId;
  286. // Say hello to the new member
  287. if (modifier.$addToSet && modifier.$addToSet.members) {
  288. memberId = modifier.$addToSet.members;
  289. if (!_.contains(doc.members, memberId)) {
  290. Activities.insert({
  291. userId,
  292. memberId,
  293. activityType: 'joinMember',
  294. boardId: doc.boardId,
  295. cardId: doc._id,
  296. });
  297. }
  298. }
  299. // Say goodbye to the former member
  300. if (modifier.$pull && modifier.$pull.members) {
  301. memberId = modifier.$pull.members;
  302. // Check that the former member is member of the card
  303. if (_.contains(doc.members, memberId)) {
  304. Activities.insert({
  305. userId,
  306. memberId,
  307. activityType: 'unjoinMember',
  308. boardId: doc.boardId,
  309. cardId: doc._id,
  310. });
  311. }
  312. }
  313. });
  314. // Remove all activities associated with a card if we remove the card
  315. // Remove also card_comments / checklists / attachments
  316. Cards.after.remove((userId, doc) => {
  317. Activities.remove({
  318. cardId: doc._id,
  319. });
  320. Checklists.remove({
  321. cardId: doc._id,
  322. });
  323. CardComments.remove({
  324. cardId: doc._id,
  325. });
  326. Attachments.remove({
  327. cardId: doc._id,
  328. });
  329. });
  330. }