diff --git a/backend/src/index.js b/backend/src/index.js index 66d86a2..95b66f9 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -16,7 +16,35 @@ app.use((req, res, next) => { const jwt = require('jsonwebtoken'); const p = jwt.verify(auth.slice(7), process.env.JWT_SECRET || 'dev-secret'); if (p?.id) { + // Prüfen ob User vorher >15 Min inaktiv war → "gerade online gegangen" + const user = db.prepare('SELECT last_active_at, role FROM users WHERE id=?').get(p.id); + const wasInactive = !user?.last_active_at || + (Date.now() - new Date(user.last_active_at).getTime()) > 15 * 60 * 1000; + db.prepare("UPDATE users SET last_active_at=datetime('now','localtime') WHERE id=?").run(p.id); + + // Pushover an alle Admins (außer wenn User selbst Admin ist) + if (wasInactive && user?.role !== 'admin') { + const username = db.prepare('SELECT username FROM users WHERE id=?').get(p.id)?.username || 'Jemand'; + const admins = db.prepare(` + SELECT p.user_key, p.app_token FROM pushover_settings p + JOIN users u ON u.id = p.user_id + WHERE u.role = 'admin' AND p.user_key IS NOT NULL AND p.app_token IS NOT NULL + `).all(); + for (const admin of admins) { + fetch('https://api.pushover.net/1/messages.json', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + token: admin.app_token, + user: admin.user_key, + title: '👤 DickenDock', + message: `${username} ist gerade online gegangen.`, + priority: -1, + }), + }).catch(() => {}); + } + } } } catch {} }