Files
wekan/server/lib/syncRecordingScope.js
T
Lauri Ojansivu af1b1bee2d Make the checklist rule actions durable in Sync.
checkAll, uncheckAll, checkItem and uncheckItem join the durable rule
actions. The command is captured with performAction's own lookups - the
checklist by title on the card, the items in the order ReactiveCache returns
them, the bulk selection, and checkItem's item query as it is - so a replay
acts on the same items with the same outcome, including the ordinary
action's no-ops.

The ordinary action writes each item in turn, and the checklist item hooks
record on every write, changed or not: uncompleteChecklist before it when
the whole checklist was finished, checkedItem or uncheckedItem after it, and
completeChecklist when the whole checklist is finished - plus a History row
on the item when isFinished changed. The command simulates exactly that on
the captured items and saves one unit per item. The runner goes item by item:
the before-write activity, the conditional write with the item's three hooks
deferred, its History, its after-write activities - each delivered durably -
so a rule triggered by one item runs before the next, as ordinarily.

The History planner now accepts checklist-item rows (entity and card kept
apart), and the recording deferral can name one checklist item. Unit tests
cover the hook sequence for check-all, uncheck-all and hidden-all lists,
no-ops and tampering; a full-app test runs a "check every item" rule through
the stored rule stage - two checkedItem, one completeChecklist, two History
rows, and nothing more on replay. 566 server tests pass.

Thanks to xet7 !
2026-09-30 12:17:20 +03:00

37 lines
2.2 KiB
JavaScript

'use strict';
const { AsyncLocalStorage } = require('node:async_hooks');
const storage = new AsyncLocalStorage();
const KINDS = ['create','archive','title','description','customFields','history','timing'];
// A checklist item's three hooks, for a scope that names that item.
const ITEM_KINDS = ['itemUncomplete','itemCheck','itemHistory'];
async function withSyncRecordingDeferred({ cardId, boardId, listId, kinds, itemId = null }, work) {
if (![cardId,boardId,listId].every(value=>typeof value==='string'&&value) || !Array.isArray(kinds) ||
(itemId!==null && (typeof itemId!=='string' || !itemId)) ||
kinds.some(kind=>!(itemId===null ? KINDS : ITEM_KINDS).includes(kind)) || typeof work!=='function') throw new Error('sync-recording-scope-invalid');
const scope={cardId,boardId,listId,itemId,kinds:new Set(kinds),active:true};
return storage.run(scope,async()=>{
try{return await work();}finally{scope.active=false;}
});
}
function deferSyncRecording(kind, doc, fields) {
const scope=storage.getStore();
if(!scope?.active || !scope.kinds.has(kind) || doc?._id!==scope.cardId ||
doc.boardId!==scope.boardId || doc.listId!==scope.listId) return false;
// Sync's own fields, and the one field a durable rule card action changes
// (server/lib/syncRuleCardCommand.js); its History is written from its plan.
if(kind==='history' && (!Array.isArray(fields) || fields.some(field=>!['title','description','spentTime','customFields','archived','labelIds','color','dueComplete','startAt','endAt','dueAt','receivedAt','members'].includes(field)))) return false;
// Each expected hook consumes its own slot. Nested/unrelated writes and
// delayed callbacks after the owning mutation must keep normal recording.
scope.kinds.delete(kind);
return true;
}
// The same one-shot slots for the one checklist item a scope names.
function deferSyncItemRecording(kind, doc) {
const scope=storage.getStore();
if(!scope?.active || scope.itemId===null || !scope.kinds.has(kind) || doc?._id!==scope.itemId ||
doc.cardId!==scope.cardId) return false;
scope.kinds.delete(kind);
return true;
}
module.exports={withSyncRecordingDeferred,deferSyncRecording,deferSyncItemRecording};