Nya notifikations alternativ

This commit is contained in:
2026-06-16 21:37:11 +02:00
parent 8491fe1386
commit 125fddfb1b
8 changed files with 395 additions and 78 deletions
+43 -29
View File
@@ -1,7 +1,7 @@
const express = require('express');
const router = express.Router();
const settings = require('../settings');
const { notify } = require('../notify');
const { testGotify, testNtfy, testSmtp, testWebhook } = require('../notify');
// GET /api/settings
router.get('/', (req, res) => {
@@ -12,8 +12,7 @@ router.get('/', (req, res) => {
router.put('/', (req, res) => {
try {
const updated = settings.update(req.body);
// Reschedule secret check if the notification time changed
if (req.body.notifications?.secret_check_time) {
if (req.body.notifications?.secret_check_time || req.body.notifications?.timezone) {
try { require('../index').scheduleSecretCheck(); } catch { /* safe to ignore */ }
}
res.json(updated);
@@ -22,36 +21,51 @@ router.put('/', (req, res) => {
}
});
// POST /api/settings/test-notification
router.post('/test-notification', async (req, res) => {
// Use the payload from the request body so the user can test
// before saving (the frontend sends the current form values)
// POST /api/settings/test-gotify
router.post('/test-gotify', async (req, res) => {
const { url, token, priority } = req.body;
if (!url || !token) {
return res.status(400).json({ error: 'URL and token are required' });
}
const base = url.replace(/\/$/, '');
if (!url || !token) return res.status(400).json({ error: 'URL and token are required' });
try {
const response = await fetch(`${base}/message?token=${encodeURIComponent(token)}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: '🦥 Sloth Manager — Test',
message: 'Gotify notifications are working correctly.',
priority: priority ?? 5,
}),
});
if (!response.ok) {
const text = await response.text().catch(() => '');
return res.status(502).json({ error: `Gotify returned ${response.status}: ${text}` });
}
await testGotify({ url, token, priority });
res.json({ success: true });
} catch (err) {
res.status(502).json({ error: `Could not reach Gotify: ${err.message}` });
res.status(502).json({ error: err.message });
}
});
// POST /api/settings/test-ntfy
router.post('/test-ntfy', async (req, res) => {
const { url, topic, token, priority } = req.body;
if (!url || !topic) return res.status(400).json({ error: 'URL and topic are required' });
try {
await testNtfy({ url, topic, token, priority });
res.json({ success: true });
} catch (err) {
res.status(502).json({ error: err.message });
}
});
// POST /api/settings/test-smtp
router.post('/test-smtp', async (req, res) => {
const { host, port, secure, username, password, from, to } = req.body;
if (!host || !from || !to) return res.status(400).json({ error: 'Host, from, and to are required' });
try {
await testSmtp({ host, port, secure, username, password, from, to });
res.json({ success: true });
} catch (err) {
res.status(502).json({ error: err.message });
}
});
// POST /api/settings/test-webhook
router.post('/test-webhook', async (req, res) => {
const { url, secret } = req.body;
if (!url) return res.status(400).json({ error: 'Webhook URL is required' });
try {
await testWebhook({ url, secret });
res.json({ success: true });
} catch (err) {
res.status(502).json({ error: err.message });
}
});