boards.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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(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(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. const nbAdmins = _.filter(doc.members, (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. const removedMemberId = modifier.$pull.members.userId;
  106. return Boolean(_.findWhere(doc.members, {
  107. userId: removedMemberId,
  108. isAdmin: true,
  109. }));
  110. },
  111. fetch: ['members'],
  112. });
  113. }
  114. Boards.helpers({
  115. isPublic() {
  116. return this.permission === 'public';
  117. },
  118. lists() {
  119. return Lists.find({ boardId: this._id, archived: false },
  120. { sort: { sort: 1 }});
  121. },
  122. activities() {
  123. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 }});
  124. },
  125. activeMembers() {
  126. return _.where(this.members, {isActive: true});
  127. },
  128. absoluteUrl() {
  129. return FlowRouter.path('board', { id: this._id, slug: this.slug });
  130. },
  131. colorClass() {
  132. return `board-color-${this.color}`;
  133. },
  134. });
  135. Boards.before.insert((userId, doc) => {
  136. // XXX We need to improve slug management. Only the id should be necessary
  137. // to identify a board in the code.
  138. // XXX If the board title is updated, the slug should also be updated.
  139. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  140. // return an empty string. This is causes bugs in our application so we set
  141. // a default slug in this case.
  142. doc.slug = doc.slug || getSlug(doc.title) || 'board';
  143. doc.createdAt = new Date();
  144. doc.archived = false;
  145. doc.members = [{
  146. userId,
  147. isAdmin: true,
  148. isActive: true,
  149. }];
  150. doc.stars = 0;
  151. doc.color = Boards.simpleSchema()._schema.color.allowedValues[0];
  152. // Handle labels
  153. const colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  154. const defaultLabelsColors = _.clone(colors).splice(0, 6);
  155. doc.labels = _.map(defaultLabelsColors, (color) => {
  156. return {
  157. color,
  158. _id: Random.id(6),
  159. name: '',
  160. };
  161. });
  162. });
  163. Boards.before.update((userId, doc, fieldNames, modifier) => {
  164. modifier.$set = modifier.$set || {};
  165. modifier.$set.modifiedAt = new Date();
  166. });
  167. if (Meteor.isServer) {
  168. // Let MongoDB ensure that a member is not included twice in the same board
  169. Meteor.startup(() => {
  170. Boards._collection._ensureIndex({
  171. _id: 1,
  172. 'members.userId': 1,
  173. }, { unique: true });
  174. });
  175. // Genesis: the first activity of the newly created board
  176. Boards.after.insert((userId, doc) => {
  177. Activities.insert({
  178. userId,
  179. type: 'board',
  180. activityTypeId: doc._id,
  181. activityType: 'createBoard',
  182. boardId: doc._id,
  183. });
  184. });
  185. // If the user remove one label from a board, we cant to remove reference of
  186. // this label in any card of this board.
  187. Boards.after.update((userId, doc, fieldNames, modifier) => {
  188. if (!_.contains(fieldNames, 'labels') ||
  189. !modifier.$pull ||
  190. !modifier.$pull.labels ||
  191. !modifier.$pull.labels._id)
  192. return;
  193. const removedLabelId = modifier.$pull.labels._id;
  194. Cards.update(
  195. { boardId: doc._id },
  196. {
  197. $pull: {
  198. labels: removedLabelId,
  199. },
  200. },
  201. { multi: true }
  202. );
  203. });
  204. // Add a new activity if we add or remove a member to the board
  205. Boards.after.update((userId, doc, fieldNames, modifier) => {
  206. if (!_.contains(fieldNames, 'members'))
  207. return;
  208. let memberId;
  209. // Say hello to the new member
  210. if (modifier.$push && modifier.$push.members) {
  211. memberId = modifier.$push.members.userId;
  212. Activities.insert({
  213. userId,
  214. memberId,
  215. type: 'member',
  216. activityType: 'addBoardMember',
  217. boardId: doc._id,
  218. });
  219. }
  220. // Say goodbye to the former member
  221. if (modifier.$pull && modifier.$pull.members) {
  222. memberId = modifier.$pull.members.userId;
  223. Activities.insert({
  224. userId,
  225. memberId,
  226. type: 'member',
  227. activityType: 'removeBoardMember',
  228. boardId: doc._id,
  229. });
  230. }
  231. });
  232. }