boards.js 10 KB

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