boards.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. Boards.helpers({
  75. isPublic() {
  76. return this.permission === 'public';
  77. },
  78. lists() {
  79. return Lists.find({ boardId: this._id, archived: false },
  80. { sort: { sort: 1 }});
  81. },
  82. activities() {
  83. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 }});
  84. },
  85. activeMembers() {
  86. return _.where(this.members, {isActive: true});
  87. },
  88. getLabel(name, color) {
  89. return this.labels.find((current) => {
  90. return ((current.name === name) && (current.color === color));
  91. });
  92. },
  93. labelIndex(labelId) {
  94. return _.indexOf(_.pluck(this.labels, '_id'), labelId);
  95. },
  96. memberIndex(memberId) {
  97. return _.indexOf(_.pluck(this.members, 'userId'), memberId);
  98. },
  99. absoluteUrl() {
  100. return FlowRouter.path('board', { id: this._id, slug: this.slug });
  101. },
  102. colorClass() {
  103. return `board-color-${this.color}`;
  104. },
  105. });
  106. Boards.mutations({
  107. archive() {
  108. return { $set: { archived: true }};
  109. },
  110. restore() {
  111. return { $set: { archived: false }};
  112. },
  113. rename(title) {
  114. return { $set: { title }};
  115. },
  116. setColor(color) {
  117. return { $set: { color }};
  118. },
  119. setVisibility(visibility) {
  120. return { $set: { permission: visibility }};
  121. },
  122. addLabel(name, color) {
  123. const _id = Random.id(6);
  124. return { $push: {labels: { _id, name, color }}};
  125. },
  126. editLabel(labelId, name, color) {
  127. const labelIndex = this.labelIndex(labelId);
  128. return {
  129. $set: {
  130. [`labels.${labelIndex}.name`]: name,
  131. [`labels.${labelIndex}.color`]: color,
  132. },
  133. };
  134. },
  135. removeLabel(labelId) {
  136. return { $pull: { labels: { _id: labelId }}};
  137. },
  138. addMember(memberId) {
  139. const memberIndex = this.memberIndex(memberId);
  140. if (memberIndex === -1) {
  141. return {
  142. $push: {
  143. members: {
  144. userId: memberId,
  145. isAdmin: false,
  146. isActive: true,
  147. },
  148. },
  149. };
  150. } else {
  151. return {
  152. $set: {
  153. [`members.${memberIndex}.isActive`]: true,
  154. [`members.${memberIndex}.isAdmin`]: false,
  155. },
  156. };
  157. }
  158. },
  159. removeMember(memberId) {
  160. const memberIndex = this.memberIndex(memberId);
  161. return {
  162. $set: {
  163. [`members.${memberIndex}.isActive`]: false,
  164. },
  165. };
  166. },
  167. setMemberPermission(memberId, isAdmin) {
  168. const memberIndex = this.memberIndex(memberId);
  169. return {
  170. $set: {
  171. [`members.${memberIndex}.isAdmin`]: isAdmin,
  172. },
  173. };
  174. },
  175. });
  176. if (Meteor.isServer) {
  177. Boards.allow({
  178. insert: Meteor.userId,
  179. update: allowIsBoardAdmin,
  180. remove: allowIsBoardAdmin,
  181. fetch: ['members'],
  182. });
  183. // The number of users that have starred this board is managed by trusted code
  184. // and the user is not allowed to update it
  185. Boards.deny({
  186. update(userId, board, fieldNames) {
  187. return _.contains(fieldNames, 'stars');
  188. },
  189. fetch: [],
  190. });
  191. // We can't remove a member if it is the last administrator
  192. Boards.deny({
  193. update(userId, doc, fieldNames, modifier) {
  194. if (!_.contains(fieldNames, 'members'))
  195. return false;
  196. // We only care in case of a $pull operation, ie remove a member
  197. if (!_.isObject(modifier.$pull && modifier.$pull.members))
  198. return false;
  199. // If there is more than one admin, it's ok to remove anyone
  200. const nbAdmins = _.filter(doc.members, (member) => {
  201. return member.isAdmin;
  202. }).length;
  203. if (nbAdmins > 1)
  204. return false;
  205. // If all the previous conditions were verified, we can't remove
  206. // a user if it's an admin
  207. const removedMemberId = modifier.$pull.members.userId;
  208. return Boolean(_.findWhere(doc.members, {
  209. userId: removedMemberId,
  210. isAdmin: true,
  211. }));
  212. },
  213. fetch: ['members'],
  214. });
  215. }
  216. Boards.before.insert((userId, doc) => {
  217. // XXX We need to improve slug management. Only the id should be necessary
  218. // to identify a board in the code.
  219. // XXX If the board title is updated, the slug should also be updated.
  220. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  221. // return an empty string. This is causes bugs in our application so we set
  222. // a default slug in this case.
  223. doc.slug = doc.slug || getSlug(doc.title) || 'board';
  224. doc.createdAt = new Date();
  225. doc.archived = false;
  226. doc.members = doc.members || [{
  227. userId,
  228. isAdmin: true,
  229. isActive: true,
  230. }];
  231. doc.stars = 0;
  232. doc.color = Boards.simpleSchema()._schema.color.allowedValues[0];
  233. // Handle labels
  234. const colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  235. const defaultLabelsColors = _.clone(colors).splice(0, 6);
  236. doc.labels = _.map(defaultLabelsColors, (color) => {
  237. return {
  238. color,
  239. _id: Random.id(6),
  240. name: '',
  241. };
  242. });
  243. });
  244. Boards.before.update((userId, doc, fieldNames, modifier) => {
  245. modifier.$set = modifier.$set || {};
  246. modifier.$set.modifiedAt = new Date();
  247. });
  248. if (Meteor.isServer) {
  249. // Let MongoDB ensure that a member is not included twice in the same board
  250. Meteor.startup(() => {
  251. Boards._collection._ensureIndex({
  252. _id: 1,
  253. 'members.userId': 1,
  254. }, { unique: true });
  255. });
  256. // Genesis: the first activity of the newly created board
  257. Boards.after.insert((userId, doc) => {
  258. Activities.insert({
  259. userId,
  260. type: 'board',
  261. activityTypeId: doc._id,
  262. activityType: 'createBoard',
  263. boardId: doc._id,
  264. });
  265. });
  266. // If the user removes a label from a board, we have to remove references to
  267. // this label in all cards of the board.
  268. Boards.after.update((userId, doc, fieldNames, modifier) => {
  269. if (!_.contains(fieldNames, 'labels') ||
  270. !modifier.$pull ||
  271. !modifier.$pull.labels ||
  272. !modifier.$pull.labels._id)
  273. return;
  274. const removedLabelId = modifier.$pull.labels._id;
  275. Cards.update(
  276. { boardId: doc._id },
  277. {
  278. $pull: {
  279. labels: removedLabelId,
  280. },
  281. },
  282. { multi: true }
  283. );
  284. });
  285. // Add a new activity if we add or remove a member to the board
  286. Boards.after.update((userId, doc, fieldNames, modifier) => {
  287. if (!_.contains(fieldNames, 'members'))
  288. return;
  289. let memberId;
  290. // Say hello to the new member
  291. if (modifier.$push && modifier.$push.members) {
  292. memberId = modifier.$push.members.userId;
  293. Activities.insert({
  294. userId,
  295. memberId,
  296. type: 'member',
  297. activityType: 'addBoardMember',
  298. boardId: doc._id,
  299. });
  300. }
  301. // Say goodbye to the former member
  302. if (modifier.$pull && modifier.$pull.members) {
  303. memberId = modifier.$pull.members.userId;
  304. Activities.insert({
  305. userId,
  306. memberId,
  307. type: 'member',
  308. activityType: 'removeBoardMember',
  309. boardId: doc._id,
  310. });
  311. }
  312. });
  313. }