boards.js 8.2 KB

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