boards.js 10 KB

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