boards.js 8.7 KB

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