39 lines
1.9 KiB
JavaScript
39 lines
1.9 KiB
JavaScript
.pragma library
|
|
|
|
function timeoutMs(milliseconds, urgency) {
|
|
// Critical notifications require an explicit dismissal.
|
|
if (urgency === 2 || milliseconds === 0) return 0;
|
|
if (milliseconds < 0) return urgency === 0 ? 3000 : 6000;
|
|
return Math.max(1, Math.min(2147483647, Math.round(milliseconds)));
|
|
}
|
|
|
|
function upsert(history, entry) {
|
|
return [entry].concat(history.filter(old => old.key !== entry.key)).slice(0, 100);
|
|
}
|
|
|
|
function validate(document) {
|
|
if (!document || document.version !== 1 || !Array.isArray(document.entries) || document.entries.length > 100)
|
|
throw new Error("Invalid notification history schema");
|
|
const keys = [];
|
|
document.entries.forEach(entry => {
|
|
if (!entry || typeof entry.key !== "string" || !entry.key || entry.key.length > 128 || keys.includes(entry.key))
|
|
throw new Error("Invalid history key");
|
|
keys.push(entry.key);
|
|
for (const field of ["appName", "summary", "body", "state"]) {
|
|
if (typeof entry[field] !== "string" || entry[field].length > 8192)
|
|
throw new Error("Invalid history field: " + field);
|
|
}
|
|
if (entry.desktopEntry !== undefined && (typeof entry.desktopEntry !== "string" || entry.desktopEntry.length > 512))
|
|
throw new Error("Invalid desktop entry id");
|
|
if (entry.session !== undefined && (typeof entry.session !== "string" || !/^[0-9]+$/.test(entry.session)))
|
|
throw new Error("Invalid notification session");
|
|
if (entry.notificationId !== undefined && (!Number.isInteger(entry.notificationId) || entry.notificationId < 1))
|
|
throw new Error("Invalid backend id");
|
|
if (entry.deadline !== undefined && (!Number.isFinite(entry.deadline) || entry.deadline < 0))
|
|
throw new Error("Invalid deadline");
|
|
if (!Number.isFinite(entry.time) || ![0, 1, 2].includes(entry.urgency))
|
|
throw new Error("Invalid history metadata");
|
|
});
|
|
return document.entries;
|
|
}
|