boards.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. Boards = new Mongo.Collection('boards');
  2. Boards.attachSchema(new SimpleSchema({
  3. title: {
  4. type: String,
  5. },
  6. slug: {
  7. type: String,
  8. autoValue() { // eslint-disable-line consistent-return
  9. // XXX We need to improve slug management. Only the id should be necessary
  10. // to identify a board in the code.
  11. // XXX If the board title is updated, the slug should also be updated.
  12. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  13. // return an empty string. This is causes bugs in our application so we set
  14. // a default slug in this case.
  15. if (this.isInsert && !this.isSet) {
  16. let slug = 'board';
  17. const title = this.field('title');
  18. if (title.isSet) {
  19. slug = getSlug(title.value) || slug;
  20. }
  21. return slug;
  22. }
  23. },
  24. },
  25. archived: {
  26. type: Boolean,
  27. autoValue() { // eslint-disable-line consistent-return
  28. if (this.isInsert && !this.isSet) {
  29. return false;
  30. }
  31. },
  32. },
  33. createdAt: {
  34. type: Date,
  35. autoValue() { // eslint-disable-line consistent-return
  36. if (this.isInsert) {
  37. return new Date();
  38. } else {
  39. this.unset();
  40. }
  41. },
  42. },
  43. // XXX Inconsistent field naming
  44. modifiedAt: {
  45. type: Date,
  46. optional: true,
  47. autoValue() { // eslint-disable-line consistent-return
  48. if (this.isUpdate) {
  49. return new Date();
  50. } else {
  51. this.unset();
  52. }
  53. },
  54. },
  55. // De-normalized number of users that have starred this board
  56. stars: {
  57. type: Number,
  58. autoValue() { // eslint-disable-line consistent-return
  59. if (this.isInsert) {
  60. return 0;
  61. }
  62. },
  63. },
  64. // De-normalized label system
  65. 'labels': {
  66. type: [Object],
  67. autoValue() { // eslint-disable-line consistent-return
  68. if (this.isInsert && !this.isSet) {
  69. const colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  70. const defaultLabelsColors = _.clone(colors).splice(0, 6);
  71. return defaultLabelsColors.map((color) => ({
  72. color,
  73. _id: Random.id(6),
  74. name: '',
  75. }));
  76. }
  77. },
  78. },
  79. 'labels.$._id': {
  80. // We don't specify that this field must be unique in the board because that
  81. // will cause performance penalties and is not necessary since this field is
  82. // always set on the server.
  83. // XXX Actually if we create a new label, the `_id` is set on the client
  84. // without being overwritten by the server, could it be a problem?
  85. type: String,
  86. },
  87. 'labels.$.name': {
  88. type: String,
  89. optional: true,
  90. },
  91. 'labels.$.color': {
  92. type: String,
  93. allowedValues: [
  94. 'green', 'yellow', 'orange', 'red', 'purple',
  95. 'blue', 'sky', 'lime', 'pink', 'black',
  96. ],
  97. },
  98. // XXX We might want to maintain more informations under the member sub-
  99. // documents like de-normalized meta-data (the date the member joined the
  100. // board, the number of contributions, etc.).
  101. 'members': {
  102. type: [Object],
  103. autoValue() { // eslint-disable-line consistent-return
  104. if (this.isInsert && !this.isSet) {
  105. return [{
  106. userId: this.userId,
  107. isAdmin: true,
  108. isActive: true,
  109. isInvited: false,
  110. }];
  111. }
  112. },
  113. },
  114. 'members.$.userId': {
  115. type: String,
  116. },
  117. 'members.$.isAdmin': {
  118. type: Boolean,
  119. },
  120. 'members.$.isActive': {
  121. type: Boolean,
  122. },
  123. permission: {
  124. type: String,
  125. allowedValues: ['public', 'private'],
  126. },
  127. color: {
  128. type: String,
  129. allowedValues: [
  130. 'belize',
  131. 'nephritis',
  132. 'pomegranate',
  133. 'pumpkin',
  134. 'wisteria',
  135. 'midnight',
  136. ],
  137. autoValue() { // eslint-disable-line consistent-return
  138. if (this.isInsert && !this.isSet) {
  139. return Boards.simpleSchema()._schema.color.allowedValues[0];
  140. }
  141. },
  142. },
  143. description: {
  144. type: String,
  145. optional: true,
  146. },
  147. }));
  148. Boards.helpers({
  149. /**
  150. * Is supplied user authorized to view this board?
  151. */
  152. isVisibleBy(user) {
  153. if(this.isPublic()) {
  154. // public boards are visible to everyone
  155. return true;
  156. } else {
  157. // otherwise you have to be logged-in and active member
  158. return user && this.isActiveMember(user._id);
  159. }
  160. },
  161. /**
  162. * Is the user one of the active members of the board?
  163. *
  164. * @param userId
  165. * @returns {boolean} the member that matches, or undefined/false
  166. */
  167. isActiveMember(userId) {
  168. if(userId) {
  169. return this.members.find((member) => (member.userId === userId && member.isActive));
  170. } else {
  171. return false;
  172. }
  173. },
  174. isPublic() {
  175. return this.permission === 'public';
  176. },
  177. lists() {
  178. return Lists.find({ boardId: this._id, archived: false }, { sort: { sort: 1 }});
  179. },
  180. activities() {
  181. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 }});
  182. },
  183. activeMembers() {
  184. return _.where(this.members, {isActive: true});
  185. },
  186. activeAdmins() {
  187. return _.where(this.members, {isActive: true, isAdmin: true});
  188. },
  189. memberUsers() {
  190. return Users.find({ _id: {$in: _.pluck(this.members, 'userId')} });
  191. },
  192. getLabel(name, color) {
  193. return _.findWhere(this.labels, { name, color });
  194. },
  195. labelIndex(labelId) {
  196. return _.pluck(this.labels, '_id').indexOf(labelId);
  197. },
  198. memberIndex(memberId) {
  199. return _.pluck(this.members, 'userId').indexOf(memberId);
  200. },
  201. hasMember(memberId) {
  202. return !!_.findWhere(this.members, {userId: memberId, isActive: true});
  203. },
  204. hasAdmin(memberId) {
  205. return !!_.findWhere(this.members, {userId: memberId, isActive: true, isAdmin: true});
  206. },
  207. absoluteUrl() {
  208. return FlowRouter.url('board', { id: this._id, slug: this.slug });
  209. },
  210. colorClass() {
  211. return `board-color-${this.color}`;
  212. },
  213. // XXX currently mutations return no value so we have an issue when using addLabel in import
  214. // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  215. pushLabel(name, color) {
  216. const _id = Random.id(6);
  217. Boards.direct.update(this._id, { $push: {labels: { _id, name, color }}});
  218. return _id;
  219. },
  220. });
  221. Boards.mutations({
  222. archive() {
  223. return { $set: { archived: true }};
  224. },
  225. restore() {
  226. return { $set: { archived: false }};
  227. },
  228. rename(title) {
  229. return { $set: { title }};
  230. },
  231. setDesciption(description) {
  232. return { $set: {description} };
  233. },
  234. setColor(color) {
  235. return { $set: { color }};
  236. },
  237. setVisibility(visibility) {
  238. return { $set: { permission: visibility }};
  239. },
  240. addLabel(name, color) {
  241. // If label with the same name and color already exists we don't want to
  242. // create another one because they would be indistinguishable in the UI
  243. // (they would still have different `_id` but that is not exposed to the
  244. // user).
  245. if (!this.getLabel(name, color)) {
  246. const _id = Random.id(6);
  247. return { $push: {labels: { _id, name, color }}};
  248. }
  249. return {};
  250. },
  251. editLabel(labelId, name, color) {
  252. if (!this.getLabel(name, color)) {
  253. const labelIndex = this.labelIndex(labelId);
  254. return {
  255. $set: {
  256. [`labels.${labelIndex}.name`]: name,
  257. [`labels.${labelIndex}.color`]: color,
  258. },
  259. };
  260. }
  261. return {};
  262. },
  263. removeLabel(labelId) {
  264. return { $pull: { labels: { _id: labelId }}};
  265. },
  266. addMember(memberId) {
  267. const memberIndex = this.memberIndex(memberId);
  268. if (memberIndex >= 0) {
  269. return {
  270. $set: {
  271. [`members.${memberIndex}.isActive`]: true,
  272. },
  273. };
  274. }
  275. return {
  276. $push: {
  277. members: {
  278. userId: memberId,
  279. isAdmin: false,
  280. isActive: true,
  281. },
  282. },
  283. };
  284. },
  285. removeMember(memberId) {
  286. const memberIndex = this.memberIndex(memberId);
  287. // we do not allow the only one admin to be removed
  288. const allowRemove = (!this.members[memberIndex].isAdmin) || (this.activeAdmins().length > 1);
  289. if (!allowRemove) {
  290. return {
  291. $set: {
  292. [`members.${memberIndex}.isActive`]: true,
  293. },
  294. };
  295. }
  296. return {
  297. $set: {
  298. [`members.${memberIndex}.isActive`]: false,
  299. [`members.${memberIndex}.isAdmin`]: false,
  300. },
  301. };
  302. },
  303. setMemberPermission(memberId, isAdmin) {
  304. const memberIndex = this.memberIndex(memberId);
  305. // do not allow change permission of self
  306. if (memberId === Meteor.userId()) {
  307. isAdmin = this.members[memberIndex].isAdmin;
  308. }
  309. return {
  310. $set: {
  311. [`members.${memberIndex}.isAdmin`]: isAdmin,
  312. },
  313. };
  314. },
  315. });
  316. if (Meteor.isServer) {
  317. Boards.allow({
  318. insert: Meteor.userId,
  319. update: allowIsBoardAdmin,
  320. remove: allowIsBoardAdmin,
  321. fetch: ['members'],
  322. });
  323. // The number of users that have starred this board is managed by trusted code
  324. // and the user is not allowed to update it
  325. Boards.deny({
  326. update(userId, board, fieldNames) {
  327. return _.contains(fieldNames, 'stars');
  328. },
  329. fetch: [],
  330. });
  331. // We can't remove a member if it is the last administrator
  332. Boards.deny({
  333. update(userId, doc, fieldNames, modifier) {
  334. if (!_.contains(fieldNames, 'members'))
  335. return false;
  336. // We only care in case of a $pull operation, ie remove a member
  337. if (!_.isObject(modifier.$pull && modifier.$pull.members))
  338. return false;
  339. // If there is more than one admin, it's ok to remove anyone
  340. const nbAdmins = _.where(doc.members, {isActive: true, isAdmin: true}).length;
  341. if (nbAdmins > 1)
  342. return false;
  343. // If all the previous conditions were verified, we can't remove
  344. // a user if it's an admin
  345. const removedMemberId = modifier.$pull.members.userId;
  346. return Boolean(_.findWhere(doc.members, {
  347. userId: removedMemberId,
  348. isAdmin: true,
  349. }));
  350. },
  351. fetch: ['members'],
  352. });
  353. Meteor.methods({
  354. quitBoard(boardId) {
  355. check(boardId, String);
  356. const board = Boards.findOne(boardId);
  357. if (board) {
  358. const userId = Meteor.userId();
  359. const index = board.memberIndex(userId);
  360. if (index>=0) {
  361. board.removeMember(userId);
  362. return true;
  363. } else throw new Meteor.Error('error-board-notAMember');
  364. } else throw new Meteor.Error('error-board-doesNotExist');
  365. },
  366. });
  367. }
  368. if (Meteor.isServer) {
  369. // Let MongoDB ensure that a member is not included twice in the same board
  370. Meteor.startup(() => {
  371. Boards._collection._ensureIndex({
  372. _id: 1,
  373. 'members.userId': 1,
  374. }, { unique: true });
  375. });
  376. // Genesis: the first activity of the newly created board
  377. Boards.after.insert((userId, doc) => {
  378. Activities.insert({
  379. userId,
  380. type: 'board',
  381. activityTypeId: doc._id,
  382. activityType: 'createBoard',
  383. boardId: doc._id,
  384. });
  385. });
  386. // If the user remove one label from a board, we cant to remove reference of
  387. // this label in any card of this board.
  388. Boards.after.update((userId, doc, fieldNames, modifier) => {
  389. if (!_.contains(fieldNames, 'labels') ||
  390. !modifier.$pull ||
  391. !modifier.$pull.labels ||
  392. !modifier.$pull.labels._id) {
  393. return;
  394. }
  395. const removedLabelId = modifier.$pull.labels._id;
  396. Cards.update(
  397. { boardId: doc._id },
  398. {
  399. $pull: {
  400. labelIds: removedLabelId,
  401. },
  402. },
  403. { multi: true }
  404. );
  405. });
  406. // Add a new activity if we add or remove a member to the board
  407. Boards.after.update((userId, doc, fieldNames, modifier) => {
  408. if (!_.contains(fieldNames, 'members')) {
  409. return;
  410. }
  411. let memberId;
  412. // Say hello to the new member
  413. if (modifier.$push && modifier.$push.members) {
  414. memberId = modifier.$push.members.userId;
  415. Activities.insert({
  416. userId,
  417. memberId,
  418. type: 'member',
  419. activityType: 'addBoardMember',
  420. boardId: doc._id,
  421. });
  422. }
  423. // Say goodbye to the former member
  424. if (modifier.$pull && modifier.$pull.members) {
  425. memberId = modifier.$pull.members.userId;
  426. Activities.insert({
  427. userId,
  428. memberId,
  429. type: 'member',
  430. activityType: 'removeBoardMember',
  431. boardId: doc._id,
  432. });
  433. }
  434. });
  435. }