boards.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. 'belize',
  66. 'nephritis',
  67. 'pomegranate',
  68. 'pumpkin',
  69. 'wisteria',
  70. 'midnight',
  71. ]
  72. }
  73. }));
  74. if (Meteor.isServer) {
  75. Boards.allow({
  76. insert: Meteor.userId,
  77. update: allowIsBoardAdmin,
  78. remove: allowIsBoardAdmin,
  79. fetch: ['members']
  80. });
  81. // The number of users that have starred this board is managed by trusted code
  82. // and the user is not allowed to update it
  83. Boards.deny({
  84. update: function(userId, board, fieldNames) {
  85. return _.contains(fieldNames, 'stars');
  86. },
  87. fetch: []
  88. });
  89. // We can't remove a member if it is the last administrator
  90. Boards.deny({
  91. update: function(userId, doc, fieldNames, modifier) {
  92. if (! _.contains(fieldNames, 'members'))
  93. return false;
  94. // We only care in case of a $pull operation, ie remove a member
  95. if (! _.isObject(modifier.$pull && modifier.$pull.members))
  96. return false;
  97. // If there is more than one admin, it's ok to remove anyone
  98. var nbAdmins = _.filter(doc.members, function(member) {
  99. return member.isAdmin;
  100. }).length;
  101. if (nbAdmins > 1)
  102. return false;
  103. // If all the previous conditions were verified, we can't remove
  104. // a user if it's an admin
  105. var removedMemberId = modifier.$pull.members.userId;
  106. return !! _.findWhere(doc.members, {
  107. userId: removedMemberId,
  108. isAdmin: true
  109. });
  110. },
  111. fetch: ['members']
  112. });
  113. }
  114. Boards.helpers({
  115. isPublic: function() {
  116. return this.permission === 'public';
  117. },
  118. lists: function() {
  119. return Lists.find({ boardId: this._id, archived: false },
  120. { sort: { sort: 1 }});
  121. },
  122. activities: function() {
  123. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 }});
  124. },
  125. absoluteUrl: function() {
  126. return Router.path('Board', { boardId: this._id, slug: this.slug });
  127. },
  128. colorClass: function() {
  129. return 'board-color-' + this.color;
  130. }
  131. });
  132. Boards.before.insert(function(userId, doc) {
  133. // XXX We need to improve slug management. Only the id should be necessary
  134. // to identify a board in the code.
  135. // XXX If the board title is updated, the slug should also be updated.
  136. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  137. // return an empty string. This is causes bugs in our application so we set
  138. // a default slug in this case.
  139. doc.slug = doc.slug || getSlug(doc.title) || 'board';
  140. doc.createdAt = new Date();
  141. doc.archived = false;
  142. doc.members = [{
  143. userId: userId,
  144. isAdmin: true,
  145. isActive: true
  146. }];
  147. doc.stars = 0;
  148. doc.color = Boards.simpleSchema()._schema.color.allowedValues[0];
  149. // Handle labels
  150. var colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  151. var defaultLabelsColors = _.clone(colors).splice(0, 6);
  152. doc.labels = _.map(defaultLabelsColors, function(val) {
  153. return {
  154. _id: Random.id(6),
  155. name: '',
  156. color: val
  157. };
  158. });
  159. });
  160. Boards.before.update(function(userId, doc, fieldNames, modifier) {
  161. modifier.$set = modifier.$set || {};
  162. modifier.$set.modifiedAt = new Date();
  163. });
  164. if (Meteor.isServer) {
  165. // Let MongoDB ensure that a member is not included twice in the same board
  166. Meteor.startup(function() {
  167. Boards._collection._ensureIndex({
  168. _id: 1,
  169. 'members.userId': 1
  170. }, { unique: true });
  171. });
  172. // Genesis: the first activity of the newly created board
  173. Boards.after.insert(function(userId, doc) {
  174. Activities.insert({
  175. type: 'board',
  176. activityTypeId: doc._id,
  177. activityType: 'createBoard',
  178. boardId: doc._id,
  179. userId: userId
  180. });
  181. });
  182. // If the user remove one label from a board, we cant to remove reference of
  183. // this label in any card of this board.
  184. Boards.after.update(function(userId, doc, fieldNames, modifier) {
  185. if (! _.contains(fieldNames, 'labels') ||
  186. ! modifier.$pull ||
  187. ! modifier.$pull.labels ||
  188. ! modifier.$pull.labels._id)
  189. return;
  190. var removedLabelId = modifier.$pull.labels._id;
  191. Cards.update(
  192. { boardId: doc._id },
  193. {
  194. $pull: {
  195. labels: removedLabelId
  196. }
  197. },
  198. { multi: true }
  199. );
  200. });
  201. // Add a new activity if we add or remove a member to the board
  202. Boards.after.update(function(userId, doc, fieldNames, modifier) {
  203. if (! _.contains(fieldNames, 'members'))
  204. return;
  205. var memberId;
  206. // Say hello to the new member
  207. if (modifier.$push && modifier.$push.members) {
  208. memberId = modifier.$push.members.userId;
  209. Activities.insert({
  210. type: 'member',
  211. activityType: 'addBoardMember',
  212. boardId: doc._id,
  213. userId: userId,
  214. memberId: memberId
  215. });
  216. }
  217. // Say goodbye to the former member
  218. if (modifier.$pull && modifier.$pull.members) {
  219. memberId = modifier.$pull.members.userId;
  220. Activities.insert({
  221. type: 'member',
  222. activityType: 'removeBoardMember',
  223. boardId: doc._id,
  224. userId: userId,
  225. memberId: memberId
  226. });
  227. }
  228. });
  229. }