boards.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. Boards = new Mongo.Collection('boards');
  2. Boards.attachSchema(new SimpleSchema({
  3. title: {
  4. type: String
  5. },
  6. slug: {
  7. type: String
  8. },
  9. archived: {
  10. type: Boolean
  11. },
  12. createdAt: {
  13. type: Date,
  14. denyUpdate: true
  15. },
  16. // XXX Inconsistent field naming
  17. modifiedAt: {
  18. type: Date,
  19. denyInsert: true,
  20. optional: true
  21. },
  22. // De-normalized number of users that have starred this board
  23. stars: {
  24. type: Number
  25. },
  26. // De-normalized label system
  27. 'labels.$._id': {
  28. // We don't specify that this field must be unique in the board because that
  29. // will cause performance penalties and is not necessary since this field is
  30. // always set on the server.
  31. // XXX Actually if we create a new label, the `_id` is set on the client
  32. // without being overwritten by the server, could it be a problem?
  33. type: String
  34. },
  35. 'labels.$.name': {
  36. type: String,
  37. optional: true
  38. },
  39. 'labels.$.color': {
  40. type: String,
  41. allowedValues: [
  42. 'green', 'yellow', 'orange', 'red', 'purple',
  43. 'blue', 'sky', 'lime', 'pink', 'black'
  44. ]
  45. },
  46. // XXX We might want to maintain more informations under the member sub-
  47. // documents like de-normalized meta-data (the date the member joined the
  48. // board, the number of contributions, etc.).
  49. 'members.$.userId': {
  50. type: String
  51. },
  52. 'members.$.isAdmin': {
  53. type: Boolean
  54. },
  55. 'members.$.isActive': {
  56. type: Boolean
  57. },
  58. permission: {
  59. type: String,
  60. allowedValues: ['public', 'private']
  61. },
  62. color: {
  63. type: String,
  64. allowedValues: [
  65. 'nephritis', 'pomegranate', 'belize',
  66. 'wisteria', 'midnight', 'pumpkin']
  67. }
  68. }));
  69. if (Meteor.isServer) {
  70. Boards.allow({
  71. insert: Meteor.userId,
  72. update: allowIsBoardAdmin,
  73. remove: allowIsBoardAdmin,
  74. fetch: ['members']
  75. });
  76. // The number of users that have starred this board is managed by trusted code
  77. // and the user is not allowed to update it
  78. Boards.deny({
  79. update: function(userId, board, fieldNames) {
  80. return _.contains(fieldNames, 'stars');
  81. },
  82. fetch: []
  83. });
  84. // We can't remove a member if it is the last administrator
  85. Boards.deny({
  86. update: function(userId, doc, fieldNames, modifier) {
  87. if (! _.contains(fieldNames, 'members'))
  88. return false;
  89. // We only care in case of a $pull operation, ie remove a member
  90. if (! _.isObject(modifier.$pull && modifier.$pull.members))
  91. return false;
  92. // If there is more than one admin, it's ok to remove anyone
  93. var nbAdmins = _.filter(doc.members, function(member) {
  94. return member.isAdmin;
  95. }).length;
  96. if (nbAdmins > 1)
  97. return false;
  98. // If all the previous conditions were verified, we can't remove
  99. // a user if it's an admin
  100. var removedMemberId = modifier.$pull.members.userId;
  101. return !! _.findWhere(doc.members, {
  102. userId: removedMemberId,
  103. isAdmin: true
  104. });
  105. },
  106. fetch: ['members']
  107. });
  108. }
  109. Boards.helpers({
  110. isPublic: function() {
  111. return this.permission === 'public';
  112. },
  113. lists: function() {
  114. return Lists.find({ boardId: this._id, archived: false },
  115. { sort: { sort: 1 }});
  116. },
  117. activities: function() {
  118. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 }});
  119. },
  120. absoluteUrl: function() {
  121. return Router.path('Board', { boardId: this._id, slug: this.slug });
  122. },
  123. colorClass: function() {
  124. return 'board-color-' + this.color;
  125. }
  126. });
  127. Boards.before.insert(function(userId, doc) {
  128. // XXX We need to improve slug management. Only the id should be necessary
  129. // to identify a board in the code.
  130. // XXX If the board title is updated, the slug should also be updated.
  131. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  132. // return an empty string. This is causes bugs in our application so we set
  133. // a default slug in this case.
  134. doc.slug = doc.slug || getSlug(doc.title) || 'board';
  135. doc.createdAt = new Date();
  136. doc.archived = false;
  137. doc.members = [{
  138. userId: userId,
  139. isAdmin: true,
  140. isActive: true
  141. }];
  142. doc.stars = 0;
  143. doc.color = Boards.simpleSchema()._schema.color.allowedValues[0];
  144. // Handle labels
  145. var colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  146. var defaultLabelsColors = _.clone(colors).splice(0, 6);
  147. doc.labels = _.map(defaultLabelsColors, function(val) {
  148. return {
  149. _id: Random.id(6),
  150. name: '',
  151. color: val
  152. };
  153. });
  154. });
  155. Boards.before.update(function(userId, doc, fieldNames, modifier) {
  156. modifier.$set = modifier.$set || {};
  157. modifier.$set.modifiedAt = new Date();
  158. });
  159. if (Meteor.isServer) {
  160. // Let MongoDB ensure that a member is not included twice in the same board
  161. Meteor.startup(function() {
  162. Boards._collection._ensureIndex({
  163. _id: 1,
  164. 'members.userId': 1
  165. }, { unique: true });
  166. });
  167. // Genesis: the first activity of the newly created board
  168. Boards.after.insert(function(userId, doc) {
  169. Activities.insert({
  170. type: 'board',
  171. activityTypeId: doc._id,
  172. activityType: 'createBoard',
  173. boardId: doc._id,
  174. userId: userId
  175. });
  176. });
  177. // If the user remove one label from a board, we cant to remove reference of
  178. // this label in any card of this board.
  179. Boards.after.update(function(userId, doc, fieldNames, modifier) {
  180. if (! _.contains(fieldNames, 'labels') ||
  181. ! modifier.$pull ||
  182. ! modifier.$pull.labels ||
  183. ! modifier.$pull.labels._id)
  184. return;
  185. var removedLabelId = modifier.$pull.labels._id;
  186. Cards.update(
  187. { boardId: doc._id },
  188. {
  189. $pull: {
  190. labels: removedLabelId
  191. }
  192. },
  193. { multi: true }
  194. );
  195. });
  196. // Add a new activity if we add or remove a member to the board
  197. Boards.after.update(function(userId, doc, fieldNames, modifier) {
  198. if (! _.contains(fieldNames, 'members'))
  199. return;
  200. var memberId;
  201. // Say hello to the new member
  202. if (modifier.$push && modifier.$push.members) {
  203. memberId = modifier.$push.members.userId;
  204. Activities.insert({
  205. type: 'member',
  206. activityType: 'addBoardMember',
  207. boardId: doc._id,
  208. userId: userId,
  209. memberId: memberId
  210. });
  211. }
  212. // Say goodbye to the former member
  213. if (modifier.$pull && modifier.$pull.members) {
  214. memberId = modifier.$pull.members.userId;
  215. Activities.insert({
  216. type: 'member',
  217. activityType: 'removeBoardMember',
  218. boardId: doc._id,
  219. userId: userId,
  220. memberId: memberId
  221. });
  222. }
  223. });
  224. }