diff --git a/src/notifications.js b/src/notifications.js index b526f51fd..7fe663e1c 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -39,19 +39,17 @@ const NOTIFICATION_FIELDS = [ 'id', 'eventId', 'title', 'message', 'creationTime function postProcess(result) { assert.strictEqual(typeof result, 'object'); result.id = String(result.id); - - // convert to boolean - result.acknowledged = !!result.acknowledged; + result.acknowledged = !!result.acknowledged; // convert to boolean return result; } -async function add(eventId, title, message) { - assert(typeof eventId === 'string' || eventId === null); +async function add(title, message, data) { assert.strictEqual(typeof title, 'string'); assert.strictEqual(typeof message, 'string'); + assert.strictEqual(typeof data, 'object'); - const query = 'INSERT INTO notifications (eventId, title, message, acknowledged) VALUES (?, ?, ?, ?)'; - const args = [ eventId, title, message, false ]; + const query = 'INSERT INTO notifications (title, message, acknowledged, eventId) VALUES (?, ?, ?, ?)'; + const args = [ title, message, false, data?.eventId || null ]; const result = await database.query(query, args); return String(result.insertId); @@ -60,7 +58,7 @@ async function add(eventId, title, message) { async function get(id) { assert.strictEqual(typeof id, 'string'); - const result = await database.query('SELECT ' + NOTIFICATION_FIELDS + ' FROM notifications WHERE id = ?', [ id ]); + const result = await database.query(`SELECT ${NOTIFICATION_FIELDS} FROM notifications WHERE id = ?`, [ id ]); if (result.length === 0) return null; return postProcess(result[0]); @@ -69,7 +67,7 @@ async function get(id) { async function getByTitle(title) { assert.strictEqual(typeof title, 'string'); - const results = await database.query('SELECT ' + NOTIFICATION_FIELDS + ' from notifications WHERE title = ? ORDER BY creationTime LIMIT 1', [ title ]); + const results = await database.query(`SELECT ${NOTIFICATION_FIELDS} from notifications WHERE title = ? ORDER BY creationTime LIMIT 1`, [ title ]); if (results.length === 0) return null; return postProcess(results[0]); @@ -79,9 +77,9 @@ async function update(notification, data) { assert.strictEqual(typeof notification, 'object'); assert.strictEqual(typeof data, 'object'); - let args = [ ]; - let fields = [ ]; - for (let k in data) { + const args = []; + const fields = []; + for (const k in data) { fields.push(k + ' = ?'); args.push(data[k]); } @@ -103,9 +101,9 @@ async function list(filters, page, perPage) { assert.strictEqual(typeof page, 'number'); assert.strictEqual(typeof perPage, 'number'); - let args = []; + const args = []; - let where = []; + const where = []; if ('acknowledged' in filters) { where.push('acknowledged=?'); args.push(filters.acknowledged); @@ -144,7 +142,7 @@ async function oomEvent(eventId, containerId, app, addonName /*, event*/) { message = `The app has been restarted automatically. If you see this notification often, consider increasing the [memory limit](${settings.dashboardOrigin()}/#/app/${app.id}/resources)`; } - await add(eventId, title, message); + await add(title, message, { eventId }); } async function appUpdated(eventId, app, fromManifest, toManifest) { @@ -160,7 +158,7 @@ async function appUpdated(eventId, app, fromManifest, toManifest) { const title = upstreamVersion ? `${toManifest.title} at ${app.fqdn} updated to ${upstreamVersion} (package version ${toManifest.version})` : `${toManifest.title} at ${app.fqdn} updated to package version ${toManifest.version}`; - await add(eventId, title, `The application installed at https://${app.fqdn} was updated.\n\nChangelog:\n${toManifest.changelog}\n`); + await add(title, `The application installed at https://${app.fqdn} was updated.\n\nChangelog:\n${toManifest.changelog}\n`, { eventId }); } async function boxInstalled(eventId, version) { @@ -170,7 +168,7 @@ async function boxInstalled(eventId, version) { const changes = changelog.getChanges(version.replace(/\.([^.]*)$/, '.0')); // last .0 release const changelogMarkdown = changes.map((m) => `* ${m}\n`).join(''); - await add(eventId, `Cloudron v${version} installed`, `Cloudron v${version} was installed.\n\nPlease join our community at ${constants.FORUM_URL} .\n\nChangelog:\n${changelogMarkdown}\n`); + await add(`Cloudron v${version} installed`, `Cloudron v${version} was installed.\n\nPlease join our community at ${constants.FORUM_URL} .\n\nChangelog:\n${changelogMarkdown}\n`, { eventId }); } async function boxUpdated(eventId, oldVersion, newVersion) { @@ -181,14 +179,14 @@ async function boxUpdated(eventId, oldVersion, newVersion) { const changes = changelog.getChanges(newVersion); const changelogMarkdown = changes.map((m) => `* ${m}\n`).join(''); - await add(eventId, `Cloudron updated to v${newVersion}`, `Cloudron was updated from v${oldVersion} to v${newVersion}.\n\nChangelog:\n${changelogMarkdown}\n`); + await add(`Cloudron updated to v${newVersion}`, `Cloudron was updated from v${oldVersion} to v${newVersion}.\n\nChangelog:\n${changelogMarkdown}\n`, { eventId }); } async function boxUpdateError(eventId, errorMessage) { assert.strictEqual(typeof eventId, 'string'); assert.strictEqual(typeof errorMessage, 'string'); - await add(eventId, 'Cloudron update failed', `Failed to update Cloudron: ${errorMessage}.`); + await add('Cloudron update failed', `Failed to update Cloudron: ${errorMessage}.`, { eventId }); } async function certificateRenewalError(eventId, fqdn, errorMessage) { @@ -196,7 +194,7 @@ async function certificateRenewalError(eventId, fqdn, errorMessage) { assert.strictEqual(typeof fqdn, 'string'); assert.strictEqual(typeof errorMessage, 'string'); - await add(eventId, `Certificate renewal of ${fqdn} failed`, `Failed to renew certs of ${fqdn}: ${errorMessage}. Renewal will be retried in 12 hours.`); + await add(`Certificate renewal of ${fqdn} failed`, `Failed to renew certs of ${fqdn}: ${errorMessage}. Renewal will be retried in 12 hours.`, { eventId }); const admins = await users.getAdmins(); for (const admin of admins) { @@ -209,7 +207,7 @@ async function backupFailed(eventId, taskId, errorMessage) { assert.strictEqual(typeof taskId, 'string'); assert.strictEqual(typeof errorMessage, 'string'); - await add(eventId, 'Backup failed', `Backup failed: ${errorMessage}. Logs are available [here](/logs.html?taskId=${taskId}).`); + await add('Backup failed', `Backup failed: ${errorMessage}. Logs are available [here](/logs.html?taskId=${taskId}).`, { eventId }); // only send mail if the past 3 automated backups failed const backupEvents = await eventlog.listPaged([eventlog.ACTION_BACKUP_FINISH], null /* search */, 1, 20); @@ -235,7 +233,7 @@ async function alert(id, title, message) { const result = await getByTitle(title); if (!result) { - return await add(null /* eventId */, title, message); + return await add(title, message, { eventId: null }); } else { await update(result, { eventId: null,