boards.js 9.0 KB

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