mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Retain detailed metadata for 30 days by default, then replace terminal jobs and commands atomically with minimal permanent identities. Preserve unfinished work, holds, generation fencing and actor-bound command replays. Sweep bounded batches independently and reconcile uncertain writes. Validated with six Node suites, real MongoDB faults and replay tests, the Meteor startup scheduler, and Chromium Recovery replay and authorization checks. Thanks to xet7.
25 lines
1.5 KiB
JavaScript
25 lines
1.5 KiB
JavaScript
'use strict';
|
|
const { createHash } = require('node:crypto');
|
|
const hash = values => createHash('sha256').update(JSON.stringify(values)).digest('hex');
|
|
const idFor = (userId, eventId) => hash([userId, eventId]);
|
|
const commandIdentity = (userId, action, actorId) => hash([userId, action, actorId]);
|
|
const keysAre = (row, keys) => Object.keys(row).sort().join(',') === [...keys].sort().join(',');
|
|
const terminalJob = state => ['sent', 'cancelled'].includes(state);
|
|
const terminalCommand = status => ['completed', 'superseded'].includes(status);
|
|
function matchesEmailJob(row, userId, eventId) {
|
|
if (!row || row._id !== idFor(userId, eventId)) return false;
|
|
if (!Object.hasOwn(row, 'compactReceiptVersion')) return row.userId === userId && row.eventId === eventId;
|
|
return row.compactReceiptVersion === 1 && terminalJob(row.state) &&
|
|
keysAre(row, ['_id', 'state', 'compactReceiptVersion']);
|
|
}
|
|
function matchesEmailCommand(row, { requestId, userId, action, actorId }) {
|
|
if (!row || row._id !== requestId) return false;
|
|
if (!Object.hasOwn(row, 'compactReceiptVersion')) {
|
|
return row.userId === userId && row.action === action && row.actorId === actorId;
|
|
}
|
|
return row.compactReceiptVersion === 1 && terminalCommand(row.status) &&
|
|
row.identityHash === commandIdentity(userId, action, actorId) &&
|
|
keysAre(row, ['_id', 'status', 'identityHash', 'compactReceiptVersion']);
|
|
}
|
|
module.exports = { idFor, commandIdentity, matchesEmailJob, matchesEmailCommand, terminalJob, terminalCommand };
|