Move every Users.findOne() to the ReactiveCache

This commit is contained in:
Martin Filser 2023-01-15 01:11:16 +01:00
parent bf48d4371c
commit 6e1ef3d94a
35 changed files with 175 additions and 125 deletions

View file

@ -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;
},
});

View file

@ -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;

View file

@ -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;
},
});

View file

@ -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('');
}
});

View file

@ -1597,7 +1597,7 @@ Boards.userBoards = (
selector = {},
projection = {},
) => {
const user = Users.findOne(userId);
const user = ReactiveCache.getUser(userId);
if (!user) {
return [];
}

View file

@ -94,7 +94,7 @@ CardComments.helpers({
},
user() {
return Users.findOne(this.userId);
return ReactiveCache.getUser(this.userId);
},
reactions() {

View file

@ -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') {

View file

@ -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));
},

View file

@ -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);
},
});

View file

@ -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;

View file

@ -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');

View file

@ -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;
},

View file

@ -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;
},
});

View file

@ -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;

View file

@ -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).