mirror of
https://github.com/wekan/wekan.git
synced 2026-11-20 14:45:01 -05:00
Match literal text across the authorized board, including cards outside lazy windows. Publish only matching IDs, refresh on content and access changes, and release observers when filters change. Verify the batched join, cancellation and permission races, and actual Chromium behavior. Thanks to neooleg and xet7 !
27 lines
1.3 KiB
JavaScript
27 lines
1.3 KiB
JavaScript
// Read-only, bounded-batch join. Callers supply an authorized card scope.
|
|
// No child text or unverified child cardId is returned to the client.
|
|
export async function boardTextSearch({ cards, children, scope, term, stopped = () => false }) {
|
|
const regex = new RegExp(term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i');
|
|
const matches = new Set();
|
|
let after;
|
|
while (!stopped()) {
|
|
const batch = await cards.find({ $and: [scope, ...(after ? [{ _id: { $gt: after } }] : [])] },
|
|
{ projection: { _id: 1, title: 1, description: 1 } }).sort({ _id: 1 }).limit(250).toArray();
|
|
if (!batch.length) break;
|
|
const ids = batch.map(card => card._id), allowed = new Set(ids);
|
|
for (const card of batch) {
|
|
if ([card.title, card.description].some(text => typeof text === 'string' && regex.test(text))) matches.add(card._id);
|
|
}
|
|
for (const { collection, field } of children) {
|
|
if (stopped()) return [];
|
|
const cursor = collection.find({ cardId: { $in: ids }, boardId: scope.boardId, [field]: regex },
|
|
{ projection: { cardId: 1 } }).batchSize(250);
|
|
try {
|
|
for await (const row of cursor) if (allowed.has(row.cardId)) matches.add(row.cardId);
|
|
} finally { await cursor.close(); }
|
|
}
|
|
after = batch[batch.length - 1]._id;
|
|
}
|
|
return stopped() ? [] : [...matches];
|
|
}
|