Change an open update request to a newer version (1.5.0)

When a site asks again after a newer version came out, the open
update_requested alert moves on to the latest version ("changed from
...") and the alert address is emailed once; the response says
changed: true with previousVersion (protocol §3a). Needs Cross Events
1.47.4 for the button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 12:16:59 +02:00
co-authored by Claude Opus 5.5
parent 322635978a
commit a3e9c5b4f6
9 changed files with 98 additions and 15 deletions
+29 -7
View File
@@ -12,6 +12,7 @@ const licenceToken = require('../utils/licenceToken');
const { checkSignature } = require('../utils/signing');
const { getSettings } = require('../utils/settings');
const { compareVersions } = require('../utils/alerts');
const { requestMessage, requestedVersionOf, isOutdated } = require('../utils/updateRequests');
const { sendMail } = require('../utils/mail');
const { releasesForInstall } = require('../utils/releases');
const { noticesForInstall } = require('../utils/notices');
@@ -176,19 +177,40 @@ router.post('/installs/update-request', updateRequestLimiter, async (req, res) =
return fail(res, 409, 'UP_TO_DATE', 'This site already runs the latest version.');
}
const open = await prisma.installEvent.findFirst({ where: { installId: install.id, type: 'update_requested', resolvedAt: null }, orderBy: { createdAt: 'desc' } });
if (open) return res.json({ requestedAt: open.createdAt.toISOString(), version: latest, alreadyRequested: true });
const customerName = async () => (await prisma.customer.findUnique({ where: { id: install.customerId }, select: { name: true } }))?.name || install.name;
const link = `${(process.env.FRONTEND_URL || '').replace(/\/$/, '')}/installs/${install.id}`;
const event = await prisma.installEvent.create({ data: { installId: install.id, type: 'update_requested', level: 'warning', message: `Update requested: runs ${current}, latest is ${latest}` } });
const open = await prisma.installEvent.findFirst({ where: { installId: install.id, type: 'update_requested', resolvedAt: null }, orderBy: { createdAt: 'desc' } });
if (open) {
// Asked for a version that has since been overtaken: the request moves on to the latest one
// (Joshua is told once). Otherwise nothing changes and nobody is emailed again.
if (!isOutdated(open.message, latest)) {
return res.json({ requestedAt: open.createdAt.toISOString(), version: latest, alreadyRequested: true, changed: false });
}
const previous = requestedVersionOf(open.message);
const now = new Date();
await prisma.installEvent.update({ where: { id: open.id }, data: { message: requestMessage(current, latest, previous) } });
if (settings.alert_email) {
const name = await customerName();
await sendMail({
to: settings.alert_email,
subject: `Cross Events Hub: update request changed by ${name}`,
text: `${name} (${install.url}) now asks to be updated from ${current} to ${latest}${previous ? ` instead of ${previous}` : ''}.\n\n${link}`,
}).catch(e => console.warn('[update-request] email failed:', e?.message));
}
return res.json({ requestedAt: now.toISOString(), version: latest, alreadyRequested: true, changed: true, previousVersion: previous });
}
const event = await prisma.installEvent.create({ data: { installId: install.id, type: 'update_requested', level: 'warning', message: requestMessage(current, latest) } });
if (settings.alert_email) {
const customer = await prisma.customer.findUnique({ where: { id: install.customerId }, select: { name: true } });
const name = await customerName();
await sendMail({
to: settings.alert_email,
subject: `Cross Events Hub: update requested by ${customer?.name || install.name}`,
text: `${customer?.name || install.name} (${install.url}) would like to be updated from ${current} to ${latest}.\n\n${(process.env.FRONTEND_URL || '').replace(/\/$/, '')}/installs/${install.id}`,
subject: `Cross Events Hub: update requested by ${name}`,
text: `${name} (${install.url}) would like to be updated from ${current} to ${latest}.\n\n${link}`,
}).catch(e => console.warn('[update-request] email failed:', e?.message));
}
res.status(201).json({ requestedAt: event.createdAt.toISOString(), version: latest, alreadyRequested: false });
res.status(201).json({ requestedAt: event.createdAt.toISOString(), version: latest, alreadyRequested: false, changed: false });
} catch (e) {
console.error('[update-request]', e);
fail(res, 500, 'SERVER_ERROR', 'The Hub could not record the update request. Try again later.');
+26
View File
@@ -0,0 +1,26 @@
/**
* Update requests (protocol §3a). The version a request asks for lives in its alert message
* ("Update requested: runs 1.46.2, latest is 1.47.0"), so a request made before a newer version
* came out can be moved on to it when the site asks again.
*/
const { compareVersions } = require('./alerts');
/** The alert message for a request from `current` to `latest`. */
function requestMessage(current, latest, previous = null) {
return `Update requested: runs ${current}, latest is ${latest}${previous ? ` (changed from ${previous})` : ''}`;
}
/** The version an open request asks for, read from its message, or null. */
function requestedVersionOf(message) {
const m = /latest is (\d+\.\d+\.\d+)/.exec(String(message || ''));
return m ? m[1] : null;
}
/** Whether an open request asks for an older version than `latest` (so asking again changes it). */
function isOutdated(message, latest) {
const asked = requestedVersionOf(message);
return !!latest && (!asked || compareVersions(asked, latest) < 0);
}
module.exports = { requestMessage, requestedVersionOf, isOutdated };