Files
wekan/server/lib/historyRecordingScope.js
T
Lauri Ojansivu 92b5ce8614 Make History work when it is opened.
The change history and its table were written without a Meteor runtime
available, and both documents said so. Opening it found four things that no
amount of reading the source would have shown, three of which made the
feature useless.

THE TABLE WAS BLANK. `{{#each row in rows}}` binds `row` as a NAME and leaves
the data context alone - unlike `{{#each rows}}`, which replaces it - so
`{{_id}}`, `{{contentSummary}}` and the rest resolved against the OUTER
context and rendered nothing. Nothing errors: the table drew one row of four
empty cells and a checkbox with no id. Same bug in the contributor loop. Every
field now goes through its loop variable.

THE ROW COULD NOT BE SELECTED. WeKan hides every bare checkbox app-wide -
forms.css: `[type="checkbox"] { display: none }` - and draws `.materialCheckBox`
divs instead, so the real one here rendered 0x0. The row was visible, could
never be ticked, and Restore stayed disabled with nothing that could enable
it. It is now the control the rest of the app uses, clicked rather than
changed, drawn from the selection state.

THE PANEL WAS 380px WIDE. A popup's default width left 129px for the
contributor pane and 201px for a four-column table; the search box was
squeezed to 32px and one row had to be scrolled sideways to be read. History
is the same shape as the export panels - two panes, opened from a menu at the
edge of the screen - so it joins them in popup.css and in the FULL_WIDTH_POPUPS
list in popupOffset.js, which is the other half that decides where a panel is
placed. The rows also scroll in a wrapper now rather than in the table itself:
`display: block` on a <table> stops the cells sharing column widths, so the
header and the rows beneath it drift apart.

AND A RESTORE WAS RECORDED TWICE. This one is a correct decision with a
consequence: History.md 8.2 says a restore re-applies content through the
SAME setters an ordinary edit uses, so validation, hooks and Activities all
still run. The field-diffing after.update hook is one of those hooks, so it
saw the restore's own write and recorded it - and restoring a title left an
`edited` row nobody made sitting above the `restored` row describing the same
write. Recording, and only recording, is now switched off for the duration of
the applier.

That switch is an AsyncLocalStorage scope, not a module-level boolean, and the
difference is not stylistic: the server handles several requests at once, and
a shared flag set during one user's restore would have silently swallowed
another user's edit landing in the same window - a data-loss bug that appears
only under concurrency and would never be seen in testing.

Verified against a running server rather than by reading: a card renamed
through the UI recorded one row with the right group, previous and new value;
History showed it; selecting it and pressing Restore put the title back and
left exactly two rows, the edit and the restore.

tests/historyOneTemplate.test.cjs pins the loop-variable rule, the checkbox
control, the panel width in both files that decide it, and the scroll wrapper.
tests/historyRestoreRecordsOnce.test.cjs pins the suppression, that a restore
still writes through the normal setters, and - running two overlapping async
operations for real - that a concurrent edit by another user is still
recorded.

Thanks to xet7 !
2026-09-05 00:15:18 +03:00

35 lines
1.6 KiB
JavaScript

import { AsyncLocalStorage } from 'node:async_hooks';
/*
* "Do not record what a restore itself writes."
*
* A restore deliberately goes back through the ordinary collection setters
* (History.md §8.2), so validation, the collection hooks and Activities all run
* exactly as they do for a human edit - `.direct` would skip them and is not
* used. The cost of that correctness is that the field-diffing `after.update`
* hook sees the restore's own write and records it as an ordinary `edited`
* change. Restoring one title therefore left TWO rows in History: the `restored`
* row the restore wrote on purpose, and an `edited` row nobody made, describing
* the same write, sitting directly above it.
*
* So recording - and only recording - is switched off for the duration of the
* applier. Everything else the write triggers still happens.
*
* AsyncLocalStorage rather than a module-level boolean: the server handles
* several requests at once and a plain flag set during one user's restore would
* silently swallow another user's edit that happened to land inside the same
* window. A store is per async context and follows the awaits of the call that
* opened it, so it can only ever cover the restore's own writes.
*/
const storage = new AsyncLocalStorage();
/** Run `fn` with history recording suppressed for whatever it writes. */
export function withoutRecording(fn) {
return storage.run(true, fn);
}
/** True while inside withoutRecording - checked by the recording hooks. */
export function isRecordingSuppressed() {
return storage.getStore() === true;
}