| 1 | /**
|
|---|
| 2 | * Web Push (docs/webpush-design.md) slice 2: enable/disable + test.
|
|---|
| 3 | * The public VAPID key is public by design (it only identifies this server to
|
|---|
| 4 | * the browser's push service); everything that touches a subscription is a
|
|---|
| 5 | * logged-in action. Web Push delivery itself is cookie-less.
|
|---|
| 6 | */
|
|---|
| 7 | import express from 'express';
|
|---|
| 8 | import db from '../config/database.js';
|
|---|
| 9 | import { requireAuth } from '../middleware/auth.js';
|
|---|
| 10 | import Push from '../services/PushService.js';
|
|---|
| 11 |
|
|---|
| 12 | const router = express.Router();
|
|---|
| 13 |
|
|---|
| 14 | // A subscription row is personal: only its creator may touch it.
|
|---|
| 15 | function ownRow(endpoint, userId) {
|
|---|
| 16 | if (!endpoint) return null;
|
|---|
| 17 | const row = db.prepare('SELECT endpoint, user_id FROM push_subscriptions WHERE endpoint = ?').get(String(endpoint));
|
|---|
| 18 | return row && row.user_id === userId ? row : null;
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | router.get('/vapid', async (req, res) => {
|
|---|
| 22 | const key = await Push.publicKey();
|
|---|
| 23 | if (!key) return res.status(503).json({ error: 'push_unavailable' });
|
|---|
| 24 | res.json({ publicKey: key });
|
|---|
| 25 | });
|
|---|
| 26 |
|
|---|
| 27 | router.post('/subscribe', requireAuth, express.json({ limit: '16kb' }), async (req, res) => {
|
|---|
| 28 | if (!(await Push.pushReady())) return res.status(503).json({ error: 'push_unavailable' });
|
|---|
| 29 | const s = req.body && req.body.subscription;
|
|---|
| 30 | const keys = s && s.keys;
|
|---|
| 31 | const ok = Push.saveSubscription({
|
|---|
| 32 | endpoint: s && s.endpoint, userId: req.session.user.id,
|
|---|
| 33 | p256dh: keys && keys.p256dh, auth: keys && keys.auth,
|
|---|
| 34 | alertTypes: req.body.alerts || null,
|
|---|
| 35 | uaLabel: String(req.body.uaLabel || '').slice(0, 120) || null,
|
|---|
| 36 | });
|
|---|
| 37 | if (!ok) return res.status(400).json({ error: 'bad_subscription' });
|
|---|
| 38 | res.json({ ok: true });
|
|---|
| 39 | });
|
|---|
| 40 |
|
|---|
| 41 | router.post('/unsubscribe', requireAuth, express.json({ limit: '4kb' }), (req, res) => {
|
|---|
| 42 | const row = ownRow(req.body && req.body.endpoint, req.session.user.id);
|
|---|
| 43 | if (!row) return res.status(404).json({ error: 'not_found' });
|
|---|
| 44 | Push.deleteSubscription(row.endpoint);
|
|---|
| 45 | res.json({ ok: true });
|
|---|
| 46 | });
|
|---|
| 47 |
|
|---|
| 48 | router.post('/alerts', requireAuth, express.json({ limit: '4kb' }), (req, res) => {
|
|---|
| 49 | const row = ownRow(req.body && req.body.endpoint, req.session.user.id);
|
|---|
| 50 | if (!row) return res.status(404).json({ error: 'not_found' });
|
|---|
| 51 | Push.updateAlerts(row.endpoint, req.session.user.id, req.body.alerts || {});
|
|---|
| 52 | res.json({ ok: true });
|
|---|
| 53 | });
|
|---|
| 54 |
|
|---|
| 55 | // A test ping to all of the caller's own devices (bypasses alert prefs).
|
|---|
| 56 | router.post('/test', requireAuth, async (req, res) => {
|
|---|
| 57 | const sent = await Push.notifyUser(req.session.user.id, {
|
|---|
| 58 | type: 'test', title: 'Klonkt-testnotificatie',
|
|---|
| 59 | body: 'Werkt. Zo komen meldingen binnen op dit apparaat.', url: '/admin/push',
|
|---|
| 60 | });
|
|---|
| 61 | res.json({ ok: true, sent });
|
|---|
| 62 | });
|
|---|
| 63 |
|
|---|
| 64 | export default router;
|
|---|