From b3851817ecd59b039f2c2228d08a1c6fd8e60d60 Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Thu, 3 Sep 2015 23:12:46 +0200 Subject: Enforce a consistent ES6 coding style Replace the old (and broken) jshint + jscsrc by eslint and configure it to support some of the ES6 features. The command `eslint` currently has one error which is a bug that was discovered by its static analysis and should be fixed (usage of a dead object). --- collections/activities.js | 24 ++++---- collections/attachments.js | 36 +++++------ collections/avatars.js | 18 +++--- collections/boards.js | 136 ++++++++++++++++++++--------------------- collections/cards.js | 145 ++++++++++++++++++++++---------------------- collections/lists.js | 44 +++++++------- collections/unsavedEdits.js | 12 ++-- collections/users.js | 102 +++++++++++++++---------------- 8 files changed, 256 insertions(+), 261 deletions(-) (limited to 'collections') diff --git a/collections/activities.js b/collections/activities.js index 1e24cf7c..5de07ee5 100644 --- a/collections/activities.js +++ b/collections/activities.js @@ -11,41 +11,41 @@ Activities = new Mongo.Collection('activities'); Activities.helpers({ - board: function() { + board() { return Boards.findOne(this.boardId); }, - user: function() { + user() { return Users.findOne(this.userId); }, - member: function() { + member() { return Users.findOne(this.memberId); }, - list: function() { + list() { return Lists.findOne(this.listId); }, - oldList: function() { + oldList() { return Lists.findOne(this.oldListId); }, - card: function() { + card() { return Cards.findOne(this.cardId); }, - comment: function() { + comment() { return CardComments.findOne(this.commentId); }, - attachment: function() { + attachment() { return Attachments.findOne(this.attachmentId); - } + }, }); -Activities.before.insert(function(userId, doc) { +Activities.before.insert((userId, doc) => { doc.createdAt = new Date(); }); // For efficiency create an index on the date of creation. if (Meteor.isServer) { - Meteor.startup(function() { + Meteor.startup(() => { Activities._collection._ensureIndex({ - createdAt: -1 + createdAt: -1, }); }); } diff --git a/collections/attachments.js b/collections/attachments.js index c8fe6b18..8ef0fef0 100644 --- a/collections/attachments.js +++ b/collections/attachments.js @@ -3,19 +3,19 @@ Attachments = new FS.Collection('attachments', { // XXX Add a new store for cover thumbnails so we don't load big images in // the general board view - new FS.Store.GridFS('attachments') - ] + new FS.Store.GridFS('attachments'), + ], }); if (Meteor.isServer) { Attachments.allow({ - insert: function(userId, doc) { + insert(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - update: function(userId, doc) { + update(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - remove: function(userId, doc) { + remove(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, // We authorize the attachment download either: @@ -26,24 +26,24 @@ if (Meteor.isServer) { // // https://github.com/CollectionFS/Meteor-CollectionFS/issues/449 // - download: function(userId, doc) { - var query = { + download(userId, doc) { + const query = { $or: [ { 'members.userId': userId }, - { permission: 'public' } - ] + { permission: 'public' }, + ], }; - return !! Boards.findOne(doc.boardId, query); + return Boolean(Boards.findOne(doc.boardId, query)); }, - fetch: ['boardId'] + fetch: ['boardId'], }); } // XXX Enforce a schema for the Attachments CollectionFS -Attachments.files.before.insert(function(userId, doc) { - var file = new FS.File(doc); +Attachments.files.before.insert((userId, doc) => { + const file = new FS.File(doc); doc.userId = userId; // If the uploaded document is not an image we need to enforce browser @@ -54,26 +54,26 @@ Attachments.files.before.insert(function(userId, doc) { // See https://github.com/libreboard/libreboard/issues/99 // XXX Should we use `beforeWrite` option of CollectionFS instead of // collection-hooks? - if (! file.isImage()) { + if (!file.isImage()) { file.original.type = 'application/octet-stream'; } }); if (Meteor.isServer) { - Attachments.files.after.insert(function(userId, doc) { + Attachments.files.after.insert((userId, doc) => { Activities.insert({ + userId, type: 'card', activityType: 'addAttachment', attachmentId: doc._id, boardId: doc.boardId, cardId: doc.cardId, - userId: userId }); }); - Attachments.files.after.remove(function(userId, doc) { + Attachments.files.after.remove((userId, doc) => { Activities.remove({ - attachmentId: doc._id + attachmentId: doc._id, }); }); } diff --git a/collections/avatars.js b/collections/avatars.js index 87af7120..53924ffb 100644 --- a/collections/avatars.js +++ b/collections/avatars.js @@ -1,27 +1,27 @@ Avatars = new FS.Collection('avatars', { stores: [ - new FS.Store.GridFS('avatars') + new FS.Store.GridFS('avatars'), ], filter: { maxSize: 72000, allow: { - contentTypes: ['image/*'] - } - } + contentTypes: ['image/*'], + }, + }, }); -var isOwner = function(userId, file) { +function isOwner(userId, file) { return userId && userId === file.userId; -}; +} Avatars.allow({ insert: isOwner, update: isOwner, remove: isOwner, - download: function() { return true; }, - fetch: ['userId'] + download() { return true; }, + fetch: ['userId'], }); -Avatars.files.before.insert(function(userId, doc) { +Avatars.files.before.insert((userId, doc) => { doc.userId = userId; }); diff --git a/collections/boards.js b/collections/boards.js index ade70466..8260fc3d 100644 --- a/collections/boards.js +++ b/collections/boards.js @@ -2,27 +2,27 @@ Boards = new Mongo.Collection('boards'); Boards.attachSchema(new SimpleSchema({ title: { - type: String + type: String, }, slug: { - type: String + type: String, }, archived: { - type: Boolean + type: Boolean, }, createdAt: { type: Date, - denyUpdate: true + denyUpdate: true, }, // XXX Inconsistent field naming modifiedAt: { type: Date, denyInsert: true, - optional: true + optional: true, }, // De-normalized number of users that have starred this board stars: { - type: Number + type: Number, }, // De-normalized label system 'labels.$._id': { @@ -31,46 +31,46 @@ Boards.attachSchema(new SimpleSchema({ // always set on the server. // XXX Actually if we create a new label, the `_id` is set on the client // without being overwritten by the server, could it be a problem? - type: String + type: String, }, 'labels.$.name': { type: String, - optional: true + optional: true, }, 'labels.$.color': { type: String, allowedValues: [ 'green', 'yellow', 'orange', 'red', 'purple', - 'blue', 'sky', 'lime', 'pink', 'black' - ] + 'blue', 'sky', 'lime', 'pink', 'black', + ], }, // XXX We might want to maintain more informations under the member sub- // documents like de-normalized meta-data (the date the member joined the // board, the number of contributions, etc.). 'members.$.userId': { - type: String + type: String, }, 'members.$.isAdmin': { - type: Boolean + type: Boolean, }, 'members.$.isActive': { - type: Boolean + type: Boolean, }, permission: { type: String, - allowedValues: ['public', 'private'] + allowedValues: ['public', 'private'], }, color: { type: String, allowedValues: [ - 'belize', - 'nephritis', - 'pomegranate', - 'pumpkin', - 'wisteria', - 'midnight', - ] - } + 'belize', + 'nephritis', + 'pomegranate', + 'pumpkin', + 'wisteria', + 'midnight', + ], + }, })); if (Meteor.isServer) { @@ -78,30 +78,30 @@ if (Meteor.isServer) { insert: Meteor.userId, update: allowIsBoardAdmin, remove: allowIsBoardAdmin, - fetch: ['members'] + fetch: ['members'], }); // The number of users that have starred this board is managed by trusted code // and the user is not allowed to update it Boards.deny({ - update: function(userId, board, fieldNames) { + update(userId, board, fieldNames) { return _.contains(fieldNames, 'stars'); }, - fetch: [] + fetch: [], }); // We can't remove a member if it is the last administrator Boards.deny({ - update: function(userId, doc, fieldNames, modifier) { - if (! _.contains(fieldNames, 'members')) + update(userId, doc, fieldNames, modifier) { + if (!_.contains(fieldNames, 'members')) return false; // We only care in case of a $pull operation, ie remove a member - if (! _.isObject(modifier.$pull && modifier.$pull.members)) + if (!_.isObject(modifier.$pull && modifier.$pull.members)) return false; // If there is more than one admin, it's ok to remove anyone - var nbAdmins = _.filter(doc.members, function(member) { + const nbAdmins = _.filter(doc.members, (member) => { return member.isAdmin; }).length; if (nbAdmins > 1) @@ -109,36 +109,36 @@ if (Meteor.isServer) { // If all the previous conditions were verified, we can't remove // a user if it's an admin - var removedMemberId = modifier.$pull.members.userId; - return !! _.findWhere(doc.members, { + const removedMemberId = modifier.$pull.members.userId; + return Boolean(_.findWhere(doc.members, { userId: removedMemberId, - isAdmin: true - }); + isAdmin: true, + })); }, - fetch: ['members'] + fetch: ['members'], }); } Boards.helpers({ - isPublic: function() { + isPublic() { return this.permission === 'public'; }, - lists: function() { + lists() { return Lists.find({ boardId: this._id, archived: false }, { sort: { sort: 1 }}); }, - activities: function() { + activities() { return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 }}); }, - absoluteUrl: function() { + absoluteUrl() { return FlowRouter.path('board', { id: this._id, slug: this.slug }); }, - colorClass: function() { - return 'board-color-' + this.color; - } + colorClass() { + return `board-color-${this.color}`; + }, }); -Boards.before.insert(function(userId, doc) { +Boards.before.insert((userId, doc) => { // XXX We need to improve slug management. Only the id should be necessary // to identify a board in the code. // XXX If the board title is updated, the slug should also be updated. @@ -149,87 +149,87 @@ Boards.before.insert(function(userId, doc) { doc.createdAt = new Date(); doc.archived = false; doc.members = [{ - userId: userId, + userId, isAdmin: true, - isActive: true + isActive: true, }]; doc.stars = 0; doc.color = Boards.simpleSchema()._schema.color.allowedValues[0]; // Handle labels - var colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues; - var defaultLabelsColors = _.clone(colors).splice(0, 6); - doc.labels = _.map(defaultLabelsColors, function(val) { + const colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues; + const defaultLabelsColors = _.clone(colors).splice(0, 6); + doc.labels = _.map(defaultLabelsColors, (color) => { return { + color, _id: Random.id(6), name: '', - color: val }; }); }); -Boards.before.update(function(userId, doc, fieldNames, modifier) { +Boards.before.update((userId, doc, fieldNames, modifier) => { modifier.$set = modifier.$set || {}; modifier.$set.modifiedAt = new Date(); }); if (Meteor.isServer) { // Let MongoDB ensure that a member is not included twice in the same board - Meteor.startup(function() { + Meteor.startup(() => { Boards._collection._ensureIndex({ _id: 1, - 'members.userId': 1 + 'members.userId': 1, }, { unique: true }); }); // Genesis: the first activity of the newly created board - Boards.after.insert(function(userId, doc) { + Boards.after.insert((userId, doc) => { Activities.insert({ + userId, type: 'board', activityTypeId: doc._id, activityType: 'createBoard', boardId: doc._id, - userId: userId }); }); // If the user remove one label from a board, we cant to remove reference of // this label in any card of this board. - Boards.after.update(function(userId, doc, fieldNames, modifier) { - if (! _.contains(fieldNames, 'labels') || - ! modifier.$pull || - ! modifier.$pull.labels || - ! modifier.$pull.labels._id) + Boards.after.update((userId, doc, fieldNames, modifier) => { + if (!_.contains(fieldNames, 'labels') || + !modifier.$pull || + !modifier.$pull.labels || + !modifier.$pull.labels._id) return; - var removedLabelId = modifier.$pull.labels._id; + const removedLabelId = modifier.$pull.labels._id; Cards.update( { boardId: doc._id }, { $pull: { - labels: removedLabelId - } + labels: removedLabelId, + }, }, { multi: true } ); }); // Add a new activity if we add or remove a member to the board - Boards.after.update(function(userId, doc, fieldNames, modifier) { - if (! _.contains(fieldNames, 'members')) + Boards.after.update((userId, doc, fieldNames, modifier) => { + if (!_.contains(fieldNames, 'members')) return; - var memberId; + let memberId; // Say hello to the new member if (modifier.$push && modifier.$push.members) { memberId = modifier.$push.members.userId; Activities.insert({ + userId, + memberId, type: 'member', activityType: 'addBoardMember', boardId: doc._id, - userId: userId, - memberId: memberId }); } @@ -237,11 +237,11 @@ if (Meteor.isServer) { if (modifier.$pull && modifier.$pull.members) { memberId = modifier.$pull.members.userId; Activities.insert({ + userId, + memberId, type: 'member', activityType: 'removeBoardMember', boardId: doc._id, - userId: userId, - memberId: memberId }); } }); diff --git a/collections/cards.js b/collections/cards.js index d9f1bf6e..97ba4e3c 100644 --- a/collections/cards.js +++ b/collections/cards.js @@ -6,162 +6,161 @@ CardComments = new Mongo.Collection('card_comments'); // of comments just to display the number of them in the board view. Cards.attachSchema(new SimpleSchema({ title: { - type: String + type: String, }, archived: { - type: Boolean + type: Boolean, }, listId: { - type: String + type: String, }, // The system could work without this `boardId` information (we could deduce // the board identifier from the card), but it would make the system more // difficult to manage and less efficient. boardId: { - type: String + type: String, }, coverId: { type: String, - optional: true + optional: true, }, createdAt: { type: Date, - denyUpdate: true + denyUpdate: true, }, dateLastActivity: { - type: Date + type: Date, }, description: { type: String, - optional: true + optional: true, }, labelIds: { type: [String], - optional: true + optional: true, }, members: { type: [String], - optional: true + optional: true, }, // XXX Should probably be called `authorId`. Is it even needed since we have // the `members` field? userId: { - type: String + type: String, }, sort: { type: Number, - decimal: true - } + decimal: true, + }, })); CardComments.attachSchema(new SimpleSchema({ boardId: { - type: String + type: String, }, cardId: { - type: String + type: String, }, // XXX Rename in `content`? `text` is a bit vague... text: { - type: String + type: String, }, // XXX We probably don't need this information here, since we already have it // in the associated comment creation activity createdAt: { type: Date, - denyUpdate: false + denyUpdate: false, }, // XXX Should probably be called `authorId` userId: { - type: String - } + type: String, + }, })); if (Meteor.isServer) { Cards.allow({ - insert: function(userId, doc) { + insert(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - update: function(userId, doc) { + update(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - remove: function(userId, doc) { + remove(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - fetch: ['boardId'] + fetch: ['boardId'], }); CardComments.allow({ - insert: function(userId, doc) { + insert(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - update: function(userId, doc) { + update(userId, doc) { return userId === doc.userId; }, - remove: function(userId, doc) { + remove(userId, doc) { return userId === doc.userId; }, - fetch: ['userId', 'boardId'] + fetch: ['userId', 'boardId'], }); } Cards.helpers({ - list: function() { + list() { return Lists.findOne(this.listId); }, - board: function() { + board() { return Boards.findOne(this.boardId); }, - labels: function() { - var self = this; - var boardLabels = self.board().labels; - var cardLabels = _.filter(boardLabels, function(label) { - return _.contains(self.labelIds, label._id); + labels() { + const boardLabels = this.board().labels; + const cardLabels = _.filter(boardLabels, (label) => { + return _.contains(this.labelIds, label._id); }); return cardLabels; }, - hasLabel: function(labelId) { + hasLabel(labelId) { return _.contains(this.labelIds, labelId); }, - user: function() { + user() { return Users.findOne(this.userId); }, - isAssigned: function(memberId) { + isAssigned(memberId) { return _.contains(this.members, memberId); }, - activities: function() { + activities() { return Activities.find({ cardId: this._id }, { sort: { createdAt: -1 }}); }, - comments: function() { + comments() { return CardComments.find({ cardId: this._id }, { sort: { createdAt: -1 }}); }, - attachments: function() { + attachments() { return Attachments.find({ cardId: this._id }, { sort: { uploadedAt: -1 }}); }, - cover: function() { + cover() { return Attachments.findOne(this.coverId); }, - absoluteUrl: function() { - var board = this.board(); + absoluteUrl() { + const board = this.board(); return FlowRouter.path('card', { boardId: board._id, slug: board.slug, - cardId: this._id + cardId: this._id, }); }, - rootUrl: function() { + rootUrl() { return Meteor.absoluteUrl(this.absoluteUrl().replace('/', '')); - } + }, }); CardComments.helpers({ - user: function() { + user() { return Users.findOne(this.userId); - } + }, }); CardComments.hookOptions.after.update = { fetchPrevious: false }; -Cards.before.insert(function(userId, doc) { +Cards.before.insert((userId, doc) => { doc.createdAt = new Date(); doc.dateLastActivity = new Date(); @@ -169,44 +168,44 @@ Cards.before.insert(function(userId, doc) { doc.archived = false; // userId native set. - if (! doc.userId) + if (!doc.userId) doc.userId = userId; }); -CardComments.before.insert(function(userId, doc) { +CardComments.before.insert((userId, doc) => { doc.createdAt = new Date(); doc.userId = userId; }); if (Meteor.isServer) { - Cards.after.insert(function(userId, doc) { + Cards.after.insert((userId, doc) => { Activities.insert({ + userId, activityType: 'createCard', boardId: doc.boardId, listId: doc.listId, cardId: doc._id, - userId: userId }); }); // New activity for card (un)archivage - Cards.after.update(function(userId, doc, fieldNames) { + Cards.after.update((userId, doc, fieldNames) => { if (_.contains(fieldNames, 'archived')) { if (doc.archived) { Activities.insert({ + userId, activityType: 'archivedCard', boardId: doc.boardId, listId: doc.listId, cardId: doc._id, - userId: userId }); } else { Activities.insert({ + userId, activityType: 'restoredCard', boardId: doc.boardId, listId: doc.listId, cardId: doc._id, - userId: userId }); } } @@ -214,34 +213,34 @@ if (Meteor.isServer) { // New activity for card moves Cards.after.update(function(userId, doc, fieldNames) { - var oldListId = this.previous.listId; + const oldListId = this.previous.listId; if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) { Activities.insert({ + userId, + oldListId, activityType: 'moveCard', listId: doc.listId, - oldListId: oldListId, boardId: doc.boardId, cardId: doc._id, - userId: userId }); } }); // Add a new activity if we add or remove a member to the card - Cards.before.update(function(userId, doc, fieldNames, modifier) { - if (! _.contains(fieldNames, 'members')) + Cards.before.update((userId, doc, fieldNames, modifier) => { + if (!_.contains(fieldNames, 'members')) return; - var memberId; + let memberId; // Say hello to the new member if (modifier.$addToSet && modifier.$addToSet.members) { memberId = modifier.$addToSet.members; - if (! _.contains(doc.members, memberId)) { + if (!_.contains(doc.members, memberId)) { Activities.insert({ + userId, + memberId, activityType: 'joinMember', boardId: doc.boardId, cardId: doc._id, - userId: userId, - memberId: memberId }); } } @@ -250,34 +249,34 @@ if (Meteor.isServer) { if (modifier.$pull && modifier.$pull.members) { memberId = modifier.$pull.members; Activities.insert({ + userId, + memberId, activityType: 'unjoinMember', boardId: doc.boardId, cardId: doc._id, - userId: userId, - memberId: memberId }); } }); // Remove all activities associated with a card if we remove the card - Cards.after.remove(function(userId, doc) { + Cards.after.remove((userId, doc) => { Activities.remove({ - cardId: doc._id + cardId: doc._id, }); }); - CardComments.after.insert(function(userId, doc) { + CardComments.after.insert((userId, doc) => { Activities.insert({ + userId, activityType: 'addComment', boardId: doc.boardId, cardId: doc.cardId, commentId: doc._id, - userId: userId }); }); - CardComments.after.remove(function(userId, doc) { - var activity = Activities.findOne({ commentId: doc._id }); + CardComments.after.remove((userId, doc) => { + const activity = Activities.findOne({ commentId: doc._id }); if (activity) { Activities.remove(activity._id); } diff --git a/collections/lists.js b/collections/lists.js index 1a30dbba..0c6ba407 100644 --- a/collections/lists.js +++ b/collections/lists.js @@ -2,92 +2,92 @@ Lists = new Mongo.Collection('lists'); Lists.attachSchema(new SimpleSchema({ title: { - type: String + type: String, }, archived: { - type: Boolean + type: Boolean, }, boardId: { - type: String + type: String, }, createdAt: { type: Date, - denyUpdate: true + denyUpdate: true, }, sort: { type: Number, decimal: true, // XXX We should probably provide a default - optional: true + optional: true, }, updatedAt: { type: Date, denyInsert: true, - optional: true - } + optional: true, + }, })); if (Meteor.isServer) { Lists.allow({ - insert: function(userId, doc) { + insert(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - update: function(userId, doc) { + update(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - remove: function(userId, doc) { + remove(userId, doc) { return allowIsBoardMember(userId, Boards.findOne(doc.boardId)); }, - fetch: ['boardId'] + fetch: ['boardId'], }); } Lists.helpers({ - cards: function() { + cards() { return Cards.find(Filter.mongoSelector({ listId: this._id, - archived: false + archived: false, }), { sort: ['sort'] }); }, - board: function() { + board() { return Boards.findOne(this.boardId); - } + }, }); // HOOKS Lists.hookOptions.after.update = { fetchPrevious: false }; -Lists.before.insert(function(userId, doc) { +Lists.before.insert((userId, doc) => { doc.createdAt = new Date(); doc.archived = false; - if (! doc.userId) + if (!doc.userId) doc.userId = userId; }); -Lists.before.update(function(userId, doc, fieldNames, modifier) { +Lists.before.update((userId, doc, fieldNames, modifier) => { modifier.$set = modifier.$set || {}; modifier.$set.modifiedAt = new Date(); }); if (Meteor.isServer) { - Lists.after.insert(function(userId, doc) { + Lists.after.insert((userId, doc) => { Activities.insert({ + userId, type: 'list', activityType: 'createList', boardId: doc.boardId, listId: doc._id, - userId: userId }); }); - Lists.after.update(function(userId, doc) { + Lists.after.update((userId, doc) => { if (doc.archived) { Activities.insert({ + userId, type: 'list', activityType: 'archivedList', listId: doc._id, boardId: doc.boardId, - userId: userId }); } }); diff --git a/collections/unsavedEdits.js b/collections/unsavedEdits.js index e9105daf..87a70e22 100644 --- a/collections/unsavedEdits.js +++ b/collections/unsavedEdits.js @@ -4,16 +4,16 @@ UnsavedEditCollection = new Mongo.Collection('unsaved-edits'); UnsavedEditCollection.attachSchema(new SimpleSchema({ fieldName: { - type: String + type: String, }, docId: { - type: String + type: String, }, value: { - type: String + type: String, }, userId: { - type: String + type: String, }, })); @@ -25,10 +25,10 @@ if (Meteor.isServer) { insert: isAuthor, update: isAuthor, remove: isAuthor, - fetch: ['userId'] + fetch: ['userId'], }); } -UnsavedEditCollection.before.insert(function(userId, doc) { +UnsavedEditCollection.before.insert((userId, doc) => { doc.userId = userId; }); diff --git a/collections/users.js b/collections/users.js index 28b63ba7..b30b7805 100644 --- a/collections/users.js +++ b/collections/users.js @@ -2,42 +2,42 @@ Users = Meteor.users; // Search a user in the complete server database by its name or username. This // is used for instance to add a new user to a board. -var searchInFields = ['username', 'profile.name']; +const searchInFields = ['username', 'profile.name']; Users.initEasySearch(searchInFields, { use: 'mongo-db', - returnFields: searchInFields + returnFields: searchInFields, }); Users.helpers({ - boards: function() { + boards() { return Boards.find({ userId: this._id }); }, - starredBoards: function() { - var starredBoardIds = this.profile.starredBoards || []; + starredBoards() { + const starredBoardIds = this.profile.starredBoards || []; return Boards.find({archived: false, _id: {$in: starredBoardIds}}); }, - hasStarred: function(boardId) { - var starredBoardIds = this.profile.starredBoards || []; + hasStarred(boardId) { + const starredBoardIds = this.profile.starredBoards || []; return _.contains(starredBoardIds, boardId); }, - isBoardMember: function() { - var board = Boards.findOne(Session.get('currentBoard')); + isBoardMember() { + const board = Boards.findOne(Session.get('currentBoard')); return board && _.contains(_.pluck(board.members, 'userId'), this._id) && _.where(board.members, {userId: this._id})[0].isActive; }, - isBoardAdmin: function() { - var board = Boards.findOne(Session.get('currentBoard')); + isBoardAdmin() { + const board = Boards.findOne(Session.get('currentBoard')); if (this.isBoardMember(board)) return _.where(board.members, {userId: this._id})[0].isAdmin; }, - getInitials: function() { - var profile = this.profile || {}; + getInitials() { + const profile = this.profile || {}; if (profile.initials) return profile.initials; else if (profile.fullname) { - return _.reduce(profile.fullname.split(/\s+/), function(memo, word) { + return _.reduce(profile.fullname.split(/\s+/), (memo, word) => { return memo + word[0]; }, '').toUpperCase(); @@ -46,43 +46,41 @@ Users.helpers({ } }, - toggleBoardStar: function(boardId) { - var queryType = this.hasStarred(boardId) ? '$pull' : '$addToSet'; - var query = {}; - query[queryType] = { - 'profile.starredBoards': boardId - }; - Meteor.users.update(this._id, query); - } + toggleBoardStar(boardId) { + const queryKind = this.hasStarred(boardId) ? '$pull' : '$addToSet'; + Meteor.users.update(this._id, { + [queryKind]: { + 'profile.starredBoards': boardId, + }, + }); + }, }); Meteor.methods({ - setUsername: function(username) { + setUsername(username) { check(username, String); - var nUsersWithUsername = Users.find({username: username}).count(); + const nUsersWithUsername = Users.find({ username }).count(); if (nUsersWithUsername > 0) { throw new Meteor.Error('username-already-taken'); } else { - Users.update(this.userId, {$set: { - username: username - }}); + Users.update(this.userId, {$set: { username }}); } - } + }, }); -Users.before.insert(function(userId, doc) { +Users.before.insert((userId, doc) => { doc.profile = doc.profile || {}; - if (! doc.username && doc.profile.name) { + if (!doc.username && doc.profile.name) { doc.username = doc.profile.name.toLowerCase().replace(/\s/g, ''); } }); if (Meteor.isServer) { // Let mongoDB ensure username unicity - Meteor.startup(function() { + Meteor.startup(() => { Users._collection._ensureIndex({ - username: 1 + username: 1, }, { unique: true }); }); @@ -94,44 +92,44 @@ if (Meteor.isServer) { Users.after.update(function(userId, user, fieldNames) { // The `starredBoards` list is hosted on the `profile` field. If this // field hasn't been modificated we don't need to run this hook. - if (! _.contains(fieldNames, 'profile')) + if (!_.contains(fieldNames, 'profile')) return; // To calculate a diff of board starred ids, we get both the previous // and the newly board ids list - var getStarredBoardsIds = function(doc) { + function getStarredBoardsIds(doc) { return doc.profile && doc.profile.starredBoards; - }; - var oldIds = getStarredBoardsIds(this.previous); - var newIds = getStarredBoardsIds(user); + } + const oldIds = getStarredBoardsIds(this.previous); + const newIds = getStarredBoardsIds(user); // The _.difference(a, b) method returns the values from a that are not in // b. We use it to find deleted and newly inserted ids by using it in one // direction and then in the other. - var incrementBoards = function(boardsIds, inc) { - _.forEach(boardsIds, function(boardId) { + function incrementBoards(boardsIds, inc) { + _.forEach(boardsIds, (boardId) => { Boards.update(boardId, {$inc: {stars: inc}}); }); - }; + } incrementBoards(_.difference(oldIds, newIds), -1); incrementBoards(_.difference(newIds, oldIds), +1); }); // XXX i18n - Users.after.insert(function(userId, doc) { - var ExampleBoard = { + Users.after.insert((userId, doc) => { + const ExampleBoard = { title: 'Welcome Board', userId: doc._id, - permission: 'private' + permission: 'private', }; // Insert the Welcome Board - Boards.insert(ExampleBoard, function(err, boardId) { + Boards.insert(ExampleBoard, (err, boardId) => { - _.forEach(['Basics', 'Advanced'], function(title) { - var list = { - title: title, - boardId: boardId, + _.forEach(['Basics', 'Advanced'], (title) => { + const list = { + title, + boardId, userId: ExampleBoard.userId, // XXX Not certain this is a bug, but we except these fields get @@ -139,7 +137,7 @@ if (Meteor.isServer) { // hook is not called in this case, we have to dublicate the logic and // set them here. archived: false, - createdAt: new Date() + createdAt: new Date(), }; Lists.insert(list); @@ -150,9 +148,7 @@ if (Meteor.isServer) { // Presence indicator if (Meteor.isClient) { - Presence.state = function() { - return { - currentBoardId: Session.get('currentBoard') - }; + Presence.state = () => { + return { currentBoardId: Session.get('currentBoard') }; }; } -- cgit v1.2.3-1-g7c22