Move every Boards.find(idOrFirstObjectSelector, options) to the ReactiveCache (directory models/)

This commit is contained in:
Martin Filser 2023-02-25 20:19:18 +01:00
parent 59ee616304
commit f83ee124d0
5 changed files with 30 additions and 28 deletions

View file

@ -1556,10 +1556,10 @@ Boards.uniqueTitle = title => {
); );
const base = escapeForRegex(m.groups.title); const base = escapeForRegex(m.groups.title);
const baseTitle = m.groups.title; const baseTitle = m.groups.title;
boards = Boards.find({ title: new RegExp(`^${base}\\s*(\\[(?<num>\\d+)]\\s*$|\\s*$)`) }); boards = ReactiveCache.getBoards({ title: new RegExp(`^${base}\\s*(\\[(?<num>\\d+)]\\s*$|\\s*$)`) });
if (boards.count() > 0) { if (boards.length > 0) {
let num = 0; let num = 0;
Boards.find({ title: new RegExp(`^${base}\\s*\\[\\d+]\\s*$`) }).forEach( ReactiveCache.getBoards({ title: new RegExp(`^${base}\\s*\\[\\d+]\\s*$`) }).forEach(
board => { board => {
const m = board.title.match( const m = board.title.match(
new RegExp('^(?<title>.*?)\\s*\\[(?<num>\\d+)]\\s*$'), new RegExp('^(?<title>.*?)\\s*\\[(?<num>\\d+)]\\s*$'),
@ -1589,7 +1589,8 @@ Boards.userSearch = (
if (userId) { if (userId) {
selector.$or.push({ members: { $elemMatch: { userId, isActive: true } } }); selector.$or.push({ members: { $elemMatch: { userId, isActive: true } } });
} }
return Boards.find(selector, projection); const ret = ReactiveCache.getBoards(selector, projection);
return ret;
}; };
Boards.userBoards = ( Boards.userBoards = (
@ -1617,7 +1618,7 @@ Boards.userBoards = (
{ teams: { $elemMatch: { teamId: { $in: user.teamIds() }, isActive: true } } }, { teams: { $elemMatch: { teamId: { $in: user.teamIds() }, isActive: true } } },
]; ];
return Boards.find(selector, projection); return ReactiveCache.getBoards(selector, projection);
}; };
Boards.userBoardIds = (userId, archived = false, selector = {}) => { Boards.userBoardIds = (userId, archived = false, selector = {}) => {
@ -1972,7 +1973,7 @@ if (Meteor.isServer) {
req.userId === paramUserId, req.userId === paramUserId,
); );
const data = Boards.find( const data = ReactiveCache.getBoards(
{ {
archived: false, archived: false,
'members.userId': paramUserId, 'members.userId': paramUserId,
@ -2008,7 +2009,7 @@ if (Meteor.isServer) {
Authentication.checkUserId(req.userId); Authentication.checkUserId(req.userId);
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 200, code: 200,
data: Boards.find( data: ReactiveCache.getBoards(
{ permission: 'public' }, { permission: 'public' },
{ {
sort: { sort: 1 /* boards default sorting */ }, sort: { sort: 1 /* boards default sorting */ },
@ -2040,8 +2041,8 @@ if (Meteor.isServer) {
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 200, code: 200,
data: { data: {
private: Boards.find({ permission: 'private' }).count(), private: ReactiveCache.getBoards({ permission: 'private' }).length,
public: Boards.find({ permission: 'public' }).count(), public: ReactiveCache.getBoards({ permission: 'public' }).length,
}, },
}); });
} catch (error) { } catch (error) {

View file

@ -170,25 +170,25 @@ CustomFields.allow({
insert(userId, doc) { insert(userId, doc) {
return allowIsAnyBoardMember( return allowIsAnyBoardMember(
userId, userId,
Boards.find({ ReactiveCache.getBoards({
_id: { $in: doc.boardIds }, _id: { $in: doc.boardIds },
}).fetch(), }),
); );
}, },
update(userId, doc) { update(userId, doc) {
return allowIsAnyBoardMember( return allowIsAnyBoardMember(
userId, userId,
Boards.find({ ReactiveCache.getBoards({
_id: { $in: doc.boardIds }, _id: { $in: doc.boardIds },
}).fetch(), }),
); );
}, },
remove(userId, doc) { remove(userId, doc) {
return allowIsAnyBoardMember( return allowIsAnyBoardMember(
userId, userId,
Boards.find({ ReactiveCache.getBoards({
_id: { $in: doc.boardIds }, _id: { $in: doc.boardIds },
}).fetch(), }),
); );
}, },
fetch: ['userId', 'boardIds'], fetch: ['userId', 'boardIds'],

View file

@ -33,7 +33,8 @@ const getBoardTitleWithMostActivities = (dateWithXdaysAgo, nbLimit) => {
}; };
const getBoards = (boardIds) => { const getBoards = (boardIds) => {
return Boards.find({ _id: { $in: boardIds } }).fetch(); const ret = ReactiveCache.getBoards({ _id: { $in: boardIds } });
return ret;
}; };
Meteor.startup(() => { Meteor.startup(() => {
WebApp.connectHandlers.use('/metrics', (req, res, next) => { WebApp.connectHandlers.use('/metrics', (req, res, next) => {
@ -77,7 +78,7 @@ Meteor.startup(() => {
metricsRes += '# Number of registered boards\n'; metricsRes += '# Number of registered boards\n';
// Get number of registered boards // Get number of registered boards
resCount = Boards.find({ archived: false, type: 'board' }).count(); // KPI 3 resCount = ReactiveCache.getBoards({ archived: false, type: 'board' }).length; // KPI 3
metricsRes += 'wekan_registeredboards ' + resCount + '\n'; metricsRes += 'wekan_registeredboards ' + resCount + '\n';
resCount = 0; resCount = 0;
@ -86,7 +87,7 @@ Meteor.startup(() => {
// Get number of registered boards by registered users // Get number of registered boards by registered users
resCount = resCount =
Boards.find({ archived: false, type: 'board' }).count() / ReactiveCache.getBoards({ archived: false, type: 'board' }).length /
ReactiveCache.getUsers({}).length; // KPI 4 ReactiveCache.getUsers({}).length; // KPI 4
metricsRes += metricsRes +=
'wekan_registeredboardsBysRegisteredUsers ' + resCount + '\n'; 'wekan_registeredboardsBysRegisteredUsers ' + resCount + '\n';
@ -96,11 +97,11 @@ Meteor.startup(() => {
metricsRes += '# Number of registered boards\n'; metricsRes += '# Number of registered boards\n';
// Get board numbers with only one member // Get board numbers with only one member
resCount = Boards.find({ resCount = ReactiveCache.getBoards({
archived: false, archived: false,
type: 'board', type: 'board',
members: { $size: 1 }, members: { $size: 1 },
}).count(); // KPI 5 }).length; // KPI 5
metricsRes += metricsRes +=
'wekan_registeredboardsWithOnlyOneMember ' + resCount + '\n'; 'wekan_registeredboardsWithOnlyOneMember ' + resCount + '\n';
resCount = 0; resCount = 0;

View file

@ -2029,7 +2029,7 @@ if (Meteor.isServer) {
delete data.services; delete data.services;
// get all boards where the user is member of // get all boards where the user is member of
let boards = Boards.find( let boards = ReactiveCache.getBoards(
{ {
type: 'board', type: 'board',
'members.userId': req.userId, 'members.userId': req.userId,
@ -2115,7 +2115,7 @@ if (Meteor.isServer) {
} }
// get all boards where the user is member of // get all boards where the user is member of
let boards = Boards.find( let boards = ReactiveCache.getBoards(
{ {
type: 'board', type: 'board',
'members.userId': id, 'members.userId': id,
@ -2174,7 +2174,7 @@ if (Meteor.isServer) {
}); });
if (data !== undefined) { if (data !== undefined) {
if (action === 'takeOwnership') { if (action === 'takeOwnership') {
data = Boards.find( data = ReactiveCache.getBoards(
{ {
'members.userId': id, 'members.userId': id,
'members.isAdmin': true, 'members.isAdmin': true,
@ -2268,7 +2268,7 @@ if (Meteor.isServer) {
let data = ReactiveCache.getUser(userId); let data = ReactiveCache.getUser(userId);
if (data !== undefined) { if (data !== undefined) {
if (action === 'add') { if (action === 'add') {
data = Boards.find({ data = ReactiveCache.getBoards({
_id: boardId, _id: boardId,
}).map(function (board) { }).map(function (board) {
if (!board.hasMember(userId)) { if (!board.hasMember(userId)) {
@ -2329,7 +2329,7 @@ if (Meteor.isServer) {
let data = ReactiveCache.getUser(userId); let data = ReactiveCache.getUser(userId);
if (data !== undefined) { if (data !== undefined) {
if (action === 'remove') { if (action === 'remove') {
data = Boards.find({ data = ReactiveCache.getBoards({
_id: boardId, _id: boardId,
}).map(function (board) { }).map(function (board) {
if (board.hasMember(userId)) { if (board.hasMember(userId)) {

View file

@ -245,7 +245,7 @@ function buildSelector(queryParams) {
const boards = Boards.userSearch(userId, { const boards = Boards.userSearch(userId, {
title: new RegExp(escapeForRegex(query), 'i'), title: new RegExp(escapeForRegex(query), 'i'),
}); });
if (boards.count()) { if (boards.length) {
boards.forEach(board => { boards.forEach(board => {
queryBoards.push(board._id); queryBoards.push(board._id);
}); });
@ -372,7 +372,7 @@ function buildSelector(queryParams) {
labels: { $elemMatch: { color: label.toLowerCase() } }, labels: { $elemMatch: { color: label.toLowerCase() } },
}); });
if (boards.count()) { if (boards.length) {
boards.forEach(board => { boards.forEach(board => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
// console.log('board:', board); // console.log('board:', board);
@ -396,7 +396,7 @@ function buildSelector(queryParams) {
labels: { $elemMatch: { name: reLabel } }, labels: { $elemMatch: { name: reLabel } },
}); });
if (boards.count()) { if (boards.length) {
boards.forEach(board => { boards.forEach(board => {
board.labels board.labels
.filter(boardLabel => { .filter(boardLabel => {