cards.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. absoluteUrl() {
  132. const board = this.board();
  133. return FlowRouter.url('card', {
  134. boardId: board._id,
  135. slug: board.slug,
  136. cardId: this._id,
  137. });
  138. },
  139. });
  140. Cards.mutations({
  141. archive() {
  142. return { $set: { archived: true }};
  143. },
  144. restore() {
  145. return { $set: { archived: false }};
  146. },
  147. setTitle(title) {
  148. return { $set: { title }};
  149. },
  150. setDescription(description) {
  151. return { $set: { description }};
  152. },
  153. move(listId, sortIndex) {
  154. const mutatedFields = { listId };
  155. if (sortIndex) {
  156. mutatedFields.sort = sortIndex;
  157. }
  158. return { $set: mutatedFields };
  159. },
  160. addLabel(labelId) {
  161. return { $addToSet: { labelIds: labelId }};
  162. },
  163. removeLabel(labelId) {
  164. return { $pull: { labelIds: labelId }};
  165. },
  166. toggleLabel(labelId) {
  167. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  168. return this.removeLabel(labelId);
  169. } else {
  170. return this.addLabel(labelId);
  171. }
  172. },
  173. assignMember(memberId) {
  174. return { $addToSet: { members: memberId }};
  175. },
  176. unassignMember(memberId) {
  177. return { $pull: { members: memberId }};
  178. },
  179. toggleMember(memberId) {
  180. if (this.members && this.members.indexOf(memberId) > -1) {
  181. return this.unassignMember(memberId);
  182. } else {
  183. return this.assignMember(memberId);
  184. }
  185. },
  186. setCover(coverId) {
  187. return { $set: { coverId }};
  188. },
  189. unsetCover() {
  190. return { $unset: { coverId: '' }};
  191. },
  192. setStart(startAt) {
  193. return { $set: { startAt }};
  194. },
  195. unsetStart() {
  196. return { $unset: { startAt: '' }};
  197. },
  198. setDue(dueAt) {
  199. return { $set: { dueAt }};
  200. },
  201. unsetDue() {
  202. return { $unset: { dueAt: '' }};
  203. },
  204. });
  205. if (Meteor.isServer) {
  206. // Cards are often fetched within a board, so we create an index to make these
  207. // queries more efficient.
  208. Meteor.startup(() => {
  209. Cards._collection._ensureIndex({ boardId: 1 });
  210. });
  211. Cards.after.insert((userId, doc) => {
  212. Activities.insert({
  213. userId,
  214. activityType: 'createCard',
  215. boardId: doc.boardId,
  216. listId: doc.listId,
  217. cardId: doc._id,
  218. });
  219. });
  220. // New activity for card (un)archivage
  221. Cards.after.update((userId, doc, fieldNames) => {
  222. if (_.contains(fieldNames, 'archived')) {
  223. if (doc.archived) {
  224. Activities.insert({
  225. userId,
  226. activityType: 'archivedCard',
  227. boardId: doc.boardId,
  228. listId: doc.listId,
  229. cardId: doc._id,
  230. });
  231. } else {
  232. Activities.insert({
  233. userId,
  234. activityType: 'restoredCard',
  235. boardId: doc.boardId,
  236. listId: doc.listId,
  237. cardId: doc._id,
  238. });
  239. }
  240. }
  241. });
  242. // New activity for card moves
  243. Cards.after.update(function(userId, doc, fieldNames) {
  244. const oldListId = this.previous.listId;
  245. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  246. Activities.insert({
  247. userId,
  248. oldListId,
  249. activityType: 'moveCard',
  250. listId: doc.listId,
  251. boardId: doc.boardId,
  252. cardId: doc._id,
  253. });
  254. }
  255. });
  256. // Add a new activity if we add or remove a member to the card
  257. Cards.before.update((userId, doc, fieldNames, modifier) => {
  258. if (!_.contains(fieldNames, 'members'))
  259. return;
  260. let memberId;
  261. // Say hello to the new member
  262. if (modifier.$addToSet && modifier.$addToSet.members) {
  263. memberId = modifier.$addToSet.members;
  264. if (!_.contains(doc.members, memberId)) {
  265. Activities.insert({
  266. userId,
  267. memberId,
  268. activityType: 'joinMember',
  269. boardId: doc.boardId,
  270. cardId: doc._id,
  271. });
  272. }
  273. }
  274. // Say goodbye to the former member
  275. if (modifier.$pull && modifier.$pull.members) {
  276. memberId = modifier.$pull.members;
  277. Activities.insert({
  278. userId,
  279. memberId,
  280. activityType: 'unjoinMember',
  281. boardId: doc.boardId,
  282. cardId: doc._id,
  283. });
  284. }
  285. });
  286. // Remove all activities associated with a card if we remove the card
  287. Cards.after.remove((userId, doc) => {
  288. Activities.remove({
  289. cardId: doc._id,
  290. });
  291. });
  292. }