initial commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
const settings = require('./settings');
|
||||
const db = require('./db');
|
||||
|
||||
/**
|
||||
* Send a Gotify notification if enabled and configured.
|
||||
* Errors are logged but never thrown — notifications are best-effort.
|
||||
*/
|
||||
async function notify(title, message) {
|
||||
const { gotify } = settings.get();
|
||||
|
||||
if (!gotify.enabled || !gotify.url || !gotify.token) return;
|
||||
|
||||
const base = gotify.url.replace(/\/$/, '');
|
||||
|
||||
try {
|
||||
const res = await fetch(`${base}/message?token=${encodeURIComponent(gotify.token)}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title,
|
||||
message,
|
||||
priority: gotify.priority ?? 5,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
console.error(`[notify] Gotify error ${res.status}: ${text}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[notify] Failed to send Gotify notification:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Convenience helpers ─────────────────────────────────────────────────────
|
||||
|
||||
function recordAdded(provider, zoneId, record) {
|
||||
const zoneName = db.getZoneName(provider, zoneId);
|
||||
return notify(
|
||||
`DNS Record Added`,
|
||||
`[${provider}] ${zoneName}\n+ ${record.type} ${record.name} → ${record.content}`,
|
||||
);
|
||||
}
|
||||
|
||||
function recordUpdated(provider, zoneId, oldRecord, newRecord) {
|
||||
const zoneName = db.getZoneName(provider, zoneId);
|
||||
return notify(
|
||||
`DNS Record Updated`,
|
||||
`[${provider}] ${zoneName}\n✎ ${oldRecord.type} ${oldRecord.name}\n ${oldRecord.content} → ${newRecord.content}`,
|
||||
);
|
||||
}
|
||||
|
||||
function recordDeleted(provider, zoneId, record) {
|
||||
const zoneName = db.getZoneName(provider, zoneId);
|
||||
return notify(
|
||||
`DNS Record Deleted`,
|
||||
`[${provider}] ${zoneName}\n− ${record.type} ${record.name} ${record.content}`,
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = { notify, recordAdded, recordUpdated, recordDeleted };
|
||||
Reference in New Issue
Block a user