mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Phase 1 of docs/Features/Reports/History/History.md §10, plus the write half of phase 2. Ctrl+Z is no longer a position-only feature. THE STORE. models/changeHistory.js is the append-only collection of §4: one row per change, carrying every container id it sits inside (boardId, swimlaneId, listId, cardId) so a container scope is a plain equality rather than a join the database cannot do - which is what makes a swimlane's history include its lists' and cards' rows. It imports NO other model, deliberately. Its predecessor imports Cards, Lists, Swimlanes, Checklists and ChecklistItems so its undo() can write to them, and that is precisely why models/cards.js could not import it, guarded on an assumed global instead, and recorded nothing for years. A collection that entities must import cannot import entities. Applying a change back to a document therefore lives in server/models/changeHistory.js, which nothing imports and which may import anything. THE RULES ARE PURE. models/lib/changeHistoryQuery.js holds the scope-to-selector translation, the search and the selection normalisation, with no Meteor and no database, because that is where this feature is either right or quietly wrong: a scope that resolves to the wrong id column shows one board's history under another board's menu, and a selection that does not normalise its input restores the wrong rows. Both are silent. 17 tests, including that an unknown or half-given scope is REFUSED rather than widened into a selector that matches everything. UNDO IS NOW THE WHOLE HISTORY. changeHistory.undoLast/redoLast replace the position-only methods; the keyboard bindings and the tested pickUndo/pickRedo rule are unchanged. One rule covers every change type instead of a case per action: undo applies previousContent, redo applies newContent. Restores go through the same setters an ordinary edit uses, so validation, hooks and Activities still run, and each restore is itself recorded twice - once attributed to whoever made the change being restored, once to whoever pressed Restore (§8.3). RECORDED SO FAR: card description edits (§10.2's first content group - the previous text is read BEFORE the write, since after it the old value is gone), card moves, list moves, and list soft-delete/restore, which is what makes "undo a deleted list" work. changeHistory is in the snap's MERGE_COLLECTIONS, as §9a.4 requires the moment the collection exists: a history row written on the copy that is not served is work that would otherwise be stranded in a database nobody opens. NOT BUILT: every UI in §7, §7a and §8 - the popup, the table, the contributor avatars, the Restore button, the "History" menu items - and recording for the remaining groups of §3. The methods those screens need exist and are tested; nothing calls them yet. Both docs now say which half is which, because the last time this document claimed more than the code did, the gap survived for months. Thanks to xet7 !
139 lines
5.2 KiB
JavaScript
139 lines
5.2 KiB
JavaScript
'use strict';
|
|
|
|
// Pure query helpers for the universal change history
|
|
// (docs/Features/Reports/History/History.md §6). No Meteor, no database, so they
|
|
// are unit-tested directly (tests/changeHistoryQuery.test.cjs) — the same
|
|
// discipline as models/lib/undoRedoSelection.js, and for the same reason: the
|
|
// selection and filtering rules are where this feature is either right or
|
|
// quietly wrong, and neither is visible by reading a Mongo selector.
|
|
//
|
|
// Three things live here:
|
|
// scopeSelector() turn a {scope, scopeId, group, userId} request into the
|
|
// Mongo selector the server queries with
|
|
// matchesSearch() whether a row matches a search term
|
|
// selectionToIds() normalise whatever the UI hands back as "checked rows"
|
|
|
|
/*
|
|
* Container scopes mean "this entity AND its descendants" (History.md §6): a
|
|
* swimlane's history includes its lists' and cards' rows. That is why the WRITE
|
|
* side stores every applicable id column on every row - boardId, swimlaneId,
|
|
* listId, cardId - so the read side is a plain equality on one column instead of
|
|
* a join the database cannot do.
|
|
*/
|
|
const SCOPE_COLUMN = {
|
|
board: 'boardId',
|
|
swimlane: 'swimlaneId',
|
|
list: 'listId',
|
|
card: 'cardId',
|
|
};
|
|
|
|
const SCOPES = Object.keys(SCOPE_COLUMN);
|
|
|
|
function scopeSelector({ scope, scopeId, group, userId, entityType, entityId } = {}) {
|
|
const selector = {};
|
|
|
|
if (scope !== undefined && scope !== null && scope !== '') {
|
|
const column = SCOPE_COLUMN[scope];
|
|
if (!column) {
|
|
throw new Error(`changeHistory: unknown scope '${scope}' (expected ${SCOPES.join(', ')})`);
|
|
}
|
|
if (typeof scopeId !== 'string' || scopeId.length === 0) {
|
|
throw new Error(`changeHistory: scope '${scope}' needs a scopeId`);
|
|
}
|
|
selector[column] = scopeId;
|
|
} else if (scopeId) {
|
|
throw new Error('changeHistory: scopeId without a scope does not say what to match');
|
|
}
|
|
|
|
// A specific entity, for the future per-entity menus (History.md §7a).
|
|
if (entityType) selector.entityType = entityType;
|
|
if (entityId) selector.entityId = entityId;
|
|
|
|
// Narrow a card scope to one group of the card details view.
|
|
if (group) selector.group = group;
|
|
|
|
// One contributor: the Member-settings view, or an avatar click inside another
|
|
// scope. Combined with the scope above rather than replacing it.
|
|
if (userId) selector.userId = userId;
|
|
|
|
return selector;
|
|
}
|
|
|
|
/*
|
|
* Search matches the rendered change-type label and a text projection of the
|
|
* content, case-insensitively (History.md §6). Numbers and booleans inside the
|
|
* content are searchable too - a date change is a number until it is rendered,
|
|
* and "the thing I typed is not found" is the complaint that follows if it is
|
|
* not. Depth is bounded so a pathological blackbox cannot hang the server.
|
|
*/
|
|
function contentText(value, depth = 0, out = []) {
|
|
if (value === null || value === undefined || depth > 4) return out;
|
|
if (typeof value === 'string') { out.push(value); return out; }
|
|
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
out.push(String(value));
|
|
return out;
|
|
}
|
|
if (value instanceof Date) { out.push(value.toISOString()); return out; }
|
|
if (Array.isArray(value)) {
|
|
for (const item of value) contentText(item, depth + 1, out);
|
|
return out;
|
|
}
|
|
if (typeof value === 'object') {
|
|
for (const key of Object.keys(value)) contentText(value[key], depth + 1, out);
|
|
return out;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function matchesSearch(row, term) {
|
|
if (typeof term !== 'string') return true;
|
|
const needle = term.trim().toLowerCase();
|
|
if (needle.length === 0) return true; // an empty search matches everything
|
|
if (!row) return false;
|
|
|
|
const haystack = [
|
|
row.changeType,
|
|
row.group,
|
|
row.entityType,
|
|
...contentText(row.newContent),
|
|
...contentText(row.previousContent),
|
|
]
|
|
.filter(part => typeof part === 'string' && part.length > 0)
|
|
.join('\n')
|
|
.toLowerCase();
|
|
|
|
return haystack.includes(needle);
|
|
}
|
|
|
|
/*
|
|
* The UI keeps checked rows in a Set on the template instance (History.md §7,
|
|
* and #6479: never on the Blaze data context). It can reach the server as a Set,
|
|
* an array, or an object of id -> checked, so normalise all three and drop
|
|
* anything that is not a usable id. Order is preserved and duplicates removed,
|
|
* which matters because a multi-row restore applies oldest to newest and must
|
|
* not apply the same row twice (History.md §8.4).
|
|
*/
|
|
function selectionToIds(selected) {
|
|
let candidates;
|
|
if (!selected) candidates = [];
|
|
else if (Array.isArray(selected)) candidates = selected;
|
|
else if (typeof selected.forEach === 'function' && typeof selected.size === 'number') {
|
|
candidates = [...selected]; // a Set
|
|
} else if (typeof selected === 'object') {
|
|
candidates = Object.keys(selected).filter(key => selected[key]);
|
|
} else if (typeof selected === 'string') candidates = [selected];
|
|
else candidates = [];
|
|
|
|
const ids = [];
|
|
const seen = new Set();
|
|
for (const value of candidates) {
|
|
if (typeof value !== 'string' || value.length === 0) continue;
|
|
if (seen.has(value)) continue;
|
|
seen.add(value);
|
|
ids.push(value);
|
|
}
|
|
return ids;
|
|
}
|
|
|
|
module.exports = { scopeSelector, matchesSearch, selectionToIds, SCOPES, SCOPE_COLUMN };
|