diff --git a/client/components/boards/boardsList.js b/client/components/boards/boardsList.js index 77201e3ad..3a94612c0 100644 --- a/client/components/boards/boardsList.js +++ b/client/components/boards/boardsList.js @@ -166,14 +166,7 @@ BlazeComponent.extendComponent({ if (allowPrivateVisibilityOnly !== undefined && allowPrivateVisibilityOnly.booleanValue) { query.$and.push({ 'permission': 'private' }); } - const currUser = Users.findOne(Meteor.userId()); - - // const currUser = Users.findOne(Meteor.userId(), { - // fields: { - // orgs: 1, - // teams: 1, - // }, - // }); + const currUser = ReactiveCache.getCurrentUser(); let orgIdsUserBelongs = currUser !== undefined && currUser.teams !== 'undefined' ? currUser.orgIdsUserBelongs() : ''; if (orgIdsUserBelongs && orgIdsUserBelongs != '') { diff --git a/client/components/cards/cardDetails.js b/client/components/cards/cardDetails.js index d2163fcb5..e98b1e41f 100644 --- a/client/components/cards/cardDetails.js +++ b/client/components/cards/cardDetails.js @@ -775,7 +775,7 @@ const filterMembers = (filterTerm) => { members = members .map(member => ({ member, - user: Users.findOne(member.userId) + user: ReactiveCache.getUser(member.userId) })) .filter(({ user }) => (user.profile.fullname !== undefined && user.profile.fullname.toLowerCase().indexOf(filterTerm.toLowerCase()) !== -1) @@ -1640,7 +1640,7 @@ Template.cardAssigneesPopup.helpers({ }, user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, }); @@ -1655,13 +1655,13 @@ Template.cardAssigneePopup.helpers({ }, memberType() { - const user = Users.findOne(this.userId); + const user = ReactiveCache.getUser(this.userId); return user && user.isBoardAdmin() ? 'admin' : 'normal'; }, /* presenceStatusClassName() { - const user = Users.findOne(this.userId); + const user = ReactiveCache.getUser(this.userId); const userPresence = presences.findOne({ userId: this.userId }); if (user && user.isInvitedTo(Session.get('currentBoard'))) return 'pending'; else if (!userPresence) return 'disconnected'; @@ -1678,7 +1678,7 @@ Template.cardAssigneePopup.helpers({ }, user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, }); diff --git a/client/components/lists/listBody.js b/client/components/lists/listBody.js index a9ec5beeb..e505e119e 100644 --- a/client/components/lists/listBody.js +++ b/client/components/lists/listBody.js @@ -353,7 +353,7 @@ BlazeComponent.extendComponent({ const currentBoard = Utils.getCurrentBoard(); callback( $.map(currentBoard.activeMembers(), member => { - const user = Users.findOne(member.userId); + const user = ReactiveCache.getUser(member.userId); return user.username.indexOf(term) === 0 ? user : null; }), ); diff --git a/client/components/main/editor.js b/client/components/main/editor.js index 2e31bb16e..250c7ee1a 100644 --- a/client/components/main/editor.js +++ b/client/components/main/editor.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + const specialHandles = [ {userId: 'board_members', username: 'board_members'}, {userId: 'card_members', username: 'card_members'} @@ -19,7 +21,7 @@ BlazeComponent.extendComponent({ currentBoard .activeMembers() .map(member => { - const user = Users.findOne(member.userId); + const user = ReactiveCache.getUser(member.userId); const username = user.username; const fullName = user.profile && user.profile !== undefined && user.profile.fullname ? user.profile.fullname : ""; return username.includes(term) || fullName.includes(term) ? user : null; @@ -334,7 +336,7 @@ Blaze.Template.registerHelper( DOMPurify.sanitize(content, { ALLOW_UNKNOWN_PROTOCOLS: true }), ); const knowedUsers = _.union(currentBoard.members.map(member => { - const u = Users.findOne(member.userId); + const u = ReactiveCache.getUser(member.userId); if (u) { member.username = u.username; } diff --git a/client/components/notifications/notifications.js b/client/components/notifications/notifications.js index c0aa6cb59..23871d3b9 100644 --- a/client/components/notifications/notifications.js +++ b/client/components/notifications/notifications.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + // this hides the notifications drawer if anyone clicks off of the panel Template.body.events({ click(event) { @@ -12,7 +14,7 @@ Template.body.events({ Template.notifications.helpers({ unreadNotifications() { - const notifications = Users.findOne(Meteor.userId()).notifications(); + const notifications = ReactiveCache.getCurrentUser().notifications(); const unreadNotifications = _.filter(notifications, v => !v.read); return unreadNotifications.length; }, diff --git a/client/components/notifications/notificationsDrawer.js b/client/components/notifications/notificationsDrawer.js index 76abeea7d..c0b1b1cfa 100644 --- a/client/components/notifications/notificationsDrawer.js +++ b/client/components/notifications/notificationsDrawer.js @@ -1,3 +1,4 @@ +import { ReactiveCache } from '/imports/reactiveCache'; import { toggleNotificationsDrawer } from './notifications.js'; Template.notificationsDrawer.onCreated(function() { @@ -14,7 +15,7 @@ Template.notificationsDrawer.onCreated(function() { Template.notificationsDrawer.helpers({ transformedProfile() { - return Users.findOne(Meteor.userId()); + return ReactiveCache.getCurrentUser(); }, readNotifications() { const readNotifications = _.filter( diff --git a/client/components/settings/adminReports.js b/client/components/settings/adminReports.js index de3135fac..bde34983c 100644 --- a/client/components/settings/adminReports.js +++ b/client/components/settings/adminReports.js @@ -1,3 +1,4 @@ +import { ReactiveCache } from '/imports/reactiveCache'; import { TAPi18n } from '/imports/i18n'; import { AttachmentStorage } from '/models/attachments'; import { CardSearchPagedComponent } from '/client/lib/cardSearch'; @@ -163,7 +164,7 @@ class AdminReport extends BlazeComponent { userNames(members) { let text = ''; members.forEach(member => { - const user = Users.findOne(member.userId); + const user = ReactiveCache.getUser(member.userId); text += text ? ', ' : ''; if (user) { text += user.username; @@ -181,7 +182,7 @@ class AdminReport extends BlazeComponent { userNames(userIds) { let text = ''; userIds.forEach(userId => { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); text += text ? ', ' : ''; text += user.username; }); diff --git a/client/components/settings/peopleBody.js b/client/components/settings/peopleBody.js index c5fe3e401..d2e447e95 100644 --- a/client/components/settings/peopleBody.js +++ b/client/components/settings/peopleBody.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + const orgsPerPage = 25; const teamsPerPage = 25; const usersPerPage = 25; @@ -202,7 +204,7 @@ Template.teamRow.helpers({ Template.peopleRow.helpers({ userData() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, }); @@ -245,7 +247,7 @@ Template.editTeamPopup.helpers({ Template.editUserPopup.helpers({ user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, authentications() { return Template.instance().authenticationMethods.get(); @@ -258,12 +260,12 @@ Template.editUserPopup.helpers({ }, isSelected(match) { const userId = Template.instance().data.userId; - const selected = Users.findOne(userId).authenticationMethod; + const selected = ReactiveCache.getUser(userId).authenticationMethod; return selected === match; }, isLdap() { const userId = Template.instance().data.userId; - const selected = Users.findOne(userId).authenticationMethod; + const selected = ReactiveCache.getUser(userId).authenticationMethod; return selected === 'ldap'; }, errorMessage() { @@ -318,7 +320,7 @@ Template.newTeamPopup.helpers({ Template.newUserPopup.helpers({ user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, authentications() { return Template.instance().authenticationMethods.get(); @@ -332,7 +334,7 @@ Template.newUserPopup.helpers({ isSelected(match) { const userId = Template.instance().data.userId; if(userId){ - const selected = Users.findOne(userId).authenticationMethod; + const selected = ReactiveCache.getUser(userId).authenticationMethod; return selected === match; } else{ @@ -341,7 +343,7 @@ Template.newUserPopup.helpers({ }, isLdap() { const userId = Template.instance().data.userId; - const selected = Users.findOne(userId).authenticationMethod; + const selected = ReactiveCache.getUser(userId).authenticationMethod; return selected === 'ldap'; }, errorMessage() { @@ -382,7 +384,7 @@ BlazeComponent.extendComponent({ BlazeComponent.extendComponent({ onCreated() {}, user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, events() { return [ @@ -440,7 +442,7 @@ BlazeComponent.extendComponent({ if(document.getElementById('addAction').checked){ for(let i = 0; i < selectedUserChkBoxUserIds.length; i++){ - currentUser = Users.findOne(selectedUserChkBoxUserIds[i]); + currentUser = ReactiveCache.getUser(selectedUserChkBoxUserIds[i]); userTms = currentUser.teams; if(userTms == undefined || userTms.length == 0){ userTms = []; @@ -469,7 +471,7 @@ BlazeComponent.extendComponent({ } else{ for(let i = 0; i < selectedUserChkBoxUserIds.length; i++){ - currentUser = Users.findOne(selectedUserChkBoxUserIds[i]); + currentUser = ReactiveCache.getUser(selectedUserChkBoxUserIds[i]); userTms = currentUser.teams; if(userTms !== undefined || userTms.length > 0) { @@ -647,7 +649,7 @@ Template.editTeamPopup.events({ Template.editUserPopup.events({ submit(event, templateInstance) { event.preventDefault(); - const user = Users.findOne(this.userId); + const user = ReactiveCache.getUser(this.userId); const username = templateInstance.find('.js-profile-username').value.trim(); const fullname = templateInstance.find('.js-profile-fullname').value.trim(); const initials = templateInstance.find('.js-profile-initials').value.trim(); @@ -1139,19 +1141,19 @@ Template.settingsUserPopup.events({ Template.settingsUserPopup.helpers({ user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, authentications() { return Template.instance().authenticationMethods.get(); }, isSelected(match) { const userId = Template.instance().data.userId; - const selected = Users.findOne(userId).authenticationMethod; + const selected = ReactiveCache.getUser(userId).authenticationMethod; return selected === match; }, isLdap() { const userId = Template.instance().data.userId; - const selected = Users.findOne(userId).authenticationMethod; + const selected = ReactiveCache.getUser(userId).authenticationMethod; return selected === 'ldap'; }, errorMessage() { diff --git a/client/components/sidebar/sidebar.js b/client/components/sidebar/sidebar.js index f9e18d046..57f30fcc8 100644 --- a/client/components/sidebar/sidebar.js +++ b/client/components/sidebar/sidebar.js @@ -170,13 +170,13 @@ EscapeActions.register( Template.memberPopup.helpers({ user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, isBoardAdmin() { return Meteor.user().isBoardAdmin(); }, memberType() { - const type = Users.findOne(this.userId).isBoardAdmin() ? 'admin' : 'normal'; + const type = ReactiveCache.getUser(this.userId).isBoardAdmin() ? 'admin' : 'normal'; if (type === 'normal') { const currentBoard = Utils.getCurrentBoard(); const commentOnly = currentBoard.hasCommentOnly(this.userId); @@ -196,7 +196,7 @@ Template.memberPopup.helpers({ } }, isInvited() { - return Users.findOne(this.userId).isInvitedTo(Session.get('currentBoard')); + return ReactiveCache.getUser(this.userId).isInvitedTo(Session.get('currentBoard')); }, }); @@ -299,7 +299,7 @@ Template.memberPopup.events({ Template.removeMemberPopup.helpers({ user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, board() { return Utils.getCurrentBoard(); @@ -1450,7 +1450,7 @@ BlazeComponent.extendComponent({ isBoardMember() { const userId = this.currentData().__originalId; - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); return user && user.isBoardMember(); }, diff --git a/client/components/users/userAvatar.js b/client/components/users/userAvatar.js index 3cbec528e..6896e6419 100644 --- a/client/components/users/userAvatar.js +++ b/client/components/users/userAvatar.js @@ -16,13 +16,13 @@ Template.userAvatar.helpers({ }, memberType() { - const user = Users.findOne(this.userId); + const user = ReactiveCache.getUser(this.userId); return user && user.isBoardAdmin() ? 'admin' : 'normal'; }, /* presenceStatusClassName() { - const user = Users.findOne(this.userId); + const user = ReactiveCache.getUser(this.userId); const userPresence = presences.findOne({ userId: this.userId }); if (user && user.isInvitedTo(Session.get('currentBoard'))) return 'pending'; else if (!userPresence) return 'disconnected'; @@ -36,12 +36,12 @@ Template.userAvatar.helpers({ Template.userAvatarInitials.helpers({ initials() { - const user = Users.findOne(this.userId); + const user = ReactiveCache.getUser(this.userId); return user && user.getInitials(); }, viewPortWidth() { - const user = Users.findOne(this.userId); + const user = ReactiveCache.getUser(this.userId); return ((user && user.getInitials().length) || 1) * 12; }, }); @@ -249,7 +249,7 @@ Template.cardMembersPopup.helpers({ }, user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, }); @@ -264,7 +264,7 @@ Template.cardMembersPopup.events({ Template.cardMemberPopup.helpers({ user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, }); diff --git a/client/config/blazeHelpers.js b/client/config/blazeHelpers.js index 332415cc0..f992f3321 100644 --- a/client/config/blazeHelpers.js +++ b/client/config/blazeHelpers.js @@ -1,3 +1,4 @@ +import { ReactiveCache } from '/imports/reactiveCache'; import { Blaze } from 'meteor/blaze'; import { Session } from 'meteor/session'; import moment from 'moment/min/moment-with-locales'; @@ -22,7 +23,7 @@ Blaze.registerHelper('currentSetting', () => { return ret; }); -Blaze.registerHelper('getUser', userId => Users.findOne(userId)); +Blaze.registerHelper('getUser', userId => ReactiveCache.getUser(userId)); Blaze.registerHelper('concat', (...args) => args.slice(0, -1).join('')); diff --git a/imports/reactiveCache.js b/imports/reactiveCache.js index 5a639e6b6..9cf4bae88 100644 --- a/imports/reactiveCache.js +++ b/imports/reactiveCache.js @@ -31,10 +31,18 @@ ReactiveCacheServer = { const ret = CustomFields.find(selector).fetch(); return ret; }, + getUser(id) { + const ret = Users.findOne(id); + return ret; + }, getCurrentSetting() { const ret = Settings.findOne(); return ret; }, + getCurrentUser() { + const ret = Meteor.user(); + return ret; + }, } // only the Client is reactive @@ -111,6 +119,16 @@ ReactiveCacheClient = { const ret = this.__customFields.get(Jsons.stringify(selector)); return ret; }, + getUser(id) { + if (!this.__user) { + this.__user = new DataCache(userId => { + const _ret = Users.findOne(userId); + return _ret; + }); + } + const ret = this.__user.get(id); + return ret; + }, getCurrentSetting() { if (!this.__currentSetting || !this.__currentSetting.get()) { this.__currentSetting = new DataCache(() => { @@ -120,6 +138,16 @@ ReactiveCacheClient = { } const ret = this.__currentSetting.get(); return ret; + }, + getCurrentUser() { + if (!this.__currentUser || !this.__currentUser.get()) { + this.__currentUser = new DataCache(() => { + const _ret = Meteor.user(); + return _ret; + }); + } + const ret = this.__currentUser.get(); + return ret; } } @@ -193,6 +221,15 @@ ReactiveCache = { } return ret; }, + getUser(id) { + let ret; + if (Meteor.isServer) { + ret = ReactiveCacheServer.getUser(id); + } else { + ret = ReactiveCacheClient.getUser(id); + } + return ret; + }, getCurrentSetting() { let ret; if (Meteor.isServer) { @@ -202,6 +239,15 @@ ReactiveCache = { } return ret; }, + getCurrentUser() { + let ret; + if (Meteor.isServer) { + ret = ReactiveCacheServer.getCurrentUser(); + } else { + ret = ReactiveCacheClient.getCurrentUser(); + } + return ret; + }, } export { ReactiveCache }; diff --git a/models/accountSettings.js b/models/accountSettings.js index 830b531ac..0ab9d11a8 100644 --- a/models/accountSettings.js +++ b/models/accountSettings.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + AccountSettings = new Mongo.Collection('accountSettings'); AccountSettings.attachSchema( @@ -44,7 +46,7 @@ AccountSettings.attachSchema( AccountSettings.allow({ update(userId) { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); return user && user.isAdmin; }, }); diff --git a/models/activities.js b/models/activities.js index 387975d07..b5f2f29b6 100644 --- a/models/activities.js +++ b/models/activities.js @@ -20,10 +20,10 @@ Activities.helpers({ return ReactiveCache.getBoard(this.oldBoardId); }, user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, member() { - return Users.findOne(this.memberId); + return ReactiveCache.getUser(this.memberId); }, list() { return ReactiveCache.getList(this.listId); @@ -203,7 +203,7 @@ if (Meteor.isServer) { if (board) { const comment = params.comment; const knownUsers = board.members.map(member => { - const u = Users.findOne(member.userId); + const u = ReactiveCache.getUser(member.userId); if (u) { member.username = u.username; member.emails = u.emails; diff --git a/models/announcements.js b/models/announcements.js index f21393673..5458cd70b 100644 --- a/models/announcements.js +++ b/models/announcements.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + Announcements = new Mongo.Collection('announcements'); Announcements.attachSchema( @@ -49,7 +51,7 @@ Announcements.attachSchema( Announcements.allow({ update(userId) { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); return user && user.isAdmin; }, }); diff --git a/models/avatars.js b/models/avatars.js index d74852101..39dde0deb 100644 --- a/models/avatars.js +++ b/models/avatars.js @@ -1,3 +1,4 @@ +import { ReactiveCache } from '/imports/reactiveCache'; import { Meteor } from 'meteor/meteor'; import { FilesCollection } from 'meteor/ostrio:files'; import { formatFleURL } from 'meteor/ostrio:files/lib'; @@ -100,7 +101,7 @@ Avatars = new FilesCollection({ const isValid = Promise.await(isFileValid(fileObj, avatarsUploadMimeTypes, avatarsUploadSize, avatarsUploadExternalProgram)); if (isValid) { - Users.findOne(fileObj.userId).setAvatarUrl(`${formatFleURL(fileObj)}?auth=false&brokenIsFine=true`); + ReactiveCache.getUser(fileObj.userId).setAvatarUrl(`${formatFleURL(fileObj)}?auth=false&brokenIsFine=true`); } else { Avatars.remove(fileObj._id); } @@ -112,7 +113,7 @@ Avatars = new FilesCollection({ onBeforeRemove(files) { files.forEach(fileObj => { if (fileObj.userId) { - Users.findOne(fileObj.userId).setAvatarUrl(''); + ReactiveCache.getUser(fileObj.userId).setAvatarUrl(''); } }); diff --git a/models/boards.js b/models/boards.js index 2ca639510..0d5ef8da0 100644 --- a/models/boards.js +++ b/models/boards.js @@ -1597,7 +1597,7 @@ Boards.userBoards = ( selector = {}, projection = {}, ) => { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); if (!user) { return []; } diff --git a/models/cardComments.js b/models/cardComments.js index 315f66a9b..b93f6dea5 100644 --- a/models/cardComments.js +++ b/models/cardComments.js @@ -94,7 +94,7 @@ CardComments.helpers({ }, user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, reactions() { diff --git a/models/cards.js b/models/cards.js index 7b35210d9..0ccc4f913 100644 --- a/models/cards.js +++ b/models/cards.js @@ -754,7 +754,7 @@ Cards.helpers({ }, user() { - return Users.findOne(this.userId); + return ReactiveCache.getUser(this.userId); }, isAssigned(memberId) { @@ -2744,7 +2744,7 @@ function cardMembers(userId, doc, fieldNames, modifier) { // Say hello to the new member if (modifier.$addToSet && modifier.$addToSet.members) { memberId = modifier.$addToSet.members; - const username = Users.findOne(memberId).username; + const username = ReactiveCache.getUser(memberId).username; if (!_.contains(doc.members, memberId)) { Activities.insert({ userId, @@ -2762,7 +2762,7 @@ function cardMembers(userId, doc, fieldNames, modifier) { // Say goodbye to the former member if (modifier.$pull && modifier.$pull.members) { memberId = modifier.$pull.members; - const username = Users.findOne(memberId).username; + const username = ReactiveCache.getUser(memberId).username; // Check that the former member is member of the card if (_.contains(doc.members, memberId)) { Activities.insert({ @@ -2785,7 +2785,7 @@ function cardAssignees(userId, doc, fieldNames, modifier) { // Say hello to the new assignee if (modifier.$addToSet && modifier.$addToSet.assignees) { assigneeId = modifier.$addToSet.assignees; - const username = Users.findOne(assigneeId).username; + const username = ReactiveCache.getUser(assigneeId).username; if (!_.contains(doc.assignees, assigneeId)) { Activities.insert({ userId, @@ -2802,7 +2802,7 @@ function cardAssignees(userId, doc, fieldNames, modifier) { // Say goodbye to the former assignee if (modifier.$pull && modifier.$pull.assignees) { assigneeId = modifier.$pull.assignees; - const username = Users.findOne(assigneeId).username; + const username = ReactiveCache.getUser(assigneeId).username; // Check that the former assignee is assignee of the card if (_.contains(doc.assignees, assigneeId)) { Activities.insert({ @@ -2967,7 +2967,7 @@ const findDueCards = days => { archived: false, dueAt: { $gte: $from, $lt: $to }, }).forEach(card => { - const username = Users.findOne(card.userId).username; + const username = ReactiveCache.getUser(card.userId).username; const activity = { userId: card.userId, username, @@ -3162,7 +3162,7 @@ if (Meteor.isServer) { }, ); } - const username = Users.findOne(userId).username; + const username = ReactiveCache.getUser(userId).username; const activity = { userId, username, @@ -3343,9 +3343,7 @@ if (Meteor.isServer) { }, { sort: ['sort'] }, ); - const check = Users.findOne({ - _id: req.body.authorId, - }); + const check = ReactiveCache.getUser(req.body.authorId); const members = req.body.members; const assignees = req.body.assignees; if (typeof check !== 'undefined') { diff --git a/models/integrations.js b/models/integrations.js index 3da910b6b..2cd6d134c 100644 --- a/models/integrations.js +++ b/models/integrations.js @@ -102,7 +102,7 @@ Integrations.Const = { }; const permissionHelper = { allow(userId, doc) { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); const isAdmin = user && Meteor.user().isAdmin; return isAdmin || allowIsBoardAdmin(userId, ReactiveCache.getBoard(doc.boardId)); }, diff --git a/models/invitationCodes.js b/models/invitationCodes.js index 501838b86..5563e4aea 100644 --- a/models/invitationCodes.js +++ b/models/invitationCodes.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + InvitationCodes = new Mongo.Collection('invitation_codes'); InvitationCodes.attachSchema( @@ -54,7 +56,7 @@ InvitationCodes.attachSchema( InvitationCodes.helpers({ author() { - return Users.findOne(this.authorId); + return ReactiveCache.getUser(this.authorId); }, }); diff --git a/models/org.js b/models/org.js index c8147e5ae..267c873b0 100644 --- a/models/org.js +++ b/models/org.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + Org = new Mongo.Collection('org'); /** @@ -78,10 +80,8 @@ Org.attachSchema( if (Meteor.isServer) { Org.allow({ insert(userId, doc) { - const user = Users.findOne({ - _id: userId, - }); - if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin)) + const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser(); + if (user?.isAdmin) return true; if (!user) { return false; @@ -89,10 +89,8 @@ if (Meteor.isServer) { return doc._id === userId; }, update(userId, doc) { - const user = Users.findOne({ - _id: userId, - }); - if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin)) + const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser(); + if (user?.isAdmin) return true; if (!user) { return false; @@ -100,10 +98,8 @@ if (Meteor.isServer) { return doc._id === userId; }, remove(userId, doc) { - const user = Users.findOne({ - _id: userId, - }); - if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin)) + const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser(); + if (user?.isAdmin) return true; if (!user) { return false; diff --git a/models/settings.js b/models/settings.js index 703694678..84388e6ad 100644 --- a/models/settings.js +++ b/models/settings.js @@ -160,7 +160,7 @@ Settings.helpers({ }); Settings.allow({ update(userId) { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); return user && user.isAdmin; }, }); @@ -250,16 +250,13 @@ if (Meteor.isServer) { function sendInvitationEmail(_id) { const icode = InvitationCodes.findOne(_id); - const author = Users.findOne(Meteor.userId()); + const author = ReactiveCache.getCurrentUser(); try { - const fullName = Users.findOne(icode.authorId) - && Users.findOne(icode.authorId).profile - && Users.findOne(icode.authorId).profile !== undefined - && Users.findOne(icode.authorId).profile.fullname ? Users.findOne(icode.authorId).profile.fullname : ""; + const fullName = ReactiveCache.getUser(icode.authorId)?.profile?.fullname || ""; const params = { email: icode.email, - inviter: fullName != "" ? fullName + " (" + Users.findOne(icode.authorId).username + " )" : Users.findOne(icode.authorId).username, + inviter: fullName != "" ? fullName + " (" + ReactiveCache.getUser(icode.authorId).username + " )" : ReactiveCache.getUser(icode.authorId).username, user: icode.email.split('@')[0], icode: icode.code, url: FlowRouter.url('sign-up'), @@ -344,7 +341,7 @@ if (Meteor.isServer) { check(emails, [String]); check(boards, [String]); - const user = Users.findOne(Meteor.userId()); + const user = ReactiveCache.getCurrentUser(); if (!user.isAdmin && !isNonAdminAllowedToSendMail(user)) { rc = -1; throw new Meteor.Error('not-allowed'); diff --git a/models/swimlanes.js b/models/swimlanes.js index 36308f90f..a28baa675 100644 --- a/models/swimlanes.js +++ b/models/swimlanes.js @@ -253,17 +253,17 @@ Swimlanes.helpers({ }, isListTemplatesSwimlane() { - const user = Users.findOne(Meteor.userId()); + const user = ReactiveCache.getCurrentUser(); return (user.profile || {}).listTemplatesSwimlaneId === this._id; }, isCardTemplatesSwimlane() { - const user = Users.findOne(Meteor.userId()); + const user = ReactiveCache.getCurrentUser(); return (user.profile || {}).cardTemplatesSwimlaneId === this._id; }, isBoardTemplatesSwimlane() { - const user = Users.findOne(Meteor.userId()); + const user = ReactiveCache.getCurrentUser(); return (user.profile || {}).boardTemplatesSwimlaneId === this._id; }, diff --git a/models/tableVisibilityModeSettings.js b/models/tableVisibilityModeSettings.js index 483d32371..3bee121e2 100644 --- a/models/tableVisibilityModeSettings.js +++ b/models/tableVisibilityModeSettings.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + TableVisibilityModeSettings = new Mongo.Collection('tableVisibilityModeSettings'); TableVisibilityModeSettings.attachSchema( @@ -44,7 +46,7 @@ TableVisibilityModeSettings.attachSchema( TableVisibilityModeSettings.allow({ update(userId) { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); return user && user.isAdmin; }, }); diff --git a/models/team.js b/models/team.js index b5f66c30e..f40d46f0f 100644 --- a/models/team.js +++ b/models/team.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + Team = new Mongo.Collection('team'); /** @@ -77,10 +79,8 @@ Team.attachSchema( if (Meteor.isServer) { Team.allow({ insert(userId, doc) { - const user = Users.findOne({ - _id: userId, - }); - if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin)) + const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser(); + if (user?.isAdmin) return true; if (!user) { return false; @@ -88,10 +88,8 @@ if (Meteor.isServer) { return doc._id === userId; }, update(userId, doc) { - const user = Users.findOne({ - _id: userId, - }); - if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin)) + const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser(); + if (user?.isAdmin) return true; if (!user) { return false; @@ -99,10 +97,8 @@ if (Meteor.isServer) { return doc._id === userId; }, remove(userId, doc) { - const user = Users.findOne({ - _id: userId, - }); - if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin)) + const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser(); + if (user?.isAdmin) return true; if (!user) { return false; diff --git a/models/users.js b/models/users.js index e9005cede..2d0fe4f8c 100644 --- a/models/users.js +++ b/models/users.js @@ -503,9 +503,7 @@ Users.attachSchema( Users.allow({ update(userId, doc) { - const user = Users.findOne({ - _id: userId, - }); + const user = ReactiveCache.getUser(userId); if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin)) return true; if (!user) { @@ -1445,7 +1443,7 @@ if (Meteor.isServer) { }); } Accounts.sendEnrollmentEmail(newUserId); - user = Users.findOne(newUserId); + user = ReactiveCache.getUser(newUserId); } board.addMember(user._id); @@ -1949,9 +1947,7 @@ if (Meteor.isServer) { Users.after.insert((userId, doc) => { // HACK - doc = Users.findOne({ - _id: doc._id, - }); + doc = ReactiveCache.getUser(doc._id); if (doc.createdThroughApi) { // The admin user should be able to create a user despite disabling registration because // it is two different things (registration and creation). diff --git a/server/notifications/email.js b/server/notifications/email.js index 301bd471e..c4ad1340b 100644 --- a/server/notifications/email.js +++ b/server/notifications/email.js @@ -1,3 +1,4 @@ +import { ReactiveCache } from '/imports/reactiveCache'; import { TAPi18n } from '/imports/i18n'; //var nodemailer = require('nodemailer'); @@ -30,7 +31,7 @@ Meteor.startup(() => { // so we pass userId with closure const userId = user._id; Meteor.setTimeout(() => { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); // for each user, in the timed period, only the first call will get the cached content, // other calls will get nothing diff --git a/server/notifications/notifications.js b/server/notifications/notifications.js index 3f103339f..783ad9f3f 100644 --- a/server/notifications/notifications.js +++ b/server/notifications/notifications.js @@ -22,7 +22,7 @@ Notifications = { getUsers: watchers => { const users = []; watchers.forEach(userId => { - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); if (user) users.push(user); }); return users; diff --git a/server/notifications/outgoing.js b/server/notifications/outgoing.js index 6dc409965..9df865ab8 100644 --- a/server/notifications/outgoing.js +++ b/server/notifications/outgoing.js @@ -134,7 +134,7 @@ if (Meteor.isServer) { }); const userId = params.userId ? params.userId : integrations[0].userId; - const user = Users.findOne(userId); + const user = ReactiveCache.getUser(userId); const text = `${params.user} ${TAPi18n.__( description, quoteParams, diff --git a/server/publications/boards.js b/server/publications/boards.js index bd8790fbc..635283c77 100644 --- a/server/publications/boards.js +++ b/server/publications/boards.js @@ -18,10 +18,10 @@ Meteor.publish('boards', function() { // Defensive programming to verify that starredBoards has the expected // format -- since the field is in the `profile` a user can modify it. - // const { starredBoards = [] } = (Users.findOne(userId) || {}).profile || {}; + // const { starredBoards = [] } = (ReactiveCache.getUser(userId) || {}).profile || {}; // check(starredBoards, [String]); - // let currUser = Users.findOne(userId); + // let currUser = ReactiveCache.getUser(userId); // let orgIdsUserBelongs = currUser!== 'undefined' && currUser.teams !== 'undefined' ? currUser.orgIdsUserBelongs() : ''; // let teamIdsUserBelongs = currUser!== 'undefined' && currUser.teams !== 'undefined' ? currUser.teamIdsUserBelongs() : ''; // let orgsIds = []; @@ -165,7 +165,7 @@ Meteor.publishRelations('board', function(boardId, isArchived) { check(isArchived, Boolean); const thisUserId = this.userId; const $or = [{ permission: 'public' }]; - let currUser = (!Match.test(thisUserId, String) || !thisUserId) ? 'undefined' : Users.findOne(thisUserId); + let currUser = (!Match.test(thisUserId, String) || !thisUserId) ? 'undefined' : ReactiveCache.getUser(thisUserId); let orgIdsUserBelongs = currUser!== 'undefined' && currUser.teams !== 'undefined' ? currUser.orgIdsUserBelongs() : ''; let teamIdsUserBelongs = currUser!== 'undefined' && currUser.teams !== 'undefined' ? currUser.teamIdsUserBelongs() : ''; let orgsIds = []; diff --git a/server/publications/org.js b/server/publications/org.js index 8c4252edd..d48d0511b 100644 --- a/server/publications/org.js +++ b/server/publications/org.js @@ -1,12 +1,14 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + Meteor.publish('org', function(query, limit) { check(query, Match.OneOf(Object, null)); check(limit, Number); - if (!Match.test(this.userId, String)) { + const user = ReactiveCache.getCurrentUser(); + if (!user) { return []; } - const user = Users.findOne(this.userId); if (user && user.isAdmin) { return Org.find(query, { limit, diff --git a/server/publications/people.js b/server/publications/people.js index af8dfcea5..fd7104327 100644 --- a/server/publications/people.js +++ b/server/publications/people.js @@ -1,12 +1,14 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + Meteor.publish('people', function(query, limit) { check(query, Match.OneOf(Object, null)); check(limit, Number); - if (!Match.test(this.userId, String)) { + const user = ReactiveCache.getCurrentUser(); + if (!user) { return []; } - const user = Users.findOne(this.userId); if (user && user.isAdmin) { return Users.find(query, { limit, diff --git a/server/publications/settings.js b/server/publications/settings.js index 050702a9a..06f8dc326 100644 --- a/server/publications/settings.js +++ b/server/publications/settings.js @@ -1,3 +1,5 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + Meteor.publish('globalwebhooks', () => { const boardId = Integrations.Const.GLOBAL_WEBHOOK_ID; return Integrations.find({ @@ -37,8 +39,10 @@ Meteor.publish('setting', () => { }); Meteor.publish('mailServer', function() { - if (!Match.test(this.userId, String)) return []; - const user = Users.findOne(this.userId); + const user = ReactiveCache.getCurrentUser(); + if (!user) { + return []; + } if (user && user.isAdmin) { return Settings.find( {}, diff --git a/server/publications/team.js b/server/publications/team.js index 6877a8266..7d4bf1297 100644 --- a/server/publications/team.js +++ b/server/publications/team.js @@ -1,12 +1,13 @@ +import { ReactiveCache } from '/imports/reactiveCache'; + Meteor.publish('team', function(query, limit) { check(query, Match.OneOf(Object, null)); check(limit, Number); - if (!Match.test(this.userId, String)) { + const user = ReactiveCache.getCurrentUser(); + if (!user) { return []; } - - const user = Users.findOne(this.userId); if (user && user.isAdmin) { return Team.find(query, { limit,