boards.js 6.8 KB

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