Files
wekan/server/lib/syncRecordingScope.js
T
Lauri Ojansivu b3b4962414 Run rule moves to the top or bottom of the card's own list through durable Sync.
A moveCardToTop or moveCardToBottom rule whose list and swimlane are '*'
on the card's own board only changes the card's sort. It now has a saved
command, like the card-field actions: the sort is computed once at
capture as the ordinary action computes it, the write is conditional on
the saved sort, the hook's position History row is planned and written
once, and the legacy UserPositionHistory row that Ctrl+Z reads is
written once with trackChange's redo-stack clearing. Replays apply the
saved command and change nothing more.

A move to another list, swimlane or board is reported to eligibility as
'<type>:elsewhere', so a board with such a rule keeps direct Sync.

Thanks to xet7 !
2026-10-02 15:34:18 +03:00

43 lines
2.5 KiB
JavaScript

'use strict';
const { AsyncLocalStorage } = require('node:async_hooks');
const storage = new AsyncLocalStorage();
// 'position' is not a hook: it lets 'history' defer the hook's position row
// too, for a durable rule move (server/lib/syncRuleMoveCommand.js).
const KINDS = ['create','archive','title','description','customFields','history','timing','position'];
// 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.
// A move's position row only when the scope says so: other Sync writes that
// change placement keep recording it ordinarily.
const allowed=['title','description','spentTime','customFields','archived','labelIds','color','dueComplete','startAt','endAt','dueAt','receivedAt','members',
...(scope.kinds.has('position') ? ['position'] : [])];
if(kind==='history' && (!Array.isArray(fields) || fields.some(field=>!allowed.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};