mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
On a seeded board of 4,000 cards, 12,000 comments, 20,000 checklist items and 32,000 activities, served from FerretDB with SQLite as the Snap is: closing a card took about 4 s and now takes 0.1 s, the board finished loading in 17.6 s and now in 6.2 s, a card open re-ran about 7,700 computations and now about 1,000, and FerretDB used 13 s of CPU and 724 MB while the board opened, now 3 s and 280 MB. - The client data cache queued one more stop callback each time a helper re-ran, and every check asked Tracker for dependents, which on close was 710 walks of 3 ms; there is now one callback per computation and key and one timer per key (imports/lib/dataCache.js). - Opening a card writes the user's last-viewed card, and avatars, @mentions, list and swimlane headers read the whole user, so everything re-ran; they read only the fields they show. The comment, checklist and subtask indexes were one object every minicard depended on and are now per card (imports/lib/keyedGroupIndex.js). - The lazy card window kept a full copy of every card of a list per window; it keeps the id and the sort keys, runs its id query once instead of five times, and sends comment text only when minicards show comments (server/publications/cardsWindow.js, models/lib/cardWindowFields.js). Comment reactions and legacy attachment lookups come with the opened card only. - Attachment downloads honour Range for filesystem, GridFS and cloud storage (206, 416, If-Range); uploads to cloud storage are spooled to a temporary file and streamed instead of gathered into one Buffer; the API copy checks the size limit before reading; the legacy attachment migration streams, and no longer stores "[object Promise]" as a file; the board export no longer concatenates buffers quadratically. The FerretDB fork pushes dotted-path filters such as meta.cardId down to SQLite (they were each a full scan) and decodes only the fields a count filters on. docs/Features/Admin-Panel/Problems/Large-Boards.md describes how a large board loads and how to size a server with 8 GB of RAM. Thanks to markusst1982 and xet7 ! Fixes #6745.
225 lines
11 KiB
JavaScript
225 lines
11 KiB
JavaScript
'use strict';
|
|
|
|
// Tests for models/lib/boardCardScope.js and its use in the card publications.
|
|
//
|
|
// These helpers retain the normal MongoDB selector shapes. Compatibility databases
|
|
// must optimize those shapes rather than requiring application-specific rewrites.
|
|
// Run: node tests/boardCardScope.test.cjs
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const {
|
|
boardScopeIds,
|
|
boardCardScope,
|
|
isAssignedOnlyMember,
|
|
assignedOnlyCardScope,
|
|
mergeCardScope,
|
|
} = require('../models/lib/boardCardScope');
|
|
|
|
let passed = 0;
|
|
function check(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
|
|
|
|
check('a board without a deposit excludes null and orphan card boards', () => {
|
|
assert.deepStrictEqual(boardScopeIds({ _id: 'B' }), ['B']);
|
|
assert.deepStrictEqual(boardCardScope({ _id: 'B' }), { boardId: { $in: ['B'] } });
|
|
});
|
|
|
|
check('null and undefined subtasksDefaultBoardId use the same MongoDB selector', () => {
|
|
assert.deepStrictEqual(boardCardScope({ _id: 'B', subtasksDefaultBoardId: null }),
|
|
{ boardId: { $in: ['B'] } });
|
|
assert.deepStrictEqual(boardCardScope({ _id: 'B' }),
|
|
{ boardId: { $in: ['B'] } });
|
|
});
|
|
|
|
check('a real subtasks-default board -> an all-string $in (still pushes down)', () => {
|
|
assert.deepStrictEqual(boardScopeIds({ _id: 'B', subtasksDefaultBoardId: 'S' }), ['B']);
|
|
assert.deepStrictEqual(boardCardScope({ _id: 'B', subtasksDefaultBoardId: 'S' }),
|
|
{ boardId: { $in: ['B'] } });
|
|
});
|
|
|
|
check('tolerates a missing board', () => {
|
|
assert.deepStrictEqual(boardScopeIds(undefined), []);
|
|
assert.deepStrictEqual(boardScopeIds({}), []);
|
|
assert.deepStrictEqual(boardCardScope({ _id: 'B', subtasksDefaultBoardId: 5 }),
|
|
{ boardId: { $in: ['B'] } });
|
|
});
|
|
|
|
// ── source guards: no card query still uses the null-containing $in ────────────
|
|
check('board/card publications use boardCardScope, not the null-in-$in literal', () => {
|
|
for (const rel of [
|
|
'server/publications/boards.js',
|
|
'server/publications/cardsWindow.js',
|
|
'server/publications/cards.js',
|
|
]) {
|
|
const src = fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
|
assert.ok(!/boardId: \{ \$in: \[board\._id, board\.subtasksDefaultBoardId\] \}/.test(src),
|
|
`${rel} must not use the null-containing $in card scope`);
|
|
assert.ok(/boardCardScope\(board\)/.test(src), `${rel} must use boardCardScope`);
|
|
}
|
|
});
|
|
|
|
check('the lazy window keeps ordinary MongoDB conjunction semantics', () => {
|
|
assert.deepStrictEqual(
|
|
mergeCardScope({ listId: 'L', swimlaneId: 'S' }, { boardId: 'B', archived: false }),
|
|
{ $and: [
|
|
{ listId: 'L', swimlaneId: 'S' },
|
|
{ boardId: 'B', archived: false },
|
|
] });
|
|
|
|
const src = fs.readFileSync(path.join(__dirname, '..', 'server', 'publications', 'cardsWindow.js'), 'utf8');
|
|
assert.ok(/mergeCardScope\(safe, \{/.test(src), 'the window selector merges through it');
|
|
});
|
|
|
|
// ── assigned-only members ─────────────────────────────────────────────────────
|
|
//
|
|
// isReadAssignedOnly / isNormalAssignedOnly / isCommentAssignedOnly all mean the
|
|
// same thing: the member may only see the cards they are assigned to. The `board`
|
|
// publication has always narrowed its card cursor that way; `boardCardsWindow` —
|
|
// which is what ships the cards in LAZY card-loading mode — did not, so whether
|
|
// the restriction applied at all depended on the board's card-loading mode.
|
|
const member = (userId, extra = {}) => ({ userId, isActive: true, ...extra });
|
|
const boardWith = (...members) => ({ _id: 'B', members });
|
|
|
|
check('each of the three assigned-only flags restricts the member', () => {
|
|
for (const flag of ['isReadAssignedOnly', 'isNormalAssignedOnly', 'isCommentAssignedOnly']) {
|
|
const board = boardWith(member('ada', { [flag]: true }));
|
|
assert.strictEqual(isAssignedOnlyMember(board, 'ada'), true, flag);
|
|
assert.deepStrictEqual(assignedOnlyCardScope(board, 'ada'),
|
|
{ assignees: { $in: ['ada'] } }, flag);
|
|
}
|
|
});
|
|
|
|
check('an ordinary member is not restricted', () => {
|
|
const board = boardWith(member('ada'), member('bob', { isAdmin: true }));
|
|
assert.strictEqual(isAssignedOnlyMember(board, 'ada'), false);
|
|
assert.strictEqual(assignedOnlyCardScope(board, 'ada'), null);
|
|
assert.strictEqual(assignedOnlyCardScope(board, 'bob'), null);
|
|
});
|
|
|
|
check('an INACTIVE assigned-only member document does not restrict', () => {
|
|
// Matches findWhere(board.members, { userId, isActive: true }) in the board
|
|
// publication: a removed member's leftover document must not narrow anything.
|
|
const board = boardWith({ userId: 'ada', isActive: false, isReadAssignedOnly: true });
|
|
assert.strictEqual(assignedOnlyCardScope(board, 'ada'), null);
|
|
});
|
|
|
|
check('a non-member and a logged-out visitor are not restricted here', () => {
|
|
// Deliberate, and the same as the board publication: the restriction narrows
|
|
// what a MEMBER sees; whether a non-member may see the board at all is decided
|
|
// before this, by boardVisibleTo / isVisibleBy.
|
|
const board = boardWith(member('ada', { isReadAssignedOnly: true }));
|
|
assert.strictEqual(assignedOnlyCardScope(board, 'zoe'), null);
|
|
assert.strictEqual(assignedOnlyCardScope(board, null), null);
|
|
assert.strictEqual(assignedOnlyCardScope(board, undefined), null);
|
|
});
|
|
|
|
check('negative: a board with no/!array members never throws', () => {
|
|
assert.strictEqual(assignedOnlyCardScope({ _id: 'B' }, 'ada'), null);
|
|
assert.strictEqual(assignedOnlyCardScope({ _id: 'B', members: null }, 'ada'), null);
|
|
assert.strictEqual(assignedOnlyCardScope({ _id: 'B', members: {} }, 'ada'), null);
|
|
assert.strictEqual(assignedOnlyCardScope(null, 'ada'), null);
|
|
assert.strictEqual(assignedOnlyCardScope({ _id: 'B', members: [null] }, 'ada'), null);
|
|
});
|
|
|
|
check('the restriction survives a client selector that filters by assignee', () => {
|
|
// The board Filter has an assignee filter of its own, so this collision is
|
|
// reachable from the UI. A top-level merge would let one silently replace the
|
|
// other — and in the direction the publication spreads them, the CLIENT's value
|
|
// would win and the restriction would be gone. $and keeps both, so an
|
|
// assigned-only member filtering for someone else gets nothing rather than
|
|
// everything.
|
|
const board = boardWith(member('ada', { isReadAssignedOnly: true }));
|
|
const clientFilter = { listId: 'L', assignees: { $in: ['bob'] } };
|
|
const merged = mergeCardScope(clientFilter, {
|
|
boardId: 'B',
|
|
archived: false,
|
|
...assignedOnlyCardScope(board, 'ada'),
|
|
});
|
|
assert.deepStrictEqual(merged, {
|
|
$and: [clientFilter, { boardId: 'B', archived: false, assignees: { $in: ['ada'] } }],
|
|
});
|
|
});
|
|
|
|
check('an unrestricted member also keeps client and server selectors separate', () => {
|
|
const board = boardWith(member('ada'));
|
|
const merged = mergeCardScope({ listId: 'L' }, {
|
|
boardId: 'B',
|
|
archived: false,
|
|
...assignedOnlyCardScope(board, 'ada'),
|
|
});
|
|
assert.deepStrictEqual(merged, {
|
|
$and: [{ listId: 'L' }, { boardId: 'B', archived: false }],
|
|
});
|
|
});
|
|
|
|
check('both the window and its COUNT are narrowed', () => {
|
|
const src = fs.readFileSync(path.join(__dirname, '..', 'server', 'publications', 'cardsWindow.js'), 'utf8');
|
|
// On the CODE: the comments explain the restriction and name the function, and a
|
|
// guard that counts its own explanation counts wrong.
|
|
const code = src.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, '');
|
|
const uses = code.match(/\.\.\.assignedOnlyCardScope\(/g) || [];
|
|
assert.strictEqual(uses.length, 2,
|
|
'boardCardsWindow AND boardListCardCount — an unrestricted count would still '
|
|
+ 'tell the member how many cards the list really holds, and offer to scroll '
|
|
+ 'in cards that never arrive');
|
|
|
|
// The children (comments/attachments/checklists/items) hang off windowCardIds,
|
|
// which builds on the same selector — so they cannot leak past the restriction.
|
|
// #6745: windowCardIds now shares ONE query per board object between the
|
|
// children (a promise memoized in a WeakMap), so it is no longer `async`;
|
|
// it must still build on windowSel(board).
|
|
assert.ok(/const windowCardIds = (?:async )?board => \{[\s\S]*?windowSel\(board\)/.test(src),
|
|
'the window children are derived from the restricted window selector');
|
|
});
|
|
|
|
check('the composite parent publishes the members its children read', () => {
|
|
// publish-composite hands each child the document as the PARENT cursor published
|
|
// it. With the old `{ _id: 1 }` projection, board.members was undefined in every
|
|
// child, so assignedOnlyCardScope() would have found no flag to act on and the
|
|
// restriction would have been dead code.
|
|
const src = fs.readFileSync(path.join(__dirname, '..', 'server', 'publications', 'cardsWindow.js'), 'utf8');
|
|
// #6745: allowsCommentsOnMinicard rides along (the window's comment projection).
|
|
assert.ok(/fields: \{ _id: 1, members: 1(, allowsCommentsOnMinicard: 1)? \}/.test(src),
|
|
'the parent cursor must publish members');
|
|
assert.ok(!/fields: \{ _id: 1 \}, limit: 1/.test(src),
|
|
'the id-only projection must not come back');
|
|
});
|
|
|
|
// AssignedBleed copy sibling (2026-10-02): copyCard, saveCardAsTemplate,
|
|
// copySwimlane, copyList and moveList checked only that the caller belongs to
|
|
// the source board, so an assigned-only member could copy cards they cannot
|
|
// see - by id, or a whole list or swimlane - into a board of their own.
|
|
check('an assigned-only member copies only a card assigned to them, never a container', () => {
|
|
const { mayCopyFromBoard } = require('../models/lib/boardCardScope');
|
|
const board = flags => ({ members: [{ userId: 'u', isActive: true, ...flags }] });
|
|
const mine = { assignees: ['u'] }, other = { assignees: ['x'] };
|
|
for (const flags of [{ isNormalAssignedOnly: true }, { isReadAssignedOnly: true }, { isCommentAssignedOnly: true }]) {
|
|
assert.strictEqual(mayCopyFromBoard(board(flags), 'u', mine), true);
|
|
assert.strictEqual(mayCopyFromBoard(board(flags), 'u', other), false, 'an unassigned card');
|
|
assert.strictEqual(mayCopyFromBoard(board(flags), 'u'), false, 'a list or swimlane');
|
|
}
|
|
// Everyone else copies as before (negative).
|
|
assert.strictEqual(mayCopyFromBoard(board({}), 'u', other), true);
|
|
assert.strictEqual(mayCopyFromBoard(board({}), 'u'), true);
|
|
});
|
|
|
|
check('every copy and move of board content asks the rule', () => {
|
|
const read = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
|
const cases = [
|
|
['server/models/cards.js', ' async copyCard(', "mayCopyFromBoard(sourceBoard, this.userId, card)"],
|
|
['server/models/cards.js', ' async saveCardAsTemplate(', "mayCopyFromBoard(sourceBoard, this.userId, card)"],
|
|
['server/models/lists.js', ' async copyList(', "mayCopyFromBoard(sourceBoard, this.userId)"],
|
|
['server/models/lists.js', ' async moveList(', "mayCopyFromBoard(sourceBoard, this.userId)"],
|
|
['server/publications/swimlanes.js', ' async copySwimlane(', "mayCopyFromBoard(sourceBoard, this.userId)"],
|
|
];
|
|
for (const [file, start, call] of cases) {
|
|
const src = read(file), at = src.indexOf(start);
|
|
assert.ok(at >= 0, `${file}${start}`);
|
|
const body = src.slice(at, src.indexOf('\n },', at));
|
|
assert.ok(body.includes(call), `${start} asks mayCopyFromBoard`);
|
|
}
|
|
});
|
|
|
|
console.log(`\nboardCardScope: ${passed} checks passed`);
|