boards.js 6.7 KB

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