fix: bind Today progress retries to evidence (Closes #1056)
This commit is contained in:
1 parent
157d485685
commit
1410a66a36
4 files changed
+160
-21
No files matched your search
@@ -55,6 +55,20 @@ function createAuthoredOutbox({ storage, fetchJson, coordinator, backgroundSync,
|
||||
});
|
||||
}
|
||||
|
||||
function operationFingerprint(message) {
|
||||
return JSON.stringify({
|
||||
kind:String(message.kind || ''), repository:String(message.repository || ''),
|
||||
number:Number(message.number || 0), notificationId:Number(message.notificationId || 0),
|
||||
body:String(message.body || ''), targetKind:String(message.targetKind || ''),
|
||||
decision:String(message.decision || 'comment'), expectedHeadSha:String(message.expectedHeadSha || ''),
|
||||
comments:Array.isArray(message.comments) ? message.comments : [],
|
||||
blockerRepository:String(message.blockerRepository || ''), blockerNumber:Number(message.blockerNumber || 0),
|
||||
present:message.present === true, title:String(message.title || ''),
|
||||
expectedUpdatedAt:String(message.expectedUpdatedAt || ''),
|
||||
attachments:messageAttachments(message).map(attachmentMetadata),
|
||||
});
|
||||
}
|
||||
|
||||
function clearConfirmedReviewState(item) {
|
||||
if (item.kind !== 'pull-review') return;
|
||||
try {
|
||||
@@ -93,7 +107,12 @@ function createAuthoredOutbox({ storage, fetchJson, coordinator, backgroundSync,
|
||||
const items = read();
|
||||
const requestedOperationId = String(message.operationId || '').slice(0, 128);
|
||||
const existing = requestedOperationId && items.find(item => item.operationId === requestedOperationId);
|
||||
if (existing) return { ...existing };
|
||||
if (existing) {
|
||||
if (operationFingerprint(existing) !== operationFingerprint(message)) {
|
||||
throw new Error('This operation ID is already bound to a different queued action.');
|
||||
}
|
||||
return { ...existing };
|
||||
}
|
||||
if (message.kind === 'pull-review') {
|
||||
const queuedReview = items.find(item => item.kind === 'pull-review' &&
|
||||
item.repository === String(message.repository || '') &&
|
||||
|
||||
+55
-10
@@ -5,6 +5,13 @@ maxLength = 2000, maxItems = 20 }) {
|
||||
const login = () => String(getLogin() || '').trim().toLowerCase();
|
||||
const storageKey = () => login() ? prefix + encodeURIComponent(login()) : '';
|
||||
const validIdentity = identity => typeof identity === 'string' && identity.length > 0 && identity.length <= 500;
|
||||
const attachmentFingerprint = attachments => JSON.stringify((Array.isArray(attachments) ? attachments : []).filter(Boolean).slice(0, 5).map(value => ({
|
||||
filename:String(value.filename || ''), contentType:String(value.contentType || ''),
|
||||
note:String(value.note || '').replace(/\s+/g, ' ').trim().slice(0, 240),
|
||||
operationId:String(value.operationId || '').slice(0, 128),
|
||||
markdown:String(value.confirmed?.markdown || ''),
|
||||
})));
|
||||
const payloadFingerprint = (body, attachments) => JSON.stringify({body, attachments:attachmentFingerprint(attachments)});
|
||||
const validRecord = record => record && typeof record.body === 'string' && (record.body.length > 0 || record.has_attachments === true) &&
|
||||
record.body.length <= maxLength && typeof record.operation_id === 'string' && record.operation_id.length > 0 &&
|
||||
record.operation_id.length <= 128;
|
||||
@@ -42,9 +49,10 @@ maxLength = 2000, maxItems = 20 }) {
|
||||
return read()[identity]?.body || '';
|
||||
}
|
||||
|
||||
function save(identity, value, hasAttachments = false) {
|
||||
function save(identity, value, attachments = []) {
|
||||
if (!validIdentity(identity) || !storageKey()) return false;
|
||||
const body = String(value || '').trim();
|
||||
const hasAttachments = attachments === true || (Array.isArray(attachments) && attachments.filter(Boolean).length > 0);
|
||||
if (body.length > maxLength) return false;
|
||||
const drafts = read();
|
||||
if (!body && !hasAttachments) {
|
||||
@@ -53,9 +61,13 @@ maxLength = 2000, maxItems = 20 }) {
|
||||
}
|
||||
if (!drafts[identity] && Object.keys(drafts).length >= maxItems) return false;
|
||||
const previous = drafts[identity];
|
||||
const fingerprint = payloadFingerprint(body, Array.isArray(attachments) ? attachments : []);
|
||||
const previousFingerprint = previous?.payload_fingerprint ||
|
||||
(previous && !previous.has_attachments ? payloadFingerprint(previous.body, []) : '');
|
||||
drafts[identity] = {
|
||||
body,
|
||||
operation_id: previous?.body === body ? previous.operation_id : String(makeId()).slice(0, 128),
|
||||
operation_id: previousFingerprint === fingerprint ? previous.operation_id : String(makeId()).slice(0, 128),
|
||||
payload_fingerprint:fingerprint,
|
||||
...(hasAttachments ? {has_attachments:true} : {}),
|
||||
};
|
||||
return write(drafts);
|
||||
@@ -69,8 +81,24 @@ maxLength = 2000, maxItems = 20 }) {
|
||||
|
||||
async function post(target, value, attachments = [], completeEvidence) {
|
||||
if (!validTarget(target)) throw new Error('An active Today issue or pull request is required.');
|
||||
const pendingRecord = read()[target.identity];
|
||||
if (pendingRecord?.cleanup_pending === true) {
|
||||
try {
|
||||
if (typeof completeEvidence === 'function') await completeEvidence();
|
||||
} catch (_error) {
|
||||
const error = new Error('Progress update is already queued; photo cleanup is pending.');
|
||||
error.deliveryAdmitted = true;
|
||||
throw error;
|
||||
}
|
||||
const pendingDrafts = read();
|
||||
if (pendingDrafts[target.identity]?.operation_id === pendingRecord.operation_id) {
|
||||
delete pendingDrafts[target.identity];
|
||||
write(pendingDrafts);
|
||||
}
|
||||
return { background:true, alreadyAdmitted:true, cleanupRecovered:true };
|
||||
}
|
||||
const evidence = (Array.isArray(attachments) ? attachments : [attachments]).filter(Boolean).slice(0, 5);
|
||||
if ((value !== undefined || evidence.length) && !save(target.identity, value ?? load(target.identity), evidence.length > 0)) {
|
||||
if ((value !== undefined || evidence.length) && !save(target.identity, value ?? load(target.identity), evidence)) {
|
||||
throw new Error('Progress update could not be saved on this device.');
|
||||
}
|
||||
const record = read()[target.identity];
|
||||
@@ -81,7 +109,18 @@ maxLength = 2000, maxItems = 20 }) {
|
||||
body:record.body, operationId:record.operation_id,
|
||||
...(evidence.length ? {attachments:evidence} : {}),
|
||||
});
|
||||
if (typeof completeEvidence === 'function') await completeEvidence();
|
||||
const admittedDrafts = read();
|
||||
if (admittedDrafts[target.identity]?.operation_id === record.operation_id) {
|
||||
admittedDrafts[target.identity] = { ...admittedDrafts[target.identity], cleanup_pending:true };
|
||||
write(admittedDrafts);
|
||||
}
|
||||
try {
|
||||
if (typeof completeEvidence === 'function') await completeEvidence();
|
||||
} catch (_error) {
|
||||
const error = new Error('Progress update is already queued; photo cleanup is pending.');
|
||||
error.deliveryAdmitted = true;
|
||||
throw error;
|
||||
}
|
||||
const drafts = read();
|
||||
if (drafts[target.identity]?.operation_id === record.operation_id) {
|
||||
delete drafts[target.identity];
|
||||
@@ -103,12 +142,16 @@ function createTodayProgressView({ progress, currentTarget, qs, photos, announce
|
||||
const update = () => { launcher.hidden = !currentTarget(); };
|
||||
const checkpoint = async () => {
|
||||
if (!openedTarget) return false;
|
||||
if (progress.save(openedTarget.identity, body.value, photos?.has?.())) {
|
||||
try { await photos?.checkpoint?.(); return true; }
|
||||
catch (error) { status.textContent = error.message + ' Your photos remain here; retry.'; return false; }
|
||||
try {
|
||||
await photos?.checkpoint?.();
|
||||
const checkpointAttachments = await photos?.serialize?.() || [];
|
||||
if (progress.save(openedTarget.identity, body.value, checkpointAttachments)) return true;
|
||||
status.textContent = 'Update must be 2,000 characters or fewer and device storage must be available.';
|
||||
return false;
|
||||
} catch (error) {
|
||||
status.textContent = error.message + ' Your photos remain here; retry.';
|
||||
return false;
|
||||
}
|
||||
status.textContent = 'Update must be 2,000 characters or fewer and device storage must be available.';
|
||||
return false;
|
||||
};
|
||||
const close = () => {
|
||||
if (sheet.open) sheet.close();
|
||||
@@ -158,7 +201,9 @@ function createTodayProgressView({ progress, currentTarget, qs, photos, announce
|
||||
'Progress update saved for next launch. Today is still on the same item.');
|
||||
close();
|
||||
} catch (error) {
|
||||
status.textContent = error.message + ' Your update remains on this item; retry.';
|
||||
status.textContent = error.deliveryAdmitted ?
|
||||
error.message + ' Retry to finish local cleanup; delivery will not be queued again.' :
|
||||
error.message + ' Your update remains on this item; retry.';
|
||||
body.focus();
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
|
||||
Reference in new issue
Block a user