Browse Source

Move every Users.findOne(idOrFirstObjectSelector, options) to the ReactiveCache

Martin Filser 2 years ago
parent
commit
0767f50af8

+ 1 - 1
client/components/cards/cardDetails.js

@@ -1646,7 +1646,7 @@ Template.cardAssigneesPopup.helpers({
 
 Template.cardAssigneePopup.helpers({
   userData() {
-    return Users.findOne(this.userId, {
+    return ReactiveCache.getUser(this.userId, {
       fields: {
         profile: 1,
         username: 1,

+ 3 - 1
client/components/import/csvMembersMapper.js

@@ -1,3 +1,5 @@
+import { ReactiveCache } from '/imports/reactiveCache';
+
 export function csvGetMembersToMap(data) {
   // we will work on the list itself (an ordered array of objects) when a
   // mapping is done, we add a 'wekan' field to the object representing the
@@ -28,7 +30,7 @@ export function csvGetMembersToMap(data) {
       username: importedMember,
       id: importedMember,
     };
-    const wekanUser = Users.findOne({ username: importedMember.username });
+    const wekanUser = ReactiveCache.getUser({ username: importedMember.username });
     if (wekanUser) importedMember.wekanId = wekanUser._id;
     membersToMap.push(importedMember);
   }

+ 3 - 2
client/components/import/import.js

@@ -1,3 +1,4 @@
+import { ReactiveCache } from '/imports/reactiveCache';
 import { trelloGetMembersToMap } from './trelloMembersMapper';
 import { wekanGetMembersToMap } from './wekanMembersMapper';
 import { csvGetMembersToMap } from './csvMembersMapper';
@@ -174,9 +175,9 @@ BlazeComponent.extendComponent({
             this._refreshMembers(
               this.members().map(member => {
                 if (!member.wekanId) {
-                  let user = Users.findOne({ username: member.username });
+                  let user = ReactiveCache.getUser({ username: member.username });
                   if (!user) {
-                    user = Users.findOne({ importUsernames: member.username });
+                    user = ReactiveCache.getUser({ importUsernames: member.username });
                   }
                   if (user) {
                     // eslint-disable-next-line no-console

+ 3 - 1
client/components/import/trelloMembersMapper.js

@@ -1,3 +1,5 @@
+import { ReactiveCache } from '/imports/reactiveCache';
+
 export function trelloGetMembersToMap(data) {
   // we will work on the list itself (an ordered array of objects) when a
   // mapping is done, we add a 'wekan' field to the object representing the
@@ -5,7 +7,7 @@ export function trelloGetMembersToMap(data) {
   const membersToMap = data.members;
   // auto-map based on username
   membersToMap.forEach(importedMember => {
-    const wekanUser = Users.findOne({ username: importedMember.username });
+    const wekanUser = ReactiveCache.getUser({ username: importedMember.username });
     if (wekanUser) {
       importedMember.wekanId = wekanUser._id;
     }

+ 3 - 1
client/components/import/wekanMembersMapper.js

@@ -1,3 +1,5 @@
+import { ReactiveCache } from '/imports/reactiveCache';
+
 export function wekanGetMembersToMap(data) {
   // we will work on the list itself (an ordered array of objects) when a
   // mapping is done, we add a 'wekan' field to the object representing the
@@ -15,7 +17,7 @@ export function wekanGetMembersToMap(data) {
       importedMember.fullName = user.profile.fullname;
     }
     importedMember.username = user.username;
-    const wekanUser = Users.findOne({ username: importedMember.username });
+    const wekanUser = ReactiveCache.getUser({ username: importedMember.username });
     if (wekanUser) {
       importedMember.wekanId = wekanUser._id;
     }

+ 3 - 1
client/components/rules/rulesMain.js

@@ -1,3 +1,5 @@
+import { ReactiveCache } from '/imports/reactiveCache';
+
 BlazeComponent.extendComponent({
   onCreated() {
     this.rulesCurrentTab = new ReactiveVar('rulesList');
@@ -55,7 +57,7 @@ BlazeComponent.extendComponent({
           let trigger = this.triggerVar.get();
           trigger.userId = '*';
           if (username !== undefined) {
-            const userFound = Users.findOne({ username });
+            const userFound = ReactiveCache.getUser({ username });
             if (userFound !== undefined) {
               trigger.userId = userFound._id;
               this.triggerVar.set(trigger);

+ 4 - 3
models/export.js

@@ -1,3 +1,4 @@
+import { ReactiveCache } from '/imports/reactiveCache';
 import { Exporter } from './exporter';
 import { Meteor } from 'meteor/meteor';
 
@@ -43,7 +44,7 @@ if (Meteor.isServer) {
       });
     } else if (!Meteor.settings.public.sandstorm) {
       Authentication.checkUserId(req.userId);
-      user = Users.findOne({ _id: req.userId, isAdmin: true });
+      user = ReactiveCache.getUser({ _id: req.userId, isAdmin: true });
     }
     const exporter = new Exporter(boardId);
     if (exporter.canExport(user) || impersonateDone) {
@@ -107,7 +108,7 @@ if (Meteor.isServer) {
         });
       } else if (!Meteor.settings.public.sandstorm) {
         Authentication.checkUserId(req.userId);
-        user = Users.findOne({ _id: req.userId, isAdmin: true });
+        user = ReactiveCache.getUser({ _id: req.userId, isAdmin: true });
       }
       const exporter = new Exporter(boardId, attachmentId);
       if (exporter.canExport(user) || impersonateDone) {
@@ -163,7 +164,7 @@ if (Meteor.isServer) {
       });
     } else if (!Meteor.settings.public.sandstorm) {
       Authentication.checkUserId(req.userId);
-      user = Users.findOne({
+      user = ReactiveCache.getUser({
         _id: req.userId,
         isAdmin: true,
       });

+ 2 - 1
models/exportExcel.js

@@ -1,3 +1,4 @@
+import { ReactiveCache } from '/imports/reactiveCache';
 import { TAPi18n } from '/imports/i18n';
 import { runOnServer } from './runOnServer';
 
@@ -46,7 +47,7 @@ runOnServer(function() {
       });
     } else if (!Meteor.settings.public.sandstorm) {
       Authentication.checkUserId(req.userId);
-      user = Users.findOne({
+      user = ReactiveCache.getUser({
         _id: req.userId,
         isAdmin: true,
       });

+ 2 - 1
models/exportPDF.js

@@ -1,3 +1,4 @@
+import { ReactiveCache } from '/imports/reactiveCache';
 import { TAPi18n } from '/imports/i18n';
 import { runOnServer } from './runOnServer';
 
@@ -48,7 +49,7 @@ runOnServer(function() {
       });
     } else if (!Meteor.settings.public.sandstorm) {
       Authentication.checkUserId(req.userId);
-      user = Users.findOne({
+      user = ReactiveCache.getUser({
         _id: req.userId,
         isAdmin: true,
       });

+ 1 - 1
models/settings.js

@@ -349,7 +349,7 @@ if (Meteor.isServer) {
       emails.forEach(email => {
         if (email && SimpleSchema.RegEx.Email.test(email)) {
           // Checks if the email is already link to an account.
-          const userExist = Users.findOne({ email });
+          const userExist = ReactiveCache.getUser({ email });
           if (userExist) {
             rc = -1;
             throw new Meteor.Error(

+ 7 - 11
models/users.js

@@ -515,7 +515,7 @@ Users.allow({
     const adminsNumber = Users.find({
       isAdmin: true,
     }).count();
-    const { isAdmin } = Users.findOne(
+    const isAdmin = ReactiveCache.getUser(
       {
         _id: userId,
       },
@@ -1262,10 +1262,8 @@ if (Meteor.isServer) {
             from: 'admin',
           });
           const user =
-            Users.findOne(username) ||
-            Users.findOne({
-              username,
-            });
+            ReactiveCache.getUser(username) ||
+            ReactiveCache.getUser({ username });
           if (user) {
             Users.update(user._id, {
               $set: {
@@ -1305,7 +1303,7 @@ if (Meteor.isServer) {
         if (Array.isArray(email)) {
           email = email.shift();
         }
-        const existingUser = Users.findOne(
+        const existingUser = ReactiveCache.getUser(
           {
             'emails.address': email,
           },
@@ -1402,7 +1400,7 @@ if (Meteor.isServer) {
       const posAt = username.indexOf('@');
       let user = null;
       if (posAt >= 0) {
-        user = Users.findOne({
+        user = ReactiveCache.getUser({
           emails: {
             $elemMatch: {
               address: username,
@@ -1411,10 +1409,8 @@ if (Meteor.isServer) {
         });
       } else {
         user =
-          Users.findOne(username) ||
-          Users.findOne({
-            username,
-          });
+          ReactiveCache.getUser(username) ||
+          ReactiveCache.getUser({ username });
       }
       if (user) {
         if (user._id === inviter._id)

+ 1 - 1
models/wekanCreator.js

@@ -213,7 +213,7 @@ export class WekanCreator {
         importedMember.fullName = user.profile.fullname;
       }
       importedMember.username = user.username;
-      const wekanUser = Users.findOne({ username: importedMember.username });
+      const wekanUser = ReactiveCache.getUser({ username: importedMember.username });
       if (wekanUser) {
         importedMember.wekanId = wekanUser._id;
       }

+ 3 - 1
models/wekanmapper.js

@@ -1,3 +1,5 @@
+import { ReactiveCache } from '/imports/reactiveCache';
+
 export function getMembersToMap(data) {
   // we will work on the list itself (an ordered array of objects) when a
   // mapping is done, we add a 'wekan' field to the object representing the
@@ -15,7 +17,7 @@ export function getMembersToMap(data) {
       importedMember.fullName = user.profile.fullname;
     }
     importedMember.username = user.username;
-    const wekanUser = Users.findOne({ username: importedMember.username });
+    const wekanUser = ReactiveCache.getUser({ username: importedMember.username });
     if (wekanUser) {
       importedMember.wekanId = wekanUser._id;
     }

+ 1 - 1
sandstorm.js

@@ -312,7 +312,7 @@ if (isSandstorm && Meteor.isServer) {
     const username = doc.services.sandstorm.preferredHandle;
     let appendNumber = 0;
     while (
-      Users.findOne({
+      ReactiveCache.getUser({
         _id: { $ne: doc._id },
         username: generateUniqueUsername(username, appendNumber),
       })

+ 2 - 2
server/authentication.js

@@ -21,7 +21,7 @@ Meteor.startup(() => {
       error.statusCode = 401;
       throw error;
     }
-    const admin = Users.findOne({ _id: userId, isAdmin: true });
+    const admin = ReactiveCache.getUser({ _id: userId, isAdmin: true });
 
     if (admin === undefined) {
       const error = new Meteor.Error('Forbidden', 'Forbidden');
@@ -44,7 +44,7 @@ Meteor.startup(() => {
   // This throws an error if otherReq is false and the user is not an admin
   Authentication.checkAdminOrCondition = function(userId, otherReq) {
     if (otherReq) return;
-    const admin = Users.findOne({ _id: userId, isAdmin: true });
+    const admin = ReactiveCache.getUser({ _id: userId, isAdmin: true });
     if (admin === undefined) {
       const error = new Meteor.Error('Forbidden', 'Forbidden');
       error.statusCode = 403;

+ 2 - 2
server/publications/cards.js

@@ -334,7 +334,7 @@ function buildSelector(queryParams) {
     if (queryParams.hasOperator(OPERATOR_USER)) {
       const users = [];
       queryParams.getPredicates(OPERATOR_USER).forEach(username => {
-        const user = Users.findOne({ username });
+        const user = ReactiveCache.getUser({ username });
         if (user) {
           users.push(user._id);
         } else {
@@ -352,7 +352,7 @@ function buildSelector(queryParams) {
       if (queryParams.hasOperator(key)) {
         const users = [];
         queryParams.getPredicates(key).forEach(username => {
-          const user = Users.findOne({ username });
+          const user = ReactiveCache.getUser({ username });
           if (user) {
             users.push(user._id);
           } else {

+ 2 - 2
server/rulesHelper.js

@@ -260,7 +260,7 @@ RulesHelper = {
       card.removeLabel(action.labelId);
     }
     if (action.actionType === 'addMember') {
-      const memberId = Users.findOne({ username: action.username })._id;
+      const memberId = ReactiveCache.getUser({ username: action.username })._id;
       card.assignMember(memberId);
     }
     if (action.actionType === 'removeMember') {
@@ -270,7 +270,7 @@ RulesHelper = {
           card.unassignMember(members[i]);
         }
       } else {
-        const memberId = Users.findOne({ username: action.username })._id;
+        const memberId = ReactiveCache.getUser({ username: action.username })._id;
         card.unassignMember(memberId);
       }
     }