cards.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // XXX Should probably be called `authorId`. Is it even needed since we have
  59. // the `members` field?
  60. userId: {
  61. type: String,
  62. autoValue() { // eslint-disable-line consistent-return
  63. if (this.isInsert && !this.isSet) {
  64. return this.userId;
  65. }
  66. },
  67. },
  68. sort: {
  69. type: Number,
  70. decimal: true,
  71. },
  72. }));
  73. Cards.allow({
  74. insert(userId, doc) {
  75. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  76. },
  77. update(userId, doc) {
  78. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  79. },
  80. remove(userId, doc) {
  81. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  82. },
  83. fetch: ['boardId'],
  84. });
  85. Cards.helpers({
  86. list() {
  87. return Lists.findOne(this.listId);
  88. },
  89. board() {
  90. return Boards.findOne(this.boardId);
  91. },
  92. labels() {
  93. const boardLabels = this.board().labels;
  94. const cardLabels = _.filter(boardLabels, (label) => {
  95. return _.contains(this.labelIds, label._id);
  96. });
  97. return cardLabels;
  98. },
  99. hasLabel(labelId) {
  100. return _.contains(this.labelIds, labelId);
  101. },
  102. user() {
  103. return Users.findOne(this.userId);
  104. },
  105. isAssigned(memberId) {
  106. return _.contains(this.members, memberId);
  107. },
  108. activities() {
  109. return Activities.find({ cardId: this._id }, { sort: { createdAt: -1 }});
  110. },
  111. comments() {
  112. return CardComments.find({ cardId: this._id }, { sort: { createdAt: -1 }});
  113. },
  114. attachments() {
  115. return Attachments.find({ cardId: this._id }, { sort: { uploadedAt: -1 }});
  116. },
  117. cover() {
  118. const cover = Attachments.findOne(this.coverId);
  119. // if we return a cover before it is fully stored, we will get errors when we try to display it
  120. // todo XXX we could return a default "upload pending" image in the meantime?
  121. return cover && cover.url() && cover;
  122. },
  123. absoluteUrl() {
  124. const board = this.board();
  125. return FlowRouter.url('card', {
  126. boardId: board._id,
  127. slug: board.slug,
  128. cardId: this._id,
  129. });
  130. },
  131. });
  132. Cards.mutations({
  133. archive() {
  134. return { $set: { archived: true }};
  135. },
  136. restore() {
  137. return { $set: { archived: false }};
  138. },
  139. setTitle(title) {
  140. return { $set: { title }};
  141. },
  142. setDescription(description) {
  143. return { $set: { description }};
  144. },
  145. move(listId, sortIndex) {
  146. const mutatedFields = { listId };
  147. if (sortIndex) {
  148. mutatedFields.sort = sortIndex;
  149. }
  150. return { $set: mutatedFields };
  151. },
  152. addLabel(labelId) {
  153. return { $addToSet: { labelIds: labelId }};
  154. },
  155. removeLabel(labelId) {
  156. return { $pull: { labelIds: labelId }};
  157. },
  158. toggleLabel(labelId) {
  159. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  160. return this.removeLabel(labelId);
  161. } else {
  162. return this.addLabel(labelId);
  163. }
  164. },
  165. assignMember(memberId) {
  166. return { $addToSet: { members: memberId }};
  167. },
  168. unassignMember(memberId) {
  169. return { $pull: { members: memberId }};
  170. },
  171. toggleMember(memberId) {
  172. if (this.members && this.members.indexOf(memberId) > -1) {
  173. return this.unassignMember(memberId);
  174. } else {
  175. return this.assignMember(memberId);
  176. }
  177. },
  178. setCover(coverId) {
  179. return { $set: { coverId }};
  180. },
  181. unsetCover() {
  182. return { $unset: { coverId: '' }};
  183. },
  184. });
  185. if (Meteor.isServer) {
  186. Cards.after.insert((userId, doc) => {
  187. Activities.insert({
  188. userId,
  189. activityType: 'createCard',
  190. boardId: doc.boardId,
  191. listId: doc.listId,
  192. cardId: doc._id,
  193. });
  194. });
  195. // New activity for card (un)archivage
  196. Cards.after.update((userId, doc, fieldNames) => {
  197. if (_.contains(fieldNames, 'archived')) {
  198. if (doc.archived) {
  199. Activities.insert({
  200. userId,
  201. activityType: 'archivedCard',
  202. boardId: doc.boardId,
  203. listId: doc.listId,
  204. cardId: doc._id,
  205. });
  206. } else {
  207. Activities.insert({
  208. userId,
  209. activityType: 'restoredCard',
  210. boardId: doc.boardId,
  211. listId: doc.listId,
  212. cardId: doc._id,
  213. });
  214. }
  215. }
  216. });
  217. // New activity for card moves
  218. Cards.after.update(function(userId, doc, fieldNames) {
  219. const oldListId = this.previous.listId;
  220. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  221. Activities.insert({
  222. userId,
  223. oldListId,
  224. activityType: 'moveCard',
  225. listId: doc.listId,
  226. boardId: doc.boardId,
  227. cardId: doc._id,
  228. });
  229. }
  230. });
  231. // Add a new activity if we add or remove a member to the card
  232. Cards.before.update((userId, doc, fieldNames, modifier) => {
  233. if (!_.contains(fieldNames, 'members'))
  234. return;
  235. let memberId;
  236. // Say hello to the new member
  237. if (modifier.$addToSet && modifier.$addToSet.members) {
  238. memberId = modifier.$addToSet.members;
  239. if (!_.contains(doc.members, memberId)) {
  240. Activities.insert({
  241. userId,
  242. memberId,
  243. activityType: 'joinMember',
  244. boardId: doc.boardId,
  245. cardId: doc._id,
  246. });
  247. }
  248. }
  249. // Say goodbye to the former member
  250. if (modifier.$pull && modifier.$pull.members) {
  251. memberId = modifier.$pull.members;
  252. Activities.insert({
  253. userId,
  254. memberId,
  255. activityType: 'unjoinMember',
  256. boardId: doc.boardId,
  257. cardId: doc._id,
  258. });
  259. }
  260. });
  261. // Remove all activities associated with a card if we remove the card
  262. Cards.after.remove((userId, doc) => {
  263. Activities.remove({
  264. cardId: doc._id,
  265. });
  266. });
  267. }